Search in sources :

Example 1 with DispatcherType

use of jakarta.servlet.DispatcherType in project tomcat by apache.

the class ApplicationFilterFactory method createFilterChain.

/**
 * Construct a FilterChain implementation that will wrap the execution of
 * the specified servlet instance.
 *
 * @param request The servlet request we are processing
 * @param wrapper The wrapper managing the servlet instance
 * @param servlet The servlet instance to be wrapped
 *
 * @return The configured FilterChain instance or null if none is to be
 *         executed.
 */
public static ApplicationFilterChain createFilterChain(ServletRequest request, Wrapper wrapper, Servlet servlet) {
    // If there is no servlet to execute, return null
    if (servlet == null) {
        return null;
    }
    // Create and initialize a filter chain object
    ApplicationFilterChain filterChain = null;
    if (request instanceof Request) {
        Request req = (Request) request;
        if (Globals.IS_SECURITY_ENABLED) {
            // Security: Do not recycle
            filterChain = new ApplicationFilterChain();
        } else {
            filterChain = (ApplicationFilterChain) req.getFilterChain();
            if (filterChain == null) {
                filterChain = new ApplicationFilterChain();
                req.setFilterChain(filterChain);
            }
        }
    } else {
        // Request dispatcher in use
        filterChain = new ApplicationFilterChain();
    }
    filterChain.setServlet(servlet);
    filterChain.setServletSupportsAsync(wrapper.isAsyncSupported());
    // Acquire the filter mappings for this Context
    StandardContext context = (StandardContext) wrapper.getParent();
    filterChain.setDispatcherWrapsSameObject(context.getDispatcherWrapsSameObject());
    FilterMap[] filterMaps = context.findFilterMaps();
    // If there are no filter mappings, we are done
    if ((filterMaps == null) || (filterMaps.length == 0)) {
        return filterChain;
    }
    // Acquire the information we will need to match filter mappings
    DispatcherType dispatcher = (DispatcherType) request.getAttribute(Globals.DISPATCHER_TYPE_ATTR);
    String requestPath = null;
    Object attribute = request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR);
    if (attribute != null) {
        requestPath = attribute.toString();
    }
    String servletName = wrapper.getName();
    // Add the relevant path-mapped filters to this filter chain
    for (FilterMap filterMap : filterMaps) {
        if (!matchDispatcher(filterMap, dispatcher)) {
            continue;
        }
        if (!matchFiltersURL(filterMap, requestPath)) {
            continue;
        }
        ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) context.findFilterConfig(filterMap.getFilterName());
        if (filterConfig == null) {
            // FIXME - log configuration problem
            continue;
        }
        filterChain.addFilter(filterConfig);
    }
    // Add filters that match on servlet name second
    for (FilterMap filterMap : filterMaps) {
        if (!matchDispatcher(filterMap, dispatcher)) {
            continue;
        }
        if (!matchFiltersServlet(filterMap, servletName)) {
            continue;
        }
        ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) context.findFilterConfig(filterMap.getFilterName());
        if (filterConfig == null) {
            // FIXME - log configuration problem
            continue;
        }
        filterChain.addFilter(filterConfig);
    }
    // Return the completed filter chain
    return filterChain;
}
Also used : Request(org.apache.catalina.connector.Request) ServletRequest(jakarta.servlet.ServletRequest) DispatcherType(jakarta.servlet.DispatcherType) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 2 with DispatcherType

use of jakarta.servlet.DispatcherType in project tomcat by apache.

the class ApplicationDispatcher method processRequest.

