Search in sources :

Example 1 with RequestPath

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);
    }
}
Also used : RequestPath(org.springframework.http.server.RequestPath)

Example 2 with RequestPath

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);
}
Also used : RequestPath(org.springframework.http.server.RequestPath) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with RequestPath

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.");
}
Also used : RequestPath(org.springframework.http.server.RequestPath)

Example 4 with RequestPath

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);
}
Also used : RequestPath(org.springframework.http.server.RequestPath) PathContainer(org.springframework.http.server.PathContainer) PathMatchInfo(org.springframework.web.util.pattern.PathPattern.PathMatchInfo) PathPattern(org.springframework.web.util.pattern.PathPattern) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser)

Example 5 with RequestPath

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);
}
Also used : RequestPath(org.springframework.http.server.RequestPath) MockHttpServletMapping(org.springframework.web.testfixture.servlet.MockHttpServletMapping) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest)

Aggregations

RequestPath (org.springframework.http.server.RequestPath)9 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 IOException (java.io.IOException)1 Test (org.junit.jupiter.api.Test)1 PathContainer (org.springframework.http.server.PathContainer)1 Nullable (org.springframework.lang.Nullable)1 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)1 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)1 HandlerMapping (org.springframework.web.servlet.HandlerMapping)1 MockHttpServletMapping (org.springframework.web.testfixture.servlet.MockHttpServletMapping)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1 PathPattern (org.springframework.web.util.pattern.PathPattern)1 PathMatchInfo (org.springframework.web.util.pattern.PathPattern.PathMatchInfo)1 PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)1