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;
}
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);
}
}
}
}
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);
}
}
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);
}
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?
}
Aggregations