Package org.refcodes.rest
Class AbstractRestServer
- java.lang.Object
-
- org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
-
- org.refcodes.rest.AbstractRestServer
-
- All Implemented Interfaces:
org.refcodes.net.BaseLocatorAccessor,org.refcodes.net.BaseLocatorAccessor.BaseLocatorBuilder<RestServer>,org.refcodes.net.BaseLocatorAccessor.BaseLocatorMutator,org.refcodes.net.BaseLocatorAccessor.BaseLocatorProperty,org.refcodes.net.MediaTypeFactoryLookup,org.refcodes.net.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup,org.refcodes.net.RealmAccessor,org.refcodes.net.RealmAccessor.RealmBuilder<RestServer>,org.refcodes.net.RealmAccessor.RealmMutator,org.refcodes.net.RealmAccessor.RealmProperty,org.refcodes.observer.Observable<RestServer>,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>
- Direct Known Subclasses:
HttpRestServerImpl,LoopbackRestServerImpl
public abstract class AbstractRestServer extends org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest> implements RestServer
Implementation of the base functionality of theRestServerinterface omitting the HTTP handling part being the foundation for variousRestServerimplementations such asHttpRestServerImplorLoopbackRestServerImpl. TheAbstractRestServeris pre-configured with the followingMediaTypeFactoryinstances:JsonMediaTypeFactoryXmlMediaTypeFactoryTextMediaTypeFactoryFormMediaTypeFactory
initMedaTypeFactories(), therein callingaddMediaTypeFactory(MediaTypeFactory)to add (by also invoking super'sinitMedaTypeFactories()) or to set your own (without invoking super'sinitMedaTypeFactories())MediaTypeFactoryinstances.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.refcodes.net.BaseLocatorAccessor
org.refcodes.net.BaseLocatorAccessor.BaseLocatorBuilder<B extends org.refcodes.net.BaseLocatorAccessor.BaseLocatorBuilder<B>>, org.refcodes.net.BaseLocatorAccessor.BaseLocatorMutator, org.refcodes.net.BaseLocatorAccessor.BaseLocatorProperty
-
Nested classes/interfaces inherited from interface org.refcodes.mixin.Disposable
org.refcodes.mixin.Disposable.Disposedable
-
Nested classes/interfaces inherited from interface org.refcodes.rest.HttpExceptionHandlerAccessor
HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<B extends HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<B>>, HttpExceptionHandlerAccessor.HttpExceptionHandlerMutator, HttpExceptionHandlerAccessor.HttpExceptionHandlerProperty
-
Nested classes/interfaces inherited from interface org.refcodes.rest.HttpExceptionHandlingAccessor
HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder<B extends HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder<B>>, HttpExceptionHandlingAccessor.HttpExceptionHandlingMutator, HttpExceptionHandlingAccessor.HttpExceptionHandlingProperty
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean_hasRequestCorrelationprotected boolean_hasSessionCorrelationprotected HttpExceptionHandler_httpExceptionHandlerprotected HttpExceptionHandling_httpExceptionHandling
-
Constructor Summary
Constructors Constructor Description AbstractRestServer()Constructs aAbstractRestServerpre-configured withMediaTypeFactoryinstances for JSON and REST.AbstractRestServer(java.util.concurrent.ExecutorService aExecutorService)CConstructs aAbstractRestServerpre-configured withMediaTypeFactoryinstances for JSON and REST.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddMediaTypeFactory(org.refcodes.net.MediaTypeFactory aMediaTypeFactory)voiddispose()protected voiddoRequestCorrelation(org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.HttpServerResponse aServerResponse)Do request correlation.protected voiddoSessionCorrelation(org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.HttpServerResponse aServerResponse)Do session correlation.protected booleanfireEvent(org.refcodes.net.HttpRequest aEvent, RestEndpoint aObserver, org.refcodes.controlflow.ExecutionStrategy aExecutionStrategy)java.lang.StringgetBaseLocator()org.refcodes.net.MediaType[]getFactoryMediaTypes()HttpExceptionHandlergetHttpExceptionHandler()Retrieves theHttpExceptionHandlerfrom theHttpExceptionHandlerproperty.HttpExceptionHandlinggetHttpExceptionHandling()Retrieves theHttpExceptionHandlingfrom theHttpExceptionHandlingproperty.java.lang.StringgetRealm()booleanhasRequestCorrelation()booleanhasSessionCorrelation()protected voidinitMedaTypeFactories()Adds the defaultMediaTypeFactoryinstances.booleanisObserversActive()java.util.Iterator<RestEndpoint>observers()protected voidonHttpRequest(java.net.InetSocketAddress aLocalAddress, java.net.InetSocketAddress aRemoteAddress, org.refcodes.net.HttpMethod aHttpMethod, org.refcodes.net.Url aUrl, org.refcodes.net.RequestHeaderFields aRequestHeaderFields, java.io.InputStream aHttpInputStream, org.refcodes.net.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(java.lang.String aBaseLocator)voidsetHttpExceptionHandler(HttpExceptionHandler aHttpErrorHandler)Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.voidsetHttpExceptionHandling(HttpExceptionHandling aHttpErrorHandling)Sets theHttpExceptionHandlingfor theHttpExceptionHandlingproperty.voidsetObserversActive(boolean isActive)voidsetRealm(java.lang.String aRealm)voidsetRequestCorrelation(boolean hasRequestCorrelation)voidsetSessionCorrelation(boolean hasSessionCorrelation)booleansubscribeObserver(RestEndpoint aObserver)org.refcodes.net.MediaTypeFactorytoMediaTypeFactory(org.refcodes.net.MediaType aMediaType)protected org.refcodes.net.ContentTypetoNegotiatedContenType(org.refcodes.net.RequestHeaderFields aRequestHeaderFields)Determines the best fitting respone'sContentType.protected byte[]toResponseBody(java.lang.Object aResponse, org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.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, size
-
Methods inherited from interface org.refcodes.rest.HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder
withHttpExceptionHandler, withOnHttpException
-
Methods inherited from interface org.refcodes.rest.HttpExceptionHandlerAccessor.HttpExceptionHandlerMutator
onHttpException
-
Methods inherited from interface org.refcodes.rest.HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder
withHttpExceptionHandling
-
Methods inherited from interface org.refcodes.mixin.Loggable
alert, alert, critical, critical, debug, error, info, notice, panic, trace, warn, warn
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.refcodes.runtime.RequestCorrelation
disableRequestCorrelation, enableRequestCorrelation
-
Methods inherited from interface org.refcodes.rest.RestServer
onDelete, onGet, onPost, onPut, onRequest, onRequest, onRequest, withBaseLocator, withDisableObservers, withDisableRequestCorrelation, withDisableSessionCorrelation, withEnableObservers, withEnableRequestCorrelation, withEnableSessionCorrelation, withObserversActive, withRealm, withRequestCorrelation, withSessionCorrelation
-
-
-
-
Field Detail
-
_hasRequestCorrelation
protected boolean _hasRequestCorrelation
-
_hasSessionCorrelation
protected boolean _hasSessionCorrelation
-
_httpExceptionHandling
protected HttpExceptionHandling _httpExceptionHandling
-
_httpExceptionHandler
protected HttpExceptionHandler _httpExceptionHandler
-
-
Constructor Detail
-
AbstractRestServer
public AbstractRestServer()
Constructs aAbstractRestServerpre-configured withMediaTypeFactoryinstances for JSON and REST.
-
AbstractRestServer
public AbstractRestServer(java.util.concurrent.ExecutorService aExecutorService)
CConstructs aAbstractRestServerpre-configured withMediaTypeFactoryinstances for JSON and REST.- Parameters:
aExecutorService- An executor service to be used when creatingThreads.
-
-
Method Detail
-
initMedaTypeFactories
protected void initMedaTypeFactories()
Adds the defaultMediaTypeFactoryinstances. Can be overridden.
-
getHttpExceptionHandler
public HttpExceptionHandler getHttpExceptionHandler()
Retrieves theHttpExceptionHandlerfrom theHttpExceptionHandlerproperty.- Specified by:
getHttpExceptionHandlerin interfaceHttpExceptionHandlerAccessor- Returns:
- The
HttpExceptionHandlerstored by theHttpExceptionHandlerproperty.
-
setHttpExceptionHandler
public void setHttpExceptionHandler(HttpExceptionHandler aHttpErrorHandler)
Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.- Specified by:
setHttpExceptionHandlerin interfaceHttpExceptionHandlerAccessor.HttpExceptionHandlerMutator- Parameters:
aHttpErrorHandler- TheHttpExceptionHandlerto be stored by theHttpExceptionHandlerproperty.
-
getHttpExceptionHandling
public HttpExceptionHandling getHttpExceptionHandling()
Retrieves theHttpExceptionHandlingfrom theHttpExceptionHandlingproperty.- Specified by:
getHttpExceptionHandlingin interfaceHttpExceptionHandlingAccessor- Returns:
- The
HttpExceptionHandlingstored by theHttpExceptionHandlingproperty.
-
setHttpExceptionHandling
public void setHttpExceptionHandling(HttpExceptionHandling aHttpErrorHandling)
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.net.HttpRequest>
-
isObserversActive
public boolean isObserversActive()
- Specified by:
isObserversActivein interfaceorg.refcodes.observer.Observers<RestEndpoint,RestServer>- Overrides:
isObserversActivein classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
-
observers
public java.util.Iterator<RestEndpoint> observers()
- Specified by:
observersin interfaceorg.refcodes.observer.Observers<RestEndpoint,RestServer>- Overrides:
observersin classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.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
public java.lang.String getRealm()
- Specified by:
getRealmin interfaceorg.refcodes.net.RealmAccessor
-
setRealm
public void setRealm(java.lang.String aRealm)
- Specified by:
setRealmin interfaceorg.refcodes.net.RealmAccessor.RealmMutator
-
getBaseLocator
public java.lang.String getBaseLocator()
- Specified by:
getBaseLocatorin interfaceorg.refcodes.net.BaseLocatorAccessor
-
setBaseLocator
public void setBaseLocator(java.lang.String aBaseLocator)
- Specified by:
setBaseLocatorin interfaceorg.refcodes.net.BaseLocatorAccessor.BaseLocatorMutator
-
subscribeObserver
public boolean subscribeObserver(RestEndpoint aObserver)
- Specified by:
subscribeObserverin interfaceorg.refcodes.observer.Observable<RestServer>- Overrides:
subscribeObserverin classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
-
unsubscribeObserver
public boolean unsubscribeObserver(RestEndpoint aObserver)
- Specified by:
unsubscribeObserverin interfaceorg.refcodes.observer.Observable<RestServer>- Overrides:
unsubscribeObserverin classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
-
addMediaTypeFactory
public boolean addMediaTypeFactory(org.refcodes.net.MediaTypeFactory aMediaTypeFactory)
- Specified by:
addMediaTypeFactoryin interfaceorg.refcodes.net.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup
-
toMediaTypeFactory
public org.refcodes.net.MediaTypeFactory toMediaTypeFactory(org.refcodes.net.MediaType aMediaType)
- Specified by:
toMediaTypeFactoryin interfaceorg.refcodes.net.MediaTypeFactoryLookup
-
getFactoryMediaTypes
public org.refcodes.net.MediaType[] getFactoryMediaTypes()
- Specified by:
getFactoryMediaTypesin interfaceorg.refcodes.net.MediaTypeFactoryLookup
-
dispose
public void dispose()
- Overrides:
disposein classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>
-
onHttpRequest
protected void onHttpRequest(java.net.InetSocketAddress aLocalAddress, java.net.InetSocketAddress aRemoteAddress, org.refcodes.net.HttpMethod aHttpMethod, org.refcodes.net.Url aUrl, org.refcodes.net.RequestHeaderFields aRequestHeaderFields, java.io.InputStream aHttpInputStream, org.refcodes.net.HttpServerResponse aHttpServerResponse) throws org.refcodes.net.HttpStatusExceptionExtensions 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.net.HttpStatusException- thrown in case of anRestEndpointresponsible for the given request encountered a problem or noneRestEndpointfelt responsible to produce aHttpServerResponse.
-
toNegotiatedContenType
protected org.refcodes.net.ContentType toNegotiatedContenType(org.refcodes.net.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(java.lang.Object aResponse, org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.ResponseHeaderFields aResponseHeaderFields) throws org.refcodes.exception.MarshalException, org.refcodes.net.UnsupportedMediaTypeExceptionCreates 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.net.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.net.HttpRequest aEvent, RestEndpoint aObserver, org.refcodes.controlflow.ExecutionStrategy aExecutionStrategy) throws org.refcodes.exception.VetoException- Specified by:
fireEventin classorg.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.net.HttpRequest>- Throws:
org.refcodes.exception.VetoException
-
doRequestCorrelation
protected void doRequestCorrelation(org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.HttpServerResponse aServerResponse)Do request correlation.- Parameters:
aRequestHeaderFields- the request Header-FieldsaServerResponse- the server response
-
doSessionCorrelation
protected void doSessionCorrelation(org.refcodes.net.RequestHeaderFields aRequestHeaderFields, org.refcodes.net.HttpServerResponse aServerResponse)Do session correlation.- Parameters:
aRequestHeaderFields- the request Header-FieldsaServerResponse- the server response
-
-