Package ej.restserver

Class RestServer


  • public class RestServer
    extends HTTPServer
    A simple implementation of HTTP server capable of easily exposing endpoints.

    REST servers can always handle requests that target endpoints. Other kind of request are handles by custom request handlers.

    See Also:
    EndpointHandler, RequestHandler
    • Constructor Detail

      • RestServer

        public RestServer​(int port,
                          int maxSimultaneousConnection,
                          int jobCountBySession)
                   throws IOException
        Creates a new server bounded to given port.

        This constructor looks for server socket service implementation using a Service Loader. The returned server socket will be used. If no service is declared, default implementation is used (Java standard server socket).

        Parameters:
        port - the port.
        maxSimultaneousConnection - the maximal number of simultaneously opened connections.
        jobCountBySession - the number of parallel jobs to process by opened sessions.
        Throws:
        IOException - if server cannot be bind to given port.
      • RestServer

        public RestServer​(int port,
                          int maxSimultaneousConnection,
                          int jobCountBySession,
                          ServerSocketFactory serverSocketFactory)
                   throws IOException
        Creates a REST server on top of the given HTTP server.
        Parameters:
        port - the port.
        maxSimultaneousConnection - the maximal number of simultaneously opened connections.
        jobCountBySession - the number of parallel jobs to process by opened sessions.
        serverSocketFactory - the serverSocketFactory to use.
        Throws:
        IOException - if server cannot be bind to given port.
    • Method Detail

      • addEndpoint

        public void addEndpoint​(RestEndpoint endpoint)
        Adds an endpoint to this server.
        Parameters:
        endpoint - the endpoint to add.
      • addRequestResolver

        public void addRequestResolver​(RequestHandler handler)
        Adds a request handler to this server. Given resolver becomes immediately active.
        Parameters:
        handler - the handler to add.
      • getEndpoints

        public List<RestEndpoint> getEndpoints()
        Gets the list of active endpoints.
        Returns:
        an unmodifiable list of server active endpoints.
      • getRequestResolvers

        public List<RequestHandler> getRequestResolvers()
        Gets the list of active request handlers. It contains at least an endpoint handler.
        Returns:
        an non-empty and unmodifiable list of server active request handlers.
        See Also:
        EndpointHandler