org.springframework.web.server.adapter.WebHttpHandlerBuilder Java Examples

The following examples show how to use org.springframework.web.server.adapter.WebHttpHandlerBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 6 votes vote down vote up
@Bean
public Server jettyServer(ApplicationContext context) throws Exception {
    HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
    Servlet servlet = new JettyHttpHandlerAdapter(handler);

    Server server = new Server();
    ServletContextHandler contextHandler = new ServletContextHandler(server, "");
    contextHandler.addServlet(new ServletHolder(servlet), "/");
    contextHandler.start();

    ServerConnector connector = new ServerConnector(server);
    connector.setHost("localhost");
    connector.setPort(port);
    server.addConnector(connector);

    return server;
}
 
Example #2
Source File: SpringWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Test
void enableSpringWebSessionConfiguresThings() {

	this.context = new AnnotationConfigApplicationContext();
	this.context.register(GoodConfig.class);
	this.context.refresh();

	WebSessionManager webSessionManagerFoundByType = this.context.getBean(WebSessionManager.class);
	Object webSessionManagerFoundByName = this.context.getBean(WebHttpHandlerBuilder.WEB_SESSION_MANAGER_BEAN_NAME);

	assertThat(webSessionManagerFoundByType).isNotNull();
	assertThat(webSessionManagerFoundByName).isNotNull();
	assertThat(webSessionManagerFoundByType).isEqualTo(webSessionManagerFoundByName);

	assertThat(this.context.getBean(ReactiveSessionRepository.class)).isNotNull();
}
 
Example #3
Source File: FilteringWebHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handleErrorFromFilter() throws Exception {

	MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
	MockServerHttpResponse response = new MockServerHttpResponse();

	TestExceptionHandler exceptionHandler = new TestExceptionHandler();

	WebHttpHandlerBuilder.webHandler(new StubWebHandler())
			.filter(new ExceptionFilter())
			.exceptionHandler(exceptionHandler).build()
			.handle(request, response)
			.block();

	assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
	assertNotNull(exceptionHandler.ex);
	assertEquals("boo", exceptionHandler.ex.getMessage());
}
 
Example #4
Source File: MockServerSpecTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void applyFiltersBeforeServerCreated() {

	this.serverSpec.webFilter(new TestWebFilter("App-A"));
	this.serverSpec.webFilter(new TestWebFilter("App-B"));

	this.serverSpec.apply(new MockServerConfigurer() {

		@Override
		public void beforeServerCreated(WebHttpHandlerBuilder builder) {
			builder.filters(filters -> {
				filters.add(0, new TestWebFilter("Fwk-A"));
				filters.add(1, new TestWebFilter("Fwk-B"));
			});
		}
	});

	this.serverSpec.build().get().uri("/")
			.exchange()
			.expectBody(String.class)
			.consumeWith(result -> assertThat(
					result.getResponseBody(), containsString("test-attribute=:Fwk-A:Fwk-B:App-A:App-B")));
}
 
Example #5
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 6 votes vote down vote up
@Bean
@Profile("default")
public Tomcat embeddedTomcatServer(ApplicationContext context) {
    HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();

    Servlet servlet = new TomcatHttpHandlerAdapter(handler);
    Tomcat tomcat = new Tomcat();

    File base = new File(System.getProperty("java.io.tmpdir"));
    Context rootContext = tomcat.addContext("", base.getAbsolutePath());
    Tomcat.addServlet(rootContext, "main", servlet).setAsyncSupported(true);
    rootContext.addServletMappingDecoded("/", "main");

    tomcat.setHostname("localhost");
    tomcat.setPort(this.port);
    tomcat.setBaseDir(System.getProperty("java.io.tmpdir"));

    return tomcat;
}
 
Example #6
Source File: FunctionalWebApplication.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example #7
Source File: FilteringWebHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handleErrorFromFilter() throws Exception {

	MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
	MockServerHttpResponse response = new MockServerHttpResponse();

	TestExceptionHandler exceptionHandler = new TestExceptionHandler();

	WebHttpHandlerBuilder.webHandler(new StubWebHandler())
			.filter(new ExceptionFilter())
			.exceptionHandler(exceptionHandler).build()
			.handle(request, response)
			.block();

	assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
	assertNotNull(exceptionHandler.ex);
	assertEquals("boo", exceptionHandler.ex.getMessage());
}
 
