@Override public void contextInitialized(ServletContextEvent sce) { ServletContext ctx = sce.getServletContext(); ServletRegistration.Dynamic sd = ctx.addServlet("DynamicServlet", "com.creditease.monitorframework.fat.DynamicServlet"); sd.addMapping("/DynamicServlet"); sd.setInitParameter("test", "test"); sd.setLoadOnStartup(1); sd.setAsyncSupported(false); FilterRegistration.Dynamic fd = ctx.addFilter("DynamicFilter", "com.creditease.monitorframework.fat.filters.DynamicFilter"); fd.addMappingForUrlPatterns(null, true, "/DynamicServlet"); fd.setInitParameter("test2", "test2"); fd.setAsyncSupported(false); ctx.addListener("com.creditease.monitorframework.fat.listeners.TestServletInitListener"); }
/** * 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("/management/metrics/*"); metricsAdminServlet.setAsyncSupported(true); metricsAdminServlet.setLoadOnStartup(2); }
static String[] collectUrlPatterns(ServletContext servletContext, Class<?> servletCls) { List<ServletRegistration> servlets = servletContext.getServletRegistrations() .values() .stream() .filter(predicate -> { return predicate.getClassName().equals(servletCls.getName()); }) .collect(Collectors.toList()); if (servlets.isEmpty()) { return new String[] {}; } ServletRegistration servletRegistration = servlets.get(0); Collection<String> mappings = servletRegistration.getMappings(); if (servlets.size() > 1) { LOGGER.info("Found {} {} registered, select the first one, mappings={}.", servlets.size(), servletCls.getName(), mappings); } return filterUrlPatterns(mappings); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch08-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); dispatcher.setAsyncSupported(true); //FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy()); // springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*"); // springSecurityFilterChain.setAsyncSupported(true); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch03-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("*.html"); dispatcher.setLoadOnStartup(1); FilterRegistration.Dynamic corsFilter = container.addFilter("corsFilter", new CorsFilter()); corsFilter.setInitParameter("cors.allowed.methods", "GET, POST, HEAD, OPTIONS, PUT, DELETE"); corsFilter.addMappingForUrlPatterns(null, true, "/*"); FilterRegistration.Dynamic filter = container.addFilter("hiddenmethodfilter", new HiddenHttpMethodFilter()); filter.addMappingForServletNames(null, true, "/*"); FilterRegistration.Dynamic multipartFilter = container.addFilter("multipartFilter", new MultipartFilter()); multipartFilter.addMappingForUrlPatterns(null, true, "/*"); }
protected static final void regiesterVIServlet(ServletContext context,Logger logger){ try { ServletRegistration.Dynamic asr = context.addServlet("VIApiServlet", VIApiServlet.class); if (asr != null) { asr.setLoadOnStartup(Integer.MAX_VALUE); asr.addMapping("/@in/api/*"); } else { logger.warn("Servlet VIApiServlet already exists"); } ServletRegistration ssr = context.addServlet("VIHttpServlet", StaticContentServlet.class); if (ssr != null) { ssr.addMapping("/@in/*"); } else { logger.warn("Servlet VIHttpServlet already exists"); } }catch (Throwable e){ logger.error("VI register servlet failed",e); } }
@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("/"); }
@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 ServletRegistration.Dynamic addServlet(final String servletName, final String className) { ensureNotProgramaticListener(); ensureNotInitialized(); try { if (deploymentInfo.getServlets().containsKey(servletName)) { return null; } ServletInfo servlet = new ServletInfo(servletName, (Class<? extends Servlet>) deploymentInfo.getClassLoader().loadClass(className)); readServletAnnotations(servlet); deploymentInfo.addServlet(servlet); ServletHandler handler = deployment.getServlets().addServlet(servlet); return new ServletRegistrationImpl(servlet, handler.getManagedServlet(), deployment); } catch (ClassNotFoundException e) { throw UndertowServletMessages.MESSAGES.cannotLoadClass(className, e); } }
public void collectDynInfo(String methodName, Object res, Object[] args) { if (methodName.equals("addServlet")) { servlets.add((ServletRegistration.Dynamic) res); } else if (methodName.equals("addFilter")) { filters.add((FilterRegistration.Dynamic) res); } else if (methodName.equals("addListener")) { String listenerClassName; if (args[0].getClass().isAssignableFrom(String.class)) { listenerClassName = args[0].toString(); } else if (args[0].getClass().isAssignableFrom(Class.class)) { listenerClassName = ((Class<?>) args[0]).getName(); } else { listenerClassName = args[0].getClass().getName(); } listeners.add(listenerClassName); } }
@Override public Map<String, ? extends ServletRegistration> getServletRegistrations() { if (!_enabled) throw new UnsupportedOperationException(); HashMap<String, ServletRegistration> registrations = new HashMap<String, ServletRegistration>(); ServletHandler handler=ServletContextHandler.this.getServletHandler(); ServletHolder[] holders=handler.getServlets(); if (holders!=null) { for (ServletHolder holder : holders) registrations.put(holder.getName(),holder.getRegistration()); } return registrations; }
/** * 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(); }
private void registerJspServlet(ServletContext context) { ServletRegistration servletRegistration = context.getServletRegistration("jsp"); servletRegistration.addMapping("/index.jsp"); String jspPath = PropertiesProvider.getString(FrameworkConstant.JSP_PATH); if (StringUtil.isNotEmpty(jspPath)) { servletRegistration.addMapping(jspPath + "*"); } }
/** * 添加jsp映射 */ @Deprecated private void registeJspServlet(ServletContext context) { ServletRegistration jspServlet = context.getServletRegistration("jsp"); jspServlet.addMapping("/index.jsp"); String jspPath = Constants.JSP_PATH_VALUE; jspServlet.addMapping(jspPath + "*"); }
@Override public ServletRegistration.Dynamic addServlet(String servletName, String className) { if (SecurityUtil.isPackageProtectionEnabled()) { return (ServletRegistration.Dynamic) doPrivileged("addServlet", new Object[] { servletName, className }); } else { return context.addServlet(servletName, className); } }
@Override public void onStartup(final ServletContext servletContext) throws ServletException { // Register DispatcherServlet super.onStartup(servletContext); // Register H2 Admin console: ServletRegistration.Dynamic h2WebServlet = servletContext.addServlet("h2WebServlet", "org.h2.server.web.WebServlet"); h2WebServlet.addMapping("/admin/h2/*"); h2WebServlet.setInitParameter("webAllowOthers", "true"); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch04-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }
/** * @since servlet-api-3.0 */ @Override public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) { if (!isStarting()) throw new IllegalStateException(); if (servletName == null || "".equals(servletName.trim())) throw new IllegalStateException("Missing servlet name"); if (!_enabled) throw new UnsupportedOperationException(); final ServletHandler handler = ServletContextHandler.this.getServletHandler(); ServletHolder holder = handler.getServlet(servletName); if (holder == null) { holder = handler.newServletHolder(Source.JAVAX_API); holder.setName(servletName); holder.setServlet(servlet); handler.addServlet(holder); return dynamicHolderAdded(holder); } //complete a partial registration if (holder.getClassName()==null && holder.getHeldClass()==null) { holder.setServlet(servlet); return holder.getRegistration(); } else return null; //existing completed registration for servlet name }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch03-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("*.html"); dispatcher.setLoadOnStartup(1); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch02-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("*.html"); dispatcher.setLoadOnStartup(1); }
private void useDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // <-- Use DispatcherConfig.java // Define mapping between <servlet> and <servlet-mapping> ServletRegistration.Dynamic dispatcher = container.addServlet("spring-dispatcher", new DispatcherServlet( dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch07-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }
/** * Add endpoint mapping to cas servlet. * * @param sce the sce * @param mapping the mapping */ protected final void addEndpointMappingToCasServlet(final ServletContextEvent sce, final String mapping) { logger.info("Adding [{}] to {} servlet context", mapping, WebUtils.CAS_SERVLET_NAME); final ServletRegistration registration = getCasServletRegistration(sce); if (registration != null) { registration.addMapping(mapping); logger.info("Added [{}] to {} servlet context", mapping, WebUtils.CAS_SERVLET_NAME); } }
@Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(MvcConfig.class); context.setServletContext(servletContext); ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); servlet.setAsyncSupported(true);//开启异步方法支持 }
@Override public void onStartup(ServletContext container) { WebApplicationContext rootContext = getContext(); container.addListener(new ContextLoaderListener(rootContext)); AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(DispatcherConfig.class); ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }
@Override public ServletRegistration.Dynamic addServlet(String servletName, String className) { if (SecurityUtil.isPackageProtectionEnabled()) { return (ServletRegistration.Dynamic) doPrivileged( "addServlet", new Object[]{servletName, className}); } else { return context.addServlet(servletName, className); } }
@Override @SuppressWarnings("unchecked") // doPrivileged() returns the correct type public Map<String, ? extends ServletRegistration> getServletRegistrations() { if (SecurityUtil.isPackageProtectionEnabled()) { return (Map<String, ? extends ServletRegistration>) doPrivileged( "getServletRegistrations", null); } else { return context.getServletRegistrations(); } }
public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(AppConfig.class); ctx.setServletContext(container); ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.setLoadOnStartup(1); servlet.addMapping("/"); }
@Override @SuppressWarnings("unchecked") // doPrivileged() returns the correct type public Map<String, ? extends ServletRegistration> getServletRegistrations() { if (SecurityUtil.isPackageProtectionEnabled()) { return (Map<String, ? extends ServletRegistration>) doPrivileged("getServletRegistrations", null); } else { return context.getServletRegistrations(); } }
@Override public ServletRegistration getServletRegistration(String servletName) { Wrapper wrapper = (Wrapper) context.findChild(servletName); if (wrapper == null) { return null; } return new ApplicationServletRegistration(wrapper, context); }
@Override public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException { Servlet s = new TesterServlet(); ServletRegistration.Dynamic r = ctx.addServlet("TesterServlet1", s); r.addMapping("/TesterServlet1"); }
@Override public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException { Servlet s = new TesterServlet(); ServletRegistration.Dynamic r = ctx.addServlet("TesterServlet2", s); r.addMapping("/TesterServlet2"); }
@Override public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException { Servlet s = new CustomDefaultServlet(); ServletRegistration.Dynamic r = ctx.addServlet(servletName, s); r.addMapping("/"); }
@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(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(Set<Class<?>> c, ServletContext ctx) throws ServletException { Servlet s; if (createServlet) { s = ctx.createServlet(servlet.getClass()); } else { s = servlet; } ServletRegistration.Dynamic r = ctx.addServlet("servlet", s); r.addMapping("/"); }