Search in sources :

Example 6 with HttpHandler

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

the class FunctionalSpringBootApplication method servletRegistrationBean.

@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;
}
Also used : RouterFunctions.toHttpHandler(org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 7 with HttpHandler

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

the class Spring5ReactiveServerClientIntegrationTest method setUp.

@BeforeAll
public static void setUp() throws Exception {
    HttpServer server = HttpServer.create("localhost", 8080);
    RouterFunction<?> route = RouterFunctions.route(POST("/task/process"), request -> ServerResponse.ok().body(request.bodyToFlux(Task.class).map(ll -> new Task("TaskName", 1)), Task.class)).and(RouterFunctions.route(GET("/task"), request -> ServerResponse.ok().body(Mono.just("server is alive"), String.class)));
    HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
    ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
    nettyContext = server.newHandler(adapter).block();
}
Also used : ReactorHttpHandlerAdapter(org.springframework.http.server.reactive.ReactorHttpHandlerAdapter) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) POST(org.springframework.web.reactive.function.server.RequestPredicates.POST) Mono(reactor.core.publisher.Mono) AfterAll(org.junit.jupiter.api.AfterAll) Flux(reactor.core.publisher.Flux) HttpHandler(org.springframework.http.server.reactive.HttpHandler) BeforeAll(org.junit.jupiter.api.BeforeAll) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) Duration(java.time.Duration) Task(com.baeldung.web.reactive.Task) NettyContext(reactor.ipc.netty.NettyContext) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Task(com.baeldung.web.reactive.Task) HttpServer(reactor.ipc.netty.http.server.HttpServer) ReactorHttpHandlerAdapter(org.springframework.http.server.reactive.ReactorHttpHandlerAdapter) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with HttpHandler

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

the class RouterFunctionsTests method toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo.

@Test
public void toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo() {
    HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
    request -> Mono.just(new ServerResponse() {

        @Override
        public HttpStatus statusCode() {
            return HttpStatus.OK;
        }

        @Override
        public int rawStatusCode() {
            return 200;
        }

        @Override
        public HttpHeaders headers() {
            return new HttpHeaders();
        }

        @Override
        public MultiValueMap<String, ResponseCookie> cookies() {
            return new LinkedMultiValueMap<>();
        }

        @Override
        public Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
        }
    });
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
    assertThat(result).isNotNull();
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) HttpHeaders(org.springframework.http.HttpHeaders) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Example 9 with HttpHandler

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

the class RouterFunctionsTests method toHttpHandlerHandlerReturnsException.

@Test
public void toHttpHandlerHandlerReturnsException() {
    HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new IllegalStateException());
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
    assertThat(result).isNotNull();
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 10 with HttpHandler

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

the class RouterFunctionsTests method toHttpHandlerWebFilter.

@Test
public void toHttpHandlerWebFilter() {
    AtomicBoolean filterInvoked = new AtomicBoolean();
    WebFilter webFilter = (exchange, chain) -> {
        filterInvoked.set(true);
        return chain.filter(exchange);
    };
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HandlerStrategies handlerStrategies = HandlerStrategies.builder().webFilter(webFilter).build();
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, handlerStrategies);
    assertThat(result).isNotNull();
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
    assertThat(filterInvoked.get()).isTrue();
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

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