Example #1
0
  @Override
  public void onStartup(ServletContext container) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(AppConfig.class);
    ctx.register(SecurityConfig.class);
    container.addListener(new ContextLoaderListener(ctx));

    container
        .addFilter("springSecurityFilterChain", DelegatingFilterProxy.class)
        .addMappingForUrlPatterns(null, false, "/*");
    Map<String, String> initParams = new HashMap<String, String>();
    initParams.put("encoding", "UTF-8");
    initParams.put("forceEncoding", "true");
    FilterRegistration.Dynamic ceFilter =
        container.addFilter("encodingFilter", CharacterEncodingFilter.class);
    ceFilter.setInitParameters(initParams);
    ceFilter.addMappingForUrlPatterns(null, false, "/*");

    ctx.setServletContext(container);

    ServletRegistration.Dynamic servlet =
        container.addServlet("dispatcher", new DispatcherServlet(ctx));

    servlet.setLoadOnStartup(1);
    servlet.addMapping("/");
  }
  @Override
  public void addFilter(final FilterModel filterModel) {
    LOG.debug("add filter [{}]", filterModel);
    final ServletContext servletContext = findOrCreateServletContext(filterModel);
    final FilterRegistration.Dynamic filterRegistration =
        servletContext.addFilter(filterModel.getName(), filterModel.getFilter());
    if (filterModel.getServletNames() != null) {
      filterRegistration.addMappingForServletNames(
          getDispatcherTypes(filterModel), /*
													 * TODO get asynch
													 * supported?
													 */ false, filterModel.getServletNames());
    } else if (filterModel.getUrlPatterns() != null) {
      filterRegistration.addMappingForServletNames(
          getDispatcherTypes(filterModel), /*
													 * TODO get asynch
													 * supported?
													 */ false, filterModel.getUrlPatterns());
    } else {
      throw new AddFilterException(
          "cannot add filter to the context; at least a not empty list of servlet names or URL patterns in exclusive mode must be provided: "
              + filterModel);
    }
    filterRegistration.setInitParameters(filterModel.getInitParams());
    // filterRegistration.setAsyncSupported(filterModel.); TODO FIXME see
    // how to get this info... ? see above
  }
Example #3
0
 /** Initializes the GZip filter. */
 private void initGzipFilter(ServletContext servletContext, EnumSet<DispatcherType> disps) {
   log.debug("Registering GZip Filter");
   FilterRegistration.Dynamic compressingFilter =
       servletContext.addFilter("gzipFilter", new GZipServletFilter());
   Map<String, String> parameters = new HashMap<>();
   compressingFilter.setInitParameters(parameters);
   compressingFilter.addMappingForUrlPatterns(disps, true, "*.css");
   compressingFilter.addMappingForUrlPatterns(disps, true, "*.json");
   compressingFilter.addMappingForUrlPatterns(disps, true, "*.html");
   compressingFilter.addMappingForUrlPatterns(disps, true, "*.js");
   compressingFilter.addMappingForUrlPatterns(disps, true, "*.svg");
   compressingFilter.addMappingForUrlPatterns(disps, true, "*.ttf");
   compressingFilter.addMappingForUrlPatterns(disps, true, "/api/*");
   compressingFilter.addMappingForUrlPatterns(disps, true, "/metrics/*");
   compressingFilter.setAsyncSupported(true);
 }