Search in sources :

Example 6 with RequestPath

use of org.springframework.http.server.RequestPath in project spring-framework by spring-projects.

the class HandlerMappingIntrospector method doWithMatchingMapping.

@Nullable
private <T> T doWithMatchingMapping(HttpServletRequest request, boolean ignoreException, BiFunction<HandlerMapping, HandlerExecutionChain, T> matchHandler) throws Exception {
    Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
    boolean parseRequestPath = !this.pathPatternHandlerMappings.isEmpty();
    RequestPath previousPath = null;
    if (parseRequestPath) {
        previousPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
        ServletRequestPathUtils.parseAndCache(request);
    }
    try {
        for (HandlerMapping handlerMapping : this.handlerMappings) {
            HandlerExecutionChain chain = null;
            try {
                chain = handlerMapping.getHandler(request);
            } catch (Exception ex) {
                if (!ignoreException) {
                    throw ex;
                }
            }
            if (chain == null) {
                continue;
            }
            return matchHandler.apply(handlerMapping, chain);
        }
    } finally {
        if (parseRequestPath) {
            ServletRequestPathUtils.setParsedRequestPath(previousPath, request);
        }
    }
    return null;
}
Also used : RequestPath(org.springframework.http.server.RequestPath) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) IOException(java.io.IOException) Nullable(org.springframework.lang.Nullable)

Example 7 with RequestPath

use of org.springframework.http.server.RequestPath in project spring-framework by spring-projects.

the class ServletRequestPathUtils method parseAndCache.

/**
 * Parse the {@link HttpServletRequest#getRequestURI() requestURI} to a
 * {@link RequestPath} and save it in the request attribute
 * {@link #PATH_ATTRIBUTE} for subsequent use with
 * {@link org.springframework.web.util.pattern.PathPattern parsed patterns}.
 * <p>The returned {@code RequestPath} will have both the contextPath and any
 * servletPath prefix omitted from the {@link RequestPath#pathWithinApplication()
 * pathWithinApplication} it exposes.
 * <p>This method is typically called by the {@code DispatcherServlet} to determine
 * if any {@code HandlerMapping} indicates that it uses parsed patterns.
 * After that the pre-parsed and cached {@code RequestPath} can be accessed
 * through {@link #getParsedRequestPath(ServletRequest)}.
 */
public static RequestPath parseAndCache(HttpServletRequest request) {
    RequestPath requestPath = ServletRequestPath.parse(request);
    request.setAttribute(PATH_ATTRIBUTE, requestPath);
    return requestPath;
}
Also used : RequestPath(org.springframework.http.server.RequestPath)

Example 8 with RequestPath

use of org.springframework.http.server.RequestPath in project spring-framework by spring-projects.

the class ServletRequestPathUtils method getParsedRequestPath.

/**
 * Return a {@link #parseAndCache  previously} parsed and cached {@code RequestPath}.
 * @throws IllegalArgumentException if not found
 */
public static RequestPath getParsedRequestPath(ServletRequest request) {
    RequestPath path = (RequestPath) request.getAttribute(PATH_ATTRIBUTE);
    Assert.notNull(path, () -> "Expected parsed RequestPath in request attribute \"" + PATH_ATTRIBUTE + "\".");
    return path;
}
Also used : RequestPath(org.springframework.http.server.RequestPath)

Example 9 with RequestPath

use of org.springframework.http.server.RequestPath in project spring-framework by spring-projects.

the class DispatcherServlet method doService.

/**
 * Exposes the DispatcherServlet-specific request attributes and delegates to {@link #doDispatch}
 * for the actual dispatching.
 */
@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
    logRequest(request);
    // Keep a snapshot of the request attributes in case of an include,
    // to be able to restore the original attributes after the include.
    Map<String, Object> attributesSnapshot = null;
    if (WebUtils.isIncludeRequest(request)) {
        attributesSnapshot = new HashMap<>();
        Enumeration<?> attrNames = request.getAttributeNames();
        while (attrNames.hasMoreElements()) {
            String attrName = (String) attrNames.nextElement();
            if (this.cleanupAfterInclude || attrName.startsWith(DEFAULT_STRATEGIES_PREFIX)) {
                attributesSnapshot.put(attrName, request.getAttribute(attrName));
            }
        }
    }
    // Make framework objects available to handlers and view objects.
    request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
    request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
    request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);
    request.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource());
    if (this.flashMapManager != null) {
        FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);
        if (inputFlashMap != null) {
            request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));
        }
        request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
        request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);
    }
    RequestPath previousRequestPath = null;
    if (this.parseRequestPath) {
        previousRequestPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
        ServletRequestPathUtils.parseAndCache(request);
    }
    try {
        doDispatch(request, response);
    } finally {
        if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
            // Restore the original attribute snapshot, in case of an include.
            if (attributesSnapshot != null) {
                restoreAttributesAfterInclude(request, attributesSnapshot);
            }
        }
        if (this.parseRequestPath) {
            ServletRequestPathUtils.setParsedRequestPath(previousRequestPath, request);
        }
    }
}
Also used : RequestPath(org.springframework.http.server.RequestPath)

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