Exemplo n.º 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("/");
  }
Exemplo n.º 2
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");
  }
Exemplo n.º 3
0
  public void onStartup(ServletContext container) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(ServletConfiguration.class);
    ctx.register(AppConfig.class);
    ctx.setServletContext(container);

    ServletRegistration.Dynamic servlet =
        container.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.setLoadOnStartup(1);
    servlet.addMapping("/multiapp/*");
    servlet.addMapping("/multiapp");
    servlet.addMapping("/hello");
  }
  @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.addMapping("/");
    servlet.setLoadOnStartup(1);
  }
 public static WebApplicationContext createJavaConfiguration(ServletContext context) {
   AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
   appContext.setServletContext(context);
   appContext.register(ZenContactConfiguration.class);
   appContext.refresh();
   return appContext;
 }
  @Override
  public void onStartup(ServletContext container) throws ServletException {
    container.getServletRegistration("default").addMapping("/resources/*");

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(RootContextConfiguration.class);
    container.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext servletContext =
        new AnnotationConfigWebApplicationContext();
    servletContext.register(ServletContextConfiguration.class);
    ServletRegistration.Dynamic dispatcher =
        container.addServlet("springDispatcher", new DispatcherServlet(servletContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
  @Override
  public void onStartup(final ServletContext servletContext) throws ServletException {
    try {
      // Force the timezone of application to UTC (required for Hibernate/JDBC)
      TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

      final Context initialContext = new InitialContext();

      final String logLocation =
          (String)
              initialContext.lookup("java:comp/env/osp/osgpAdapterDomainPublicLighting/log-config");
      LogbackConfigurer.initLogging(logLocation);

      final AnnotationConfigWebApplicationContext rootContext =
          new AnnotationConfigWebApplicationContext();
      rootContext.register(ApplicationContext.class);

      servletContext.addListener(new ContextLoaderListener(rootContext));

    } catch (final NamingException e) {
      throw new ServletException("naming exception", e);
    } catch (final FileNotFoundException e) {
      throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
      throw new ServletException("Logback exception", e);
    }
  }
  /**
   * 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());
  }
Exemplo n.º 9
0
 /**
  * Set up default filters for character encoding and cors
  *
  * @param servletContext
  */
 public static void defaultSetup(ServletContext servletContext, Class<?> appConfig) {
   // Create the 'root' Spring application context
   AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
   rootContext.register(appConfig);
   // rootContext.register(ConfigApp.class);
   defaultSetup(servletContext, rootContext);
 }
 @Test
 public void testWithAuthServerAndGlobalMethodSecurity() throws Exception {
   AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
   context.setServletContext(new MockServletContext());
   context.register(ResourceServerAndAuthorizationServerContextAndGlobalMethodSecurity.class);
   context.refresh();
   context.close();
 }
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebConfig.class);
    servletContext.addListener(new ContextLoaderListener(ctx));

    ServletRegistration.Dynamic servlet =
        servletContext.addServlet(DISPATCHER, new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
  }
Exemplo n.º 12
0
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(AppWebConfig.class);
    ctx.setServletContext(servletContext);

    Dynamic dynamic = servletContext.addServlet("web-dispatcher", new DispatcherServlet(ctx));
    dynamic.addMapping("/");
    dynamic.setLoadOnStartup(1);
    dynamic.setAsyncSupported(true);
  }
Exemplo n.º 13
0
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(AppConfig.class);

    ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
Exemplo n.º 14
0
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(AppConfig.class);

    ctx.setServletContext(servletContext);

    Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
  }
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(WebConfig.class);

    ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/*");
    servletContext.addListener(new ContextLoaderListener(rootContext));
  }
  @Override
  public void onStartup(ServletContext cs) {

    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(SpringRootConfig.class);
    // Manage the lifecycle of the root application context
    cs.addListener(new ContextLoaderListener(rootContext));

    // Create the dispatcher servlet's Spring application context
    AnnotationConfigWebApplicationContext dispatcherServlet =
        new AnnotationConfigWebApplicationContext();
    dispatcherServlet.register(MvcConfig.class);

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher =
        cs.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
  private void startServer() throws Exception {
    jetty = new Server(0);

    final AnnotationConfigWebApplicationContext applicationContext =
        new AnnotationConfigWebApplicationContext();
    applicationContext.register(ConfigClass.class, getClass());
    applicationContext.register(configurationClasses());

    final ServletHolder servletHolder =
        new ServletHolder(new DispatcherServlet(applicationContext));
    final ServletContextHandler context = new ServletContextHandler();

    context.setErrorHandler(null); // use Spring exception handler(s)
    context.setContextPath("/");
    context.setBaseResource(Resource.newResource(new ClassPathResource("").getURI().toString()));
    context.setSessionHandler(new SessionHandler());
    context.addServlet(servletHolder, "/");

    jetty.setHandler(context);
    jetty.start();
  }
Exemplo n.º 18
0
  @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, "/*");
  }
Exemplo n.º 19
0
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

    // Déclaration du la classe de configuration du context (dataSource, repositories,
    // spring-security ...)
    rootContext.register(ContextConfig.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));

    // Création du distapcher de servlet
    AnnotationConfigWebApplicationContext dispatcherServlet =
        new AnnotationConfigWebApplicationContext();
    dispatcherServlet.register(WebMvcConfig.class);

    // Déclaration du distapcher de servlet
    ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext webApplicationContext =
        new AnnotationConfigWebApplicationContext();
    webApplicationContext.register(ApplicationConfig.class, WebMvcConfig.class);

    Dynamic dynamc =
        servletContext.addServlet(
            "dispatcherServlet", new DispatcherServlet(webApplicationContext));
    dynamc.addMapping("/api/v1/*");
    dynamc.setLoadOnStartup(1);
  }
Exemplo n.º 21
0
  @Override
  public void onStartup(ServletContext container) {
    // Create the dispatcher servlet's Spring application context
    AnnotationConfigWebApplicationContext dispatcherContext =
        new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(AppConfig.class);

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher =
        container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
  @Override
  public void onStartup(ServletContext ctx) throws ServletException {
    // Register the ContextLoaderListener
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(DistanceConfiguration.class);

    ctx.addListener(new ContextLoaderListener(appContext));

    // Register the Servlet
    ServletRegistration.Dynamic registration = ctx.addServlet("distance", new DistanceServlet());
    registration.setLoadOnStartup(1);
    registration.addMapping("/distance");
  }
Exemplo n.º 23
0
  @Override
  public void run(GPIOConfiguration configuration, Environment environment) {
    // nothing to do yet
    AnnotationConfigWebApplicationContext parent = new AnnotationConfigWebApplicationContext();
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();

    parent.refresh();
    parent.getBeanFactory().registerSingleton("configuration", configuration);
    parent.registerShutdownHook();
    parent.start();

    // the real main app context has a link to the parent context
    ctx.setParent(parent);
    ctx.register(GPIOSpringConfiguration.class);
    ctx.refresh();
    ctx.registerShutdownHook();
    ctx.start();

    // now that Spring is started, let's get all the beans that matter into DropWizard

    // health checks
    Map<String, HealthCheck> healthChecks = ctx.getBeansOfType(HealthCheck.class);
    for (Map.Entry<String, HealthCheck> entry : healthChecks.entrySet()) {
      environment.addHealthCheck(entry.getValue());
    }

    // resources
    Map<String, Object> resources = ctx.getBeansWithAnnotation(Path.class);
    for (Map.Entry<String, Object> entry : resources.entrySet()) {
      environment.addResource(entry.getValue());
    }

    // tasks
    Map<String, Task> tasks = ctx.getBeansOfType(Task.class);
    for (Map.Entry<String, Task> entry : tasks.entrySet()) {
      environment.addTask(entry.getValue());
    }

    // JAX-RS providers
    Map<String, Object> providers = ctx.getBeansWithAnnotation(Provider.class);
    for (Map.Entry<String, Object> entry : providers.entrySet()) {
      environment.addProvider(entry.getValue());
    }

    // last, but not least, let's link Spring to the embedded Jetty in Dropwizard
    environment.addServletListeners(new SpringContextLoaderListener(ctx));

    // activate Spring Security filter
    environment.addFilter(DelegatingFilterProxy.class, "/*").setName("springSecurityFilterChain");
  }
  public DispatcherServletChannelInitializer() throws ServletException {

    MockServletContext servletContext = new MockServletContext();
    MockServletConfig servletConfig = new MockServletConfig(servletContext);

    AnnotationConfigWebApplicationContext wac = new AnnotationConfigWebApplicationContext();
    wac.setServletContext(servletContext);
    wac.setServletConfig(servletConfig);
    wac.register(WebConfig.class);
    wac.refresh();

    this.dispatcherServlet = new DispatcherServlet(wac);
    this.dispatcherServlet.init(servletConfig);
  }
Exemplo n.º 25
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, "/*");
  }
Exemplo n.º 26
0
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.getEnvironment().setActiveProfiles("dev");
    ctx.register(WebAppConfig.class);
    servletContext.addListener(new ContextLoaderListener(ctx));

    ctx.setServletContext(servletContext);

    registerHiddenHttpMethodFilter(servletContext);

    Dynamic servlet =
        servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
  }
 @Test
 public void testCustomAuthenticationEntryPoint() throws Exception {
   tokenStore.storeAccessToken(token, authentication);
   AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
   context.setServletContext(new MockServletContext());
   context.register(AuthenticationEntryPointContext.class);
   context.refresh();
   MockMvc mvc =
       MockMvcBuilders.webAppContextSetup(context)
           .addFilters(
               new DelegatingFilterProxy(
                   context.getBean("springSecurityFilterChain", Filter.class)))
           .build();
   mvc.perform(MockMvcRequestBuilders.get("/").header("Authorization", "Bearer FOO"))
       .andExpect(MockMvcResultMatchers.status().isFound());
   context.close();
 }
  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, "/*");
  }
Exemplo n.º 29
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("/");
  }
 @Test
 public void createLayoutFromConfigClass() throws Exception {
   AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
   context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
   MockServletContext servletContext = new MockServletContext();
   context.setServletContext(servletContext);
   context.refresh();
   ThymeleafView view =
       (ThymeleafView)
           context.getBean(ThymeleafViewResolver.class).resolveViewName("view", Locale.UK);
   MockHttpServletResponse response = new MockHttpServletResponse();
   MockHttpServletRequest request = new MockHttpServletRequest();
   request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
   view.render(Collections.singletonMap("foo", "bar"), request, response);
   String result = response.getContentAsString();
   assertThat(result).contains("<title>Content</title>");
   assertThat(result).contains("<span>bar</span>");
   context.close();
 }