|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -179,6 +179,27 @@ public static boolean hasCachedPath(ServletRequest request) {
|
179 | 179 | request.getAttribute(UrlPathHelper.PATH_ATTRIBUTE) != null);
|
180 | 180 | }
|
181 | 181 |
|
| 182 | + /** |
| 183 | + * Check if the Servlet is mapped by a path prefix, and if so return that |
| 184 | + * path prefix. |
| 185 | + * @param request the current request |
| 186 | + * @return the prefix, or {@code null} if the Servlet is not mapped by prefix |
| 187 | + * @since 6.2.3 |
| 188 | + */ |
| 189 | + @Nullable |
| 190 | + public static String getServletPathPrefix(HttpServletRequest request) { |
| 191 | + HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING); |
| 192 | + mapping = (mapping != null ? mapping : request.getHttpServletMapping()); |
| 193 | + if (ObjectUtils.nullSafeEquals(mapping.getMappingMatch(), MappingMatch.PATH)) { |
| 194 | + String servletPath = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE); |
| 195 | + servletPath = (servletPath != null ? servletPath : request.getServletPath()); |
| 196 | + servletPath = (servletPath.endsWith("/") ? servletPath.substring(0, servletPath.length() - 1) : servletPath); |
| 197 | + return servletPath; |
| 198 | + } |
| 199 | + return null; |
| 200 | + } |
| 201 | + |
| 202 | + |
182 | 203 |
|
183 | 204 | /**
|
184 | 205 | * Simple wrapper around the default {@link RequestPath} implementation that
|
@@ -251,22 +272,11 @@ public static RequestPath parse(HttpServletRequest request) {
|
251 | 272 | String requestUri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
|
252 | 273 | requestUri = (requestUri != null ? requestUri : request.getRequestURI());
|
253 | 274 | String servletPathPrefix = getServletPathPrefix(request);
|
254 |
| - return (StringUtils.hasText(servletPathPrefix) ? |
255 |
| - new ServletRequestPath(new PathElements(requestUri, request.getContextPath(), servletPathPrefix)) : |
256 |
| - RequestPath.parse(requestUri, request.getContextPath())); |
257 |
| - } |
258 |
| - |
259 |
| - @Nullable |
260 |
| - private static String getServletPathPrefix(HttpServletRequest request) { |
261 |
| - HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING); |
262 |
| - mapping = (mapping != null ? mapping : request.getHttpServletMapping()); |
263 |
| - if (ObjectUtils.nullSafeEquals(mapping.getMappingMatch(), MappingMatch.PATH)) { |
264 |
| - String servletPath = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE); |
265 |
| - servletPath = (servletPath != null ? servletPath : request.getServletPath()); |
266 |
| - servletPath = (servletPath.endsWith("/") ? servletPath.substring(0, servletPath.length() - 1) : servletPath); |
267 |
| - return UriUtils.encodePath(servletPath, StandardCharsets.UTF_8); |
| 275 | + if (!StringUtils.hasLength(servletPathPrefix)) { |
| 276 | + return RequestPath.parse(requestUri, request.getContextPath()); |
268 | 277 | }
|
269 |
| - return null; |
| 278 | + servletPathPrefix = UriUtils.encodePath(servletPathPrefix, StandardCharsets.UTF_8); |
| 279 | + return new ServletRequestPath(new PathElements(requestUri, request.getContextPath(), servletPathPrefix)); |
270 | 280 | }
|
271 | 281 |
|
272 | 282 | record PathElements(String rawPath, @Nullable String contextPath, String servletPathPrefix) {
|
|
0 commit comments