Package ej.hoka.http
Class HttpServer
- java.lang.Object
-
- ej.hoka.http.HttpServer
-
public class HttpServer extends java.lang.ObjectHTTP 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".
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHttpServer.HttpServerBuilderHttpServer.HttpServerBuilderbuilder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafter(RequestHandler handler)Map the handler as an after filter on all paths.voidafter(java.lang.String path, RequestHandler handler)Map the handler as an after filter on the path.voidbefore(RequestHandler handler)Map the handler as a before filter on all paths.voidbefore(java.lang.String path, RequestHandler handler)Map the handler as a before filter on the path.static HttpServer.HttpServerBuilderbuilder()Configure and build anHttpServerinstance.voidconnect(java.lang.String path, RequestHandler handler)Map the handler to HTTP CONNECT requests on the path.voidconnect(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP CONNECT requests on the path.voiddelete(java.lang.String path, RequestHandler handler)Map the handler to HTTP POST requests on the path.voiddelete(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP DELETE requests on the path.voidexception(java.lang.Class<? extends java.lang.Exception> exception, RequestHandler handler)Map a handler to a specific exception.voidget(java.lang.String path, RequestHandler handler)Map the handler to HTTP GET requests on the path.voidget(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP GET requests on the path.java.net.InetAddressgetInetAddress()Gets the address to which this server is bound, if any.intgetPort()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 voidhalt()Immediately Stop the execution and return a 200 OK HTTP response.static voidhalt(java.lang.String status)immediately Stop the execution and return an HTTP response with the status.static voidhalt(java.lang.String status, java.lang.String body)immediately Stop the execution and return an HTTP response with the status and body.voidhead(java.lang.String path, RequestHandler handler)Map the handler to HTTP HEAD requests on the path.voidhead(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP HEAD requests on the path.voidinternalServerError(java.lang.String response)Override internal server error message 501.voidinternalServerError(java.lang.String response, java.lang.String contentType)Override internal server error message 501.voidnotFoundError(java.lang.String response)Override not found error 404.voidnotFoundError(java.lang.String response, java.lang.String contentType)Override not found error 404.voidoptions(java.lang.String path, RequestHandler handler)Map the handler to HTTP OPTIONS requests on the path.voidoptions(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP OPTIONS requests on the path.voidpatch(java.lang.String path, RequestHandler handler)Map the handler to HTTP PATCH requests on the path.voidpatch(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP PATCH requests on the path.voidpost(java.lang.String path, RequestHandler handler)Map the handler to HTTP POST requests on the path.voidpost(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP POST requests on the path.voidput(java.lang.String path, RequestHandler handler)Map the handler to HTTP PUT requests on the path.voidput(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP PUT requests on the path.voidstart()Start theHttpServer(in a dedicated thread): start listening for connections and start workers to process opened connections.voidstop()Stops theHttpServer.voidtrace(java.lang.String path, RequestHandler handler)Map the handler to HTTP TRACE requests on the path.voidtrace(java.lang.String path, java.lang.String acceptType, RequestHandler handler)Map the handler to HTTP TRACE requests on the path.
-
-
-
Method Detail
-
builder
public static HttpServer.HttpServerBuilder builder()
Configure and build anHttpServerinstance.- Returns:
HttpServer.HttpServerBuilderinstance
-
start
public void start() throws java.io.IOExceptionStart theHttpServer(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 theHttpServer. 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathacceptType- accepted typehandler- 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 pathhandler- 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 pathhandler- 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 statusbody- 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 messagecontentType- 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 messagecontentType- 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 handlehandler- 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.
-
-