java.lang.Object
org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
org.refcodes.rest.AbstractRestServer
- All Implemented Interfaces:
org.refcodes.mixin.Disposable,org.refcodes.mixin.Loggable,org.refcodes.observer.Observable<RestEndpoint>,org.refcodes.observer.Observers<RestEndpoint,,RestServer> HttpExceptionHandlerAccessor,HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<RestServer>,HttpExceptionHandlerAccessor.HttpExceptionHandlerMutator,HttpExceptionHandlerAccessor.HttpExceptionHandlerProperty,HttpExceptionHandlingAccessor,HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder<RestServer>,HttpExceptionHandlingAccessor.HttpExceptionHandlingMutator,HttpExceptionHandlingAccessor.HttpExceptionHandlingProperty,RestServer,org.refcodes.runtime.RequestCorrelation<RestServer>,org.refcodes.runtime.SessionCorrelation<RestServer>,org.refcodes.web.BaseLocatorAccessor,org.refcodes.web.BaseLocatorAccessor.BaseLocatorBuilder<RestServer>,org.refcodes.web.BaseLocatorAccessor.BaseLocatorMutator,org.refcodes.web.BaseLocatorAccessor.BaseLocatorProperty,org.refcodes.web.MediaTypeFactoryLookup,org.refcodes.web.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup,org.refcodes.web.RealmAccessor,org.refcodes.web.RealmAccessor.RealmBuilder<RestServer>,org.refcodes.web.RealmAccessor.RealmMutator,org.refcodes.web.RealmAccessor.RealmProperty
- Direct Known Subclasses:
HttpRestServerImpl,LoopbackRestServerImpl
public abstract class AbstractRestServer
extends org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
implements RestServer
Implementation of the base functionality of the
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:
JsonMediaTypeFactoryXmlMediaTypeFactoryTextMediaTypeFactoryFormMediaTypeFactory
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.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.refcodes.web.BaseLocatorAccessor
org.refcodes.web.BaseLocatorAccessor.BaseLocatorBuilder<B extends org.refcodes.web.BaseLocatorAccessor.BaseLocatorBuilder<B>>, org.refcodes.web.BaseLocatorAccessor.BaseLocatorMutator, org.refcodes.web.BaseLocatorAccessor.BaseLocatorPropertyNested classes/interfaces inherited from interface org.refcodes.rest.HttpExceptionHandlerAccessor
HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<B extends HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<B>>, HttpExceptionHandlerAccessor.HttpExceptionHandlerMutator, HttpExceptionHandlerAccessor.HttpExceptionHandlerPropertyNested classes/interfaces inherited from interface org.refcodes.rest.HttpExceptionHandlingAccessor
HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder<B extends HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder<B>>, HttpExceptionHandlingAccessor.HttpExceptionHandlingMutator, HttpExceptionHandlingAccessor.HttpExceptionHandlingPropertyNested classes/interfaces inherited from interface org.refcodes.web.MediaTypeFactoryLookup
org.refcodes.web.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookupNested classes/interfaces inherited from interface org.refcodes.web.RealmAccessor
org.refcodes.web.RealmAccessor.RealmBuilder<B extends org.refcodes.web.RealmAccessor.RealmBuilder<B>>, org.refcodes.web.RealmAccessor.RealmMutator, org.refcodes.web.RealmAccessor.RealmProperty -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected booleanprotected HttpExceptionHandlerprotected HttpExceptionHandlingFields inherited from interface org.refcodes.mixin.Loggable
RUNTIME_LOGGER_CLASS, RUNTIME_LOGGER_FACTORY_CLASS, RUNTIME_LOGGER_FACTORY_METHOD -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aAbstractRestServerpre-configured withMediaTypeFactoryinstances for JSON and REST.AbstractRestServer(ExecutorService aExecutorService) CConstructs aAbstractRestServerpre-configured withMediaTypeFactoryinstances for JSON and REST. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddMediaTypeFactory(org.refcodes.web.MediaTypeFactory aMediaTypeFactory) voiddispose()protected voiddoRequestCorrelation(org.refcodes.web.RequestHeaderFields aRequestHeaderFields, org.refcodes.web.HttpServerResponse aServerResponse) Do request correlation.protected voiddoSessionCorrelation(org.refcodes.web.RequestHeaderFields aRequestHeaderFields, org.refcodes.web.HttpServerResponse aServerResponse) Do session correlation.protected booleanfireEvent(org.refcodes.web.HttpRequest aEvent, RestEndpoint aObserver, org.refcodes.controlflow.ExecutionStrategy aExecutionStrategy) org.refcodes.web.MediaType[]Retrieves theHttpExceptionHandlerfrom theHttpExceptionHandlerproperty.Retrieves theHttpExceptionHandlingfrom theHttpExceptionHandlingproperty.getRealm()booleanbooleanprotected voidAdds the defaultMediaTypeFactoryinstances.booleanprotected voidonHttpRequest(InetSocketAddress aLocalAddress, InetSocketAddress aRemoteAddress, org.refcodes.web.HttpMethod aHttpMethod, org.refcodes.web.Url aUrl, org.refcodes.web.RequestHeaderFields aRequestHeaderFields, InputStream aHttpInputStream, org.refcodes.web.HttpServerResponse aHttpServerResponse) Extensions of this class disect an incoming request and pass it to this method for doing the actual invocation of the registeredRestEndpointinstances.voidsetBaseLocator(String aBaseLocator) voidsetHttpExceptionHandler(HttpExceptionHandler aHttpErrorHandler) Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.voidsetHttpExceptionHandling(HttpExceptionHandling aHttpErrorHandling) Sets theHttpExceptionHandlingfor theHttpExceptionHandlingproperty.voidsetObserversActive(boolean isActive) voidvoidsetRequestCorrelation(boolean hasRequestCorrelation) voidsetSessionCorrelation(boolean hasSessionCorrelation) booleansubscribeObserver(RestEndpoint aObserver) org.refcodes.web.MediaTypeFactorytoMediaTypeFactory(org.refcodes.web.MediaType aMediaType) protected org.refcodes.web.ContentTypetoNegotiatedContenType(org.refcodes.web.RequestHeaderFields aRequestHeaderFields) Determines the best fitting respone'sContentType.protected byte[]toResponseBody(Object aResponse, org.refcodes.web.RequestHeaderFields aRequestHeaderFields, org.refcodes.web.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 org.refcodes.observer.AbstractObservable
clear, doHandleEventListenerException, fireEvent, getThreadPriority, hasObserverSubscription, isEmpty, setThreadPriority, sizeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.refcodes.web.BaseLocatorAccessor.BaseLocatorProperty
letBaseLocatorMethods inherited from interface org.refcodes.rest.HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder
withHttpExceptionHandler, withOnHttpExceptionMethods inherited from interface org.refcodes.rest.HttpExceptionHandlerAccessor.HttpExceptionHandlerMutator
onHttpExceptionMethods inherited from interface org.refcodes.rest.HttpExceptionHandlerAccessor.HttpExceptionHandlerProperty
letHttpExceptionHandlerMethods inherited from interface org.refcodes.rest.HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder
withHttpExceptionHandlingMethods inherited from interface org.refcodes.rest.HttpExceptionHandlingAccessor.HttpExceptionHandlingProperty
letHttpExceptionHandlingMethods inherited from interface org.refcodes.mixin.Loggable
alert, alert, critical, critical, debug, error, info, notice, panic, trace, warn, warnMethods inherited from interface org.refcodes.web.MediaTypeFactoryLookup
hasMediaTypeFactoryMethods inherited from interface org.refcodes.observer.Observable
hasObserverSubscriptionMethods inherited from interface org.refcodes.observer.Observers
disableObservers, enableObserversMethods inherited from interface org.refcodes.web.RealmAccessor.RealmProperty
letRealmMethods inherited from interface org.refcodes.runtime.RequestCorrelation
disableRequestCorrelation, enableRequestCorrelationMethods inherited from interface org.refcodes.rest.RestServer
onDelete, onDelete, onGet, onGet, onPost, onPost, onPut, onPut, onRequest, onRequest, onRequest, onRequest, onRequest, withBaseLocator, withDisableObservers, withDisableRequestCorrelation, withDisableSessionCorrelation, withEnableObservers, withEnableRequestCorrelation, withEnableSessionCorrelation, withObserversActive, withRealm, withRequestCorrelation, withSessionCorrelationMethods inherited from interface org.refcodes.runtime.SessionCorrelation
disableSessionCorrelation, enableSessionCorrelation
-
Field Details
-
_hasRequestCorrelation
protected boolean _hasRequestCorrelation -
_hasSessionCorrelation
protected boolean _hasSessionCorrelation -
_httpExceptionHandling
-
_httpExceptionHandler
-
-
Constructor Details
-
AbstractRestServer
public AbstractRestServer()Constructs aAbstractRestServerpre-configured withMediaTypeFactoryinstances for JSON and REST. -
AbstractRestServer
CConstructs aAbstractRestServerpre-configured withMediaTypeFactoryinstances for JSON and REST.- Parameters:
aExecutorService- An executor service to be used when creatingThreads.
-
-
Method Details
-
initMedaTypeFactories
protected void initMedaTypeFactories()Adds the defaultMediaTypeFactoryinstances. Can be overridden. -
getHttpExceptionHandler
Retrieves theHttpExceptionHandlerfrom theHttpExceptionHandlerproperty.- Specified by:
getHttpExceptionHandlerin interfaceHttpExceptionHandlerAccessor- Returns:
- The
HttpExceptionHandlerstored by theHttpExceptionHandlerproperty.
-
setHttpExceptionHandler
Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.- Specified by:
setHttpExceptionHandlerin interfaceHttpExceptionHandlerAccessor.HttpExceptionHandlerMutator- Parameters:
aHttpErrorHandler- TheHttpExceptionHandlerto be stored by theHttpExceptionHandlerproperty.
-
getHttpExceptionHandling
Retrieves theHttpExceptionHandlingfrom theHttpExceptionHandlingproperty.- Specified by:
getHttpExceptionHandlingin interfaceHttpExceptionHandlingAccessor- Returns:
- The
HttpExceptionHandlingstored by theHttpExceptionHandlingproperty.
-
setHttpExceptionHandling
Sets theHttpExceptionHandlingfor theHttpExceptionHandlingproperty.- Specified by:
setHttpExceptionHandlingin interfaceHttpExceptionHandlingAccessor.HttpExceptionHandlingMutator- Parameters:
aHttpErrorHandling- TheHttpExceptionHandlingto be stored by theHttpExceptionHandlingproperty.
-
setObserversActive
public void setObserversActive(boolean isActive) - Specified by:
setObserversActivein interfaceorg.refcodes.observer.Observers<RestEndpoint,RestServer> - Overrides:
setObserversActivein classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
-
isObserversActive
public boolean isObserversActive()- Specified by:
isObserversActivein interfaceorg.refcodes.observer.Observers<RestEndpoint,RestServer> - Overrides:
isObserversActivein classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
-
observers
- Specified by:
observersin interfaceorg.refcodes.observer.Observers<RestEndpoint,RestServer> - Overrides:
observersin classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
-
setRequestCorrelation
public void setRequestCorrelation(boolean hasRequestCorrelation) - Specified by:
setRequestCorrelationin interfaceorg.refcodes.runtime.RequestCorrelation<RestServer>
-
hasRequestCorrelation
public boolean hasRequestCorrelation()- Specified by:
hasRequestCorrelationin interfaceorg.refcodes.runtime.RequestCorrelation<RestServer>
-
setSessionCorrelation
public void setSessionCorrelation(boolean hasSessionCorrelation) - Specified by:
setSessionCorrelationin interfaceorg.refcodes.runtime.SessionCorrelation<RestServer>
-
hasSessionCorrelation
public boolean hasSessionCorrelation()- Specified by:
hasSessionCorrelationin interfaceorg.refcodes.runtime.SessionCorrelation<RestServer>
-
getRealm
- Specified by:
getRealmin interfaceorg.refcodes.web.RealmAccessor
-
setRealm
- Specified by:
setRealmin interfaceorg.refcodes.web.RealmAccessor.RealmMutator
-
getBaseLocator
- Specified by:
getBaseLocatorin interfaceorg.refcodes.web.BaseLocatorAccessor
-
setBaseLocator
- Specified by:
setBaseLocatorin interfaceorg.refcodes.web.BaseLocatorAccessor.BaseLocatorMutator
-
subscribeObserver
- Specified by:
subscribeObserverin interfaceorg.refcodes.observer.Observable<RestEndpoint>- Overrides:
subscribeObserverin classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
-
unsubscribeObserver
- Specified by:
unsubscribeObserverin interfaceorg.refcodes.observer.Observable<RestEndpoint>- Overrides:
unsubscribeObserverin classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
-
addMediaTypeFactory
public boolean addMediaTypeFactory(org.refcodes.web.MediaTypeFactory aMediaTypeFactory) - Specified by:
addMediaTypeFactoryin interfaceorg.refcodes.web.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup
-
toMediaTypeFactory
public org.refcodes.web.MediaTypeFactory toMediaTypeFactory(org.refcodes.web.MediaType aMediaType) - Specified by:
toMediaTypeFactoryin interfaceorg.refcodes.web.MediaTypeFactoryLookup
-
getFactoryMediaTypes
public org.refcodes.web.MediaType[] getFactoryMediaTypes()- Specified by:
getFactoryMediaTypesin interfaceorg.refcodes.web.MediaTypeFactoryLookup
-
dispose
public void dispose()- Specified by:
disposein interfaceorg.refcodes.mixin.Disposable- Overrides:
disposein classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
-
onHttpRequest
protected void onHttpRequest(InetSocketAddress aLocalAddress, InetSocketAddress aRemoteAddress, org.refcodes.web.HttpMethod aHttpMethod, org.refcodes.web.Url aUrl, org.refcodes.web.RequestHeaderFields aRequestHeaderFields, InputStream aHttpInputStream, org.refcodes.web.HttpServerResponse aHttpServerResponse) throws org.refcodes.web.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:
org.refcodes.web.HttpStatusException- thrown in case of anRestEndpointresponsible for the given request encountered a problem or noneRestEndpointfelt responsible to produce aHttpServerResponse.
-
toNegotiatedContenType
protected org.refcodes.web.ContentType toNegotiatedContenType(org.refcodes.web.RequestHeaderFields aRequestHeaderFields) 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, org.refcodes.web.RequestHeaderFields aRequestHeaderFields, org.refcodes.web.ResponseHeaderFields aResponseHeaderFields) throws org.refcodes.exception.MarshalException, org.refcodes.web.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:
org.refcodes.exception.MarshalException- thrown when marshaling / serializing an object failed.org.refcodes.web.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(org.refcodes.web.HttpRequest aEvent, RestEndpoint aObserver, org.refcodes.controlflow.ExecutionStrategy aExecutionStrategy) throws org.refcodes.exception.VetoException - Specified by:
fireEventin classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest> - Throws:
org.refcodes.exception.VetoException
-
doRequestCorrelation
protected void doRequestCorrelation(org.refcodes.web.RequestHeaderFields aRequestHeaderFields, org.refcodes.web.HttpServerResponse aServerResponse) Do request correlation.- Parameters:
aRequestHeaderFields- the request Header-FieldsaServerResponse- the server response
-
doSessionCorrelation
protected void doSessionCorrelation(org.refcodes.web.RequestHeaderFields aRequestHeaderFields, org.refcodes.web.HttpServerResponse aServerResponse) Do session correlation.- Parameters:
aRequestHeaderFields- the request Header-FieldsaServerResponse- the server response
-