@Bean public ServletRegistrationBean getODataServletRegistrationBean() { ServletRegistrationBean odataServletRegistrationBean = new ServletRegistrationBean(new CXFNonSpringJaxrsServlet(), "/odata.svc/*"); Map<String, String> initParameters = new HashMap<String, String>(); initParameters.put("javax.ws.rs.Application", "org.apache.olingo.odata2.core.rest.app.ODataApplication"); initParameters.put("org.apache.olingo.odata2.service.factory", "com.sap.mentors.lemonaid.odata.JPAServiceFactory"); odataServletRegistrationBean.setInitParameters(initParameters); return odataServletRegistrationBean; }
@Bean public ServletRegistrationBean facesServletRegistraiton() { ServletRegistrationBean registration = new ServletRegistrationBean(new javax.faces.webapp.FacesServlet(), "*.xhtml"); registration.setName("Faces Servlet"); registration.setLoadOnStartup(1); return registration; }
@Bean public ServletRegistrationBean facesServletRegistraiton() { ServletRegistrationBean registration = new ServletRegistrationBean(new FacesServlet(), "*.xhtml"); registration.setName("Faces Servlet"); registration.setLoadOnStartup(1); return registration; }
@Bean public ServletRegistrationBean getODataServletRegistrationBean() { ServletRegistrationBean odataServletRegistrationBean = new ServletRegistrationBean(new CXFNonSpringJaxrsServlet(), "/odata.svc/*"); Map<String, String> initParameters = new HashMap<String, String>(); initParameters.put("javax.ws.rs.Application", "org.apache.olingo.odata2.core.rest.app.ODataApplication"); initParameters.put("org.apache.olingo.odata2.service.factory", "com.penninkhof.odata.utils.JPAServiceFactory"); odataServletRegistrationBean.setInitParameters(initParameters); return odataServletRegistrationBean; }
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) public ServletRegistrationBean dispatcherServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean( dispatcherServlet(), this.server.getServletMapping()); registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } return registration; }
@Bean(name="springBootServletRegistrationBean") public ServletRegistrationBean servletRegistrationBean() { SpringVaadinServlet servlet = new SpringVaadinServlet() { private static final long serialVersionUID = 1L; @Override public void servletInitialized() throws ServletException { super.servletInitialized(); getService().addSessionInitListener(new TraderSessionInitListener()); } }; return new ServletRegistrationBean(servlet, "/ui/*", "/VAADIN/*"); }
@Bean(name = "configServlet") public ServletRegistrationBean configServlet() { DispatcherServlet dispatcherServlet = new DispatcherServlet(); AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); applicationContext.register(ConfigServletConfig.class); dispatcherServlet.setApplicationContext(applicationContext); ServletRegistrationBean registrationBean = new ServletRegistrationBean(dispatcherServlet, "/config/*"); registrationBean.setLoadOnStartup(1); return registrationBean; }
@Bean public ServletRegistrationBean registerServlet(){ ServletRegistrationBean statViewServletRegistrationBean = new ServletRegistrationBean(dispatchDruidServlet(), "/druid/*"); /* * According to the definition of StatViewServlet, the white-list of druid StatViewServlet * can be specifed with the format as "xxx.xxx.xxx.xxx/yyy.yyy.yyy.yyy" to limit the access to * the StatViewServlet. */ statViewServletRegistrationBean.addInitParameter(StatViewServlet.PARAM_NAME_ALLOW, "127.0.0.1"); statViewServletRegistrationBean.addInitParameter(StatViewServlet.PARAM_NAME_USERNAME, "admin"); statViewServletRegistrationBean.addInitParameter(StatViewServlet.PARAM_NAME_PASSWORD, "password"); return statViewServletRegistrationBean; }
@Bean public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean(servlet, "/ws/*"); }
@Bean public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { log.info("Registering message dispatcher servlet."); MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean(servlet, "/soap-api/*"); }
@Bean @Order public ServletRegistrationBean statViewServlet() { StatViewServlet servlet = new StatViewServlet(); ServletRegistrationBean bean = new ServletRegistrationBean(servlet, "/druid/*"); return bean; }
@Bean public ServletRegistrationBean facesServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean( new FacesServlet(), "*.xhtml"); registration.setLoadOnStartup(1); return registration; }
@Override public void doHandle(Map<String, Object> attributes, BeanDefinition beanDefinition, BeanDefinitionRegistry registry) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ServletRegistrationBean.class); builder.addPropertyValue("asyncSupported", attributes.get("asyncSupported")); builder.addPropertyValue("initParameters", extractInitParameters(attributes)); builder.addPropertyValue("loadOnStartup", attributes.get("loadOnStartup")); String name = determineName(attributes, beanDefinition); builder.addPropertyValue("name", name); builder.addPropertyValue("servlet", beanDefinition); builder.addPropertyValue("urlMappings", extractUrlPatterns("urlPatterns", attributes)); registry.registerBeanDefinition(name, builder.getBeanDefinition()); }
@Bean ServletRegistrationBean h2servletRegistration() { ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet()); registrationBean.addUrlMappings("/console/*"); return registrationBean; }
@Bean @Autowired public ServletRegistrationBean servletRegistrationBean(MetricRegistry metricRegistry) { MetricsServlet ms = new MetricsServlet(metricRegistry); ServletRegistrationBean srb = new ServletRegistrationBean(ms, "/stats/*"); srb.setLoadOnStartup(1); return srb; }
@Bean @Autowired public ServletRegistrationBean servletHealthRegistryBean(HealthCheckRegistry healthCheckRegistry) { HealthCheckServlet hc = new HealthCheckServlet(healthCheckRegistry); ServletRegistrationBean srb = new ServletRegistrationBean(hc, "/health/*"); srb.setLoadOnStartup(2); return srb; }
@Bean ServletRegistrationBean h2servletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet()); registration.addUrlMappings("/console/*"); registration.addInitParameter("webAllowOthers", "true"); return registration; }
@Bean public static BeanDefinitionRegistryPostProcessor removeUnwantedAutomaticFilterRegistration() { return new BeanDefinitionRegistryPostProcessor() { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException { DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) bf; Set<String> filtersToDisable = ImmutableSet.of("samlEntryPoint", "samlFilter", "samlIDPDiscovery", "metadataDisplayFilter", "samlWebSSOHoKProcessingFilter", "samlWebSSOProcessingFilter", "samlLogoutProcessingFilter", "samlLogoutFilter", "metadataGeneratorFilter"); Arrays.stream(beanFactory.getBeanNamesForType(javax.servlet.Filter.class)) .filter(filtersToDisable::contains) .forEach(name -> { BeanDefinition definition = BeanDefinitionBuilder .genericBeanDefinition(FilterRegistrationBean.class) .setScope(BeanDefinition.SCOPE_SINGLETON) .addConstructorArgReference(name) .addConstructorArgValue(new ServletRegistrationBean[]{}) .addPropertyValue("enabled", false) .getBeanDefinition(); beanFactory.registerBeanDefinition(name + "FilterRegistrationBean", definition); }); } }; }
@Bean public ServletRegistrationBean memoryMonitorStartupServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new MemoryMonitorStartupServlet(), "/admin4j/memory"); registration.setLoadOnStartup(1); return registration; }
@Bean public ServletRegistrationBean monetaServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new MonetaServlet(), "/moneta/topic/*"); registration.addInitParameter( MonetaServlet.CONFIG_IGNORED_CONTEXT_PATH_NODES, "moneta,topic"); return registration; }
@Bean public ServletRegistrationBean monetaTopicListServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new MonetaTopicListServlet(), "/moneta/topics/*"); return registration; }
@Override public void doHandle(Map<String, Object> attributes, BeanDefinition beanDefinition, BeanDefinitionRegistry registry) { BeanDefinitionBuilder builder = BeanDefinitionBuilder .rootBeanDefinition(ServletRegistrationBean.class); builder.addPropertyValue("asyncSupported", attributes.get("asyncSupported")); builder.addPropertyValue("initParameters", extractInitParameters(attributes)); builder.addPropertyValue("loadOnStartup", attributes.get("loadOnStartup")); String name = determineName(attributes, beanDefinition); builder.addPropertyValue("name", name); builder.addPropertyValue("servlet", beanDefinition); builder.addPropertyValue("urlMappings", extractUrlPatterns("urlPatterns", attributes)); registry.registerBeanDefinition(name, builder.getBeanDefinition()); }
@Test public void errorPage404() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/hello")); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(), "/hello")); this.container.start(); assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World")); assertThat(getResponse(getLocalUrl("/not-found")), equalTo("Hello World")); }
@Override @SuppressWarnings("serial") // Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646 protected String setUpFactoryForCompression(final int contentSize, String[] mimeTypes, String[] excludedUserAgents) throws Exception { char[] chars = new char[contentSize]; Arrays.fill(chars, 'F'); final String testContent = new String(chars); AbstractEmbeddedServletContainerFactory factory = getFactory(); Compression compression = new Compression(); compression.setEnabled(true); if (mimeTypes != null) { compression.setMimeTypes(mimeTypes); } if (excludedUserAgents != null) { compression.setExcludedUserAgents(excludedUserAgents); } factory.setCompression(compression); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentLength(contentSize); resp.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain"); resp.getWriter().print(testContent); } }, "/test.txt")); this.container.start(); return testContent; }
private ConditionOutcome checkServletRegistrations( ConfigurableListableBeanFactory beanFactory) { List<String> registrations = Arrays.asList(beanFactory .getBeanNamesForType(ServletRegistrationBean.class, false, false)); boolean containsDispatcherRegistrationBean = beanFactory .containsBean(DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); if (registrations.isEmpty()) { if (containsDispatcherRegistrationBean) { return ConditionOutcome.noMatch("found no ServletRegistrationBean " + "but a non-ServletRegistrationBean named " + DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); } return ConditionOutcome.match("no ServletRegistrationBean found"); } if (registrations .contains(DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)) { return ConditionOutcome.noMatch("found ServletRegistrationBean named " + DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); } if (containsDispatcherRegistrationBean) { return ConditionOutcome.noMatch("found non-ServletRegistrationBean named " + DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); } return ConditionOutcome .match("one or more ServletRegistrationBeans is found and none is named " + DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); }
@Bean @ConditionalOnMissingBean(name = "jerseyServletRegistration") @ConditionalOnProperty(prefix = "spring.jersey", name = "type", havingValue = "servlet", matchIfMissing = true) public ServletRegistrationBean jerseyServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean( new ServletContainer(this.config), this.path); addInitParameters(registration); registration.setName("jerseyServlet"); return registration; }
@Test public void registrationProperties() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.register(ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class); this.context.setServletContext(new MockServletContext()); this.context.refresh(); assertNotNull(this.context.getBean(DispatcherServlet.class)); ServletRegistrationBean registration = this.context .getBean(ServletRegistrationBean.class); assertEquals("[/]", registration.getUrlMappings().toString()); }
@Test public void registrationOverride() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.register(CustomDispatcherRegistration.class, ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class); this.context.setServletContext(new MockServletContext()); this.context.refresh(); ServletRegistrationBean registration = this.context .getBean(ServletRegistrationBean.class); assertEquals("[/foo]", registration.getUrlMappings().toString()); assertEquals("customDispatcher", registration.getServletName()); assertEquals(0, this.context.getBeanNamesForType(DispatcherServlet.class).length); }
@Test(expected = UnsatisfiedDependencyException.class) public void registrationOverrideWithAutowiredServlet() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.register(CustomAutowiredRegistration.class, ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class); this.context.setServletContext(new MockServletContext()); this.context.refresh(); ServletRegistrationBean registration = this.context .getBean(ServletRegistrationBean.class); assertEquals("[/foo]", registration.getUrlMappings().toString()); assertEquals("customDispatcher", registration.getServletName()); assertEquals(1, this.context.getBeanNamesForType(DispatcherServlet.class).length); }
@Test public void servletPath() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "server.servlet_path:/spring"); this.context.refresh(); assertNotNull(this.context.getBean(DispatcherServlet.class)); ServletRegistrationBean registration = this.context .getBean(ServletRegistrationBean.class); assertEquals("[/spring/*]", registration.getUrlMappings().toString()); assertNull(registration.getMultipartConfig()); }
@Test public void multipartConfig() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(MultipartConfiguration.class, ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class); this.context.refresh(); ServletRegistrationBean registration = this.context .getBean(ServletRegistrationBean.class); assertNotNull(registration.getMultipartConfig()); }
@Bean public ServletRegistrationBean dispatcherServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean( new DispatcherServlet(), "/foo"); registration.setName("customDispatcher"); return registration; }
@Bean public ServletRegistrationBean dispatcherServletRegistration( DispatcherServlet dispatcherServlet) { ServletRegistrationBean registration = new ServletRegistrationBean( dispatcherServlet, "/foo"); registration.setName("customDispatcher"); return registration; }
@Test public void consoleIsDisabledByDefault() { this.context.register(H2ConsoleAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBeansOfType(ServletRegistrationBean.class).size(), is(equalTo(0))); }
@Test public void propertyCanEnableConsole() { this.context.register(H2ConsoleAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.h2.console.enabled:true"); this.context.refresh(); assertThat(this.context.getBeansOfType(ServletRegistrationBean.class).size(), is(equalTo(1))); assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings(), hasItems("/h2-console/*")); }
@Test public void customPathWithTrailingSlash() { this.context.register(H2ConsoleAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.h2.console.enabled:true", "spring.h2.console.path:/custom/"); this.context.refresh(); assertThat(this.context.getBeansOfType(ServletRegistrationBean.class).size(), is(equalTo(1))); assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings(), hasItems("/custom/*")); }