1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.server.handlers;
20
21 import io.undertow.predicate.Predicate;
22 import io.undertow.server.HttpHandler;
23 import io.undertow.server.HttpServerExchange;
24
25 /**
26  * @author Stuart Douglas
27  */

28 public class PredicateHandler implements HttpHandler {
29
30     private volatile Predicate predicate;
31     private volatile HttpHandler trueHandler;
32     private volatile HttpHandler falseHandler;
33
34     public PredicateHandler(final Predicate predicate, final HttpHandler trueHandler, final HttpHandler falseHandler) {
35         this.predicate = predicate;
36         this.trueHandler = trueHandler;
37         this.falseHandler = falseHandler;
38     }
39
40     @Override
41     public void handleRequest(final HttpServerExchange exchange) throws Exception {
42         HttpHandler next = predicate.resolve(exchange) ? trueHandler : falseHandler;
43         next.handleRequest(exchange);
44     }
45
46     public Predicate getPredicate() {
47         return predicate;
48     }
49
50     public PredicateHandler setPredicate(final Predicate predicate) {
51         this.predicate = predicate;
52         return this;
53     }
54
55     public HttpHandler getTrueHandler() {
56         return trueHandler;
57     }
58
59     public PredicateHandler setTrueHandler(final HttpHandler trueHandler) {
60         this.trueHandler = trueHandler;
61         return this;
62     }
63
64     public HttpHandler getFalseHandler() {
65         return falseHandler;
66     }
67
68     public PredicateHandler setFalseHandler(final HttpHandler falseHandler) {
69         this.falseHandler = falseHandler;
70         return this;
71     }
72 }
73