Class AbstractRestServer
- All Implemented Interfaces:
Disposable, Observable<RestEndpoint>, Observers<RestEndpoint, RestServer>, RestServer, BaseLocatorAccessor, BaseLocatorAccessor.BaseLocatorBuilder<RestServer>, BaseLocatorAccessor.BaseLocatorMutator, BaseLocatorAccessor.BaseLocatorProperty, MediaTypeFactoryLookup, MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup, RealmAccessor, RealmAccessor.RealmBuilder<RestServer>, RealmAccessor.RealmMutator, RealmAccessor.RealmProperty
- Direct Known Subclasses:
JdkHttpRestServer, LoopbackRestServer
Implementation of the base functionality of the RestServer interface
omitting the HTTP handling part being the foundation for various
RestServer implementations such as JdkHttpRestServer or
LoopbackRestServer. The AbstractRestServer is preconfigured
with the following MediaTypeFactory instances:
In your sub-classes, overwrite the method 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.
Set the system property SystemProperty.LOG_DEBUG to true (set when
invoking the JRA by passing the argument -Dlog.debug=true to the
java executable) to log additional erroneous situations e.g.
related to content types and accept types alongside marshaling and
unmarshaling.
-
Nested Class Summary
Nested classes/interfaces inherited from interface BaseLocatorAccessor
BaseLocatorAccessor.BaseLocatorBuilder<B>, BaseLocatorAccessor.BaseLocatorMutator, BaseLocatorAccessor.BaseLocatorPropertyNested classes/interfaces inherited from interface MediaTypeFactoryLookup
MediaTypeFactoryLookup.MutableMediaTypeFactoryLookupNested classes/interfaces inherited from interface RealmAccessor
RealmAccessor.RealmBuilder<B>, RealmAccessor.RealmMutator, RealmAccessor.RealmProperty -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aAbstractRestServerpreconfigured withMediaTypeFactoryinstances for JSON and REST.AbstractRestServer(boolean isVerbose) Constructs aAbstractRestServerpreconfigured withMediaTypeFactoryinstances for JSON and REST.AbstractRestServer(ExecutorService aExecutorService) Constructs aAbstractRestServerpreconfigured withMediaTypeFactoryinstances for JSON and REST.AbstractRestServer(ExecutorService aExecutorService, boolean isVerbose) Constructs aAbstractRestServerpreconfigured withMediaTypeFactoryinstances for JSON and REST. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddMediaTypeFactory(MediaTypeFactory aMediaTypeFactory) voiddispose()protected booleanfireEvent(HttpRequest aEvent, RestEndpoint aObserver, ExecutionStrategy aExecutionStrategy) getRealm()protected voidAdds the defaultMediaTypeFactoryinstances.booleanprotected voidonHttpRequest(InetSocketAddress aLocalAddress, InetSocketAddress aRemoteAddress, HttpMethod aHttpMethod, Url aUrl, RequestHeaderFields aRequestHeaderFields, InputStream aHttpInputStream, HttpServerResponse aHttpServerResponse) Extensions of this class disect an incoming request and pass it to this method for doing the actual invocation of the registeredRestEndpointinstances.protected voidpostIntercept(HttpServerRequest aRequest, HttpServerResponse aResponse) Invoked to post-process aHttpServerRequestalongside aHttpServerResponse.protected voidpreIntercept(HttpServerRequest aRequest, HttpServerResponse aResponse) Invoked to pre-process aHttpServerRequestalongside aHttpServerResponse.voidsetBaseLocator(String aBaseLocator) voidsetObserversActive(boolean isActive) voidbooleansubscribeObserver(RestEndpoint aObserver) toMediaTypeFactory(MediaType aMediaType) protected ContentTypetoNegotiatedContenType(RequestHeaderFields aRequestHeaderFields) Determines the best fitting respone'sContentType.protected byte[]toResponseBody(Object aResponse, RequestHeaderFields aRequestHeaderFields, ResponseHeaderFields aResponseHeaderFields) Creates aStringMediaTypeencoded as of theHeaderField.CONTENT_TYPEfrom the response header or if not set as of theHeaderField.ACCEPTfrom the request header or if not set as of theHeaderField.CONTENT_TYPEfrom the request header.booleanunsubscribeObserver(RestEndpoint aObserver) Methods inherited from class AbstractObservable
clear, doHandleEventListenerException, fireEvent, getThreadPriority, hasObserver, isEmpty, setThreadPriority, sizeMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface BaseLocatorAccessor.BaseLocatorProperty
letBaseLocatorMethods inherited from interface MediaTypeFactoryLookup
hasMediaTypeFactoryMethods inherited from interface Observable
hasObserverMethods inherited from interface Observers
disableObservers, enableObserversMethods inherited from interface RealmAccessor.RealmProperty
letRealmMethods inherited from interface RestServer
onDelete, onDelete, onGet, onGet, onPost, onPost, onPut, onPut, onRequest, onRequest, onRequest, onRequest, onRequest, withBaseLocator, withDisableObservers, withEnableObservers, withObserversActive, withRealm
-
Field Details
-
_isVerbose
protected boolean _isVerbose
-
-
Constructor Details
-
AbstractRestServer
public AbstractRestServer()Constructs aAbstractRestServerpreconfigured withMediaTypeFactoryinstances for JSON and REST. -
AbstractRestServer
public AbstractRestServer(boolean isVerbose) Constructs aAbstractRestServerpreconfigured withMediaTypeFactoryinstances for JSON and REST.- Parameters:
isVerbose- When true, any unknown content- and accept-types are logged.
-
AbstractRestServer
Constructs aAbstractRestServerpreconfigured withMediaTypeFactoryinstances for JSON and REST.- Parameters:
aExecutorService- An executor service to be used when creatingThreads.
-
AbstractRestServer
Constructs aAbstractRestServerpreconfigured withMediaTypeFactoryinstances for JSON and REST.- Parameters:
aExecutorService- An executor service to be used when creatingThreads.isVerbose- When true, any unknown content- and accept-types are logged.
-
-
Method Details
-
initMedaTypeFactories
protected void initMedaTypeFactories()Adds the defaultMediaTypeFactoryinstances. Can be overridden. -
setObserversActive
public void setObserversActive(boolean isActive) - Specified by:
setObserversActivein interfaceObservers<RestEndpoint, RestServer>- Overrides:
setObserversActivein classAbstractObservable<RestEndpoint, HttpRequest>
-
isObserversActive
public boolean isObserversActive()- Specified by:
isObserversActivein interfaceObservers<RestEndpoint, RestServer>- Overrides:
isObserversActivein classAbstractObservable<RestEndpoint, HttpRequest>
-
observers
- Specified by:
observersin interfaceObservers<RestEndpoint, RestServer>- Overrides:
observersin classAbstractObservable<RestEndpoint, HttpRequest>
-
getRealm
- Specified by:
getRealmin interfaceRealmAccessor
-
setRealm
- Specified by:
setRealmin interfaceRealmAccessor.RealmMutator
-
getBaseLocator
- Specified by:
getBaseLocatorin interfaceBaseLocatorAccessor
-
setBaseLocator
- Specified by:
setBaseLocatorin interfaceBaseLocatorAccessor.BaseLocatorMutator
-
subscribeObserver
- Specified by:
subscribeObserverin interfaceObservable<RestEndpoint>- Overrides:
subscribeObserverin classAbstractObservable<RestEndpoint, HttpRequest>
-
unsubscribeObserver
- Specified by:
unsubscribeObserverin interfaceObservable<RestEndpoint>- Overrides:
unsubscribeObserverin classAbstractObservable<RestEndpoint, HttpRequest>
-
addMediaTypeFactory
- Specified by:
addMediaTypeFactoryin interfaceMediaTypeFactoryLookup.MutableMediaTypeFactoryLookup
-
toMediaTypeFactory
- Specified by:
toMediaTypeFactoryin interfaceMediaTypeFactoryLookup
-
getFactoryMediaTypes
- Specified by:
getFactoryMediaTypesin interfaceMediaTypeFactoryLookup
-
dispose
public void dispose()- Specified by:
disposein interfaceDisposable- Overrides:
disposein classAbstractObservable<RestEndpoint, HttpRequest>
-
onHttpRequest
protected void onHttpRequest(InetSocketAddress aLocalAddress, InetSocketAddress aRemoteAddress, HttpMethod aHttpMethod, Url aUrl, RequestHeaderFields aRequestHeaderFields, InputStream aHttpInputStream, HttpServerResponse aHttpServerResponse) throws HttpStatusException Extensions of this class disect an incoming request and pass it to this method for doing the actual invocation of the registeredRestEndpointinstances. An extension might call this method from inside an event (request) handler.- Parameters:
aLocalAddress- The host and port of your REST service.aRemoteAddress- The host and port for the caller.aHttpMethod- TheHttpMethodof the request.aUrl- TheUrlfrom which to take the URL specific data.aRequestHeaderFields- The Header-Fields (HeaderFields) belonging to the request.aHttpInputStream- The body passed by the request.aHttpServerResponse- AHttpServerResponseinstance to be used by the extension to produce an according HTTP-Response.- Throws:
HttpStatusException- thrown in case of anRestEndpointresponsible for the given request encountered a problem or noneRestEndpointfelt responsible to produce aHttpServerResponse.
-
toNegotiatedContenType
Determines the best fitting respone'sContentType. The default Content-Type-Negotiation implementation of this method makes use of theRequestHeaderFieldsand matches them against the supportedMediaTypetypes ( retrieved viagetFactoryMediaTypes()). May be overwritten to enforce another Content-Type-Negotiation strategy.- Parameters:
aRequestHeaderFields- The request'sHeaderFieldinstance to use when determining the best fitting respone'sContentType.- Returns:
- The best fitting (as of the implemented Content-Type-Negotiation strategy) Content-Type to be used for the response.
-
toResponseBody
protected byte[] toResponseBody(Object aResponse, RequestHeaderFields aRequestHeaderFields, ResponseHeaderFields aResponseHeaderFields) throws MarshalException, UnsupportedMediaTypeException Creates aStringMediaTypeencoded as of theHeaderField.CONTENT_TYPEfrom the response header or if not set as of theHeaderField.ACCEPTfrom the request header or if not set as of theHeaderField.CONTENT_TYPEfrom the request header.- Parameters:
aResponse- The response which to encode as of the detectedMediaTypes.aRequestHeaderFields- The Header-Fields from the request.aResponseHeaderFields- The Header-Fields from the response.- Returns:
- An accordingly encoded response as byte array.
- Throws:
MarshalException- thrown when marshaling / serializing an object failed.UnsupportedMediaTypeException- thrown in case none of the identified media types is supported, e.g. no requiredMediaTypeFactoryhas been registered as ofaddMediaTypeFactory(MediaTypeFactory).
-
fireEvent
protected boolean fireEvent(HttpRequest aEvent, RestEndpoint aObserver, ExecutionStrategy aExecutionStrategy) throws VetoException - Specified by:
fireEventin classAbstractObservable<RestEndpoint, HttpRequest>- Throws:
VetoException
-
preIntercept
Invoked to pre-process aHttpServerRequestalongside aHttpServerResponse.- Parameters:
aRequest- TheHttpServerRequestto pre-process.aResponse- TheHttpServerResponseto post-process.
-
postIntercept
Invoked to post-process aHttpServerRequestalongside aHttpServerResponse.- Parameters:
aRequest- TheHttpServerRequestto post-process.aResponse- TheHttpServerResponseto post-process.
-