private void addControllerServlet(Class<?> klass) throws InstantiationException, IllegalAccessException { assert klass != null; ControllerServlet servlet = null; Controller controller; if (Controller.class.isAssignableFrom(klass)) { controller = controllerFactory.createController(klass.asSubclass(Controller.class)); } else { SimpleControllerWrapper.checkValidSimpleControllerClass(klass); SimpleControllerWrapper simpleController = SimpleControllerWrapper.createInstance(controllerFactory.createController(klass)); configure(simpleController); controller = simpleController; } URL url = klass.getAnnotation(URL.class); servlet = new ControllerServlet(controller); AllowedHttpMethods httpMethods = klass.getAnnotation(AllowedHttpMethods.class); if (httpMethods != null) { servlet.setValidHttpMethods(httpMethods.value()); } ControllerServlet.AfterControllerAction action = null; if (klass.isAnnotationPresent(ForwardTo.class)) { action = new ControllerServlet.ForwardAction(klass.getAnnotation(ForwardTo.class).value()); } else if (klass.isAnnotationPresent(RedirectTo.class)) { action = new ControllerServlet.ForwardAction(klass.getAnnotation(RedirectTo.class).value()); } servlet.setAfterControllerAction(action); ServletRegistration.Dynamic dynamic = context.addServlet(klass.getSimpleName(), servlet); dynamic.addMapping(url.value()); }
/** * Register and configure all Servlet container components necessary to power the web application. */ @Override public void onStartup(final ServletContext sc) throws ServletException { System.out.println("MyWebAppInitializer.onStartup()"); // Create the 'root' Spring application context final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); root.scan("org.baeldung.config.parent"); // root.getEnvironment().setDefaultProfiles("embedded"); // Manages the lifecycle of the root application context sc.addListener(new ContextLoaderListener(root)); // Handles requests into the application final AnnotationConfigWebApplicationContext childWebApplicationContext = new AnnotationConfigWebApplicationContext(); childWebApplicationContext.scan("org.baeldung.config.child"); final ServletRegistration.Dynamic appServlet = sc.addServlet("api", new DispatcherServlet(childWebApplicationContext)); appServlet.setLoadOnStartup(1); final Set<String> mappingConflicts = appServlet.addMapping("/"); if (!mappingConflicts.isEmpty()) { throw new IllegalStateException( "'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); } // spring security filter final DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy("springSecurityFilterChain"); final Dynamic addedFilter = sc.addFilter("springSecurityFilterChain", springSecurityFilterChain); addedFilter.addMappingForUrlPatterns(null, false, "/*"); }
@Override protected void registerDispatcherServlet(ServletContext servletContext) { String servletName = getServletName(); WebApplicationContext servletAppContext = createServletApplicationContext(); DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext); // throw NoHandlerFoundException to Controller when a User requests a non-existent page // dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet); registration.setLoadOnStartup(1); registration.addMapping(getServletMappings()); registration.setAsyncSupported(isAsyncSupported()); Filter[] filters = getServletFilters(); if (!ObjectUtils.isEmpty(filters)) { for (Filter filter : filters) { registerServletFilter(servletContext, filter); } } customizeRegistration(registration); }
@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("/"); }
/** * 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 { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); // rootContext.register(WebConfig.class); rootContext.scan("org.saiku.admin.config"); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); servletContext.addListener(new ContextLoaderListener(rootContext)); // FilterRegistration.Dynamic fr = servletContext.addFilter("encodingFilter", new // CharacterEncodingFilter()); // fr.setInitParameter("encoding", "UTF-8"); // fr.setInitParameter("forceEncoding", "true"); // fr.addMappingForUrlPatterns(null, true, "/*"); // FilterRegistration.Dynamic fr = servletContext.addFilter("authFilter", new // AuthFilter()); // fr.addMappingForUrlPatterns(null, true, "/*"); // // fr = servletContext.addFilter("requestsLoggerServletFilter", new // RequestsLoggerServletFilter()); // fr.addMappingForUrlPatterns(null, true, "/*"); // // fr = servletContext.addFilter("TeeFilter", new // ch.qos.logback.access.servlet.TeeFilter()); // fr.addMappingForUrlPatterns(null, true, "/*"); }
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("/"); }
/** Initializes H2 console. */ private void initH2Console(ServletContext servletContext) { log.debug("Initialize H2 console"); ServletRegistration.Dynamic h2ConsoleServlet = servletContext.addServlet("H2Console", new org.h2.server.web.WebServlet()); h2ConsoleServlet.addMapping("/h2-console/*"); h2ConsoleServlet.setInitParameter("-properties", "src/main/resources/"); h2ConsoleServlet.setLoadOnStartup(1); }
@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(MAPPING_URL); dispatcher.setAsyncSupported(true); }
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); }
@Override public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException { // Register and map servlet s = new Bug51376Servlet(); ServletRegistration.Dynamic sr = ctx.addServlet("bug51376", s); sr.addMapping("/bug51376"); if (loadOnStartUp) { sr.setLoadOnStartup(1); } }
@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("/"); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context)); dispatcher.setAsyncSupported(true); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/secure/*"); servletContext.addListener(new ContextLoaderListener(context)); servletContext.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE)); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(AppConfig.class); ctx.setServletContext(servletContext); ServletRegistration.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)); }
public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(ToyboxConfiguration.class); ctx.setServletContext(container); ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.setLoadOnStartup(1); servlet.addMapping("/"); }
private void registerDispatcherServlet(ServletContext servletContext) { logger.debug( "Registering Dispatcher Servlet {} [name:{}]", WebMvcContextConfig.class.getName(), DISPATCHER_SERVLET); WebApplicationContext dispatcherContext = createContext(WebMvcContextConfig.class); DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext); ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET, dispatcherServlet); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }
@Override public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException { // Register and map servlet Servlet s = new Bug50015Servlet(); ServletRegistration.Dynamic sr = ctx.addServlet("bug50015", s); sr.addMapping("/bug50015"); // Limit access to users in the Tomcat role HttpConstraintElement hce = new HttpConstraintElement(TransportGuarantee.NONE, "tomcat"); ServletSecurityElement sse = new ServletSecurityElement(hce); sr.setServletSecurity(sse); }
@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"); }
private void setupWebApp(ServletContext context, Configuration config) { LOGGER.debug("setupWebApp"); Router router = new Router(context); ServletRegistration.Dynamic servlet = context.addServlet(VISALLO_SERVLET_NAME, router); servlet.addMapping("/*"); servlet.setAsyncSupported(true); addSecurityConstraint(servlet, config); addAtmosphereServlet(context, config); addDebugFilter(context); addCacheFilter(context); addGzipFilter(context); LOGGER.warn( "JavaScript / Less modifications will not be reflected on server. Run `grunt` from webapp directory in development"); }
@Override public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException { ArrayList<String> portletNames = null; try { URL url = ctx.getResource("WEB-INF/portlet.xml"); // if (url != null) { // XPath to get the portlet names portletNames = new ArrayList<String>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(url.openStream()); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile("//portlet-name/text()"); NodeList portletNameNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < portletNameNodes.getLength(); i++) { Text portletNameNode = (Text) portletNameNodes.item(i); portletNames.add(portletNameNode.getWholeText().trim()); } } else { ctx.log("No portlets inside this web application"); } } catch (Exception e) { ctx.log("Could not obtain a valid portlet.xml file", e); } // if (portletNames != null) { ctx.log( "Detected portlet XML file with portlets " + portletNames + " -> Bootstrapping GateIn Portlet Container"); // ServletRegistration.Dynamic embedRegistration = ctx.addServlet("EmbedServlet", EmbedServlet.class); embedRegistration.addMapping("/embed/*"); // ServletRegistration.Dynamic redirectRegistration = ctx.addServlet("RedirectServlet", new RedirectServlet(portletNames)); redirectRegistration.addMapping("/"); } }
@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); }
@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("/"); }
/** * Delegate for ServletRegistration.Dynamic.setServletSecurity method * * @param registration ServletRegistration.Dynamic instance that setServletSecurity was called on * @param servletSecurityElement new security info * @return the set of exact URL mappings currently associated with the registration that are also * present in the web.xml security constraints and thus will be unaffected by this call. */ public Set<String> setServletSecurity( ServletRegistration.Dynamic registration, ServletSecurityElement servletSecurityElement) { // Default implementation is to just accept them all. If using a webapp, then this behaviour is // overridden in WebAppContext.setServletSecurity Collection<String> pathSpecs = registration.getMappings(); if (pathSpecs != null) { for (String pathSpec : pathSpecs) { List<ConstraintMapping> mappings = ConstraintSecurityHandler.createConstraintsWithMappingsForPath( registration.getName(), pathSpec, servletSecurityElement); for (ConstraintMapping m : mappings) ((ConstraintAware) getSecurityHandler()).addConstraintMapping(m); } } return Collections.emptySet(); }
@Override public void onStartup(ServletContext container) throws ServletException { XmlWebApplicationContext appContext = new XmlWebApplicationContext(); appContext.setConfigLocation("/WEB-INF/spring/appServlet/servlet-context.xml"); ServletRegistration.Dynamic dispatcher = container.addServlet("appServlet", new DispatcherServlet(appContext)); MultipartConfigElement multipartConfigElement = new MultipartConfigElement(null, 5000000, 5000000, 0); dispatcher.setMultipartConfig(multipartConfigElement); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }
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, "/*"); }
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("/"); }
@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("/"); }
/** 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); }