@Override protected WebHttpHandlerBuilder initHttpHandlerBuilder() { return WebHttpHandlerBuilder.applicationContext(this.applicationContext); }
@Override protected WebHttpHandlerBuilder initHttpHandlerBuilder() { return WebHttpHandlerBuilder.applicationContext(initApplicationContext()); }
@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); }
@Override protected HttpHandler createHttpHandler() { this.restTemplate = initRestTemplate(); this.applicationContext = initApplicationContext(); return WebHttpHandlerBuilder.applicationContext(this.applicationContext).build(); }
@Test // SPR-15074 public void orderedWebExceptionHandlerBeans() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(OrderedExceptionHandlerBeanConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); httpHandler.handle(request, response).block(ofMillis(5000)); assertEquals("ExceptionHandlerB", response.getBodyAsString().block(ofMillis(5000))); }
@Test public void configWithoutFilters() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(NoFilterConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); httpHandler.handle(request, response).block(ofMillis(5000)); assertEquals("handled", response.getBodyAsString().block(ofMillis(5000))); }
private HttpHandler createHttpHandler() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DispatcherConfig.class, this.serverConfigClass); context.register(getWebConfigClass()); context.refresh(); return WebHttpHandlerBuilder.applicationContext(context).build(); }
@Test public void forwardedHeaderFilter() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(ForwardedHeaderFilterConfig.class); context.refresh(); WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.applicationContext(context); builder.filters(filters -> assertEquals(Collections.emptyList(), filters)); assertTrue(builder.hasForwardedHeaderTransformer()); }
@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); }
@Test // SPR-15074 public void orderedWebFilterBeans() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(OrderedWebFilterBeanConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); assertTrue(httpHandler instanceof HttpWebHandlerAdapter); assertSame(context, ((HttpWebHandlerAdapter) httpHandler).getApplicationContext()); MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); httpHandler.handle(request, response).block(ofMillis(5000)); assertEquals("FilterB::FilterA", response.getBodyAsString().block(ofMillis(5000))); }
private HttpHandler createHttpHandler() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(WebSocketTestConfig.class); context.refresh(); return WebHttpHandlerBuilder.applicationContext(context).build(); }
@Test public void servletPathMapping() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(WebAppConfig.class); context.refresh(); File base = new File(System.getProperty("java.io.tmpdir")); TomcatHttpServer server = new TomcatHttpServer(base.getAbsolutePath()); server.setContextPath("/app"); server.setServletMapping("/api/*"); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); server.setHandler(httpHandler); server.afterPropertiesSet(); server.start(); try { RestTemplate restTemplate = new RestTemplate(); String actual; String url = "http://localhost:" + server.getPort() + "/app/api/test"; actual = restTemplate.getForObject(url, String.class); assertEquals("Tested in /app/api", actual); } finally { server.stop(); } }
@Test public void multipleWebFluxApps() throws Exception { AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(); context1.register(WebAppConfig.class); context1.refresh(); AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(); context2.register(WebAppConfig.class); context2.refresh(); HttpHandler webApp1Handler = WebHttpHandlerBuilder.applicationContext(context1).build(); HttpHandler webApp2Handler = WebHttpHandlerBuilder.applicationContext(context2).build(); ReactorHttpServer server = new ReactorHttpServer(); server.registerHttpHandler("/webApp1", webApp1Handler); server.registerHttpHandler("/webApp2", webApp2Handler); server.afterPropertiesSet(); server.start(); try { RestTemplate restTemplate = new RestTemplate(); String actual; String url = "http://localhost:" + server.getPort() + "/webApp1/test"; actual = restTemplate.getForObject(url, String.class); assertEquals("Tested in /webApp1", actual); url = "http://localhost:" + server.getPort() + "/webApp2/test"; actual = restTemplate.getForObject(url, String.class); assertEquals("Tested in /webApp2", actual); } finally { server.stop(); } }
@Test // SPR-16972 public void cloneWithApplicationContext() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(NoFilterConfig.class); context.refresh(); WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.applicationContext(context); assertSame(context, ((HttpWebHandlerAdapter) builder.build()).getApplicationContext()); assertSame(context, ((HttpWebHandlerAdapter) builder.clone().build()).getApplicationContext()); }
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
@Bean public Undertow undertowServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); // (2) // Undertow UndertowHttpHandlerAdapter undertowAdapter = new UndertowHttpHandlerAdapter(handler); Undertow server = Undertow.builder() .addHttpListener(port, "localhost") .setHandler(undertowAdapter) .build(); return server; }