Esempio n. 1
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 static WebApplicationContext createJavaConfiguration(ServletContext context) {
   AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
   appContext.setServletContext(context);
   appContext.register(ZenContactConfiguration.class);
   appContext.refresh();
   return appContext;
 }
 @Test
 public void testWithAuthServerAndGlobalMethodSecurity() throws Exception {
   AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
   context.setServletContext(new MockServletContext());
   context.register(ResourceServerAndAuthorizationServerContextAndGlobalMethodSecurity.class);
   context.refresh();
   context.close();
 }
Esempio n. 4
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 static AnnotationConfigWebApplicationContext debugRefresh(
     AnnotationConfigWebApplicationContext context) {
   EnvironmentTestUtils.addEnvironment(context, "debug:true");
   LoggingApplicationListener logging = new LoggingApplicationListener();
   logging.onApplicationEvent(
       new SpringApplicationBeforeRefreshEvent(new SpringApplication(), context, new String[0]));
   AutoConfigurationReportLoggingInitializer initializer =
       new AutoConfigurationReportLoggingInitializer();
   initializer.initialize(context);
   context.refresh();
   initializer.onApplicationEvent(new ContextRefreshedEvent(context));
   return context;
 }
  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);
  }
 @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();
 }
 @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();
 }
  public static void createAndStartServer() {
    server = new Server(HTTP_SERVER_PORT);

    HashSessionIdManager idmanager = new HashSessionIdManager();
    server.setSessionIdManager(idmanager);

    AnnotationConfigWebApplicationContext applicationContext =
        new AnnotationConfigWebApplicationContext();
    applicationContext.register(JPAApplicationConfiguration.class);
    applicationContext.refresh();

    appContext = applicationContext;

    try {
      server.setHandler(getServletContextHandler(applicationContext));
      server.start();
    } catch (Exception e) {
      log.error("Error starting server", e);
    }
  }
Esempio n. 10
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("/");
  }