Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Latest commit

 

History

History
42 lines (29 loc) · 1.06 KB

File metadata and controls

42 lines (29 loc) · 1.06 KB
title Predicate Handler
description A predicate handler executes one of two handlers depending on a predicate condition.

Predicate Handler

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));