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.servlet.predicate;
20
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25
26 import javax.servlet.DispatcherType;
27
28 import io.undertow.predicate.Predicate;
29 import io.undertow.predicate.PredicateBuilder;
30 import io.undertow.server.HttpServerExchange;
31 import io.undertow.servlet.handlers.ServletRequestContext;
32
33 /**
34  * Predicate that returns true if the dispatcher type matches the specified type.
35  *
36  * @author Stuart Douglas
37  */

38 public class DispatcherTypePredicate implements Predicate {
39
40     public static final DispatcherTypePredicate FORWARD = new DispatcherTypePredicate(DispatcherType.FORWARD);
41     public static final DispatcherTypePredicate INCLUDE = new DispatcherTypePredicate(DispatcherType.INCLUDE);
42     public static final DispatcherTypePredicate REQUEST = new DispatcherTypePredicate(DispatcherType.REQUEST);
43     public static final DispatcherTypePredicate ASYNC = new DispatcherTypePredicate(DispatcherType.ASYNC);
44     public static final DispatcherTypePredicate ERROR = new DispatcherTypePredicate(DispatcherType.ERROR);
45
46
47     private final DispatcherType dispatcherType;
48
49     public DispatcherTypePredicate(final DispatcherType dispatcherType) {
50         this.dispatcherType = dispatcherType;
51     }
52
53     @Override
54     public boolean resolve(final HttpServerExchange value) {
55         return value.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getDispatcherType() == dispatcherType;
56     }
57
58
59     public static class Builder implements PredicateBuilder {
60
61         @Override
62         public String name() {
63             return "dispatcher";
64         }
65
66         @Override
67         public Map<String, Class<?>> parameters() {
68             final Map<String, Class<?>> params = new HashMap<>();
69             params.put("value", String.class);
70             return params;
71         }
72
73         @Override
74         public Set<String> requiredParameters() {
75             final Set<String> params = new HashSet<>();
76             params.add("value");
77             return params;
78         }
79
80         @Override
81         public String defaultParameter() {
82             return "value";
83         }
84
85         @Override
86         public Predicate build(final Map<String, Object> config) {
87             String value = (String) config.get("value");
88             return new DispatcherTypePredicate(DispatcherType.valueOf(value));
89         }
90     }
91
92 }
93