Java Code Examples for javax.servlet.FilterRegistration.Dynamic#setAsyncSupported()

The following examples show how to use javax.servlet.FilterRegistration.Dynamic#setAsyncSupported() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractDispatcherServletInitializer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
	String filterName = Conventions.getVariableName(filter);
	Dynamic registration = servletContext.addFilter(filterName, filter);

	if (registration == null) {
		int counter = 0;
		while (registration == null) {
			if (counter == 100) {
				throw new IllegalStateException("Failed to register filter with name '" + filterName + "'. " +
						"Check if there is another filter registered under the same name.");
			}
			registration = servletContext.addFilter(filterName + "#" + counter, filter);
			counter++;
		}
	}

	registration.setAsyncSupported(isAsyncSupported());
	registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
	return registration;
}
 
Example 2
Source File: AbstractDispatcherServletInitializer.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
	String filterName = Conventions.getVariableName(filter);
	Dynamic registration = servletContext.addFilter(filterName, filter);

	if (registration == null) {
		int counter = 0;
		while (registration == null) {
			if (counter == 100) {
				throw new IllegalStateException("Failed to register filter with name '" + filterName + "'. " +
						"Check if there is another filter registered under the same name.");
			}
			registration = servletContext.addFilter(filterName + "#" + counter, filter);
			counter++;
		}
	}

	registration.setAsyncSupported(isAsyncSupported());
	registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
	return registration;
}
 
Example 3
Source File: AbstractDispatcherServletInitializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
	String filterName = Conventions.getVariableName(filter);
	Dynamic registration = servletContext.addFilter(filterName, filter);
	if (registration == null) {
		int counter = -1;
		while (counter == -1 || registration == null) {
			counter++;
			registration = servletContext.addFilter(filterName + "#" + counter, filter);
			Assert.isTrue(counter < 100,
					"Failed to register filter '" + filter + "'." +
					"Could the same Filter instance have been registered already?");
		}
	}
	registration.setAsyncSupported(isAsyncSupported());
	registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
	return registration;
}
 
Example 4
Source File: AbstractDispatcherServletInitializer.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
	String filterName = Conventions.getVariableName(filter);
	Dynamic registration = servletContext.addFilter(filterName, filter);
	if (registration == null) {
		int counter = -1;
		while (counter == -1 || registration == null) {
			counter++;
			registration = servletContext.addFilter(filterName + "#" + counter, filter);
			Assert.isTrue(counter < 100,
					"Failed to register filter '" + filter + "'." +
					"Could the same Filter instance have been registered already?");
		}
	}
	registration.setAsyncSupported(isAsyncSupported());
	registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
	return registration;
}
 
Example 5
Source File: Initializer.java    From alf.io with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);

    Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler());

    configureSessionCookie(servletContext);
    
    CharacterEncodingFilter cef = new CharacterEncodingFilter();
    cef.setEncoding("UTF-8");
    cef.setForceEncoding(true);
    
    Dynamic characterEncodingFilter = servletContext.addFilter("CharacterEncodingFilter", cef);
    characterEncodingFilter.setAsyncSupported(true);
    characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");

    //force log initialization, then disable it
    XRLog.setLevel(XRLog.EXCEPTION, Level.WARNING);
    XRLog.setLoggingEnabled(false);

}
 
Example 6
Source File: ITSpanCustomizingAsyncHandlerInterceptor.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public void init(ServletContextHandler handler) {
  AnnotationConfigWebApplicationContext appContext =
    new AnnotationConfigWebApplicationContext() {
      // overriding this allows us to register dependencies of TracingHandlerInterceptor
      // without passing static state to a configuration class.
      @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
        beanFactory.registerSingleton("httpTracing", httpTracing);
        super.loadBeanDefinitions(beanFactory);
      }
    };

  appContext.register(Servlet3TestController.class); // the test resource
  appContext.register(TracingConfig.class); // generic tracing setup
  DispatcherServlet servlet = new DispatcherServlet(appContext);
  servlet.setDispatchOptionsRequest(true);
  ServletHolder servletHolder = new ServletHolder(servlet);
  servletHolder.setAsyncSupported(true);
  handler.addServlet(servletHolder, "/*");
  handler.addEventListener(new ContextLoaderListener(appContext));

  // add the trace filter, which lazy initializes a real tracing filter from the spring context
  Dynamic filterRegistration =
    handler.getServletContext().addFilter("tracingFilter", DelegatingTracingFilter.class);
  filterRegistration.setAsyncSupported(true);
  filterRegistration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
 
Example 7
Source File: OpenTracingInitializer.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();

    String skipPatternAttribute = servletContext.getInitParameter(TracingFilter.SKIP_PATTERN);
    if (null != skipPatternAttribute && !skipPatternAttribute.isEmpty()) {
        servletContext.setAttribute(TracingFilter.SKIP_PATTERN, Pattern.compile(skipPatternAttribute));
    }

    logger.info("Registering Tracing Filter");
    Dynamic filterRegistration = servletContext
        .addFilter("tracingFilter", new TracingFilter());
    filterRegistration.setAsyncSupported(true);
    filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
}
 
