Search in sources :

Example 1 with SimpleUrlHandlerMapping

use of org.springframework.web.reactive.handler.SimpleUrlHandlerMapping in project spring-boot by spring-projects.

the class WebFluxAnnotationAutoConfigurationTests method shouldRegisterResourceHandlerMapping.

@Test
public void shouldRegisterResourceHandlerMapping() throws Exception {
    load();
    SimpleUrlHandlerMapping hm = this.context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
    assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
    ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
    assertThat(staticHandler.getLocations()).hasSize(5);
    assertThat(hm.getUrlMap().get("/webjars/**")).isInstanceOf(ResourceWebHandler.class);
    ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/webjars/**");
    assertThat(webjarsHandler.getLocations()).hasSize(1);
    assertThat(webjarsHandler.getLocations().get(0)).isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
}
Also used : ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 2 with SimpleUrlHandlerMapping

use of org.springframework.web.reactive.handler.SimpleUrlHandlerMapping in project spring-boot by spring-projects.

the class WebFluxAnnotationAutoConfigurationTests method shouldMapResourcesToCustomPath.

@Test
public void shouldMapResourcesToCustomPath() throws Exception {
    load("spring.webflux.static-path-pattern:/static/**");
    SimpleUrlHandlerMapping hm = this.context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
    assertThat(hm.getUrlMap().get("/static/**")).isInstanceOf(ResourceWebHandler.class);
    ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/static/**");
    assertThat(staticHandler.getLocations()).hasSize(5);
}
Also used : ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 3 with SimpleUrlHandlerMapping

use of org.springframework.web.reactive.handler.SimpleUrlHandlerMapping in project spring-boot by spring-projects.

the class WebFluxAnnotationAutoConfigurationTests method resourceHandlerChainEnabled.

@Test
public void resourceHandlerChainEnabled() throws Exception {
    load("spring.resources.chain.enabled:true");
    SimpleUrlHandlerMapping hm = this.context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
    assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
    ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
    assertThat(staticHandler.getResourceResolvers()).extractingResultOf("getClass").containsOnly(CachingResourceResolver.class, PathResourceResolver.class);
    assertThat(staticHandler.getResourceTransformers()).extractingResultOf("getClass").containsOnly(CachingResourceTransformer.class);
}
Also used : ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 4 with SimpleUrlHandlerMapping

use of org.springframework.web.reactive.handler.SimpleUrlHandlerMapping in project spring-framework by spring-projects.

the class ResourceUrlProvider method detectResourceHandlers.

protected void detectResourceHandlers(ApplicationContext appContext) {
    logger.debug("Looking for resource handler mappings");
    Map<String, SimpleUrlHandlerMapping> map = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
    List<SimpleUrlHandlerMapping> handlerMappings = new ArrayList<>(map.values());
    AnnotationAwareOrderComparator.sort(handlerMappings);
    for (SimpleUrlHandlerMapping hm : handlerMappings) {
        for (String pattern : hm.getHandlerMap().keySet()) {
            Object handler = hm.getHandlerMap().get(pattern);
            if (handler instanceof ResourceWebHandler) {
                ResourceWebHandler resourceHandler = (ResourceWebHandler) handler;
                if (logger.isDebugEnabled()) {
                    logger.debug("Found resource handler mapping: URL pattern=\"" + pattern + "\", " + "locations=" + resourceHandler.getLocations() + ", " + "resolvers=" + resourceHandler.getResourceResolvers());
                }
                this.handlerMap.put(pattern, resourceHandler);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping)

Example 5 with SimpleUrlHandlerMapping

use of org.springframework.web.reactive.handler.SimpleUrlHandlerMapping 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

SimpleUrlHandlerMapping (org.springframework.web.reactive.handler.SimpleUrlHandlerMapping)6 Test (org.junit.Test)4 ResourceWebHandler (org.springframework.web.reactive.resource.ResourceWebHandler)4 WebHandler (org.springframework.web.server.WebHandler)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 ApplicationContext (org.springframework.context.ApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 AbstractHandlerMapping (org.springframework.web.reactive.handler.AbstractHandlerMapping)1