use of org.springframework.http.server.RequestPath in project spring-framework by spring-projects.
the class ServletRequestPathFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
RequestPath previousRequestPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
ServletRequestPathUtils.parseAndCache((HttpServletRequest) request);
try {
chain.doFilter(request, response);
} finally {
ServletRequestPathUtils.setParsedRequestPath(previousRequestPath, request);
}
}
use of org.springframework.http.server.RequestPath in project spring-framework by spring-projects.
the class DispatcherServletTests method parsedRequestPathIsRestoredOnForward.
// gh-26318
@Test
public void parsedRequestPathIsRestoredOnForward() throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(PathPatternParserConfig.class);
DispatcherServlet servlet = new DispatcherServlet(context);
servlet.init(servletConfig);
RequestPath previousRequestPath = RequestPath.parse("/", null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
request.setDispatcherType(DispatcherType.FORWARD);
request.setAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE, previousRequestPath);
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getContentAsString()).isEqualTo("test-body");
assertThat(request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE)).isSameAs(previousRequestPath);
}
use of org.springframework.http.server.RequestPath in project spring-framework by spring-projects.
the class ServletRequestPathUtils method getCachedPath.
// Methods to select either parsed RequestPath or resolved String lookupPath
/**
* Return the {@link UrlPathHelper#resolveAndCacheLookupPath pre-resolved}
* String lookupPath or the {@link #parseAndCache(HttpServletRequest)
* pre-parsed} {@code RequestPath}.
* <p>In Spring MVC, when at least one {@code HandlerMapping} has parsed
* {@code PathPatterns} enabled, the {@code DispatcherServlet} eagerly parses
* and caches the {@code RequestPath} and the same can be also done earlier with
* {@link org.springframework.web.filter.ServletRequestPathFilter
* ServletRequestPathFilter}. In other cases where {@code HandlerMapping}s
* use String pattern matching with {@code PathMatcher}, the String
* lookupPath is resolved separately by each {@code HandlerMapping}.
* @param request the current request
* @return a String lookupPath or a {@code RequestPath}
* @throws IllegalArgumentException if neither is available
*/
public static Object getCachedPath(ServletRequest request) {
// The RequestPath is pre-parsed if any HandlerMapping uses PathPatterns.
// The lookupPath is re-resolved or cleared per HandlerMapping.
// So check for lookupPath first.
String lookupPath = (String) request.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
if (lookupPath != null) {
return lookupPath;
}
RequestPath requestPath = (RequestPath) request.getAttribute(PATH_ATTRIBUTE);
if (requestPath != null) {
return requestPath.pathWithinApplication();
}
throw new IllegalArgumentException("Neither a pre-parsed RequestPath nor a pre-resolved String lookupPath is available.");
}
use of org.springframework.http.server.RequestPath in project flow by vaadin.
the class EndpointUtil method getEndpoint.
private Optional<Method> getEndpoint(HttpServletRequest request) {
PathPatternParser pathParser = new PathPatternParser();
PathPattern pathPattern = pathParser.parse(endpointProperties.getEndpointPrefix() + EndpointController.ENDPOINT_METHODS);
RequestPath requestPath = ServletRequestPathUtils.parseAndCache(request);
PathContainer pathWithinApplication = requestPath.pathWithinApplication();
PathMatchInfo matchInfo = pathPattern.matchAndExtract(pathWithinApplication);
if (matchInfo == null) {
return Optional.empty();
}
Map<String, String> uriVariables = matchInfo.getUriVariables();
String endpointName = uriVariables.get("endpoint");
String endpointMethod = uriVariables.get("method");
EndpointRegistry.VaadinEndpointData data = registry.get(endpointName);
if (data == null) {
return Optional.empty();
}
return data.getMethod(endpointMethod);
}
use of org.springframework.http.server.RequestPath in project spring-framework by spring-projects.
the class ServletRequestPathUtilsTests method testParseAndCache.
private void testParseAndCache(String requestUri, String contextPath, String servletPath, String pathWithinApplication) {
MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri);
request.setContextPath(contextPath);
request.setServletPath(servletPath);
request.setHttpServletMapping(new MockHttpServletMapping(pathWithinApplication, contextPath, "myServlet", MappingMatch.PATH));
RequestPath requestPath = ServletRequestPathUtils.parseAndCache(request);
assertThat(requestPath.contextPath().value()).isEqualTo(contextPath);
assertThat(requestPath.pathWithinApplication().value()).isEqualTo(pathWithinApplication);
}
Aggregations