Class HttpHandlers
HttpHandler
that implement various useful handlers, such as a static response handler,
or a conditional handler that complements one handler with another.
The factory method of(int, Headers, String) provides a
means to create handlers with pre-set static response state. For example, a
jsonHandler that always returns 200 with the same json:
HttpHandlers.of(200,
Headers.of("Content-Type", "application/json"),
Files.readString(Path.of("some.json")));
or a notAllowedHandler that always replies with 405 -
Method Not Allowed, and indicates the set of methods that are allowed:
HttpHandlers.of(405, Headers.of("Allow", "GET"), "");
The functionality of a handler can be extended or enhanced through the
use of handleOrElse,
which allows to complement a given handler. For example, complementing a
jsonHandler with notAllowedHandler:
Predicate<Request> IS_GET = r -> r.getRequestMethod().equals("GET");
var handler = HttpHandlers.handleOrElse(IS_GET, jsonHandler, notAllowedHandler);
The above handleOrElse handler offers an if-else like construct;
if the request method is "GET" then handling of the exchange is delegated to
the jsonHandler, otherwise handling of the exchange is delegated to
the notAllowedHandler.- Since:
- 18
-
Method Summary
Modifier and TypeMethodDescriptionstatic HttpHandlerhandleOrElse(Predicate<Request> handlerTest, HttpHandler handler, HttpHandler fallbackHandler) Complements a conditionalHttpHandlerwith another handler.static HttpHandlerReturns anHttpHandlerthat sends a response comprising the givenstatusCode,headers, andbody.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Method Details
-
handleOrElse
public static HttpHandler handleOrElse(Predicate<Request> handlerTest, HttpHandler handler, HttpHandler fallbackHandler) Complements a conditionalHttpHandlerwith another handler.This method creates a handleOrElse handler; an if-else like construct. Exchanges who's request matches the
handlerTestpredicate are handled by thehandler. All remaining exchanges are handled by thefallbackHandler.Example of a nested handleOrElse handler:
ThePredicate<Request> IS_GET = r -> r.getRequestMethod().equals("GET"); Predicate<Request> WANTS_DIGEST = r -> r.getRequestHeaders().containsKey("Want-Digest"); var h1 = new SomeHandler(); var h2 = HttpHandlers.handleOrElse(IS_GET, new SomeGetHandler(), h1); var h3 = HttpHandlers.handleOrElse(WANTS_DIGEST.and(IS_GET), new SomeDigestHandler(), h2);h3handleOrElse handler delegates handling of the exchange toSomeDigestHandlerif the "Want-Digest" request header is present and the request method isGET, otherwise it delegates handling of the exchange to theh2handler. Theh2handleOrElse handler, in turn, delegates handling of the exchange toSomeGetHandlerif the request method isGET, otherwise it delegates handling of the exchange to theh1handler. Theh1handler handles all exchanges that are not previously delegated to eitherSomeGetHandlerorSomeDigestHandler.- Parameters:
handlerTest- a request predicatehandler- a conditional handlerfallbackHandler- a fallback handler- Returns:
- a handler
- Throws:
NullPointerException- if any argument is null
-
of
Returns anHttpHandlerthat sends a response comprising the givenstatusCode,headers, andbody.This method creates a handler that reads and discards the request body before it sets the response state and sends the response.
headersare the effective headers of the response. The response body bytes are aUTF-8encoded byte sequence ofbody. The response headers are sent with the givenstatusCodeand the body bytes' length (or-1if the body is empty). The body bytes are then sent as response body, unless the body is empty, in which case no response body is sent.- Parameters:
statusCode- a response status codeheaders- a headersbody- a response body string- Returns:
- a handler
- Throws:
IllegalArgumentException- if statusCode is not a positive 3-digit integer, as per rfc2616, section 6.1.1NullPointerException- if headers or body are null
-