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.handlers;
20
21 import javax.servlet.http.MappingMatch;
22
23 /**
24  * @author Stuart Douglas
25  */

26 public class ServletPathMatch {
27
28     private final String matched;
29     private final String remaining;
30     private final boolean requiredWelcomeFileMatch;
31     private final ServletChain servletChain;
32     private final String rewriteLocation;
33     private final Type type;
34
35     public ServletPathMatch(final ServletChain target, final String uri, boolean requiredWelcomeFileMatch) {
36         this.servletChain = target;
37         this.requiredWelcomeFileMatch = requiredWelcomeFileMatch;
38         this.type = Type.NORMAL;
39         this.rewriteLocation = null;
40         if (target.getServletPath() == null) {
41             //the default servlet is always considered to have matched the full path.
42             this.matched = uri;
43             this.remaining = null;
44         } else {
45             this.matched = target.getServletPath();
46             if(uri.length() == matched.length()) {
47                 remaining = null;
48             } else {
49                 remaining = uri.substring(matched.length());
50             }
51         }
52     }
53
54     public ServletPathMatch(final ServletChain target, final String matched, final String remaining, final Type type, final String rewriteLocation) {
55         this.servletChain = target;
56         this.matched = matched;
57         this.remaining = remaining;
58         this.requiredWelcomeFileMatch = false;
59         this.type = type;
60         this.rewriteLocation = rewriteLocation;
61     }
62
63     public String getMatched() {
64         return matched;
65     }
66
67     public String getRemaining() {
68         return remaining;
69     }
70
71     public boolean isRequiredWelcomeFileMatch() {
72         return requiredWelcomeFileMatch;
73     }
74
75     public ServletChain getServletChain() {
76         return servletChain;
77     }
78
79     public String getRewriteLocation() {
80         return rewriteLocation;
81     }
82
83     public Type getType() {
84         return type;
85     }
86
87     public String getMatchString() {
88         return servletChain.getPattern();
89     }
90
91     public MappingMatch getMappingMatch() {
92         return servletChain.getMappingMatch();
93     }
94
95     public enum Type {
96         /**
97          * A normal servlet match, the invocation should proceed as normal
98          */

99         NORMAL,
100         /**
101          * A redirect is required, as the path does not end with a trailing slash
102          */

103         REDIRECT,
104         /**
105          * An internal rewrite is required, because the path matched a welcome file.
106          * The provided match data is the match data after the rewrite.
107          */

108         REWRITE;
109     }
110 }
111