/**
   * A Molgenis common web application initializer
   *
   * @param servletContext
   * @param appConfig
   * @param isDasUsed is the molgenis-omx-das module used?
   * @throws ServletException
   */
  protected void onStartup(ServletContext servletContext, Class<?> appConfig, boolean isDasUsed)
      throws ServletException {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(appConfig);

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

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcherServlet =
        servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
    if (dispatcherServlet == null) {
      logger.warn(
          "ServletContext already contains a complete ServletRegistration for servlet 'dispatcher'");
    } else {
      final int maxSize = 32 * 1024 * 1024;
      int loadOnStartup = (isDasUsed ? 2 : 1);
      dispatcherServlet.setLoadOnStartup(loadOnStartup);
      dispatcherServlet.addMapping("/");
      dispatcherServlet.setMultipartConfig(
          new MultipartConfigElement(null, maxSize, maxSize, maxSize));
      dispatcherServlet.setInitParameter("dispatchOptionsRequest", "true");
    }

    // add filters
    javax.servlet.FilterRegistration.Dynamic etagFilter =
        servletContext.addFilter("etagFilter", new ShallowEtagHeaderFilter());
    etagFilter.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "dispatcher");

    // enable use of request scoped beans in FrontController
    servletContext.addListener(new RequestContextListener());
  }
  //	@Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
    System.out.println("##################################################");

    if (rootAppContext != null) {

      servletContext.addFilter("app", "org.apache.tapestry5.TapestryFilter");

      servletContext.setInitParameter("tapestry.app-package", "com.noofinc");
      FilterRegistration.Dynamic tapestryFilter =
          servletContext.addFilter("app", new TapestryFilter());
      tapestryFilter.addMappingForUrlPatterns(null, true, "/*");
      tapestryFilter.setAsyncSupported(true);

      servletContext.addListener(
          new ContextLoaderListener(rootAppContext) {
            @Override
            public void contextInitialized(ServletContextEvent event) {
              // no-op because the application context is already initialized
            }
          });
    } else {
      this.logger.debug(
          "No ContextLoaderListener registered, as "
              + "createRootApplicationContext() did not "
              + "return an application context");
    }
  }
  @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
  }
Esempio n. 4
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("/");
  }
Esempio n. 5
0
 protected FilterRegistration.Dynamic registerServletFilter(
     ServletContext servletContext, Filter filter) {
   String filterName = Conventions.getVariableName(filter);
   FilterRegistration.Dynamic registration = servletContext.addFilter(filterName, filter);
   registration.setAsyncSupported(true);
   registration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
   return registration;
 }
