Search in sources :

Example 1 with HttpHandler

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

the class AbstractServletHttpHandlerAdapterInitializer method registerHandlerAdapter.

/**
	 * Register a {@link ServletHttpHandlerAdapter} against the given servlet context.
	 * <p>This method will create a {@code HttpHandler} using {@link #createHttpHandler()},
	 * and use it to create a {@code ServletHttpHandlerAdapter} with the name returned by
	 * {@link #getServletName()}, and mapping it to the patterns
	 * returned from {@link #getServletMappings()}.
	 * <p>Further customization can be achieved by overriding {@link
	 * #customizeRegistration(ServletRegistration.Dynamic)} or
	 * {@link #createServlet(HttpHandler)}.
	 * @param servletContext the context to register the servlet against
	 */
protected void registerHandlerAdapter(ServletContext servletContext) {
    String servletName = getServletName();
    Assert.hasLength(servletName, "getServletName() must not return empty or null");
    HttpHandler httpHandler = createHttpHandler();
    Assert.notNull(httpHandler, "createHttpHandler() did not return a HttpHandler for servlet [" + servletName + "]");
    ServletHttpHandlerAdapter servlet = createServlet(httpHandler);
    Assert.notNull(servlet, "createHttpHandler() did not return a ServletHttpHandlerAdapter for servlet [" + servletName + "]");
    ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet);
    Assert.notNull(registration, "Failed to register servlet with name '" + servletName + "'." + "Check if there is another servlet registered under the same name.");
    registration.setLoadOnStartup(1);
    registration.addMapping(getServletMappings());
    registration.setAsyncSupported(true);
    customizeRegistration(registration);
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) ServletRegistration(javax.servlet.ServletRegistration) ServletHttpHandlerAdapter(org.springframework.http.server.reactive.ServletHttpHandlerAdapter)

Example 2 with HttpHandler

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

the class ContextPathIntegrationTests method setup.

@Before
public void setup() throws Exception {
    AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext();
    context1.register(WebApp1Config.class);
    context1.refresh();
    AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext();
    context2.register(WebApp2Config.class);
    context2.refresh();
    HttpHandler webApp1Handler = DispatcherHandler.toHttpHandler(context1);
    HttpHandler webApp2Handler = DispatcherHandler.toHttpHandler(context2);
    this.server = new ReactorHttpServer();
    this.server.registerHttpHandler("/webApp1", webApp1Handler);
    this.server.registerHttpHandler("/webApp2", webApp2Handler);
    this.server.afterPropertiesSet();
    this.server.start();
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ReactorHttpServer(org.springframework.http.server.reactive.bootstrap.ReactorHttpServer) Before(org.junit.Before)

Example 3 with HttpHandler

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

the class HttpServerTests method setUp.

@Before
public void setUp() throws Exception {
    HttpHandler httpHandler = RouterFunctions.toHttpHandler(route(GET("/test"), request -> ServerResponse.ok().body(Mono.just("It works!"), String.class)));
    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 : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ReactorHttpServer(org.springframework.http.server.reactive.bootstrap.ReactorHttpServer) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) After(org.junit.After) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Before(org.junit.Before) RouterFunctions.route(org.springframework.web.reactive.function.server.RouterFunctions.route) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ReactorHttpServer(org.springframework.http.server.reactive.bootstrap.ReactorHttpServer) Before(org.junit.Before)

Example 4 with HttpHandler

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

the class HttpHandlerAutoConfigurationTests method shouldConfigureWebFiltersAnnotation.

@Test
public void shouldConfigureWebFiltersAnnotation() {
    load(AnnotationConfigWithWebFilters.class);
    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("firstWebFilter", WebFilter.class), this.context.getBean("aWebFilter", WebFilter.class), this.context.getBean("lastWebFilter", 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 5 with HttpHandler

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

the class SpringSecurity5Application method nettyContext.

@Bean
public NettyContext nettyContext(ApplicationContext context) {
    HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
    ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
    HttpServer httpServer = HttpServer.create("localhost", 8080);
    return httpServer.newHandler(adapter).block();
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpServer(reactor.ipc.netty.http.server.HttpServer) ReactorHttpHandlerAdapter(org.springframework.http.server.reactive.ReactorHttpHandlerAdapter) Bean(org.springframework.context.annotation.Bean)

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