Example #8
Source File: MockServerSpecTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void applyFiltersBeforeServerCreated() {

	this.serverSpec.webFilter(new TestWebFilter("App-A"));
	this.serverSpec.webFilter(new TestWebFilter("App-B"));

	this.serverSpec.apply(new MockServerConfigurer() {

		@Override
		public void beforeServerCreated(WebHttpHandlerBuilder builder) {
			builder.filters(filters -> {
				filters.add(0, new TestWebFilter("Fwk-A"));
				filters.add(1, new TestWebFilter("Fwk-B"));
			});
		}
	});

	this.serverSpec.build().get().uri("/")
			.exchange()
			.expectBody(String.class)
			.consumeWith(result -> assertThat(
					result.getResponseBody(), containsString("test-attribute=:Fwk-A:Fwk-B:App-A:App-B")));
}
 
Example #9
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #10
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #11
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public HttpServer httpServer(ApplicationContext context) {
    HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
    ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
    return  HttpServer.create()
            .host("localhost")
            .port(this.port)
            .handle(adapter);
}
 
Example #13
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #14
Source File: SpringWebFluxTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() {
  APPLICATION_CONTEXT.registerBean("jettyReactiveWebServerFactory", JettyReactiveWebServerFactory.class, () -> new JettyReactiveWebServerFactory(0));
  APPLICATION_CONTEXT.registerBean("httpHandler", HttpHandler.class, () -> WebHttpHandlerBuilder.applicationContext(APPLICATION_CONTEXT).build());
  APPLICATION_CONTEXT.registerBean("webHandler", WebHandler.class, () -> SpringWebFluxTest::handler);
  APPLICATION_CONTEXT.refresh();

  final int serverPort = APPLICATION_CONTEXT.getWebServer().getPort();
  testRestTemplate = new TestRestTemplate(new RestTemplateBuilder().rootUri("http://127.0.0.1:" + serverPort));
}
 
Example #15
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #16
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #17
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #18
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #22
Source File: ExchangeMutatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void afterConfigurerAdded(WebTestClient.Builder builder,
		@Nullable WebHttpHandlerBuilder httpHandlerBuilder,
		@Nullable ClientHttpConnector connector) {

	Assert.notNull(httpHandlerBuilder, "Not a mock server");
	httpHandlerBuilder.filters(filters -> {
		filters.removeIf(filter -> filter instanceof IdentityFilter);
		filters.add(0, this.filter);
	});
}
 
Example #23
Source File: MockServerSpecTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
	return WebHttpHandlerBuilder.webHandler(exchange -> {
		DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
		String text = exchange.getAttributes().toString();
		DataBuffer buffer = factory.wrap(text.getBytes(StandardCharsets.UTF_8));
		return exchange.getResponse().writeWith(Mono.just(buffer));
	});
}
 
Example #24
Source File: FunctionalSpringBootApplication.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ServletRegistrationBean servletRegistrationBean() throws Exception {
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler((WebHandler) toHttpHandler(routingFunction()))
        .filter(new IndexRewriteFilter())
        .build();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new RootServlet(httpHandler), "/");
    registrationBean.setLoadOnStartup(1);
    registrationBean.setAsyncSupported(true);
    return registrationBean;
}
 
Example #25
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #26
Source File: AbstractMockServerSpec.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public WebTestClient.Builder configureClient() {
	WebHttpHandlerBuilder builder = initHttpHandlerBuilder();
	if (!CollectionUtils.isEmpty(this.filters)) {
		builder.filters(theFilters -> theFilters.addAll(0, this.filters));
	}
	if (!builder.hasSessionManager() && this.sessionManager != null) {
		builder.sessionManager(this.sessionManager);
	}
	if (!CollectionUtils.isEmpty(this.configurers)) {
		this.configurers.forEach(configurer -> configurer.beforeServerCreated(builder));
	}
	return new DefaultWebTestClientBuilder(builder);
}
 
Example #27
Source File: DefaultRouterFunctionSpec.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
	WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies);
	return WebHttpHandlerBuilder.webHandler(webHandler)
			.filters(filters -> filters.addAll(this.handlerStrategies.webFilters()))
			.exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers()))
			.localeContextResolver(this.handlerStrategies.localeContextResolver());
}
 
Example #28
Source File: DefaultWebTestClientBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
private DefaultWebTestClientBuilder(@Nullable WebClient.Builder webClientBuilder,
		@Nullable WebHttpHandlerBuilder httpHandlerBuilder, @Nullable ClientHttpConnector connector,
		@Nullable Duration responseTimeout) {

	Assert.isTrue(httpHandlerBuilder != null || connector != null,
			"Either WebHttpHandlerBuilder or ClientHttpConnector must be provided");

	this.webClientBuilder = (webClientBuilder != null ? webClientBuilder : WebClient.builder());
	this.httpHandlerBuilder = (httpHandlerBuilder != null ? httpHandlerBuilder.clone() : null);
	this.connector = connector;
	this.responseTimeout = responseTimeout;
}
 
Example #29
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #30
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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);
}