Esempio n. 6
0
  /** Initializes the caching HTTP Headers Filter. */
  private void initCachingHttpHeadersFilter(
      ServletContext servletContext, EnumSet<DispatcherType> disps) {
    log.debug("Registering Caching HTTP Headers Filter");
    FilterRegistration.Dynamic cachingHttpHeadersFilter =
        servletContext.addFilter("cachingHttpHeadersFilter", new CachingHttpHeadersFilter(env));

    cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/assets/*");
    cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/scripts/*");
    cachingHttpHeadersFilter.setAsyncSupported(true);
  }
Esempio n. 7
0
 private void addCacheFilter(ServletContext context) {
   FilterRegistration.Dynamic filter =
       context.addFilter(CACHE_FILTER_NAME, CacheServletFilter.class);
   filter.setAsyncSupported(true);
   String[] mappings =
       new String[] {"/", "*.html", "*.css", "*.js", "*.ejs", "*.less", "*.hbs", "*.map"};
   for (String mapping : mappings) {
     filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, mapping);
   }
 }
Esempio n. 8
0
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    ServletContext servletContext = sce.getServletContext();
    log.info("Web application configuration");

    log.debug("Configuring Spring root application context");
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(ApplicationConfiguration.class);
    rootContext.refresh();

    servletContext.setAttribute(
        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootContext);

    log.debug("Configuring Spring Web application context");
    AnnotationConfigWebApplicationContext dispatcherServletConfig =
        new AnnotationConfigWebApplicationContext();
    dispatcherServletConfig.setParent(rootContext);
    dispatcherServletConfig.register(DispatcherServletConfig.class);

    log.debug("Registering Spring MVC Servlet");
    ServletRegistration.Dynamic dispatcherServlet =
        servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfig));
    dispatcherServlet.addMapping("/tatami/*");
    dispatcherServlet.setLoadOnStartup(2);

    log.debug("Registering Meteor Servlet for online users");

    ServletRegistration.Dynamic meteorServlet =
        servletContext.addServlet("atmosphereServlet", new MeteorServlet());

    meteorServlet.setAsyncSupported(true);
    meteorServlet.addMapping("/realtime/*");
    meteorServlet.setLoadOnStartup(3);

    meteorServlet.setInitParameter(
        "org.atmosphere.servlet", "fr.ippon.tatami.web.atmosphere.users.OnlineUsersServlet");

    meteorServlet.setInitParameter(
        "org.atmosphere.cpr.broadcasterCacheClass", "org.atmosphere.cache.HeaderBroadcasterCache");

    meteorServlet.setInitParameter(
        "org.atmosphere.cpr.broadcastFilterClasses",
        "org.atmosphere.client.TrackMessageSizeFilter");

    meteorServlet.setInitParameter("org.atmosphere.useNative", "true");

    log.debug("Registering Spring Security Filter");
    FilterRegistration.Dynamic springSecurityFilter =
        servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
    EnumSet<DispatcherType> disps =
        EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC);
    springSecurityFilter.addMappingForServletNames(disps, true, "dispatcher", "atmosphereServlet");

    log.debug("Web application fully configured");
  }
 private void configureCrossOriginFilter(final Environment environment, final String urlPattern) {
   // http://www.eclipse.org/jetty/documentation/current/cross-origin-filter.html
   final FilterRegistration.Dynamic filter =
       environment.servlets().addFilter("CORS", CrossOriginFilter.class);
   filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, urlPattern);
   filter.setInitParameter("allowedOrigins", "*");
   filter.setInitParameter(
       "allowedHeaders",
       "Content-Type,Content-Length,Accept,Origin,X-Session,Authorization,User-Agent,X-Requested-With");
   filter.setInitParameter("allowedMethods", "POST,GET,PUT");
 }
Esempio n. 10
0
 private void addGzipFilter(ServletContext context) {
   FilterRegistration.Dynamic filter =
       context.addFilter(GZIP_FILTER_NAME, ApplicationGzipFilter.class);
   filter.setInitParameter(
       "mimeTypes",
       "application/json,text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,image/svg+xml");
   filter.setAsyncSupported(true);
   String[] mappings = new String[] {"/*"};
   for (String mapping : mappings) {
     filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, mapping);
   }
 }
Esempio n. 11
0
 @Override
 public void contextInitialized(ServletContextEvent sce) {
   ServerWebSocketContainer container =
       (ServerWebSocketContainer)
           sce.getServletContext().getAttribute(ServerContainer.class.getName());
   FilterRegistration.Dynamic filter =
       sce.getServletContext().addFilter(FILTER_NAME, JsrWebSocketFilter.class);
   filter.setAsyncSupported(true);
   if (!container.getConfiguredServerEndpoints().isEmpty()) {
     filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
   } else {
     container.setContextToAddFilter((ServletContextImpl) sce.getServletContext());
   }
 }
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(SpringWebAppConfig.class);

    ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("springDispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

    // UtF8 Character Filter.
    FilterRegistration.Dynamic fr =
        servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);

    fr.setInitParameter("encoding", "UTF-8");
    fr.setInitParameter("forceEncoding", "true");
    fr.addMappingForUrlPatterns(null, true, "/*");
  }
Esempio n. 13
0
  public void onStartup(ServletContext container) throws javax.servlet.ServletException {
    AnnotationConfigWebApplicationContext applContext = new AnnotationConfigWebApplicationContext();
    applContext.register(Config.class);

    container.addListener(new ContextLoaderListener(applContext));

    FilterRegistration.Dynamic characterEncodingFilter =
        container.addFilter("characterEncodingFilter", new CharacterEncodingFilter());
    characterEncodingFilter.addMappingForUrlPatterns(
        EnumSet.allOf(DispatcherType.class), true, "/*");
    characterEncodingFilter.setInitParameter("encoding", "UTF-8");
    characterEncodingFilter.setInitParameter("forceEncoding", "true");

    ServletRegistration.Dynamic dispatcher =
        container.addServlet("dispatcher", new DispatcherServlet(applContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
Esempio n. 14
0
  @Override
  public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
    ctx.addServlet(ConfigurationServlet.class.getSimpleName(), new ConfigurationServlet())
        .addMapping(ConfigurationServlet.CONFIGURATION_ENDPOINT);
    ctx.addServlet(
            StagemonitorMetricsServlet.class.getSimpleName(), new StagemonitorMetricsServlet())
        .addMapping("/stagemonitor/metrics");
    ctx.addServlet(RumServlet.class.getSimpleName(), new RumServlet())
        .addMapping("/stagemonitor/public/rum");
    ctx.addServlet(FileServlet.class.getSimpleName(), new FileServlet())
        .addMapping("/stagemonitor/static/*", "/stagemonitor/public/static/*");
    ctx.addServlet(WidgetServlet.class.getSimpleName(), new WidgetServlet())
        .addMapping("/stagemonitor");

    final ServletRegistration.Dynamic requestTraceServlet =
        ctx.addServlet(RequestTraceServlet.class.getSimpleName(), new RequestTraceServlet());
    requestTraceServlet.addMapping("/stagemonitor/request-traces");
    requestTraceServlet.setAsyncSupported(true);

    final FilterRegistration.Dynamic securityFilter =
        ctx.addFilter(
            StagemonitorSecurityFilter.class.getSimpleName(), new StagemonitorSecurityFilter());
    // Add as last filter so that other filters have the chance to set the
    // WebPlugin.STAGEMONITOR_SHOW_WIDGET request attribute that overrides the widget visibility.
    // That way the application can decide whether a particular user is allowed to see the widget.P
    securityFilter.addMappingForUrlPatterns(
        EnumSet.of(DispatcherType.REQUEST), true, "/stagemonitor/*");
    securityFilter.setAsyncSupported(true);

    final FilterRegistration.Dynamic monitorFilter =
        ctx.addFilter(
            HttpRequestMonitorFilter.class.getSimpleName(), new HttpRequestMonitorFilter());
    monitorFilter.addMappingForUrlPatterns(
        EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
    monitorFilter.setAsyncSupported(true);

    final FilterRegistration.Dynamic userFilter =
        ctx.addFilter(UserNameFilter.class.getSimpleName(), new UserNameFilter());
    // Have this filter run last because user information may be populated by other filters e.g.
    // Spring Security
    userFilter.addMappingForUrlPatterns(
        EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), true, "/*");
    userFilter.setAsyncSupported(true);

    ctx.addListener(MDCListener.class);
    ctx.addListener(MonitoredHttpRequest.StagemonitorServletContextListener.class);
    ctx.addListener(SpringMonitoredHttpRequest.HandlerMappingServletContextListener.class);
    ctx.addListener(SessionCounter.class);
  }
Esempio n. 15
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);
 }
Esempio n. 16
0
  /** Initializes Metrics. */
  private void initMetrics(ServletContext servletContext, EnumSet<DispatcherType> disps) {
    log.debug("Initializing Metrics registries");
    servletContext.setAttribute(InstrumentedFilter.REGISTRY_ATTRIBUTE, metricRegistry);
    servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY, metricRegistry);

    log.debug("Registering Metrics Filter");
    FilterRegistration.Dynamic metricsFilter =
        servletContext.addFilter("webappMetricsFilter", new InstrumentedFilter());

    metricsFilter.addMappingForUrlPatterns(disps, true, "/*");
    metricsFilter.setAsyncSupported(true);

    log.debug("Registering Metrics Servlet");
    ServletRegistration.Dynamic metricsAdminServlet =
        servletContext.addServlet("metricsServlet", new MetricsServlet());

    metricsAdminServlet.addMapping("/metrics/metrics/*");
    metricsAdminServlet.setAsyncSupported(true);
    metricsAdminServlet.setLoadOnStartup(2);
  }
