Search in sources :

Example 1 with WebHandler

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

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

the class WebHttpHandlerBuilder method build.

/**
	 * Build the {@link HttpHandler}.
	 */
public HttpHandler build() {
    WebHandler decorated;
    decorated = new FilteringWebHandler(this.webHandler, this.filters);
    decorated = new ExceptionHandlingWebHandler(decorated, this.exceptionHandlers);
    HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated);
    if (this.sessionManager != null) {
        adapted.setSessionManager(this.sessionManager);
    }
    return adapted;
}
Also used : ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebHandler(org.springframework.web.server.WebHandler) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler)

Example 3 with WebHandler

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

the class SimpleHandlerAdapter method handle.

@Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
    WebHandler webHandler = (WebHandler) handler;
    Mono<Void> mono = webHandler.handle(exchange);
    return mono.then(aVoid -> Mono.empty());
}
Also used : WebHandler(org.springframework.web.server.WebHandler)

Example 4 with WebHandler

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

the class AbstractDispatcherHandlerInitializer method registerDispatcherHandler.

/**
	 * Register a {@link DispatcherHandler} against the given servlet context.
	 * <p>This method will create a {@link DispatcherHandler}, initializing it with the application
	 * context returned from {@link #createApplicationContext()}. The created handler will be
	 * wrapped in a {@link ServletHttpHandlerAdapter} servlet with the name
     * returned by {@link #getServletName()}, mapping it to the pattern
	 * returned from {@link #getServletMapping()}.
	 * <p>Further customization can be achieved by overriding {@link
	 * #customizeRegistration(ServletRegistration.Dynamic)} or
	 * {@link #createDispatcherHandler(ApplicationContext)}.
	 * @param servletContext the context to register the servlet against
	 */
protected void registerDispatcherHandler(ServletContext servletContext) {
    String servletName = getServletName();
    Assert.hasLength(servletName, "getServletName() must not return empty or null");
    ApplicationContext applicationContext = createApplicationContext();
    Assert.notNull(applicationContext, "createApplicationContext() did not return an application " + "context for servlet [" + servletName + "]");
    refreshApplicationContext(applicationContext);
    registerCloseListener(servletContext, applicationContext);
    WebHandler dispatcherHandler = createDispatcherHandler(applicationContext);
    Assert.notNull(dispatcherHandler, "createDispatcherHandler() did not return a WebHandler for servlet [" + servletName + "]");
    ServletHttpHandlerAdapter handlerAdapter = createHandlerAdapter(dispatcherHandler);
    Assert.notNull(handlerAdapter, "createHttpHandler() did not return a ServletHttpHandlerAdapter for servlet [" + servletName + "]");
    ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, handlerAdapter);
    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(getServletMapping());
    registration.setAsyncSupported(true);
    customizeRegistration(registration);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ServletRegistration(javax.servlet.ServletRegistration) ServletHttpHandlerAdapter(org.springframework.http.server.reactive.ServletHttpHandlerAdapter) WebHandler(org.springframework.web.server.WebHandler)

Example 5 with WebHandler

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

the class WebFluxConfigurationSupportTests method resourceHandler.

@Test
public void resourceHandler() throws Exception {
    ApplicationContext context = loadConfig(CustomResourceHandlingConfig.class);
    String name = "resourceHandlerMapping";
    AbstractHandlerMapping handlerMapping = context.getBean(name, AbstractHandlerMapping.class);
    assertNotNull(handlerMapping);
    assertEquals(Ordered.LOWEST_PRECEDENCE - 1, handlerMapping.getOrder());
    assertNotNull(handlerMapping.getPathHelper());
    assertNotNull(handlerMapping.getPathMatcher());
    SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping;
    WebHandler webHandler = (WebHandler) urlHandlerMapping.getUrlMap().get("/images/**");
    assertNotNull(webHandler);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) WebHandler(org.springframework.web.server.WebHandler) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) AbstractHandlerMapping(org.springframework.web.reactive.handler.AbstractHandlerMapping) 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