Example 8
Source File: OpenTracingContextInitializer.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
  ServletContext servletContext = servletContextEvent.getServletContext();
  Dynamic filterRegistration = servletContext
      .addFilter("tracingFilter", new SpanFinishingFilter());
  filterRegistration.setAsyncSupported(true);
  filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
}
 
Example 9
Source File: AbstractHttpSessionApplicationInitializer.java    From spring-session with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the provided filter using the {@link #isAsyncSessionSupported()} and
 * {@link #getSessionDispatcherTypes()}.
 * @param servletContext the servlet context
 * @param insertBeforeOtherFilters should this Filter be inserted before or after
 * other {@link Filter}
 * @param filterName the filter name
 * @param filter the filter
 */
private void registerFilter(ServletContext servletContext, boolean insertBeforeOtherFilters, String filterName,
		Filter filter) {
	Dynamic registration = servletContext.addFilter(filterName, filter);
	if (registration == null) {
		throw new IllegalStateException("Duplicate Filter registration for '" + filterName
				+ "'. Check to ensure the Filter is only configured once.");
	}
	registration.setAsyncSupported(isAsyncSessionSupported());
	EnumSet<DispatcherType> dispatcherTypes = getSessionDispatcherTypes();
	registration.addMappingForUrlPatterns(dispatcherTypes, !insertBeforeOtherFilters, "/*");
}
 
Example 10
Source File: WebAppSecurityInitializer.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
  // add filters
  Dynamic forwardedHeaderFilter =
      servletContext.addFilter("forwardedHeaderFilter", ForwardedHeaderFilter.class);
  forwardedHeaderFilter.setAsyncSupported(true);
  forwardedHeaderFilter.addMappingForUrlPatterns(EnumSet.of(REQUEST, ERROR, ASYNC), false, "*");
}
 
Example 11
Source File: ITSpanCustomizingApplicationEventListener.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void init(ServletContextHandler handler) {
  ResourceConfig config = new ResourceConfig();
  config.register(new TestResource(httpTracing));
  config.register(SpanCustomizingApplicationEventListener.create());
  handler.addServlet(new ServletHolder(new ServletContainer(config)), "/*");

  // add the underlying servlet tracing filter which the event listener decorates with more tags
  Dynamic filterRegistration =
    handler.getServletContext().addFilter("tracingFilter", TracingFilter.create(httpTracing));
  filterRegistration.setAsyncSupported(true);
  // isMatchAfter=true is required for async tests to pass!
  filterRegistration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
 
Example 12
Source File: MolgenisWebAppInitializer.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
/** A Molgenis common web application initializer */
protected void onStartup(ServletContext servletContext, Class<?> appConfig, int maxFileSize) {
  // Create the 'root' Spring application context
  AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
  rootContext.registerShutdownHook();
  rootContext.setAllowBeanDefinitionOverriding(false);
  rootContext.register(appConfig);

  // Manage the lifecycle of the root application context
  servletContext.addListener(new ContextLoaderListener(rootContext));

  // Register and map the dispatcher servlet
  DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext);
  dispatcherServlet.setDispatchOptionsRequest(true);
  // instead of throwing a 404 when a handler is not found allow for custom handling
  dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

  ServletRegistration.Dynamic dispatcherServletRegistration =
      servletContext.addServlet("dispatcher", dispatcherServlet);
  if (dispatcherServletRegistration == null) {
    LOG.warn(
        "ServletContext already contains a complete ServletRegistration for servlet 'dispatcher'");
  } else {
    final long maxSize = (long) maxFileSize * MB;
    dispatcherServletRegistration.addMapping("/");
    dispatcherServletRegistration.setMultipartConfig(
        new MultipartConfigElement(null, maxSize, maxSize, FILE_SIZE_THRESHOLD));
    dispatcherServletRegistration.setAsyncSupported(true);
  }

  // Add filters
  Dynamic browserDetectionFiler =
      servletContext.addFilter("browserDetectionFilter", BrowserDetectionFilter.class);
  browserDetectionFiler.setAsyncSupported(true);
  browserDetectionFiler.addMappingForUrlPatterns(
      EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), false, "*");

  Dynamic etagFilter = servletContext.addFilter("etagFilter", ShallowEtagHeaderFilter.class);
  etagFilter.setAsyncSupported(true);
  etagFilter.addMappingForServletNames(
      EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), true, "dispatcher");

  // enable use of request scoped beans in FrontController
  servletContext.addListener(new RequestContextListener());

  servletContext.addListener(HttpSessionEventPublisher.class);
}