@Override public void onStartup(ServletContext ctx) { if (ServletContainerInitializerUtil.avoidDoubleInit(this, ctx)) return; final AlertingPlugin alertingPlugin = Stagemonitor.getPlugin(AlertingPlugin.class); ctx.addServlet(AlerterTypeServlet.class.getSimpleName(), new AlerterTypeServlet(alertingPlugin, Stagemonitor.getMeasurementSession())) .addMapping("/stagemonitor/alerter-types"); ctx.addServlet(IncidentServlet.class.getSimpleName(), new IncidentServlet(alertingPlugin)) .addMapping("/stagemonitor/incidents"); ctx.addServlet(TestAlertSenderServlet.class.getSimpleName(), new TestAlertSenderServlet()) .addMapping("/stagemonitor/test-alert"); } }
@Override public void onStartup(ServletContext servletContext) throws ServletException { String servletName = getServletName(); Assert.hasLength(servletName, "getServletName() must not return null or empty"); ApplicationContext applicationContext = createApplicationContext(); Assert.notNull(applicationContext, "createApplicationContext() must not return null"); refreshApplicationContext(applicationContext); registerCloseListener(servletContext, applicationContext); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build(); ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet); if (registration == null) { throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " + "Check if there is another servlet registered under the same name."); } registration.setLoadOnStartup(1); registration.addMapping(getServletMapping()); registration.setAsyncSupported(true); }
dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers()); ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet); if (registration == null) { throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " + registration.setLoadOnStartup(1); registration.addMapping(getServletMappings()); registration.setAsyncSupported(isAsyncSupported());
/** * Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing * {@code servlet-mapping}. */ private static void addServletWithApplication(final ServletContext context, final Class<? extends Application> clazz, final Set<Class<?>> defaultClasses) throws ServletException { final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class); if (ap != null) { // App is annotated with ApplicationPath final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, defaultClasses) .addProperties(Utils.getContextParams(context)); final ServletContainer s = new ServletContainer(resourceConfig); final ServletRegistration.Dynamic dsr = context.addServlet(clazz.getName(), s); dsr.setAsyncSupported(true); dsr.setLoadOnStartup(1); final String mapping = createMappingPath(ap); if (!mappingExists(context, mapping)) { dsr.addMapping(mapping); LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping)); } else { LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping)); } } }
/** * Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing * {@code servlet-mapping}. */ private static void addServletWithApplication(final ServletContext context, final Class<? extends Application> clazz, final Set<Class<?>> defaultClasses) throws ServletException { final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class); if (ap != null) { // App is annotated with ApplicationPath final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, defaultClasses) .addProperties(Utils.getContextParams(context)); final ServletContainer s = new ServletContainer(resourceConfig); final ServletRegistration.Dynamic dsr = context.addServlet(clazz.getName(), s); dsr.setAsyncSupported(true); dsr.setLoadOnStartup(1); final String mapping = createMappingPath(ap); if (!mappingExists(context, mapping)) { dsr.addMapping(mapping); LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping)); } else { LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping)); } } }
Stagemonitor.init(); if (ServletContainerInitializerUtil.avoidDoubleInit(this, ctx)) return; ctx.addServlet(ConfigurationServlet.class.getSimpleName(), new ConfigurationServlet()) .addMapping(ConfigurationServlet.CONFIGURATION_ENDPOINT); ctx.addServlet(StagemonitorMetricsServlet.class.getSimpleName(), new StagemonitorMetricsServlet()) .addMapping("/stagemonitor/metrics"); ctx.addServlet(ClientSpanServlet.class.getSimpleName(), new ClientSpanServlet()) .addMapping("/stagemonitor/public/eum"); final ClientSpanJavaScriptServlet servlet = new ClientSpanJavaScriptServlet(); Stagemonitor.getPlugin(ServletPlugin.class).setClientSpanJavaScriptServlet(servlet); ctx.addServlet(ClientSpanJavaScriptServlet.class.getSimpleName(), servlet) .addMapping("/stagemonitor/public/eum.js"); ctx.addServlet(StagemonitorFileServlet.class.getSimpleName(), new StagemonitorFileServlet()) .addMapping("/stagemonitor/static/*", "/stagemonitor/public/static/*"); ctx.addServlet(WidgetServlet.class.getSimpleName(), new WidgetServlet()) .addMapping("/stagemonitor"); final ServletRegistration.Dynamic spanServlet = ctx.addServlet(SpanServlet.class.getSimpleName(), new SpanServlet()); spanServlet.addMapping("/stagemonitor/spans"); spanServlet.setAsyncSupported(true); final ServletRegistration.Dynamic healthServlet = ctx.addServlet(HealthCheckServlet.class.getSimpleName(), new HealthCheckServlet(Stagemonitor.getHealthCheckRegistry())); healthServlet.addMapping("/stagemonitor/status"); healthServlet.setAsyncSupported(true);
public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(WebSpringConfig.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(); dispatcherContext.register(DispatcherConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
private void handleServlet(ServletConfiguration servlet,ServletContext webappContext){ servlet.getServlet().fold(clazz-> { setInitParameters(webappContext.addServlet(getName(servlet), clazz), servlet) .addMapping(servlet.getMapping()); return 1; }, obj-> { ServletRegistration.Dynamic servletReg = webappContext.addServlet( servlet.getName(), obj); servletReg.addMapping(servlet.getMapping()); return 2; }); } private void addAutoDiscoveredServlets(ServletContext webappContext) {
private void addExplicitlyDeclaredServlets(ServletContext webappContext) { for (ServletData servletData : servletData) { ServletRegistration.Dynamic servletReg = webappContext.addServlet( servletData.getServletName(), servletData.getServlet()); servletReg.addMapping(servletData.getMapping()); logServlet(servletData); } }
@Override public void onStartup(ServletContext servletContext) throws ServletException { String servletName = getServletName(); Assert.hasLength(servletName, "getServletName() must not return null or empty"); ApplicationContext applicationContext = createApplicationContext(); Assert.notNull(applicationContext, "createApplicationContext() must not return null"); refreshApplicationContext(applicationContext); registerCloseListener(servletContext, applicationContext); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build(); ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet); if (registration == null) { throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " + "Check if there is another servlet registered under the same name."); } registration.setLoadOnStartup(1); registration.addMapping(getServletMapping()); registration.setAsyncSupported(true); }
final ServletRegistration.Dynamic dynamicRegistration = context.addServlet(clazz.getName(), servlet); dynamicRegistration.setAsyncSupported(true); dynamicRegistration.setLoadOnStartup(1); registration = dynamicRegistration;
final ServletRegistration.Dynamic dynamicRegistration = context.addServlet(clazz.getName(), servlet); dynamicRegistration.setAsyncSupported(true); dynamicRegistration.setLoadOnStartup(1); registration = dynamicRegistration;
dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers()); ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet); if (registration == null) { throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " + registration.setLoadOnStartup(1); registration.addMapping(getServletMappings()); registration.setAsyncSupported(isAsyncSupported());
/** * Enhance default servlet (named {@link Application}) configuration. */ private static void addServletWithDefaultConfiguration(final ServletContext context, final Set<Class<?>> classes) throws ServletException { ServletRegistration registration = context.getServletRegistration(Application.class.getName()); if (registration != null) { final Set<Class<?>> appClasses = getRootResourceAndProviderClasses(classes); final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(ResourceConfig.class, appClasses) .addProperties(getInitParams(registration)) .addProperties(Utils.getContextParams(context)); if (registration.getClassName() != null) { // class name present - complete servlet registration from container point of view Utils.store(resourceConfig, context, registration.getName()); } else { // no class name - no complete servlet registration from container point of view final ServletContainer servlet = new ServletContainer(resourceConfig); registration = context.addServlet(registration.getName(), servlet); ((ServletRegistration.Dynamic) registration).setLoadOnStartup(1); if (registration.getMappings().isEmpty()) { // Error LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING(registration.getName())); } else { LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_CLASSES(registration.getName(), appClasses)); } } } }
/** * Enhance default servlet (named {@link Application}) configuration. */ private static void addServletWithDefaultConfiguration(final ServletContext context, final Set<Class<?>> classes) throws ServletException { ServletRegistration registration = context.getServletRegistration(Application.class.getName()); if (registration != null) { final Set<Class<?>> appClasses = getRootResourceAndProviderClasses(classes); final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(ResourceConfig.class, appClasses) .addProperties(getInitParams(registration)) .addProperties(Utils.getContextParams(context)); if (registration.getClassName() != null) { // class name present - complete servlet registration from container point of view Utils.store(resourceConfig, context, registration.getName()); } else { // no class name - no complete servlet registration from container point of view final ServletContainer servlet = new ServletContainer(resourceConfig); registration = context.addServlet(registration.getName(), servlet); ((ServletRegistration.Dynamic) registration).setLoadOnStartup(1); if (registration.getMappings().isEmpty()) { // Error LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING(registration.getName())); } else { LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_CLASSES(registration.getName(), appClasses)); } } } }
javax.servlet.ServletRegistration.Dynamic servletRegistration = webappContext.addServlet(config.getName() + "-" + serverData.getModule() .getContext(), config.getServlet()); servletRegistration.setInitParameter("context", serverData.getModule() .getContext()); servletRegistration.setLoadOnStartup(1); servletRegistration.addMapping(serverData.getBaseUrlPattern());
public Dynamic inject(ServletContext servletContext, String urlPattern) { String[] urlPatterns = splitUrlPattern(urlPattern); if (urlPatterns.length == 0) { LOGGER.warn("urlPattern is empty, ignore register {}.", SERVLET_NAME); return null; } String listenAddress = ServletConfig.getLocalServerAddress(); if (!ServletUtils.canPublishEndpoint(listenAddress)) { LOGGER.warn("ignore register {}.", SERVLET_NAME); return null; } // dynamic deploy a servlet to handle serviceComb RESTful request Dynamic dynamic = servletContext.addServlet(SERVLET_NAME, RestServlet.class); dynamic.setAsyncSupported(true); dynamic.addMapping(urlPatterns); dynamic.setLoadOnStartup(0); LOGGER.info("RESTful servlet url pattern: {}.", Arrays.toString(urlPatterns)); return dynamic; }
ServletRegistration.Dynamic reg = servletContext.addServlet(applicationClass.getName(), HttpServlet30Dispatcher.class); reg.setLoadOnStartup(1); reg.setAsyncSupported(true); reg.setInitParameter("javax.ws.rs.Application", applicationClass.getName()); reg.addMapping(mapping);
private void register(ServletContext servletContext, ExposableServletEndpoint endpoint) { String name = endpoint.getEndpointId().toLowerCaseString() + "-actuator-endpoint"; String path = this.basePath + "/" + endpoint.getRootPath(); String urlMapping = path.endsWith("/") ? path + "*" : path + "/*"; EndpointServlet endpointServlet = endpoint.getEndpointServlet(); Dynamic registration = servletContext.addServlet(name, endpointServlet.getServlet()); registration.addMapping(urlMapping); registration.setInitParameters(endpointServlet.getInitParameters()); logger.info("Registered '" + path + "' to " + name); }
ServletRegistration.Dynamic probe = servletContext.addServlet("probe", ProbeServlet.class); probe.setInitParameters(initParameters); probe.setLoadOnStartup(0); probe.addMapping("*.htm"); probe.addMapping("*.ajax"); probe.addMapping("/logs/*"); probe.addMapping("/chart.png");