Esempio n. 17
0
 private void addControllerFilter(Class<?> klass)
     throws InstantiationException, IllegalAccessException {
   assert klass != null;
   ControllerFilter filter;
   if (Controller.class.isAssignableFrom(klass)) {
     filter =
         new ControllerFilter(
             controllerFactory.createController(klass.asSubclass(Controller.class)));
   } else {
     SimpleControllerWrapper.checkValidSimpleControllerClass(klass);
     SimpleControllerWrapper simpleController =
         SimpleControllerWrapper.createInstance(controllerFactory.createController(klass));
     configure(simpleController);
     filter = new ControllerFilter(simpleController);
   }
   FilterRegistration.Dynamic dynamic = context.addFilter(klass.getSimpleName(), filter);
   BeforeURL before = klass.getAnnotation(BeforeURL.class);
   EnumSet<DispatcherType> set = EnumSet.of(DispatcherType.REQUEST);
   dynamic.addMappingForUrlPatterns(set, false, before.value());
 }
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

    rootContext.register(ApplicationConfig.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext dispatcherContext =
        new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(WebConfig.class);

    ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

    FilterRegistration.Dynamic hiddenHttpMethodFilter =
        servletContext.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class);
    hiddenHttpMethodFilter.addMappingForUrlPatterns(null, false, "/*");
  }
 private void configureCors(Environment environment) {
   FilterRegistration.Dynamic filter =
       environment.servlets().addFilter("CORS", CrossOriginFilter.class);
   filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
   filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
   filter.setInitParameter(
       CrossOriginFilter.ALLOWED_HEADERS_PARAM,
       "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
   filter.setInitParameter(
       CrossOriginFilter.ALLOWED_METHODS_PARAM, "OPTIONS,GET,PUT,POST,PATCH,DELETE,HEAD");
   filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
   filter.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, "true");
   filter.setInitParameter(CrossOriginFilter.CHAIN_PREFLIGHT_PARAM, "false");
 }
