Java Code Examples for javax.servlet.FilterRegistration#setInitParameter()

The following examples show how to use javax.servlet.FilterRegistration#setInitParameter() . 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: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new ToolListener());
    servletContext.addListener(new SakaiContextLoaderListener(rootContext));
    
    FilterRegistration requestFilterRegistration = servletContext.addFilter("sakai.request", RequestFilter.class);
    requestFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
    requestFilterRegistration.setInitParameter(RequestFilter.CONFIG_UPLOAD_ENABLED, "true");       

    Dynamic servlet = servletContext.addServlet("sakai-site-group-manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example 2
Source File: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new ToolListener());
    servletContext.addListener(new SakaiContextLoaderListener(rootContext));
    
    FilterRegistration requestFilterRegistration = servletContext.addFilter("sakai.request", RequestFilter.class);
    requestFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
    requestFilterRegistration.setInitParameter(RequestFilter.CONFIG_UPLOAD_ENABLED, "true");       

    Dynamic servlet = servletContext.addServlet("sakai-site-group-manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example 3
Source File: FilterRegistrationTest.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test setInitParameters method.
 */
@Test
public void testSetInitParameters() {
    webApp.addFilter("filter", TestFilterRegistrationFilter.class);
    FilterRegistration registration = webApp.getFilterRegistration("filter");
    registration.setInitParameter("name", "value");
    assertTrue(registration.setInitParameters(new HashMap<>()).isEmpty());
}
 
Example 4
Source File: HttpConductorImpl.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
private void addCsrfFilter(ContextHandler handler) throws ServletException {
    CsrfProtectionType type = safeParseCsrfType(CONFIG_CSRF_TYPE);
    switch (type) {
        case NONE:
            break;
        case REFERER:
            FilterRegistration reg = handler.getServletContext().addFilter("CSRFFilter", CsrfProtectionRefererFilter.class);
            reg.addMappingForServletNames(null, false, "*");
            reg.setInitParameter(CsrfProtectionRefererFilter.ALLOWED_REFERERS_PARAM,
                    configurationService.getProperty(CONFIG_CSRF_ALLOWED_REFERERS));
            break;
        default:
            throw new IllegalArgumentException("Invalid " + CONFIG_CSRF_TYPE + " property: " + type);
    }
}
 
Example 5
Source File: HttpConductorImpl.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
private void addCrossOriginFilter(ContextHandler handler) throws ServletException {
    FilterRegistration reg = handler.getServletContext().addFilter("CrossOriginFilter", CrossOriginFilter.class);
    reg.addMappingForServletNames(null, false, "*");
    reg.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM,
                         configurationService.getProperty(CONFIG_XORIGIN_ORIGINS));
    reg.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM,
                         configurationService.getProperty(CONFIG_XORIGIN_METHODS));
    reg.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM,
                         configurationService.getProperty(CONFIG_XORIGIN_HEADERS));
    reg.setInitParameter(CrossOriginFilter.PREFLIGHT_MAX_AGE_PARAM,
                         configurationService.getProperty(CONFIG_XORIGIN_MAX_AGE));
    reg.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM,
                         configurationService.getProperty(CONFIG_XORIGIN_CREDENTIALS));
}
 
Example 6
Source File: WicketWebInitializer.java    From wicket-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
	
	String[] beanNamesForType = applicationContext.getBeanNamesForType(WicketBootWebApplication.class);
	if(beanNamesForType.length != 1){
		throw new IllegalStateException("Could not find exactly one bean for type WicketBootWebApplication " + beanNamesForType.length);
	}
	
	FilterRegistration filter = servletContext.addFilter(WICKET_FILTERNAME, wicketWebInitializerConfig.filterClass());

	// Spring configuration
	filter.setInitParameter(WicketFilter.APP_FACT_PARAM, SpringWebApplicationFactory.class.getName());
	filter.setInitParameter("applicationBean", beanNamesForType[0]);

	filter.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, props.getFilterMappingParam());
	filter.addMappingForUrlPatterns(EnumSet.copyOf( props.getDispatcherTypes() ), false, props.getFilterMappingParam());

	Map<String, String> initParameters = props.getInitParameters();
	for (Entry<String, String> initParam : initParameters.entrySet()) {
		filter.setInitParameter(initParam.getKey(), initParam.getValue());
	}
	
	
	wicketEndpointRepository.add(new WicketAutoConfig.Builder(this.getClass())
			.withDetail("wicketFilterName", WICKET_FILTERNAME)
			.withDetail("wicketFilterClass", wicketWebInitializerConfig.filterClass())
			.withDetail("properties", props)
			.build());
	
	
}
 
