Class HttpRestServerImpl

java.lang.Object
org.refcodes.observer.AbstractObservable<RestEndpoint,org.refcodes.web.HttpRequest>
org.refcodes.rest.AbstractRestServer
org.refcodes.rest.HttpRestServerImpl
All Implemented Interfaces:
org.refcodes.component.Closable, org.refcodes.component.Closable.CloseBuilder<org.refcodes.component.ConnectionComponent.ConnectionComponentBuilder<org.refcodes.web.HttpServerContext,HttpRestServer>>, org.refcodes.component.ConnectionComponent<org.refcodes.web.HttpServerContext>, org.refcodes.component.ConnectionComponent.ConnectionComponentBuilder<org.refcodes.web.HttpServerContext,HttpRestServer>, org.refcodes.component.ConnectionOpenable<org.refcodes.web.HttpServerContext>, org.refcodes.component.ConnectionOpenable.ConnectionOpenBuilder<org.refcodes.web.HttpServerContext,org.refcodes.component.ConnectionComponent.ConnectionComponentBuilder<org.refcodes.web.HttpServerContext,HttpRestServer>>, org.refcodes.component.ConnectionStatusAccessor, org.refcodes.component.LinkComponent, org.refcodes.component.Openable, org.refcodes.component.OpenedAccessor, org.refcodes.io.MaxConnectionsAccessor, org.refcodes.io.MaxConnectionsAccessor.MaxConnectionsBuilder<HttpRestServer>, org.refcodes.io.MaxConnectionsAccessor.MaxConnectionsMutator, org.refcodes.io.MaxConnectionsAccessor.MaxConnectionsProperty, org.refcodes.mixin.Disposable, org.refcodes.mixin.Loggable, org.refcodes.mixin.PortAccessor, org.refcodes.mixin.PortAccessor.PortBuilder<HttpRestServer>, org.refcodes.mixin.PortAccessor.PortMutator, org.refcodes.mixin.PortAccessor.PortProperty, 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, HttpRestServer, RestServer, org.refcodes.runtime.RequestCorrelation<RestServer>, org.refcodes.runtime.SessionCorrelation<RestServer>, org.refcodes.security.KeyStoreDescriptorAccessor, org.refcodes.security.KeyStoreDescriptorAccessor.KeyStoreDescriptorBuilder<HttpRestServer>, org.refcodes.security.KeyStoreDescriptorAccessor.KeyStoreDescriptorMutator, org.refcodes.security.KeyStoreDescriptorAccessor.KeyStoreDescriptorProperty, org.refcodes.web.BaseLocatorAccessor, org.refcodes.web.BaseLocatorAccessor.BaseLocatorBuilder<RestServer>, org.refcodes.web.BaseLocatorAccessor.BaseLocatorMutator, org.refcodes.web.BaseLocatorAccessor.BaseLocatorProperty, org.refcodes.web.BasicAuthObservable<HttpRestServer>, org.refcodes.web.HttpsConnectionRequestObservable<HttpRestServer>, 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, org.refcodes.web.SchemeAccessor, org.refcodes.web.SchemeAccessor.SchemeBuilder<HttpRestServer>, org.refcodes.web.SchemeAccessor.SchemeMutator, org.refcodes.web.SchemeAccessor.SchemeProperty
Direct Known Subclasses:
HttpRestServerSingleton

public class HttpRestServerImpl extends AbstractRestServer implements HttpRestServer
Implementation of the HttpRestServer interface using the HttpServer defined in the com.sun.net.httpserver package. The HttpRestServerImpl is being initialized with some common MediaTypeFactory instances (as implemented by the AbstractRestServer). At the time of writing this document the MediaTypeFactory instances being preconfigured are:
  • JsonMediaTypeFactory
  • XmlMediaTypeFactory
  • TextMediaTypeFactory
  • FormMediaTypeFactory
