Example usage for org.springframework.web.server.adapter WebHttpHandlerBuilder webHandler

List of usage examples for org.springframework.web.server.adapter WebHttpHandlerBuilder webHandler

Introduction

In this page you can find the example usage for org.springframework.web.server.adapter WebHttpHandlerBuilder webHandler.

Prototype

WebHandler webHandler

To view the source code for org.springframework.web.server.adapter WebHttpHandlerBuilder webHandler.

Click Source Link

Usage

From source file:playground.app.Application.java

public static HttpHandler createHttpHandler() throws IOException {
    Properties prop = new Properties();
    prop.load(Application.class.getClassLoader().getResourceAsStream("application.properties"));
    String profiles = prop.getProperty("profiles");
    if (profiles != null) {
        System.setProperty("spring.profiles.active", profiles);
    }//from  w w  w  .j a v a 2  s.c  o  m

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("playground");

    DispatcherHandler dispatcherHandler = new DispatcherHandler();
    dispatcherHandler.setApplicationContext(context);

    Map<String, WebFilter> beanNameToFilters = context.getBeansOfType(WebFilter.class);
    WebFilter[] filters = beanNameToFilters.values().toArray(new WebFilter[0]);
    Arrays.sort(filters, AnnotationAwareOrderComparator.INSTANCE);

    return WebHttpHandlerBuilder.webHandler(dispatcherHandler)
            .exceptionHandlers(new ResponseStatusExceptionHandler()).filters(filters).build();
}

From source file:org.springframework.cloud.gateway.test.sse.SseIntegrationTests.java

private HttpHandler createHttpHandler() {
    this.wac = new AnnotationConfigApplicationContext();
    this.wac.register(TestConfiguration.class);
    this.wac.refresh();

    return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(this.wac)).build();
}

From source file:org.springframework.web.reactive.function.server.RouterFunctions.java

/**
 * Convert the given {@linkplain RouterFunction router function} into a {@link HttpHandler},
 * using the given strategies.//  w w w .  ja  va  2  s . c om
 * <p>The returned {@code HttpHandler} can be adapted to run in
 * <ul>
 * <li>Servlet 3.1+ using the
 * {@link org.springframework.http.server.reactive.ServletHttpHandlerAdapter},</li>
 * <li>Reactor using the
 * {@link org.springframework.http.server.reactive.ReactorHttpHandlerAdapter},</li>
 * <li>Undertow using the
 * {@link org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.</li>
 * </ul>
 * @param routerFunction the router function to convert
 * @param strategies the strategies to use
 * @return an http handler that handles HTTP request using the given router function
 */
public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) {
    Assert.notNull(routerFunction, "RouterFunction must not be null");
    Assert.notNull(strategies, "HandlerStrategies must not be null");

    WebHandler webHandler = toWebHandler(routerFunction, strategies);
    return WebHttpHandlerBuilder.webHandler(webHandler)
            .filters(filters -> filters.addAll(strategies.webFilters()))
            .exceptionHandlers(handlers -> handlers.addAll(strategies.exceptionHandlers()))
            .localeContextResolver(strategies.localeContextResolver()).build();
}

From source file:org.springframework.web.server.handler.FilteringWebHandlerTests.java

@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);//from w  ww  .  j  av a 2s  . c o  m
    assertEquals("boo", exceptionHandler.ex.getMessage());
}