@Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); servletContext.addListener(new ContextLoaderListener(context)); servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter("UTF-8")); DispatcherServlet dispatcherServlet = new DispatcherServlet(context); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("Dispatcher", dispatcherServlet); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); CXFServlet cxf = new CXFServlet(); BusFactory.setDefaultBus(cxf.getBus()); ServletRegistration.Dynamic cxfServlet = servletContext.addServlet("CXFServlet", cxf); cxfServlet.setLoadOnStartup(1); cxfServlet.addMapping("/services/*"); servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")).addMappingForUrlPatterns(null, false, "/*"); servletContext.getSessionCookieConfig().setSecure(cookieSecure); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { //On charge le contexte de l'app AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.setDisplayName("scrumtracker"); rootContext.register(ApplicationContext.class); //Context loader listener servletContext.addListener(new ContextLoaderListener(rootContext)); //Dispatcher servlet ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }
/** * Configure the given {@link ServletContext} with any servlets, filters, listeners * context-params and attributes necessary for initializing this web application. See examples * {@linkplain WebApplicationInitializer above}. * * @param servletContext the {@code ServletContext} to initialize * @throws ServletException if any call against the given {@code ServletContext} throws a {@code ServletException} */ public void onStartup(ServletContext servletContext) throws ServletException { // Spring Context Bootstrapping AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext(); rootAppContext.register(AutoPivotConfig.class); servletContext.addListener(new ContextLoaderListener(rootAppContext)); // Set the session cookie name. Must be done when there are several servers (AP, // Content server, ActiveMonitor) with the same URL but running on different ports. // Cookies ignore the port (See RFC 6265). CookieUtil.configure(servletContext.getSessionCookieConfig(), CookieUtil.COOKIE_NAME); // The main servlet/the central dispatcher final DispatcherServlet servlet = new DispatcherServlet(rootAppContext); servlet.setDispatchOptionsRequest(true); Dynamic dispatcher = servletContext.addServlet("springDispatcherServlet", servlet); dispatcher.addMapping("/*"); dispatcher.setLoadOnStartup(1); // Spring Security Filter final FilterRegistration.Dynamic springSecurity = servletContext.addFilter(SPRING_SECURITY_FILTER_CHAIN, new DelegatingFilterProxy()); springSecurity.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); }
@Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); // 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 sc) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(AppConfig.class); sc.addListener(new ContextLoaderListener(context)); sc.setInitParameter("defaultHtmlEscape", "true"); ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcherServlet", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.setAsyncSupported(true); dispatcher.addMapping("/"); FilterRegistration.Dynamic securityFilter = sc.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class); securityFilter.addMappingForUrlPatterns(null, false, "/*"); securityFilter.setAsyncSupported(true); }
private WebAppContext initWebApp() throws IOException { WebAppContext ctx = new WebAppContext(); ctx.setContextPath(CONFIG.getContextPath()); ctx.setResourceBase(new ClassPathResource("webapp").getURI().toString()); // Disable directory listings if no index.html is found. ctx.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); ServletHolder web = new ServletHolder("spring-dispatcher", new DispatcherServlet(springContext)); ServletHolder cxf = new ServletHolder("cxf", new CXFServlet()); ctx.addEventListener(new ContextLoaderListener(springContext)); ctx.addServlet(web, CONFIG.getSpringMapping()); ctx.addServlet(cxf, CONFIG.getCxfMapping()); if (CONFIG.getProfile().isProd()) { addSecurityFilter(ctx); } initJSP(ctx); return ctx; }
private static void startServer() throws Exception, InterruptedException { Server server = new Server(port); Context context = new Context(server, "/", Context.SESSIONS); context.addServlet(DefaultServlet.class, "/*"); context.addEventListener(new ContextLoaderListener(getContext())); context.addEventListener(new RequestContextListener()); WicketFilter filter = new WicketFilter(); filter.setFilterPath("/"); FilterHolder holder = new FilterHolder(filter); holder.setInitParameter("applicationFactoryClassName", APP_FACTORY_NAME); context.addFilter(holder, "/*", Handler.DEFAULT); server.setHandler(context); server.start(); server.join(); }
@Test public void abstractRefreshableWAC_respectsProgrammaticConfigLocations() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("programmatic.xml"); ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage(), t.getMessage().endsWith( "Could not open ServletContext resource [/programmatic.xml]")); } }
/** * If a contextConfigLocation init-param has been specified for the ContextLoaderListener, * then it should take precedence. This is generally not a recommended practice, but * when it does happen, the init-param should be considered more specific than the * programmatic configuration, given that it still quite possibly externalized in * hybrid web.xml + WebApplicationInitializer cases. */ @Test public void abstractRefreshableWAC_respectsInitParam_overProgrammaticConfigLocations() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("programmatic.xml"); ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage(), t.getMessage().endsWith( "Could not open ServletContext resource [/from-init-param.xml]")); } }
/** * If setConfigLocation has not been called explicitly against the application context, * then fall back to the ContextLoaderListener init-param if present. */ @Test public void abstractRefreshableWAC_fallsBackToInitParam() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage().endsWith( "Could not open ServletContext resource [/from-init-param.xml]")); } }
/** * Ensure that any custom default locations are still respected. */ @Test public void customAbstractRefreshableWAC_fallsBackToInitParam() { XmlWebApplicationContext ctx = new XmlWebApplicationContext() { @Override protected String[] getDefaultConfigLocations() { return new String[] { "/WEB-INF/custom.xml" }; } }; //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML System.out.println(t.getMessage()); assertTrue(t.getMessage().endsWith( "Could not open ServletContext resource [/from-init-param.xml]")); } }
/** * If context config locations have been specified neither against the application * context nor the context loader listener, then fall back to default values. */ @Test public void abstractRefreshableWAC_fallsBackToConventionBasedNaming() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); // no init-param set //sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML System.out.println(t.getMessage()); assertTrue(t.getMessage().endsWith( "Could not open ServletContext resource [/WEB-INF/applicationContext.xml]")); } }
@Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.getServletRegistration("default").addMapping("/resources/*"); AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(RootApplicationContextConfig.class); servletContext.addListener(new ContextLoaderListener(rootContext)); AnnotationConfigWebApplicationContext servletContextConfig = new AnnotationConfigWebApplicationContext(); servletContextConfig.register(ServletApplicationContextConfig.class); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("springDispatcher", new DispatcherServlet(servletContextConfig)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); FilterRegistration.Dynamic registration = servletContext.addFilter("preSecFilter", new PreSescLoggingFilter()); registration.addMappingForUrlPatterns(null, false, "/*"); FilterRegistration.Dynamic reg = servletContext.addFilter("encoding", new CharacterEncodingFilter("UTF-8", true)); reg.addMappingForUrlPatterns(null, false, "/*"); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { log.debug("Initializing servlet application context"); AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext(); // register webapp spring configuration servletAppContext.register(SpringInitializer.class); log.debug("Registering Spring ContextLoaderListener"); registerListener(servletContext, new ContextLoaderListener(servletAppContext)); log.debug("Registering Spring DispatcherServlet"); registerServlet(servletContext, new DispatcherServlet(servletAppContext), "/").setLoadOnStartup(1); loadActiveSpringProfiles(servletContext, servletAppContext); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { // Logger initialization is deferred in case a ordered // LogServletContextInitializer is being used this.logger = LogFactory.getLog(getClass()); WebApplicationContext rootAppContext = createRootApplicationContext( servletContext); if (rootAppContext != null) { 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"); } }
public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebAppConfig.class); servletContext.addListener(new ContextLoaderListener(ctx)); // CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); // characterEncodingFilter.setEncoding("UTF-8"); // characterEncodingFilter.setForceEncoding(true); // javax.servlet.FilterRegistration.Dynamic filter = // servletContext.addFilter("characterEncodingFilter", characterEncodingFilter); // filter.addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST), // true, "/*"); ctx.setServletContext(servletContext); javax.servlet.ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); }
public void startPeopleService() throws Exception { final File base = createBaseDirectory(); log.info("Using base folder: " + base.getAbsolutePath()); final Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); tomcat.setBaseDir(base.getAbsolutePath()); Context context = tomcat.addContext("/", base.getAbsolutePath()); Tomcat.addServlet(context, "CXFServlet", new CXFServlet()); context.addServletMapping("/rest/*", "CXFServlet"); context.addApplicationListener(ContextLoaderListener.class.getName()); context.setLoader(new WebappLoader(Thread.currentThread().getContextClassLoader())); context.addParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName()); context.addParameter("contextConfigLocation", AppConfig.class.getName()); tomcat.start(); tomcat.getServer().await(); }
public static void main(final String[] args) throws Exception { final File base = createBaseDirectory(); log.info("Using base folder: " + base.getAbsolutePath()); final Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); tomcat.setBaseDir( base.getAbsolutePath() ); Context context = tomcat.addContext( "/", base.getAbsolutePath() ); Tomcat.addServlet( context, "CXFServlet", new CXFServlet() ); context.addServletMapping( "/rest/*", "CXFServlet" ); context.addApplicationListener( ContextLoaderListener.class.getName() ); context.setLoader( new WebappLoader( Thread.currentThread().getContextClassLoader() ) ); context.addParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() ); context.addParameter( "contextConfigLocation", MusicConfig.class.getName() ); tomcat.start(); tomcat.getServer().await(); }
private void createMainDispatcherServlet(ApplicationContext rootContext) { mainWebApplicationContext = new XmlWebApplicationContext(); mainWebApplicationContext .setConfigLocation(getRequiredInitParameter("communoteWebContextConfigLocation")); mainWebApplicationContext.setParent(rootContext); // add ContextLoaderListener with web-ApplicationContext which publishes it under a // ServletContext attribute and closes it on shutdown. The former is required for // WebApplicationContextUtils which are used by DelegatingFilterProxy (spring security). // Closing is also done by dispatcher servlet's implementation of destroy method. this.servletContext.addListener(new ContextLoaderListener(mainWebApplicationContext)); DispatcherServlet dispatcherServlet = new DispatcherServlet(mainWebApplicationContext); ServletRegistration.Dynamic addedServlet = this.servletContext.addServlet( getInitParameter("communoteServletName", "communote"), dispatcherServlet); addedServlet.setLoadOnStartup(1); addedServlet.addMapping(getRequiredInitParameter("communoteServletUrlPattern")); this.mainDispatcherServlet = dispatcherServlet; }
@Override public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); container.addListener(new ContextLoaderListener(rootContext)); //container.addListener(EnvironmentLoaderListener.class); //container.setInitParameter("shiroEnvironmentClass","org.apache.shiro.web.env.DefaultWebEnvironment"); container.addListener(SessionSupport.class); container.setInitParameter("org.atmosphere.cpr.sessionSupport","true"); EnumSet<DispatcherType> d = EnumSet.of(DispatcherType.REQUEST ,DispatcherType.ASYNC ,DispatcherType.ERROR,DispatcherType.FORWARD,DispatcherType.INCLUDE); FilterRegistration.Dynamic shiroFilter = container.addFilter("shiroFilter", DelegatingFilterProxy.class); shiroFilter.setInitParameter("targetFilterLifecycle","true"); shiroFilter.addMappingForUrlPatterns(d, false, "/*"); shiroFilter.setAsyncSupported(true); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { for (String line : BANNER) { logger.info(line); } logger.info(Strings.repeat("=", 100)); logger.info(String.format("UI-DESIGNER : %s edition v.%s", prop.getProperty("designer.edition"), prop.getProperty("designer.version"))); logger.info(Strings.repeat("=", 100)); // Create the root context Spring AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(new Class<?>[] { ApplicationConfig.class }); // Manage the lifecycle of the root application context servletContext.addListener(new ContextLoaderListener(rootContext)); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.setMultipartConfig(new MultipartConfigElement(System.getProperty("java.io.tmpdir"))); dispatcher.setAsyncSupported(true); dispatcher.addMapping("/"); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { // Setup Spring AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(SpringConfiguration.class); rootContext.refresh(); servletContext.addListener(new ContextLoaderListener(rootContext)); LoggerFactory.getLogger(WebApplicationInitializer.class).info("Spring setup done."); // Register Thrift servlet as endpoint TServlet thriftServlet = rootContext.getBean("thriftServlet", TServlet.class); ServletRegistration.Dynamic servletRegistration = servletContext.addServlet("apiServlet", thriftServlet); servletRegistration.setLoadOnStartup(2); servletRegistration.addMapping("/api"); LoggerFactory.getLogger(WebApplicationInitializer.class).info("Registered Thrift servlet."); }
public static ServletAdapter initRestTestServletAdapter(Class<?> applicationConfigClass, int testServerPort, String testContextPath, String testServletPath, boolean enableJPA) { final ServletAdapter adapter = new ServletAdapter(); adapter.addInitParameter(ServletContainer.APPLICATION_CONFIG_CLASS, applicationConfigClass.getName()); adapter.addInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", LoggingFilter.class.getName()); adapter.addInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", LoggingFilter.class.getName()); adapter.addServletListener(ContextLoaderListener.class.getName()); if (enableJPA) { adapter.addFilter(new OpenEntityManagerInViewFilter(), "openEntityManagerInViewFilter", null); } adapter.setServletInstance(new SpringServlet()); adapter.setContextPath(testContextPath); adapter.setServletPath(testServletPath); return adapter; }
@Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); servletContext.addListener(new ContextLoaderListener(rootContext)); AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext(); dispatcherServlet.register(MvcConfig.class); ServletRegistration.Dynamic dispatcher = servletContext.addServlet(MVC_DISPATCHER_NAME, new DispatcherServlet(dispatcherServlet)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/1/*"); dispatcher.addMapping("/oauth/token"); FilterRegistration charEncodingFilterReg = servletContext.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class); charEncodingFilterReg.setInitParameter("encoding", "UTF-8"); charEncodingFilterReg.setInitParameter("forceEncoding", "true"); charEncodingFilterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*"); }
/** * Initializes the context loader listener which bootstraps Spring and provides access to the application context. * * @param servletContext the servlet context. */ protected void initContextLoaderListener(ServletContext servletContext) { // Add the context loader listener for the base (i.e. root) Spring configuration. // We register all our @Configuration annotated classes with the context so Spring will load all the @Bean's via these classes. // We also set the application context in an application context holder before "registering" so static @Bean's // (e.g. PropertySourcesPlaceholderConfigurer) will have access to it since they can't take advantage of autowiring or having a class be // ApplicationContextAware to get it. AnnotationConfigWebApplicationContext contextLoaderListenerContext = new AnnotationConfigWebApplicationContext(); ApplicationContextHolder.setApplicationContext(contextLoaderListenerContext); contextLoaderListenerContext .register(CoreSpringModuleConfig.class, DaoSpringModuleConfig.class, DaoEnvSpringModuleConfig.class, ServiceSpringModuleConfig.class, ServiceEnvSpringModuleConfig.class, UiSpringModuleConfig.class, UiEnvSpringModuleConfig.class, RestSpringModuleConfig.class, AppSpringModuleConfig.class); servletContext.addListener(new ContextLoaderListener(contextLoaderListenerContext)); }
default List<ServletContextListener> getListeners(ServerData data) { List<ServletContextListener> list = new ArrayList<>(); if (data.getRootContext() instanceof WebApplicationContext) { list.add(new ContextLoaderListener( (WebApplicationContext) data.getRootContext())); } ListX<Plugin> modules = PluginLoader.INSTANCE.plugins.get(); ListX<ServletContextListener> listeners = modules.stream() .filter(module -> module.servletContextListeners() != null) .flatMapI(Plugin::servletContextListeners) .map(fn -> fn.apply(data)) .to().listX(); return listeners.plusAll(list); }
@Override public void onStartup(ServletContext servletContext) { log.info("Called WebXmlCustomServer onStartup()"); // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(Main.class, SpringConfiguration.class); // Manage the lifecycle of the root application context servletContext.addListener(new ContextLoaderListener(rootContext)); try { WebXmlCommon.initialize(servletContext, false); } catch (ServletException ex) { log.error("couldn't initialize WebXmlCommon", ex); } }
@Override public void onStartup(ServletContext servletContext) throws ServletException { //create a new Spring webapp context and add core listeners for requests and lifecycle events AnnotationConfigWebApplicationContext webAppContext = new AnnotationConfigWebApplicationContext(); servletContext.addListener(RequestContextListener.class); servletContext.addListener(new ContextLoaderListener(webAppContext)); //tell Spring where to find @Configuration classes for further Spring config webAppContext.setConfigLocation(SPRING_CONFIG_PACKAGE); //create Spring's Dispatcher servlet and tell the servletContext it is present DispatcherServlet dispatcherServlet = new DispatcherServlet(webAppContext); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcherServlet", dispatcherServlet); //perform additional dispatcher servlet config dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING); EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD); CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); characterEncodingFilter.setForceEncoding(true); FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("characterEncoding", characterEncodingFilter); characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*"); servletContext.addListener(new ContextLoaderListener(rootContext)); }
/** * Creates web application context * @param servletContext to be used during creation and registration * @return web application context for the application */ private WebApplicationContext createRootContext( ServletContext servletContext) { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.getEnvironment().setActiveProfiles("prod"); Class<?>[] annotatedClasseses = configurations2Register(); if (annotatedClasseses != null && annotatedClasseses.length > 0) { rootContext.register(annotatedClasseses); } rootContext.refresh(); servletContext.addListener(new ContextLoaderListener(rootContext)); servletContext.setInitParameter("defaultHtmlEscape", "true"); return rootContext; }
/** * Starts the this server and its services. **/ public void start() throws Exception { if (this.started.compareAndSet(false, true)) { ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS); context.addEventListener(new ContextLoaderListener()); context.setInitParameter("contextConfigLocation", "classpath*:META-INF/applicationContext.xml"); context.setContextPath(contextPath); ServletHolder sh = new ServletHolder(new org.glassfish.jersey.servlet.ServletContainer()); sh.setInitParameter("javax.ws.rs.Application", ApplicationConfig.class.getName()); sh.setInitOrder(1); context.addServlet(sh, "/*"); context.addEventListener(new ApplicationServletContextListener()); server.setHandler(context); server.start(); NodeManagerFactory.getManagerReference().start(); server.join(); } LOG.debug("Server already started!"); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext webAppContext = new AnnotationConfigWebApplicationContext(); webAppContext.setConfigLocation(getClass().getPackage().getName()); ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(webAppContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING); OpenEntityManagerInViewFilter openEntityManagerInViewFilter = new OpenEntityManagerInViewFilter(); openEntityManagerInViewFilter.setEntityManagerFactoryBeanName("entityManagerFactory"); Dynamic dynamic = servletContext.addFilter("openEntityManagerInViewFilter", openEntityManagerInViewFilter); dynamic.addMappingForUrlPatterns(getDispatcherTypes(), false, DISPATCHER_SERVLET_MAPPING); servletContext.addListener(new ContextLoaderListener(webAppContext)); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); servletContext.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); @SuppressWarnings("unused") EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD); // CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); // characterEncodingFilter.setEncoding("UTF-8"); // characterEncodingFilter.setForceEncoding(true); // FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("characterEncoding", characterEncodingFilter); // characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*"); }