Esempio n. 20
0
  @Override
  public void onStartup(ServletContext container) {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebConfig.class);
    ctx.scan("com.taskstrategy.data", "com.taskstrategy.business");
    ctx.setServletContext(container);
    ctx.refresh();

    container.addListener(new ContextLoaderListener(ctx));
    container.setInitParameter("defaultHtmlEscape", "true");

    FilterRegistration.Dynamic securityFilter =
        container.addFilter(
            "securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"));
    securityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");

    ServletRegistration.Dynamic registration =
        container.addServlet("dispatcher", new DispatcherServlet(ctx));
    registration.setLoadOnStartup(1);
    registration.setAsyncSupported(true);
    registration.addMapping("/");
  }
  @Override
  public void run(InflectorServerConfiguration configuration, Environment environment)
      throws Exception {
    final FilterRegistration.Dynamic cors =
        environment.servlets().addFilter("crossOriginRequsts", CrossOriginFilter.class);
    cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");

    SwaggerInflector inflector =
        new SwaggerInflector(Configuration.read(configuration.getConfig()));
    environment.jersey().getResourceConfig().registerResources(inflector.getResources());

    // add serializers for swagger
    environment.jersey().register(SwaggerSerializers.class);

    // example serializers
    environment.jersey().register(ExampleSerializer.class);

    // mappers
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(new JsonNodeExampleSerializer());
    Json.mapper().registerModule(simpleModule);
    Yaml.mapper().registerModule(simpleModule);
  }