Example 7
Source File: WebInitializer.java    From spring-boot-example-wicket with Apache License 2.0 5 votes vote down vote up
@Override
  public void onStartup(ServletContext sc) throws ServletException {
      FilterRegistration filter = sc.addFilter("wicket-filter",
              WicketFilter.class);
      filter.setInitParameter(WicketFilter.APP_FACT_PARAM,
              SpringWebApplicationFactory.class.getName());
      filter.setInitParameter(PARAM_APP_BEAN, "wicketWebApplication");
// This line is the only surprise when comparing to the equivalent
      // web.xml. Without some initialization seems to be missing.
      filter.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*");
      filter.addMappingForUrlPatterns(null, false, "/*");
  }
 
Example 8
Source File: JavaMelodyAutoConfiguration.java    From javamelody with Apache License 2.0 4 votes vote down vote up
/**
 * Registers the JavaMelody {@link MonitoringFilter}. The filter can be overridden completely by creating a custom
 * {@link FilterRegistrationBean} with the name "javamelody-registration" in the application context.
 * @param properties JavaMelodyConfigurationProperties
 * @param servletContext ServletContext
 * @return FilterRegistrationBean
 */
@Bean(name = REGISTRATION_BEAN_NAME)
@ConditionalOnMissingBean(name = REGISTRATION_BEAN_NAME)
public FilterRegistrationBean<MonitoringFilter> monitoringFilter(
		JavaMelodyConfigurationProperties properties, ServletContext servletContext) {
	final FilterRegistrationBean<MonitoringFilter> registrationBean = new FilterRegistrationBean<>();

	// Create the monitoring filter and set its configuration parameters.
	final MonitoringFilter filter;
	if (properties.isManagementEndpointMonitoringEnabled()) {
		// if the management endpoint is enabled, disable the /monitoring reports on the application port
		filter = new MonitoringFilter() {
			@Override
			protected boolean isAllowed(HttpServletRequest request,
					HttpServletResponse response) throws IOException {
				response.sendError(HttpServletResponse.SC_FORBIDDEN, "Forbidden access");
				return false;
			}
		};
	} else {
		filter = new MonitoringFilter();
	}
	filter.setApplicationType("Spring Boot");

	// Wrap the monitoring filter in the registration bean.
	registrationBean.setFilter(filter);
	registrationBean.setAsyncSupported(true);
	registrationBean.setName("javamelody");
	registrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);

	// Set the initialization parameter for the monitoring filter.
	for (final Entry<String, String> parameter : properties.getInitParameters().entrySet()) {
		registrationBean.addInitParameter(parameter.getKey(), parameter.getValue());
	}

	// Set the URL patterns to activate the monitoring filter for.
	registrationBean.addUrlPatterns("/*");

	final FilterRegistration filterRegistration = servletContext
			.getFilterRegistration("javamelody");
	if (filterRegistration != null) {
		// if webapp deployed as war in a container with MonitoringFilter already added by web-fragment.xml,
		// do not try to add it again
		registrationBean.setEnabled(false);
		for (final Map.Entry<String, String> entry : registrationBean.getInitParameters()
				.entrySet()) {
			filterRegistration.setInitParameter(entry.getKey(), entry.getValue());
		}
	}
	return registrationBean;
}