Package ej.hoka.http

Class HttpServer


  • public class HttpServer
    extends 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 Detail

      • start

        public void start()
                   throws 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:
        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​(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​(String path,
                        @Nullable
                        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​(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​(String path,
                         @Nullable
                         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​(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​(String path,
                        @Nullable
                        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​(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​(String path,
                           @Nullable
                           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​(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​(String path,
                         @Nullable
                         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​(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​(String path,
                            @Nullable
                            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​(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​(String path,
                            @Nullable
                            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​(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​(String path,
                          @Nullable
                          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​(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​(String path,
                          @Nullable
                          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​(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​(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​(String status)
        immediately Stop the execution and return an HTTP response with the status.
        Parameters:
        status - http status
      • halt

        public static final void halt​(String status,
                                      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
                                        String response)
        Override not found error 404.
        Parameters:
        response - error message returned as text/plain
      • notFoundError

        public final void notFoundError​(@Nullable
                                        String response,
                                        @Nullable
                                        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
                                              String response)
        Override internal server error message 501.
        Parameters:
        response - error message returned as text/plain
      • internalServerError

        public final void internalServerError​(@Nullable
                                              String response,
                                              @Nullable
                                              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
                                    Class<? extends Exception> exception,
                                    @Nullable
                                    RequestHandler handler)
        Map a handler to a specific exception.
        Parameters:
        exception - exception to handle
        handler - exception handler
      • getRoutes

        public final 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 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.