public class HttpServer
extends java.lang.Object
Features + limitations:
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".
| Modifier and Type | Class and Description |
|---|---|
static class |
HttpServer.HttpServerBuilder
HttpServer.HttpServerBuilder builder. |
| Modifier and Type | Method and 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.
|
public static HttpServer.HttpServerBuilder builder()
HttpServer instance.HttpServer.HttpServerBuilder instancepublic void start()
throws java.io.IOException
HttpServer (in a dedicated thread): start listening for connections and start workers to
process opened connections.
Multiple start is not allowed.
java.io.IOException - if an error occurs during the creation of the socket.public void stop()
HttpServer. Stops listening for connections. This method blocks until all session jobs are
stopped.public void get(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void get(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void post(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void post(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void put(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void put(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void delete(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void delete(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void head(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void head(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void connect(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void connect(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void options(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void options(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void trace(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void trace(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void patch(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void patch(java.lang.String path,
@Nullable
java.lang.String acceptType,
RequestHandler handler)
path - request pathacceptType - accepted typehandler - request handlerpublic void before(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void before(RequestHandler handler)
handler - request handlerpublic void after(java.lang.String path,
RequestHandler handler)
path - request pathhandler - request handlerpublic void after(RequestHandler handler)
handler - request handlerpublic static final void halt()
public static final void halt(java.lang.String status)
status - http statuspublic static final void halt(java.lang.String status,
java.lang.String body)
status - http statusbody - response bodypublic final void notFoundError(@Nullable
java.lang.String response)
response - error message returned as text/plainpublic final void notFoundError(@Nullable
java.lang.String response,
@Nullable
java.lang.String contentType)
response - error messagecontentType - response content type. application/json for examplepublic final void internalServerError(@Nullable
java.lang.String response)
response - error message returned as text/plainpublic final void internalServerError(@Nullable
java.lang.String response,
@Nullable
java.lang.String contentType)
response - error messagecontentType - response content type. application/json for examplepublic final void exception(@Nullable
java.lang.Class<? extends java.lang.Exception> exception,
@Nullable
RequestHandler handler)
exception - exception to handlehandler - exception handlerpublic final java.util.List<Route> getRoutes()
public int getPort()
@Nullable public java.net.InetAddress getInetAddress()