Esempio n. 22
0
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    Dynamic servlet =
        servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
    servlet.setLoadOnStartup(1);
    servlet.addMapping("/*");

    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);
    FilterRegistration.Dynamic characterEncoding =
        servletContext.addFilter("characterEncoding", characterEncodingFilter);
    EnumSet<DispatcherType> dispatcherTypes =
        EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
    characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/");

    FilterRegistration.Dynamic corsFilter =
        servletContext.addFilter("corsFilter", CORSFilter.class);
    corsFilter.addMappingForUrlPatterns(null, false, "/*");

    super.onStartup(servletContext);
  }
  //	@Override
  public void onStartup2(ServletContext servletContext) throws ServletException {

    System.out.println("GreenhouseWebAppInitializer.onStartup()");

    WebApplicationContext context = getContext();

    servletContext.addListener(new ContextLoaderListener(context));
    servletContext.addFilter("app", "org.apache.tapestry5.TapestryFilter");

    servletContext.setInitParameter("tapestry.app-package", "com.noofinc");

    FilterRegistration.Dynamic tapestryFilter =
        servletContext.addFilter("app", new TapestryFilter());
    tapestryFilter.addMappingForUrlPatterns(null, true, "/*");
    tapestryFilter.setAsyncSupported(true);

    ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("servletDispatcher", new DispatcherServlet(context));
    dispatcher.setAsyncSupported(true);

    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/*");
  }
Esempio n. 24
0
 @Override
 protected void doAdditionalModuleStartLogic() throws Exception {
   if (StringUtils.isNotBlank(dispatchServletName)) {
     DispatcherServlet loaderServlet =
         new DispatcherServlet(
             (WebApplicationContext)
                 ((SpringResourceLoader) rootResourceLoader.getResourceLoaders().get(0))
                     .getContext());
     ServletRegistration registration =
         getServletContext().addServlet(dispatchServletName, loaderServlet);
     registration.addMapping("/" + dispatchServletName + "/*");
     if (dispatchServletMappings != null) {
       dispatchServletMappings
           .stream()
           .map(mapping -> "/" + mapping + "/*")
           .forEach(registration::addMapping);
     }
     if (mapFilters) {
       for (String filterName : filtersToMap) {
         FilterRegistration filter = getServletContext().getFilterRegistration(filterName);
         filter.addMappingForServletNames(null, true, dispatchServletName);
       }
     }
     if (enableSpringSecurity) {
       DelegatingFilterProxy filterProxy =
           new DelegatingFilterProxy(
               SPRING_SECURITY_FILTER_CHAIN,
               (WebApplicationContext)
                   ((SpringResourceLoader) rootResourceLoader.getResourceLoaders().get(0))
                       .getContext());
       FilterRegistration.Dynamic securityFilter =
           getServletContext()
               .addFilter(KC_PREFIX + getModuleName() + SPRING_SECURITY_FILTER_PROXY, filterProxy);
       securityFilter.addMappingForServletNames(null, true, dispatchServletName);
     }
   }
 }
Esempio n. 25
0
  /** Initializes the static resources production Filter. */
  private void initStaticResourcesProductionFilter(
      ServletContext servletContext, EnumSet<DispatcherType> disps) {

    log.debug("Registering static resources production Filter");
    FilterRegistration.Dynamic staticResourcesProductionFilter =
        servletContext.addFilter(
            "staticResourcesProductionFilter", new StaticResourcesProductionFilter());

    staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/");
    staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/index.html");
    staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/assets/*");
    staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/scripts/*");
    staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/favicon.ico");
    staticResourcesProductionFilter.setAsyncSupported(true);
  }
Esempio n. 26
0
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setConfigLocation("com.sms.config");

    // 加入logback监听器
    servletContext.addListener(new LogbackConfigListener());

    // 加入spring监听器
    servletContext.addListener(new ContextLoaderListener(context));

    EnumSet<DispatcherType> dispatcherTypes =
        EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);

    // 加入spring mvc配置
    ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("spring-mvc", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

    // 加入OpenEntityManagerInViewFilter
    FilterRegistration.Dynamic openEntityManagerInViewFilter =
        servletContext.addFilter(
            "openEntityManagerInViewFilter", OpenEntityManagerInViewFilter.class);
    openEntityManagerInViewFilter.addMappingForUrlPatterns(dispatcherTypes, true, "/*");

    // 加入security过滤器
    FilterRegistration.Dynamic securityFilterChain =
        servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
    securityFilterChain.addMappingForUrlPatterns(dispatcherTypes, true, "/*");

    // 加入Spring编码过滤器
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);

    FilterRegistration.Dynamic encodingFilter =
        servletContext.addFilter("characterEncodingFilter", characterEncodingFilter);
    encodingFilter.addMappingForUrlPatterns(dispatcherTypes, true, "/");
  }
Esempio n. 27
0
  @Override
  public void run(AcikSozlukConfiguration acikSozlukConfiguration, Environment environment)
      throws Exception {
    //  CORS Settings
    FilterRegistration.Dynamic filter =
        environment.servlets().addFilter("CORS", CrossOriginFilter.class);
    filter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS");
    filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
    filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    filter.setInitParameter(
        CrossOriginFilter.ALLOWED_HEADERS_PARAM,
        "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
    filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
    //  CORS Settings

    final HeaderDAO headerDAO = new HeaderDAO(hibernateBundle.getSessionFactory());
    final EntryDAO entryDAO = new EntryDAO(hibernateBundle.getSessionFactory());
    final WriterDAO writerDAO = new WriterDAO(hibernateBundle.getSessionFactory());

    environment.jersey().register(new HeaderResource(headerDAO, entryDAO, writerDAO));
    environment.jersey().register(new WriterResource(writerDAO, headerDAO, entryDAO));
    environment.jersey().register(new EntryResource(entryDAO, headerDAO, writerDAO));
  }
Esempio n. 28
0
  /**
   * Initialize servlet when application on startup.
   *
   * @param servletContext Get ServletContext.
   * @throws ServletException push ServletException in up.
   */
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebAppConfig.class);
    ctx.register(SecurityConfig.class);
    servletContext.addListener(new ContextLoaderListener(ctx));
    ctx.setServletContext(servletContext);

    Dynamic servlet =
        servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(ctx));
    servlet.setLoadOnStartup(1);
    servlet.addMapping("/");

    FilterRegistration.Dynamic encodingFilter =
        servletContext.addFilter(ENCODING_FILTER_NAME, new CharacterEncodingFilter());
    encodingFilter.setInitParameter("encoding", "UTF-8");
    encodingFilter.setInitParameter("forceEncoding", "true");
    encodingFilter.addMappingForUrlPatterns(null, true, "/*");

    FilterRegistration.Dynamic springSecurityFilterChain =
        servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
    springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");
  }
  WsServerContainer(ServletContext servletContext) {

    this.servletContext = servletContext;

    // Configure servlet context wide defaults
    String value =
        servletContext.getInitParameter(Constants.BINARY_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
    if (value != null) {
      setDefaultMaxBinaryMessageBufferSize(Integer.parseInt(value));
    }

    value = servletContext.getInitParameter(Constants.TEXT_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
    if (value != null) {
      setDefaultMaxTextMessageBufferSize(Integer.parseInt(value));
    }

    value =
        servletContext.getInitParameter(
            Constants.ENFORCE_NO_ADD_AFTER_HANDSHAKE_CONTEXT_INIT_PARAM);
    if (value != null) {
      setEnforceNoAddAfterHandshake(Boolean.parseBoolean(value));
    }
    // Executor config
    int executorCoreSize = 0;
    long executorKeepAliveTimeSeconds = 60;
    value = servletContext.getInitParameter(Constants.EXECUTOR_CORE_SIZE_INIT_PARAM);
    if (value != null) {
      executorCoreSize = Integer.parseInt(value);
    }
    value = servletContext.getInitParameter(Constants.EXECUTOR_KEEPALIVETIME_SECONDS_INIT_PARAM);
    if (value != null) {
      executorKeepAliveTimeSeconds = Long.parseLong(value);
    }

    FilterRegistration.Dynamic fr =
        servletContext.addFilter("Tomcat WebSocket (JSR356) Filter", new WsFilter());
    fr.setAsyncSupported(true);

    EnumSet<DispatcherType> types = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);

    fr.addMappingForUrlPatterns(types, true, "/*");

    // Use a per web application executor for any threads that the WebSocket
    // server code needs to create. Group all of the threads under a single
    // ThreadGroup.
    StringBuffer threadGroupName = new StringBuffer("WebSocketServer-");
    threadGroupName.append(servletContext.getVirtualServerName());
    threadGroupName.append('-');
    if ("".equals(servletContext.getContextPath())) {
      threadGroupName.append("ROOT");
    } else {
      threadGroupName.append(servletContext.getContextPath());
    }
    threadGroup = new ThreadGroup(threadGroupName.toString());
    WsThreadFactory wsThreadFactory = new WsThreadFactory(threadGroup);

    executorService =
        new ThreadPoolExecutor(
            executorCoreSize,
            Integer.MAX_VALUE,
            executorKeepAliveTimeSeconds,
            TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(),
            wsThreadFactory);
  }
Esempio n. 30
0
 private void registerHiddenHttpMethodFilter(ServletContext servletContext) {
   FilterRegistration.Dynamic fr =
       servletContext.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class);
   fr.addMappingForServletNames(
       EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, DISPATCHER_SERVLET_NAME);
 }