1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.catalina.core;
18
19 import javax.servlet.http.HttpServletMapping;
20 import javax.servlet.http.MappingMatch;
21
22 import org.apache.catalina.mapper.MappingData;
23
24 public class ApplicationMapping {
25
26     private final MappingData mappingData;
27
28     private volatile HttpServletMapping mapping = null;
29
30     public ApplicationMapping(MappingData mappingData) {
31         this.mappingData = mappingData;
32     }
33
34     public HttpServletMapping getHttpServletMapping() {
35         if (mapping == null) {
36             String servletName;
37             if (mappingData.wrapper == null) {
38                 servletName = "";
39             } else {
40                 servletName = mappingData.wrapper.getName();
41             }
42             if (mappingData.matchType == null) {
43                 mapping = new MappingImpl(""""null, servletName);
44             } else {
45                 switch (mappingData.matchType) {
46                     case CONTEXT_ROOT:
47                         mapping = new MappingImpl("""", mappingData.matchType, servletName);
48                         break;
49                     case DEFAULT:
50                         mapping = new MappingImpl("""/", mappingData.matchType, servletName);
51                         break;
52                     case EXACT:
53                         mapping = new MappingImpl(mappingData.wrapperPath.toString().substring(1),
54                                 mappingData.wrapperPath.toString(), mappingData.matchType, servletName);
55                         break;
56                     case EXTENSION:
57                         String path = mappingData.wrapperPath.toString();
58                         int extIndex = path.lastIndexOf('.');
59                         mapping = new MappingImpl(path.substring(1, extIndex),
60                                 "*" + path.substring(extIndex), mappingData.matchType, servletName);
61                         break;
62                     case PATH:
63                         String matchValue;
64                         if (mappingData.pathInfo.isNull()) {
65                             matchValue = null;
66                         } else {
67                             matchValue = mappingData.pathInfo.toString().substring(1);
68                         }
69                         mapping = new MappingImpl(matchValue, mappingData.wrapperPath.toString() + "/*",
70                                 mappingData.matchType, servletName);
71                         break;
72                 }
73             }
74         }
75
76         return mapping;
77     }
78
79     public void recycle() {
80         mapping = null;
81     }
82
83     private static class MappingImpl implements HttpServletMapping {
84
85         private final String matchValue;
86         private final String pattern;
87         private final MappingMatch mappingType;
88         private final String servletName;
89
90         public MappingImpl(String matchValue, String pattern, MappingMatch mappingType,
91                 String servletName) {
92             this.matchValue = matchValue;
93             this.pattern = pattern;
94             this.mappingType = mappingType;
95             this.servletName = servletName;
96         }
97
98         @Override
99         public String getMatchValue() {
100             return matchValue;
101         }
102
103         @Override
104         public String getPattern() {
105             return pattern;
106         }
107
108         @Override
109         public MappingMatch getMappingMatch() {
110             return mappingType;
111         }
112
113         @Override
114         public String getServletName() {
115             return servletName;
116         }
117     }
118 }
119