Package ej.hoka.http

Class HttpServer


  • public class HttpServer
    extends java.lang.Object
    HTTP Server.

    Features + limitations:

    • CLDC 1.1
    • No fixed configuration files, logging, authorization, encryption.
    • Supports parameter parsing of GET and POST methods
    • Supports both dynamic content and file serving
    • Never caches anything
    • Doesn't limit bandwidth, request time or simultaneous connections
    • Contains a built-in list of most common MIME types
    • All header names are converted to lower case
    • Keep-alive is not supported

    Example:

     // get a new server which uses a ResourceRequestHandler
     HttpServer http = HttpServer.builder() //
                    .port(8080) //
                    .simultaneousConnections(3) //
                    .workerCount(3) //
                    .build();
    
     // register a route
     http.get("/", requestHandler);
    
     // start the server
     http.start();
     

    To parse the request and write the response, a buffer size of 4096 by default is used. To change this value, set the property "hoka.buffer.size".

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void after​(RequestHandler handler)
      Map the handler as an after filter on all paths.
      void after​(java.lang.String path, RequestHandler handler)
      Map the handler as an after filter on the path.
      void before​(RequestHandler handler)
      Map the handler as a before filter on all paths.
      void before​(java.lang.String path, RequestHandler handler)
      Map the handler as a before filter on the path.
      static HttpServer.HttpServerBuilder builder()
      Configure and build an HttpServer instance.
      void connect​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP CONNECT requests on the path.
      void connect​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP CONNECT requests on the path.
      void delete​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP POST requests on the path.
      void delete​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP DELETE requests on the path.
      void exception​(java.lang.Class<? extends java.lang.Exception> exception, RequestHandler handler)
      Map a handler to a specific exception.
      void get​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP GET requests on the path.
      void get​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP GET requests on the path.
      java.net.InetAddress getInetAddress()
      Gets the address to which this server is bound, if any.
      int getPort()
      Gets the port number to which this server is listening to, if any.
      java.util.List<Route> getRoutes()
      Gets the list of registered routes.
      static void halt()
      Immediately Stop the execution and return a 200 OK HTTP response.
      static void halt​(java.lang.String status)
      immediately Stop the execution and return an HTTP response with the status.
      static void halt​(java.lang.String status, java.lang.String body)
      immediately Stop the execution and return an HTTP response with the status and body.
      void head​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP HEAD requests on the path.
      void head​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP HEAD requests on the path.
      void internalServerError​(java.lang.String response)
      Override internal server error message 501.
      void internalServerError​(java.lang.String response, java.lang.String contentType)
      Override internal server error message 501.
      void notFoundError​(java.lang.String response)
      Override not found error 404.
      void notFoundError​(java.lang.String response, java.lang.String contentType)
      Override not found error 404.
      void options​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP OPTIONS requests on the path.
      void options​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP OPTIONS requests on the path.
      void patch​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP PATCH requests on the path.
      void patch​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP PATCH requests on the path.
      void post​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP POST requests on the path.
      void post​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP POST requests on the path.
      void put​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP PUT requests on the path.
      void put​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP PUT requests on the path.
      void start()
      Start the HttpServer (in a dedicated thread): start listening for connections and start workers to process opened connections.
      void stop()
      Stops the HttpServer.
      void trace​(java.lang.String path, RequestHandler handler)
      Map the handler to HTTP TRACE requests on the path.
      void trace​(java.lang.String path, java.lang.String acceptType, RequestHandler handler)
      Map the handler to HTTP TRACE requests on the path.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • start

        public void start()
                   throws java.io.IOException
        Start the HttpServer (in a dedicated thread): start listening for connections and start workers to process opened connections.

        Multiple start is not allowed.

        Throws:
        java.io.IOException - if an error occurs during the creation of the socket.
      • stop

        public void stop()
        Stops the HttpServer. Stops listening for connections. This method blocks until all session jobs are stopped.
      • get

        public void get​(java.lang.String path,
                        RequestHandler handler)
        Map the handler to HTTP GET requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • get

        public void get​(java.lang.String path,
                        @Nullable
                        java.lang.String acceptType,
                        RequestHandler handler)
        Map the handler to HTTP GET requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • post

        public void post​(java.lang.String path,
                         RequestHandler handler)
        Map the handler to HTTP POST requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • post

        public void post​(java.lang.String path,
                         @Nullable
                         java.lang.String acceptType,
                         RequestHandler handler)
        Map the handler to HTTP POST requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • put

        public void put​(java.lang.String path,
                        RequestHandler handler)
        Map the handler to HTTP PUT requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • put

        public void put​(java.lang.String path,
                        @Nullable
                        java.lang.String acceptType,
                        RequestHandler handler)
        Map the handler to HTTP PUT requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • delete

        public void delete​(java.lang.String path,
                           RequestHandler handler)
        Map the handler to HTTP POST requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • delete

        public void delete​(java.lang.String path,
                           @Nullable
                           java.lang.String acceptType,
                           RequestHandler handler)
        Map the handler to HTTP DELETE requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • head

        public void head​(java.lang.String path,
                         RequestHandler handler)
        Map the handler to HTTP HEAD requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • head

        public void head​(java.lang.String path,
                         @Nullable
                         java.lang.String acceptType,
                         RequestHandler handler)
        Map the handler to HTTP HEAD requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • connect

        public void connect​(java.lang.String path,
                            RequestHandler handler)
        Map the handler to HTTP CONNECT requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • connect

        public void connect​(java.lang.String path,
                            @Nullable
                            java.lang.String acceptType,
                            RequestHandler handler)
        Map the handler to HTTP CONNECT requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • options

        public void options​(java.lang.String path,
                            RequestHandler handler)
        Map the handler to HTTP OPTIONS requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • options

        public void options​(java.lang.String path,
                            @Nullable
                            java.lang.String acceptType,
                            RequestHandler handler)
        Map the handler to HTTP OPTIONS requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • trace

        public void trace​(java.lang.String path,
                          RequestHandler handler)
        Map the handler to HTTP TRACE requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • trace

        public void trace​(java.lang.String path,
                          @Nullable
                          java.lang.String acceptType,
                          RequestHandler handler)
        Map the handler to HTTP TRACE requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • patch

        public void patch​(java.lang.String path,
                          RequestHandler handler)
        Map the handler to HTTP PATCH requests on the path.
        Parameters:
        path - request path
        handler - request handler
      • patch

        public void patch​(java.lang.String path,
                          @Nullable
                          java.lang.String acceptType,
                          RequestHandler handler)
        Map the handler to HTTP PATCH requests on the path.
        Parameters:
        path - request path
        acceptType - accepted type
        handler - request handler
      • before

        public void before​(java.lang.String path,
                           RequestHandler handler)
        Map the handler as a before filter on the path.
        Parameters:
        path - request path
        handler - request handler
      • before

        public void before​(RequestHandler handler)
        Map the handler as a before filter on all paths.
        Parameters:
        handler - request handler
      • after

        public void after​(java.lang.String path,
                          RequestHandler handler)
        Map the handler as an after filter on the path.
        Parameters:
        path - request path
        handler - request handler
      • after

        public void after​(RequestHandler handler)
        Map the handler as an after filter on all paths.
        Parameters:
        handler - request handler
      • halt

        public static final void halt()
        Immediately Stop the execution and return a 200 OK HTTP response.
      • halt

        public static final void halt​(java.lang.String status)
        immediately Stop the execution and return an HTTP response with the status.
        Parameters:
        status - http status
      • halt

        public static final void halt​(java.lang.String status,
                                      java.lang.String body)
        immediately Stop the execution and return an HTTP response with the status and body.
        Parameters:
        status - http status
        body - response body
      • notFoundError

        public final void notFoundError​(@Nullable
                                        java.lang.String response)
        Override not found error 404.
        Parameters:
        response - error message returned as text/plain
      • notFoundError

        public final void notFoundError​(@Nullable
                                        java.lang.String response,
                                        @Nullable
                                        java.lang.String contentType)
        Override not found error 404.
        Parameters:
        response - error message
        contentType - response content type. application/json for example
      • internalServerError

        public final void internalServerError​(@Nullable
                                              java.lang.String response)
        Override internal server error message 501.
        Parameters:
        response - error message returned as text/plain
      • internalServerError

        public final void internalServerError​(@Nullable
                                              java.lang.String response,
                                              @Nullable
                                              java.lang.String contentType)
        Override internal server error message 501.
        Parameters:
        response - error message
        contentType - response content type. application/json for example
      • exception

        public final void exception​(@Nullable
                                    java.lang.Class<? extends java.lang.Exception> exception,
                                    @Nullable
                                    RequestHandler handler)
        Map a handler to a specific exception.
        Parameters:
        exception - exception to handle
        handler - exception handler
      • getRoutes

        public final java.util.List<Route> getRoutes()
        Gets the list of registered routes.
        Returns:
        list of registered routes
      • getPort

        public int getPort()
        Gets the port number to which this server is listening to, if any.
        Returns:
        the port number to which this server is listening or-1 if the socket is not bound yet.
      • getInetAddress

        @Nullable
        public java.net.InetAddress getInetAddress()
        Gets the address to which this server is bound, if any.
        Returns:
        the address to which this server is bound,or null if the socket is unbound.