23
23
import java .util .Properties ;
24
24
25
25
import javax .servlet .http .HttpServletRequest ;
26
+ import javax .servlet .http .MappingMatch ;
26
27
27
28
import org .apache .commons .logging .Log ;
28
29
import org .apache .commons .logging .LogFactory ;
29
30
30
31
import org .springframework .lang .Nullable ;
32
+ import org .springframework .util .ClassUtils ;
31
33
import org .springframework .util .LinkedMultiValueMap ;
32
34
import org .springframework .util .MultiValueMap ;
33
35
import org .springframework .util .StringUtils ;
49
51
*/
50
52
public class UrlPathHelper {
51
53
54
+ private static boolean isServlet4Present =
55
+ ClassUtils .isPresent ("javax.servlet.http.HttpServletMapping" ,
56
+ UrlPathHelper .class .getClassLoader ());
57
+
52
58
/**
53
59
* Special WebSphere request attribute, indicating the original request URI.
54
60
* Preferable over the standard Servlet 2.4 forward attribute on WebSphere,
@@ -154,6 +160,26 @@ protected String getDefaultEncoding() {
154
160
}
155
161
156
162
163
+ /**
164
+ * Variant of {@link #getLookupPathForRequest(HttpServletRequest)} that
165
+ * automates checking for a previously computed lookupPath saved as a
166
+ * request attribute. The attribute is only used for lookup purposes.
167
+ * @param request current HTTP request
168
+ * @param lookupPathAttributeName the request attribute to check
169
+ * @return the lookup path
170
+ * @since 5.2
171
+ * @see org.springframework.web.servlet.HandlerMapping#LOOKUP_PATH
172
+ */
173
+ public String getLookupPathForRequest (HttpServletRequest request , @ Nullable String lookupPathAttributeName ) {
174
+ if (lookupPathAttributeName != null ) {
175
+ String result = (String ) request .getAttribute (lookupPathAttributeName );
176
+ if (result != null ) {
177
+ return result ;
178
+ }
179
+ }
180
+ return getLookupPathForRequest (request );
181
+ }
182
+
157
183
/**
158
184
* Return the mapping lookup path for the given request, within the current
159
185
* servlet mapping if applicable, else within the web application.
@@ -165,7 +191,7 @@ protected String getDefaultEncoding() {
165
191
*/
166
192
public String getLookupPathForRequest (HttpServletRequest request ) {
167
193
// Always use full path within current servlet context?
168
- if (this .alwaysUseFullPath ) {
194
+ if (this .alwaysUseFullPath || skipServletPathDetermination ( request ) ) {
169
195
return getPathWithinApplication (request );
170
196
}
171
197
// Else, use path within current servlet mapping if applicable
@@ -178,24 +204,14 @@ public String getLookupPathForRequest(HttpServletRequest request) {
178
204
}
179
205
}
180
206
181
- /**
182
- * Variant of {@link #getLookupPathForRequest(HttpServletRequest)} that
183
- * automates checking for a previously computed lookupPath saved as a
184
- * request attribute. The attribute is only used for lookup purposes.
185
- * @param request current HTTP request
186
- * @param lookupPathAttributeName the request attribute to check
187
- * @return the lookup path
188
- * @since 5.2
189
- * @see org.springframework.web.servlet.HandlerMapping#LOOKUP_PATH
190
- */
191
- public String getLookupPathForRequest (HttpServletRequest request , @ Nullable String lookupPathAttributeName ) {
192
- if (lookupPathAttributeName != null ) {
193
- String result = (String ) request .getAttribute (lookupPathAttributeName );
194
- if (result != null ) {
195
- return result ;
207
+ private boolean skipServletPathDetermination (HttpServletRequest request ) {
208
+ if (isServlet4Present ) {
209
+ if (request .getHttpServletMapping ().getMappingMatch () != null ) {
210
+ return !request .getHttpServletMapping ().getMappingMatch ().equals (MappingMatch .PATH ) ||
211
+ request .getHttpServletMapping ().getPattern ().equals ("/*" );
196
212
}
197
213
}
198
- return getLookupPathForRequest ( request ) ;
214
+ return false ;
199
215
}
200
216
201
217
/**
@@ -658,4 +674,11 @@ public void setDefaultEncoding(String defaultEncoding) {
658
674
throw new UnsupportedOperationException ();
659
675
}
660
676
};
677
+
678
+
679
+ private static class HttpServletMappingHelper {
680
+
681
+
682
+
683
+ }
661
684
}
0 commit comments