/**
 * Prepare the request based on the filter configuration.
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param state The RD state
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
private void processRequest(ServletRequest request, ServletResponse response, State state) throws IOException, ServletException {
    DispatcherType disInt = (DispatcherType) request.getAttribute(Globals.DISPATCHER_TYPE_ATTR);
    if (disInt != null) {
        boolean doInvoke = true;
        if (context.getFireRequestListenersOnForwards() && !context.fireRequestInitEvent(request)) {
            doInvoke = false;
        }
        if (doInvoke) {
            if (disInt != DispatcherType.ERROR) {
                state.outerRequest.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR, getCombinedPath());
                state.outerRequest.setAttribute(Globals.DISPATCHER_TYPE_ATTR, DispatcherType.FORWARD);
                invoke(state.outerRequest, response, state);
            } else {
                invoke(state.outerRequest, response, state);
            }
            if (context.getFireRequestListenersOnForwards()) {
                context.fireRequestDestroyEvent(request);
            }
        }
    }
}
Also used : DispatcherType(jakarta.servlet.DispatcherType)

Example 3 with DispatcherType

use of jakarta.servlet.DispatcherType in project spring-framework by spring-projects.

the class AnnotationConfigDispatcherServletInitializerTests method register.

@Test
public void register() throws ServletException {
    initializer.onStartup(servletContext);
    assertThat(servlets.size()).isEqualTo(1);
    assertThat(servlets.get(SERVLET_NAME)).isNotNull();
    DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
    WebApplicationContext wac = servlet.getWebApplicationContext();
    ((AnnotationConfigWebApplicationContext) wac).refresh();
    assertThat(wac.containsBean("bean")).isTrue();
    boolean condition = wac.getBean("bean") instanceof MyBean;
    assertThat(condition).isTrue();
    assertThat(servletRegistrations.size()).isEqualTo(1);
    assertThat(servletRegistrations.get(SERVLET_NAME)).isNotNull();
    MockServletRegistration servletRegistration = servletRegistrations.get(SERVLET_NAME);
    assertThat(servletRegistration.getMappings()).isEqualTo(Collections.singleton(SERVLET_MAPPING));
    assertThat(servletRegistration.getLoadOnStartup()).isEqualTo(1);
    assertThat(servletRegistration.getRunAsRole()).isEqualTo(ROLE_NAME);
    assertThat(servletRegistration.isAsyncSupported()).isTrue();
    assertThat(filterRegistrations.size()).isEqualTo(4);
    assertThat(filterRegistrations.get("hiddenHttpMethodFilter")).isNotNull();
    assertThat(filterRegistrations.get("delegatingFilterProxy")).isNotNull();
    assertThat(filterRegistrations.get("delegatingFilterProxy#0")).isNotNull();
    assertThat(filterRegistrations.get("delegatingFilterProxy#1")).isNotNull();
    for (MockFilterRegistration filterRegistration : filterRegistrations.values()) {
        assertThat(filterRegistration.isAsyncSupported()).isTrue();
        EnumSet<DispatcherType> enumSet = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC);
        assertThat(filterRegistration.getMappings().get(SERVLET_NAME)).isEqualTo(enumSet);
    }
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) DispatcherType(jakarta.servlet.DispatcherType) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 4 with DispatcherType

use of jakarta.servlet.DispatcherType in project spring-security by spring-projects.

the class AbstractRequestMatcherRegistry method dispatcherTypeMatchers.

/**
 * Maps a {@link List} of
 * {@link org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher}
 * instances.
 * @param method the {@link HttpMethod} to use or {@code null} for any
 * {@link HttpMethod}.
 * @param dispatcherTypes the dispatcher types to match against
 * @return the object that is chained after creating the {@link RequestMatcher}
 */
public C dispatcherTypeMatchers(@Nullable HttpMethod method, DispatcherType... dispatcherTypes) {
    Assert.state(!this.anyRequestConfigured, "Can't configure dispatcherTypeMatchers after anyRequest");
    List<RequestMatcher> matchers = new ArrayList<>();
    for (DispatcherType dispatcherType : dispatcherTypes) {
        matchers.add(new DispatcherTypeRequestMatcher(dispatcherType, method));
    }
    return chainRequestMatchers(matchers);
}
Also used : DispatcherTypeRequestMatcher(org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher) RegexRequestMatcher(org.springframework.security.web.util.matcher.RegexRequestMatcher) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AnyRequestMatcher(org.springframework.security.web.util.matcher.AnyRequestMatcher) MvcRequestMatcher(org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) ArrayList(java.util.ArrayList) DispatcherType(jakarta.servlet.DispatcherType) DispatcherTypeRequestMatcher(org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher)

Example 5 with DispatcherType

use of jakarta.servlet.DispatcherType in project tomcat by apache.

the class ApplicationFilterRegistration method addMappingForServletNames.

@Override
public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) {
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(filterDef.getFilterName());
    if (dispatcherTypes != null) {
        for (DispatcherType dispatcherType : dispatcherTypes) {
            filterMap.setDispatcher(dispatcherType.name());
        }
    }
    if (servletNames != null) {
        for (String servletName : servletNames) {
            filterMap.addServletName(servletName);
        }
        if (isMatchAfter) {
            context.addFilterMap(filterMap);
        } else {
            context.addFilterMapBefore(filterMap);
        }
    }
// else error?
}
Also used : DispatcherType(jakarta.servlet.DispatcherType) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Aggregations

DispatcherType (jakarta.servlet.DispatcherType)9 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)3 ServletException (jakarta.servlet.ServletException)2 IOException (java.io.IOException)2 HttpHandler (io.undertow.server.HttpHandler)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 FilterInfo (io.undertow.servlet.api.FilterInfo)1 ServletInfo (io.undertow.servlet.api.ServletInfo)1 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)1 Filter (jakarta.servlet.Filter)1 Dynamic (jakarta.servlet.FilterRegistration.Dynamic)1 Servlet (jakarta.servlet.Servlet)1 ServletRequest (jakarta.servlet.ServletRequest)1 UnavailableException (jakarta.servlet.UnavailableException)1 ArrayList (java.util.ArrayList)1 Container (org.apache.catalina.Container)1 Context (org.apache.catalina.Context)1 ClientAbortException (org.apache.catalina.connector.ClientAbortException)1 Request (org.apache.catalina.connector.Request)1 CloseNowException (org.apache.coyote.CloseNowException)1