public abstract class AbstractRestServer extends org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest> implements RestServer
RestServer
interface
omitting the HTTP handling part being the foundation for various
RestServer
implementations such as HttpRestServerImpl
or
LoopbackRestServerImpl
.
The AbstractRestServer
is pre-configured with the following
MediaTypeFactory
instances:
ApplicationJsonFactory
ApplicationXmlFactory
TextPlainFactory
ApplicationFormFactory
initMedaTypeFactories()
,
therein calling addMediaTypeFactory(MediaTypeFactory)
to add (by
also invoking super's initMedaTypeFactories()
) or to set your own
(without invoking super's initMedaTypeFactories()
)
MediaTypeFactory
instances.org.refcodes.net.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup
org.refcodes.net.RealmAccessor.RealmBuilder<B extends org.refcodes.net.RealmAccessor.RealmBuilder<B>>, org.refcodes.net.RealmAccessor.RealmMutator, org.refcodes.net.RealmAccessor.RealmProperty
org.refcodes.net.BaseLocatorAccessor.BaseLocatorBuilder<B extends org.refcodes.net.BaseLocatorAccessor.BaseLocatorBuilder<B>>, org.refcodes.net.BaseLocatorAccessor.BaseLocatorMutator, org.refcodes.net.BaseLocatorAccessor.BaseLocatorProperty
Modifier and Type | Field and Description |
---|---|
protected boolean |
_hasRequestCorrelation |
protected boolean |
_hasSessionCorrelation |
Constructor and Description |
---|
AbstractRestServer()
Constructs a
AbstractRestServer pre-configured with
MediaTypeFactory instances for JSON and REST. |
AbstractRestServer(ExecutorService aExecutorService)
CConstructs a
AbstractRestServer pre-configured with
MediaTypeFactory instances for JSON and REST. |
Modifier and Type | Method and Description |
---|---|
boolean |
addMediaTypeFactory(org.refcodes.net.MediaTypeFactory aMediaTypeFactory) |
void |
dispose() |
protected void |
doRequestCorrelation(org.refcodes.net.RequestHeaderFields aRequestHeaderFields,
org.refcodes.net.HttpServerResponse aServerResponse)
Do request correlation.
|
protected void |
doSessionCorrelation(org.refcodes.net.RequestHeaderFields aRequestHeaderFields,
org.refcodes.net.HttpServerResponse aServerResponse)
Do session correlation.
|
protected boolean |
fireEvent(org.refcodes.net.HttpRequest aEvent,
RestEndpoint aObserver,
org.refcodes.controlflow.ExecutionStrategy aExecutionStrategy) |
String |
getBaseLocator() |
org.refcodes.net.MediaType[] |
getFactoryMediaTypes() |
String |
getRealm() |
boolean |
hasRequestCorrelation() |
boolean |
hasSessionCorrelation() |
protected void |
initMedaTypeFactories()
Adds the default
MediaTypeFactory instances. |
protected org.refcodes.net.HttpServerResponse |
onHttpRequest(InetSocketAddress aLocalAddress,
InetSocketAddress aRemoteAddress,
org.refcodes.net.HttpMethod aHttpMethod,
String aLocator,
String aQueryString,
org.refcodes.net.RequestHeaderFields aRequestHeaderFields,
InputStream aHttpInputStream)
Extensions of this class disect an incoming request and pass it to this
method for doing the actual invocation of the registered
RestEndpoint instances. |
RestEndpointBuilder |
onRequest(org.refcodes.net.HttpMethod aHttpMethod,
String aLocatorPattern,
RestRequestObserver aRequestObserver)
Registers a pre-configured
RestEndpoint with the least required
attributes and returns its instance. |
void |
setBaseLocator(String aBaseLocator) |
void |
setRealm(String aRealm) |
void |
setRequestCorrelation(boolean hasRequestCorrelation) |
void |
setSessionCorrelation(boolean hasSessionCorrelation) |
boolean |
subscribeObserver(RestEndpoint aObserver) |
org.refcodes.net.MediaTypeFactory |
toMediaTypeFactory(org.refcodes.net.MediaType aMediaType) |
protected org.refcodes.net.ContentType |
toNegotiatedContenType(org.refcodes.net.RequestHeaderFields aRequestHeaderFields)
Determines the best fitting respone's
ContentType . |
protected byte[] |
toResponseBody(Object aResponse,
org.refcodes.net.RequestHeaderFields aRequestHeaderFields,
org.refcodes.net.ResponseHeaderFields aResponseHeaderFields)
Creates a
String MediaType encoded as of the
HeaderField.CONTENT_TYPE from the response header or if not set
as of the HeaderField.ACCEPT from the request header or if not
set as of the HeaderField.CONTENT_TYPE from the request header. |
boolean |
unsubscribeObserver(RestEndpoint aObserver) |
clear, doHandleEventListenerException, fireEvent, getThreadPriority, hasObserverSubscription, isEmpty, setThreadPriority, size
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
onDelete, onGet, onPost, onPut, onRequest, onRequest, withBaseLocator, withDisableRequestCorrelation, withDisableSessionCorrelation, withEnableRequestCorrelation, withEnableSessionCorrelation, withRealm, withRequestCorrelation, withSessionCorrelation
protected boolean _hasRequestCorrelation
protected boolean _hasSessionCorrelation
public AbstractRestServer()
AbstractRestServer
pre-configured with
MediaTypeFactory
instances for JSON and REST.public AbstractRestServer(ExecutorService aExecutorService)
AbstractRestServer
pre-configured with
MediaTypeFactory
instances for JSON and REST.aExecutorService
- An executor service to be used when creating
Thread
s.protected void initMedaTypeFactories()
MediaTypeFactory
instances. Can be overridden.public void setRequestCorrelation(boolean hasRequestCorrelation)
setRequestCorrelation
in interface org.refcodes.runtime.RequestCorrelation<RestServer>
public boolean hasRequestCorrelation()
hasRequestCorrelation
in interface org.refcodes.runtime.RequestCorrelation<RestServer>
public void setSessionCorrelation(boolean hasSessionCorrelation)
setSessionCorrelation
in interface org.refcodes.runtime.SessionCorrelation<RestServer>
public boolean hasSessionCorrelation()
hasSessionCorrelation
in interface org.refcodes.runtime.SessionCorrelation<RestServer>
public String getRealm()
getRealm
in interface org.refcodes.net.RealmAccessor
public void setRealm(String aRealm)
setRealm
in interface org.refcodes.net.RealmAccessor.RealmMutator
public String getBaseLocator()
getBaseLocator
in interface org.refcodes.net.BaseLocatorAccessor
public void setBaseLocator(String aBaseLocator)
setBaseLocator
in interface org.refcodes.net.BaseLocatorAccessor.BaseLocatorMutator
public boolean subscribeObserver(RestEndpoint aObserver)
subscribeObserver
in interface org.refcodes.observer.Observable<RestEndpoint>
subscribeObserver
in class org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
public RestEndpointBuilder onRequest(org.refcodes.net.HttpMethod aHttpMethod, String aLocatorPattern, RestRequestObserver aRequestObserver)
RestEndpoint
with the least required
attributes and returns its instance. To unsubscribe via
Observable.unsubscribeObserver(Object)
, use the returned instance.
Attention: Invoke Openable.open()
to activate this
endpoint!onRequest
in interface RestServer
aHttpMethod
- The HTTP-Method to which this RestEndpoint
is
bound. A value of null
means that any HTTP-Method may
trigger this RestEndpoint
.aLocatorPattern
- The local Locator-Pattern to which this
RestEndpoint
is bound. A value of null
means
that any locator may trigger this RestEndpoint
.aRequestObserver
- The listener processing a request targeted at
this RestEndpoint
.RestEndpoint
which acts as handle to
unsubscribe the RestEndpoint
via
Observable.unsubscribeObserver(Object)
.public boolean unsubscribeObserver(RestEndpoint aObserver)
unsubscribeObserver
in interface org.refcodes.observer.Observable<RestEndpoint>
unsubscribeObserver
in class org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
public boolean addMediaTypeFactory(org.refcodes.net.MediaTypeFactory aMediaTypeFactory)
addMediaTypeFactory
in interface org.refcodes.net.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup
public org.refcodes.net.MediaTypeFactory toMediaTypeFactory(org.refcodes.net.MediaType aMediaType)
toMediaTypeFactory
in interface org.refcodes.net.MediaTypeFactoryLookup
public org.refcodes.net.MediaType[] getFactoryMediaTypes()
getFactoryMediaTypes
in interface org.refcodes.net.MediaTypeFactoryLookup
public void dispose()
dispose
in interface org.refcodes.mixin.Disposable
dispose
in class org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
protected org.refcodes.net.HttpServerResponse onHttpRequest(InetSocketAddress aLocalAddress, InetSocketAddress aRemoteAddress, org.refcodes.net.HttpMethod aHttpMethod, String aLocator, String aQueryString, org.refcodes.net.RequestHeaderFields aRequestHeaderFields, InputStream aHttpInputStream) throws org.refcodes.net.HttpStatusException
RestEndpoint
instances. An extension might call this method from
inside an event (request) handler.aLocalAddress
- The host and port of your REST service.aRemoteAddress
- The host and port for the caller.aHttpMethod
- The HttpMethod
of the request.aLocator
- The locator (URL) targeted by the request (without the
query string).aQueryString
- The query string part of the request.aRequestHeaderFields
- The Header-Fields (HeaderFields
)
belonging to the request.aHttpInputStream
- The body passed by the request.HttpServerResponse
instance to by used by the extension
to produce an according HTTP-Response.org.refcodes.net.HttpStatusException
- thrown in case of an RestEndpoint
responsible for the given request encountered a problem or none
RestEndpoint
felt responsible to produce a
HttpServerResponse
.protected org.refcodes.net.ContentType toNegotiatedContenType(org.refcodes.net.RequestHeaderFields aRequestHeaderFields)
ContentType
. The default
Content-Type-Negotiation implementation of this method makes use of the
RequestHeaderFields
and matches them against the supported
MediaType
types ( retrieved via getFactoryMediaTypes()
).
May be overwritten to enforce another Content-Type-Negotiation strategy.aRequestHeaderFields
- The request's HeaderField
instance to
use when determining the best fitting respone's
ContentType
.protected byte[] toResponseBody(Object aResponse, org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.ResponseHeaderFields aResponseHeaderFields) throws org.refcodes.exception.MarshalException, org.refcodes.net.UnsupportedMediaTypeException
String
MediaType
encoded as of the
HeaderField.CONTENT_TYPE
from the response header or if not set
as of the HeaderField.ACCEPT
from the request header or if not
set as of the HeaderField.CONTENT_TYPE
from the request header.aResponse
- The response which to encode as of the detected
MediaType
s.aRequestHeaderFields
- The Header-Fields from the request.aResponseHeaderFields
- The Header-Fields from the response.org.refcodes.exception.MarshalException
- thrown when marshaling / serializing an object
failed.org.refcodes.net.UnsupportedMediaTypeException
- thrown in case none of the
identified media types is supported, e.g. no required
MediaTypeFactory
has been registered as of
addMediaTypeFactory(MediaTypeFactory)
.protected boolean fireEvent(org.refcodes.net.HttpRequest aEvent, RestEndpoint aObserver, org.refcodes.controlflow.ExecutionStrategy aExecutionStrategy) throws org.refcodes.exception.VetoException
fireEvent
in class org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
org.refcodes.exception.VetoException
protected void doRequestCorrelation(org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.HttpServerResponse aServerResponse)
aRequestHeaderFields
- the a request Header-FieldsaServerResponse
- the a server responseprotected void doSessionCorrelation(org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.HttpServerResponse aServerResponse)
aRequestHeaderFields
- the a request Header-FieldsaServerResponse
- the a server responseCopyright © 2017. All rights reserved.