Search in sources :

Example 16 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project spring-boot by spring-projects.

the class HttpHandlerAutoConfigurationTests method shouldConfigureWebFiltersFunctional.

@Test
public void shouldConfigureWebFiltersFunctional() {
    load(FunctionalConfigWithWebFilters.class);
    assertThat(this.context.getBeansOfType(HttpHandler.class).size()).isEqualTo(1);
    HttpHandler handler = this.context.getBean(HttpHandler.class);
    assertThat(handler).isInstanceOf(WebHandler.class);
    WebHandler webHandler = (WebHandler) handler;
    while (webHandler instanceof WebHandlerDecorator) {
        if (webHandler instanceof FilteringWebHandler) {
            FilteringWebHandler filteringWebHandler = (FilteringWebHandler) webHandler;
            assertThat(filteringWebHandler.getFilters()).containsExactly(this.context.getBean("customWebFilter", WebFilter.class));
            return;
        }
        webHandler = ((WebHandlerDecorator) webHandler).getDelegate();
    }
    fail("Did not find any FilteringWebHandler");
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) WebHandlerDecorator(org.springframework.web.server.handler.WebHandlerDecorator) WebHandler(org.springframework.web.server.WebHandler) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) Test(org.junit.Test)

Example 17 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project tutorials by eugenp.

the class FunctionalWebApplication method start.

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;
}
Also used : Context(org.apache.catalina.Context) RouterFunctions.toHttpHandler(org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Tomcat(org.apache.catalina.startup.Tomcat) TomcatWebServer(org.springframework.boot.web.embedded.tomcat.TomcatWebServer) ServletHttpHandlerAdapter(org.springframework.http.server.reactive.ServletHttpHandlerAdapter) WebHandler(org.springframework.web.server.WebHandler)

Example 18 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project tutorials by eugenp.

the class ExploreSpring5URLPatternUsingRouterFunctions method start.

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;
}
Also used : Context(org.apache.catalina.Context) RouterFunctions.toHttpHandler(org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Tomcat(org.apache.catalina.startup.Tomcat) TomcatWebServer(org.springframework.boot.web.embedded.tomcat.TomcatWebServer) ServletHttpHandlerAdapter(org.springframework.http.server.reactive.ServletHttpHandlerAdapter) WebHandler(org.springframework.web.server.WebHandler)

Example 19 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project tutorials by eugenp.

the class FunctionalWebApplication method start.

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;
}
Also used : Context(org.apache.catalina.Context) RouterFunctions.toHttpHandler(org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Tomcat(org.apache.catalina.startup.Tomcat) TomcatWebServer(org.springframework.boot.web.embedded.tomcat.TomcatWebServer) ServletHttpHandlerAdapter(org.springframework.http.server.reactive.ServletHttpHandlerAdapter) WebHandler(org.springframework.web.server.WebHandler)

Example 20 with HttpHandler

use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.

the class HttpServerTests method start.

@BeforeEach
public void start() throws Exception {
    HttpHandler httpHandler = RouterFunctions.toHttpHandler(route(GET("/test"), request -> ServerResponse.ok().bodyValue("It works!")));
    this.server = new ReactorHttpServer();
    this.server.setHandler(httpHandler);
    this.server.afterPropertiesSet();
    this.server.start();
    this.client = WebTestClient.bindToServer().baseUrl("http://localhost:" + this.server.getPort()).build();
}
Also used : Test(org.junit.jupiter.api.Test) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) BeforeEach(org.junit.jupiter.api.BeforeEach) AfterEach(org.junit.jupiter.api.AfterEach) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) ReactorHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer) RouterFunctions.route(org.springframework.web.reactive.function.server.RouterFunctions.route) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ReactorHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

HttpHandler (org.springframework.http.server.reactive.HttpHandler)29 Test (org.junit.jupiter.api.Test)17 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)14 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)13 Mono (reactor.core.publisher.Mono)13 WebFilter (org.springframework.web.server.WebFilter)12 Flux (reactor.core.publisher.Flux)12 DataBuffer (org.springframework.core.io.buffer.DataBuffer)11 HttpHeaders (org.springframework.http.HttpHeaders)11 StandardCharsets (java.nio.charset.StandardCharsets)10 Collections (java.util.Collections)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 ServerWebExchange (org.springframework.web.server.ServerWebExchange)10 MultiValueMap (org.springframework.util.MultiValueMap)9 Optional (java.util.Optional)8 BDDMockito.given (org.mockito.BDDMockito.given)8 Mockito.mock (org.mockito.Mockito.mock)8 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)8 HttpStatus (org.springframework.http.HttpStatus)8