/
ServletPathMapping.java
154 lines (137 loc) · 4.94 KB
/
ServletPathMapping.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.ee10.servlet;
import jakarta.servlet.http.HttpServletMapping;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.MappingMatch;
import org.eclipse.jetty.http.pathmap.PathSpec;
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
/**
* Implementation of HttpServletMapping.
*
* Represents the application of a {@link ServletPathSpec} to a specific path
* that resulted in a mapping to a {@link jakarta.servlet.Servlet}.
* As well as supporting the standard {@link HttpServletMapping} methods, this
* class also carries fields, which can be precomputed for the implementation
* of {@link HttpServletRequest#getServletPath()} and
* {@link HttpServletRequest#getPathInfo()}
*/
public class ServletPathMapping implements HttpServletMapping
{
private final MappingMatch _mappingMatch;
private final String _matchValue;
private final String _pattern;
private final String _servletName;
private final String _servletPath;
private final String _pathInfo;
public ServletPathMapping(PathSpec pathSpec, String servletName, String pathInContext)
{
_servletName = (servletName == null ? "" : servletName);
_pattern = pathSpec == null ? null : pathSpec.getDeclaration();
if (pathSpec instanceof ServletPathSpec && pathInContext != null)
{
switch (pathSpec.getGroup())
{
case ROOT:
_mappingMatch = MappingMatch.CONTEXT_ROOT;
_matchValue = "";
_servletPath = "";
_pathInfo = "/";
break;
case DEFAULT:
_mappingMatch = MappingMatch.DEFAULT;
_matchValue = "";
_servletPath = pathInContext;
_pathInfo = null;
break;
case EXACT:
_mappingMatch = MappingMatch.EXACT;
_matchValue = _pattern.startsWith("/") ? _pattern.substring(1) : _pattern;
_servletPath = _pattern;
_pathInfo = null;
break;
case PREFIX_GLOB:
_mappingMatch = MappingMatch.PATH;
_servletPath = pathSpec.getPrefix();
// TODO avoid the substring on the known servletPath!
_matchValue = _servletPath.startsWith("/") ? _servletPath.substring(1) : _servletPath;
_pathInfo = pathSpec.getPathInfo(pathInContext);
break;
case SUFFIX_GLOB:
_mappingMatch = MappingMatch.EXTENSION;
int dot = pathInContext.lastIndexOf('.');
_matchValue = pathInContext.substring(pathInContext.startsWith("/") ? 1 : 0, dot);
_servletPath = pathInContext;
_pathInfo = null;
break;
case MIDDLE_GLOB:
default:
throw new IllegalStateException();
}
}
else if (pathSpec != null)
{
_mappingMatch = null;
_servletPath = pathSpec.getPathMatch(pathInContext);
_matchValue = _servletPath.startsWith("/") ? _servletPath.substring(1) : _servletPath;
_pathInfo = pathSpec.getPathInfo(pathInContext);
}
else
{
_mappingMatch = null;
_matchValue = "";
_servletPath = pathInContext;
_pathInfo = null;
}
}
@Override
public String getMatchValue()
{
return _matchValue;
}
@Override
public String getPattern()
{
return _pattern;
}
@Override
public String getServletName()
{
return _servletName;
}
@Override
public MappingMatch getMappingMatch()
{
return _mappingMatch;
}
public String getServletPath()
{
return _servletPath;
}
public String getPathInfo()
{
return _pathInfo;
}
@Override
public String toString()
{
return "ServletPathMapping{" +
"matchValue=" + _matchValue +
", pattern=" + _pattern +
", servletName=" + _servletName +
", mappingMatch=" + _mappingMatch +
", servletPath=" + _servletPath +
", pathInfo=" + _pathInfo +
"}";
}
}