Search in sources :

Example 6 with WebHandler

use of org.springframework.web.server.WebHandler in project spring-framework by spring-projects.

the class ResourceHandlerRegistry method getHandlerMapping.

/**
	 * Return a handler mapping with the mapped resource handlers; or {@code null} in case
	 * of no registrations.
	 */
protected AbstractHandlerMapping getHandlerMapping() {
    if (this.registrations.isEmpty()) {
        return null;
    }
    Map<String, WebHandler> urlMap = new LinkedHashMap<>();
    for (ResourceHandlerRegistration registration : this.registrations) {
        for (String pathPattern : registration.getPathPatterns()) {
            ResourceWebHandler handler = registration.getRequestHandler();
            handler.setContentTypeResolver(this.contentTypeResolver);
            try {
                handler.afterPropertiesSet();
                handler.afterSingletonsInstantiated();
            } catch (Exception ex) {
                throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
            }
            urlMap.put(pathPattern, handler);
        }
    }
    SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
    handlerMapping.setOrder(this.order);
    handlerMapping.setUrlMap(urlMap);
    return handlerMapping;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) WebHandler(org.springframework.web.server.WebHandler) ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with WebHandler

use of org.springframework.web.server.WebHandler in project spring-framework by spring-projects.

the class DispatcherHandlerErrorTests method webExceptionHandler.

@Test
public void webExceptionHandler() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/unknown-argument-type").toExchange();
    List<WebExceptionHandler> handlers = Collections.singletonList(new ServerError500ExceptionHandler());
    WebHandler webHandler = new ExceptionHandlingWebHandler(this.dispatcherHandler, handlers);
    webHandler.handle(exchange).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) WebHandler(org.springframework.web.server.WebHandler) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) Test(org.junit.Test)

Example 8 with WebHandler

use of org.springframework.web.server.WebHandler 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)

Aggregations

WebHandler (org.springframework.web.server.WebHandler)8 Test (org.junit.Test)4 FilteringWebHandler (org.springframework.web.server.handler.FilteringWebHandler)3 ApplicationContext (org.springframework.context.ApplicationContext)2 HttpHandler (org.springframework.http.server.reactive.HttpHandler)2 SimpleUrlHandlerMapping (org.springframework.web.reactive.handler.SimpleUrlHandlerMapping)2 WebFilter (org.springframework.web.server.WebFilter)2 ExceptionHandlingWebHandler (org.springframework.web.server.handler.ExceptionHandlingWebHandler)2 WebHandlerDecorator (org.springframework.web.server.handler.WebHandlerDecorator)2 LinkedHashMap (java.util.LinkedHashMap)1 ServletRegistration (javax.servlet.ServletRegistration)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 ServletHttpHandlerAdapter (org.springframework.http.server.reactive.ServletHttpHandlerAdapter)1 AbstractHandlerMapping (org.springframework.web.reactive.handler.AbstractHandlerMapping)1 ResourceWebHandler (org.springframework.web.reactive.resource.ResourceWebHandler)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 WebExceptionHandler (org.springframework.web.server.WebExceptionHandler)1