The HttpRestServerImpl supports HTTP as well as HTTPS protocols as being based on the HttpServer as well as on the HttpsServer. For opening up an HTTPS connection, refer to the methods such as HttpRestServer.open(String, KeyStoreDescriptor, int) or HttpRestServer.open(KeyStoreDescriptor, int) and the like. 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.
  • Field Details

    • CONTEXT_PATH

      protected static final String CONTEXT_PATH
  • Constructor Details

    • HttpRestServerImpl

      public HttpRestServerImpl(org.refcodes.controlflow.ThreadingModel aThreadingModel)
      Constructs a HttpRestServerImpl. Use HttpRestServer.open(int) or similar to make it listen on your port. The provided ThreadingModel defines whether to use a single-threaded or a multi-threaded threading model. Threads are created as daemon threads. For more control on thread generation use the constructor HttpRestServerImpl(ExecutorService).
      Parameters:
      aThreadingModel - Specifies to either use a single-threaded threading model (no ExecutorService) or a multi-threaded threading model with a default ExecutorService as of ControlFlowUtility.createCachedExecutorService(boolean).
    • HttpRestServerImpl

      public HttpRestServerImpl(org.refcodes.controlflow.ThreadingModel aThreadingModel, boolean isVerbose)
      Constructs a HttpRestServerImpl. Use HttpRestServer.open(int) or similar to make it listen on your port. The provided ThreadingModel defines whether to use a single-threaded or a multi-threaded threading model. Threads are created as daemon threads. For more control on thread generation use the constructor HttpRestServerImpl(ExecutorService).
      Parameters:
      aThreadingModel - Specifies to either use a single-threaded threading model (no ExecutorService) or a multi-threaded threading model with a default ExecutorService as of ControlFlowUtility.createCachedExecutorService(boolean).
      isVerbose - When true, any unknown content- and accept-types are logged.
    • HttpRestServerImpl

      public HttpRestServerImpl()
      Constructs a HttpRestServerImpl. Use HttpRestServer.open(int) or similar to make it listen on your port. Uses a multi threaded threading model with a default ExecutorService as of ControlFlowUtility.createCachedExecutorService(boolean). Threads are created as daemon threads. For more control on thread generation use the constructor HttpRestServerImpl(ExecutorService).
    • HttpRestServerImpl

      public HttpRestServerImpl(boolean isVerbose)
      Constructs a HttpRestServerImpl. Use HttpRestServer.open(int) or similar to make it listen on your port. Uses a multi threaded threading model with a default ExecutorService as of ControlFlowUtility.createCachedExecutorService(boolean). Threads are created as daemon threads. For more control on thread generation use the constructor HttpRestServerImpl(ExecutorService).
      Parameters:
      isVerbose - When true, any unknown content- and accept-types are logged.
    • HttpRestServerImpl

      public HttpRestServerImpl(ExecutorService aExecutorService)
      Constructs a HttpRestServerImpl. Use HttpRestServer.open(int) or similar to make it listen on your port. Uses a multi threaded threading model.
      Parameters:
      aExecutorService - An executor service to be used when creating Threads.
    • HttpRestServerImpl

      public HttpRestServerImpl(ExecutorService aExecutorService, boolean isVerbose)
      Constructs a HttpRestServerImpl. Use HttpRestServer.open(int) or similar to make it listen on your port. Uses a multi threaded threading model.
      Parameters:
      aExecutorService - An executor service to be used when creating Threads.
      isVerbose - When true, any unknown content- and accept-types are logged.
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface org.refcodes.component.Closable
      Throws:
      IOException
    • open

      public void open(String aProtocol, org.refcodes.security.KeyStoreDescriptor aStoreDescriptor, int aPort, int aMaxConnections) throws IOException
      Opens the HTTPS sever connection (socket) at the given port allowing the given number of maximum connections at the same time using the provided HTTPS configuration parameters.
      Specified by:
      open in interface HttpRestServer
      Parameters:
      aProtocol - The protocol to use, e.g. "TLS".
      aStoreDescriptor - The store descriptor describing your KeyStore required for HTTPS.
      aPort - The port on which to listen for requests.
      aMaxConnections - The number of maximum connections at the same time.
      Throws:
      IOException - thrown in case something went wrong such as the port being already in use.
    • getConnectionStatus

      public org.refcodes.component.ConnectionStatus getConnectionStatus()
      Specified by:
      getConnectionStatus in interface org.refcodes.component.ConnectionStatusAccessor
    • onConnectionRequest

      public HttpRestServer onConnectionRequest(org.refcodes.web.HttpsConnectionRequestObserver aObserver)
      Specified by:
      onConnectionRequest in interface org.refcodes.web.HttpsConnectionRequestObservable<HttpRestServer>
    • onBasicAuthRequest

      public HttpRestServer onBasicAuthRequest(org.refcodes.web.BasicAuthObserver aBasicAuthObserver)
      Specified by:
      onBasicAuthRequest in interface org.refcodes.web.BasicAuthObservable<HttpRestServer>
    • withRealm

      public HttpRestServer withRealm(String aRealm)
      Specified by:
      withRealm in interface HttpRestServer
      Specified by:
      withRealm in interface org.refcodes.web.RealmAccessor.RealmBuilder<RestServer>
      Specified by:
      withRealm in interface RestServer
    • setPort

      public void setPort(int aPort)
      Specified by:
      setPort in interface org.refcodes.mixin.PortAccessor.PortMutator
    • getPort

      public int getPort()
      Specified by:
      getPort in interface org.refcodes.mixin.PortAccessor
    • setScheme

      public void setScheme(org.refcodes.data.Scheme aScheme)
      Specified by:
      setScheme in interface org.refcodes.web.SchemeAccessor.SchemeMutator
    • getScheme

      public org.refcodes.data.Scheme getScheme()
      Specified by:
      getScheme in interface org.refcodes.web.SchemeAccessor
    • toProtocol

      public String toProtocol()
      Specified by:
      toProtocol in interface org.refcodes.web.SchemeAccessor
    • setProtocol

      public void setProtocol(String aProtocol)
      Specified by:
      setProtocol in interface org.refcodes.web.SchemeAccessor.SchemeMutator
    • getKeyStoreDescriptor

      public org.refcodes.security.KeyStoreDescriptor getKeyStoreDescriptor()
      Specified by:
      getKeyStoreDescriptor in interface org.refcodes.security.KeyStoreDescriptorAccessor
    • setKeyStoreDescriptor

      public void setKeyStoreDescriptor(org.refcodes.security.KeyStoreDescriptor aKeyStoreDescriptor)
      Specified by:
      setKeyStoreDescriptor in interface org.refcodes.security.KeyStoreDescriptorAccessor.KeyStoreDescriptorMutator
    • getMaxConnections

      public int getMaxConnections()
      Specified by:
      getMaxConnections in interface org.refcodes.io.MaxConnectionsAccessor
    • setMaxConnections

      public void setMaxConnections(int aMaxConnections)
      Specified by:
      setMaxConnections in interface org.refcodes.io.MaxConnectionsAccessor.MaxConnectionsMutator
    • getHttpServer

      protected HttpServer getHttpServer()
      Gets the http server.
      Returns:
      the http server
    • open

      protected void open(HttpServer aHttpServer) throws IOException
      A hook to be used when using custom HttpServer ( HttpsServer) by custom open(...) methods of sub-classes of this HttpRestServerImpl. E.g HttpRestServerImpl uses this hook to pre-configure a HttpsServer for HTTPS. The passed HttpServer ( HttpsServer) must already be bound to a port and enabled with the number of concurrent connections as of HttpServer.bind(InetSocketAddress, int).
      Parameters:
      aHttpServer - The HttpServer to be used. E.g. an HttpsServer might be used to enable HTTPS.
      Throws:
      IOException - in case opening with the provided HttpRestServer fails.