| title | Predicate Handler |
|---|---|
| description | A predicate handler executes one of two handlers depending on a predicate condition. |
The PredicateHandler accepts a Predicate<HttpExchange> and uses that to determine which handler to send the request to.
Predicate<HttpExchange> predicate = new Predicate<>(){
@Override
public boolean test(HttpExchange exchange){
return true;
}
};
HttpHandler trueHandler = new HttpHandler(){
@Override
public void handle(HttpExchange httpExchange){
// handle if true
}
};
HttpHandler falseHandler = new HttpHandler(){
@Override
public void handle(HttpExchange httpExchange){
// handle if false
}
};
HttpServer server = HttpServer.create(8080);
server.createContext("/", new PredicateHandler(predicate, trueHandler, falseHandler));