public class HttpServer extends 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(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(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(String path,
RequestHandler handler)
Map the handler to HTTP CONNECT requests on the path.
|
void |
connect(String path,
String acceptType,
RequestHandler handler)
Map the handler to HTTP CONNECT requests on the path.
|
void |
delete(String path,
RequestHandler handler)
Map the handler to HTTP POST requests on the path.
|
void |
delete(String path,
String acceptType,
RequestHandler handler)
Map the handler to HTTP DELETE requests on the path.
|
void |
exception(Class<? extends Exception> exception,
RequestHandler handler)
Map a handler to a specific exception.
|
void |
get(String path,
RequestHandler handler)
Map the handler to HTTP GET requests on the path.
|
void |
get(String path,
String acceptType,
RequestHandler handler)
Map the handler to HTTP GET requests on the path.
|
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.
|
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(String status)
immediately Stop the execution and return an HTTP response with the status.
|
static void |
halt(String status,
String body)
immediately Stop the execution and return an HTTP response with the status and body.
|
void |
head(String path,
RequestHandler handler)
Map the handler to HTTP HEAD requests on the path.
|
void |
head(String path,
String acceptType,
RequestHandler handler)
Map the handler to HTTP HEAD requests on the path.
|
void |
internalServerError(String response)
Override internal server error message 501.
|
void |
internalServerError(String response,
String contentType)
Override internal server error message 501.
|
void |
notFoundError(String response)
Override not found error 404.
|
void |
notFoundError(String response,
String contentType)
Override not found error 404.
|
void |
options(String path,
RequestHandler handler)
Map the handler to HTTP OPTIONS requests on the path.
|
void |
options(String path,
String acceptType,
RequestHandler handler)
Map the handler to HTTP OPTIONS requests on the path.
|
void |
patch(String path,
RequestHandler handler)
Map the handler to HTTP PATCH requests on the path.
|
void |
patch(String path,
String acceptType,
RequestHandler handler)
Map the handler to HTTP PATCH requests on the path.
|
void |
post(String path,
RequestHandler handler)
Map the handler to HTTP POST requests on the path.
|
void |
post(String path,
String acceptType,
RequestHandler handler)
Map the handler to HTTP POST requests on the path.
|
void |
put(String path,
RequestHandler handler)
Map the handler to HTTP PUT requests on the path.
|
void |
put(String path,
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(String path,
RequestHandler handler)
Map the handler to HTTP TRACE requests on the path.
|
void |
trace(String path,
String acceptType,
RequestHandler handler)
Map the handler to HTTP TRACE requests on the path.
|
public void after(RequestHandler handler)
handler
- request handlerpublic void after(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void before(RequestHandler handler)
handler
- request handlerpublic void before(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic static HttpServer.HttpServerBuilder builder()
HttpServer
instance.HttpServer.HttpServerBuilder
instancepublic void connect(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void connect(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handlerpublic void delete(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void delete(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handlerpublic final void exception(@Nullable Class<? extends Exception> exception, @Nullable RequestHandler handler)
exception
- exception to handlehandler
- exception handlerpublic void get(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void get(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handler@Nullable public InetAddress getInetAddress()
public int getPort()
public final List<Route> getRoutes()
public static final void halt()
public static final void halt(String status)
status
- http statuspublic static final void halt(String status, String body)
status
- http statusbody
- response bodypublic void head(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void head(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handlerpublic final void internalServerError(@Nullable String response)
response
- error message returned as text/plainpublic final void internalServerError(@Nullable String response, @Nullable String contentType)
response
- error messagecontentType
- response content type. application/json for examplepublic final void notFoundError(@Nullable String response)
response
- error message returned as text/plainpublic final void notFoundError(@Nullable String response, @Nullable String contentType)
response
- error messagecontentType
- response content type. application/json for examplepublic void options(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void options(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handlerpublic void patch(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void patch(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handlerpublic void post(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void post(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handlerpublic void put(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void put(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handlerpublic void start() throws IOException
HttpServer
(in a dedicated thread): start listening for connections and start workers to
process opened connections.
Multiple start is not allowed.
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 trace(String path, RequestHandler handler)
path
- request pathhandler
- request handlerpublic void trace(String path, @Nullable String acceptType, RequestHandler handler)
path
- request pathacceptType
- accepted typehandler
- request handler