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

The following examples show how to use org.springframework.web.server.adapter.WebHttpHandlerBuilder. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: FilteringWebHandlerTests.java    License: 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 2
Source Project: tutorials   Source File: FunctionalWebApplication.java    License: 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 3
Source Project: spring-analysis-note   Source File: MockServerSpecTests.java    License: 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 4
Source Project: java-technology-stack   Source File: FilteringWebHandlerTests.java    License: 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 5
Source Project: java-technology-stack   Source File: MockServerSpecTests.java    License: 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 6
@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 7
@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 8
@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 9
private HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.register(DispatcherConfig.class, this.serverConfigClass);
	context.register(getWebConfigClass());
	context.refresh();
	return WebHttpHandlerBuilder.applicationContext(context).build();
}
 
Example 10
@Override
protected HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
	wac.register(TestConfiguration.class);
	wac.refresh();

	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
 
Example 11
@Override
protected HttpHandler createHttpHandler() {
	this.wac = new AnnotationConfigApplicationContext();
	this.wac.register(TestConfiguration.class);
	this.wac.refresh();

	DispatcherHandler webHandler = new DispatcherHandler();
	webHandler.setApplicationContext(this.wac);

	return WebHttpHandlerBuilder.webHandler(webHandler).build();
}
 
Example 12
@Override
protected HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
	wac.register(WebConfig.class);
	wac.refresh();

	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac))
			.exceptionHandler(new ResponseStatusExceptionHandler())
			.build();
}
 
Example 13
Source Project: spring-analysis-note   Source File: MultipartIntegrationTests.java    License: MIT License 5 votes vote down vote up
@Override
protected HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
	wac.register(TestConfiguration.class);
	wac.refresh();
	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
 
Example 14
Source Project: spring-analysis-note   Source File: SseIntegrationTests.java    License: MIT License 5 votes vote down vote up
@Override
protected HttpHandler createHttpHandler() {
	this.wac = new AnnotationConfigApplicationContext();
	this.wac.register(TestConfiguration.class);
	this.wac.refresh();

	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(this.wac)).build();
}
 
Example 15
@Override
protected HttpHandler createHttpHandler() {
	this.wac = new AnnotationConfigApplicationContext();
	this.wac.register(TestConfiguration.class);
	this.wac.refresh();

	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(this.wac)).build();
}
 
Example 16
Source Project: spring-analysis-note   Source File: ContextPathIntegrationTests.java    License: MIT License 5 votes vote down vote up
@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();
	}
}
 
Example 17
Source Project: spring-analysis-note   Source File: ContextPathIntegrationTests.java    License: MIT License 5 votes vote down vote up
@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();
	}
}
 
Example 18
Source Project: tutorials   Source File: SpringSecurity5Application.java    License: MIT License 5 votes vote down vote up
@Bean
public DisposableServer disposableServer(ApplicationContext context) {
    HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context)
            .build();
    ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
    HttpServer httpServer = HttpServer.create();
    httpServer.host("localhost");
    httpServer.port(8080);
    return httpServer.handle(adapter).bindNow();
}
 
Example 19
@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 Project: spring-analysis-note   Source File: DefaultWebTestClientBuilder.java    License: 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 21
@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 Project: spring-analysis-note   Source File: DefaultRouterFunctionSpec.java    License: 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 23
@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 24
Source Project: spring-analysis-note   Source File: MockServerSpecTests.java    License: 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 25
Source Project: spring-analysis-note   Source File: ExchangeMutatorTests.java    License: 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 26
private HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.register(DispatcherConfig.class, this.serverConfigClass);
	context.register(getWebConfigClass());
	context.refresh();
	return WebHttpHandlerBuilder.applicationContext(context).build();
}
 
Example 27
@Override
protected HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
	wac.register(TestConfiguration.class);
	wac.refresh();

	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
 
Example 28
@Override
protected HttpHandler createHttpHandler() {
	this.wac = new AnnotationConfigApplicationContext();
	this.wac.register(TestConfiguration.class);
	this.wac.refresh();

	DispatcherHandler webHandler = new DispatcherHandler();
	webHandler.setApplicationContext(this.wac);

	return WebHttpHandlerBuilder.webHandler(webHandler).build();
}
 
Example 29
Source Project: java-technology-stack   Source File: MultipartIntegrationTests.java    License: MIT License 5 votes vote down vote up
@Override
protected HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
	wac.register(TestConfiguration.class);
	wac.refresh();
	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
 
Example 30
@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);
}