Search in sources :

Example 6 with ResourceWebHandler

use of org.springframework.web.reactive.resource.ResourceWebHandler in project spring-framework by spring-projects.

the class ResourceHandlerRegistryTests method resourceChainWithVersionResolver.

@Test
public void resourceChainWithVersionResolver() throws Exception {
    VersionResourceResolver versionResolver = new VersionResourceResolver().addFixedVersionStrategy("fixed", "/**/*.js").addContentVersionStrategy("/**");
    this.registration.resourceChain(true).addResolver(versionResolver).addTransformer(new AppCacheManifestTransformer());
    ResourceWebHandler handler = getHandler("/resources/**");
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
    assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
    assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
    assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
    assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers, Matchers.hasSize(3));
    assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
    assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
    assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
}
Also used : CachingResourceResolver(org.springframework.web.reactive.resource.CachingResourceResolver) CssLinkResourceTransformer(org.springframework.web.reactive.resource.CssLinkResourceTransformer) PathResourceResolver(org.springframework.web.reactive.resource.PathResourceResolver) AppCacheManifestTransformer(org.springframework.web.reactive.resource.AppCacheManifestTransformer) WebJarsResourceResolver(org.springframework.web.reactive.resource.WebJarsResourceResolver) VersionResourceResolver(org.springframework.web.reactive.resource.VersionResourceResolver) CachingResourceResolver(org.springframework.web.reactive.resource.CachingResourceResolver) VersionResourceResolver(org.springframework.web.reactive.resource.VersionResourceResolver) PathResourceResolver(org.springframework.web.reactive.resource.PathResourceResolver) ResourceResolver(org.springframework.web.reactive.resource.ResourceResolver) WebJarsResourceResolver(org.springframework.web.reactive.resource.WebJarsResourceResolver) CachingResourceTransformer(org.springframework.web.reactive.resource.CachingResourceTransformer) ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) CachingResourceTransformer(org.springframework.web.reactive.resource.CachingResourceTransformer) CssLinkResourceTransformer(org.springframework.web.reactive.resource.CssLinkResourceTransformer) ResourceTransformer(org.springframework.web.reactive.resource.ResourceTransformer) Test(org.junit.Test)

Example 7 with ResourceWebHandler

use of org.springframework.web.reactive.resource.ResourceWebHandler in project spring-framework by spring-projects.

the class ResourceHandlerRegistration method getRequestHandler.

/**
	 * Returns a {@link ResourceWebHandler} instance.
	 */
protected ResourceWebHandler getRequestHandler() {
    ResourceWebHandler handler = new ResourceWebHandler();
    if (this.resourceChainRegistration != null) {
        handler.setResourceResolvers(this.resourceChainRegistration.getResourceResolvers());
        handler.setResourceTransformers(this.resourceChainRegistration.getResourceTransformers());
    }
    handler.setLocations(this.locations);
    if (this.cacheControl != null) {
        handler.setCacheControl(this.cacheControl);
    }
    return handler;
}
Also used : ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler)

Example 8 with ResourceWebHandler

use of org.springframework.web.reactive.resource.ResourceWebHandler 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 9 with ResourceWebHandler

use of org.springframework.web.reactive.resource.ResourceWebHandler in project spring-framework by spring-projects.

the class ResourceHandlerRegistryTests method resourceChainWithOverrides.

@Test
public void resourceChainWithOverrides() throws Exception {
    CachingResourceResolver cachingResolver = Mockito.mock(CachingResourceResolver.class);
    VersionResourceResolver versionResolver = Mockito.mock(VersionResourceResolver.class);
    WebJarsResourceResolver webjarsResolver = Mockito.mock(WebJarsResourceResolver.class);
    PathResourceResolver pathResourceResolver = new PathResourceResolver();
    CachingResourceTransformer cachingTransformer = Mockito.mock(CachingResourceTransformer.class);
    AppCacheManifestTransformer appCacheTransformer = Mockito.mock(AppCacheManifestTransformer.class);
    CssLinkResourceTransformer cssLinkTransformer = new CssLinkResourceTransformer();
    this.registration.setCacheControl(CacheControl.maxAge(3600, TimeUnit.MILLISECONDS)).resourceChain(false).addResolver(cachingResolver).addResolver(versionResolver).addResolver(webjarsResolver).addResolver(pathResourceResolver).addTransformer(cachingTransformer).addTransformer(appCacheTransformer).addTransformer(cssLinkTransformer);
    ResourceWebHandler handler = getHandler("/resources/**");
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
    assertThat(resolvers.get(0), Matchers.sameInstance(cachingResolver));
    assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
    assertThat(resolvers.get(2), Matchers.sameInstance(webjarsResolver));
    assertThat(resolvers.get(3), Matchers.sameInstance(pathResourceResolver));
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers, Matchers.hasSize(3));
    assertThat(transformers.get(0), Matchers.sameInstance(cachingTransformer));
    assertThat(transformers.get(1), Matchers.sameInstance(appCacheTransformer));
    assertThat(transformers.get(2), Matchers.sameInstance(cssLinkTransformer));
}
Also used : CachingResourceResolver(org.springframework.web.reactive.resource.CachingResourceResolver) CssLinkResourceTransformer(org.springframework.web.reactive.resource.CssLinkResourceTransformer) PathResourceResolver(org.springframework.web.reactive.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.reactive.resource.WebJarsResourceResolver) AppCacheManifestTransformer(org.springframework.web.reactive.resource.AppCacheManifestTransformer) VersionResourceResolver(org.springframework.web.reactive.resource.VersionResourceResolver) CachingResourceResolver(org.springframework.web.reactive.resource.CachingResourceResolver) VersionResourceResolver(org.springframework.web.reactive.resource.VersionResourceResolver) PathResourceResolver(org.springframework.web.reactive.resource.PathResourceResolver) ResourceResolver(org.springframework.web.reactive.resource.ResourceResolver) WebJarsResourceResolver(org.springframework.web.reactive.resource.WebJarsResourceResolver) CachingResourceTransformer(org.springframework.web.reactive.resource.CachingResourceTransformer) ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) CachingResourceTransformer(org.springframework.web.reactive.resource.CachingResourceTransformer) CssLinkResourceTransformer(org.springframework.web.reactive.resource.CssLinkResourceTransformer) ResourceTransformer(org.springframework.web.reactive.resource.ResourceTransformer) Test(org.junit.Test)

Example 10 with ResourceWebHandler

use of org.springframework.web.reactive.resource.ResourceWebHandler in project spring-framework by spring-projects.

the class ResourceHandlerRegistryTests method mapPathToLocation.

@Test
public void mapPathToLocation() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    exchange.getAttributes().put(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/testStylesheet.css");
    ResourceWebHandler handler = getHandler("/resources/**");
    handler.handle(exchange).block(Duration.ofSeconds(5));
    StepVerifier.create(exchange.getResponse().getBody()).consumeNextWith(buf -> assertEquals("test stylesheet content", DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8))).expectComplete().verify();
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ResourceWebHandler(org.springframework.web.reactive.resource.ResourceWebHandler) Test(org.junit.Test)

Aggregations

ResourceWebHandler (org.springframework.web.reactive.resource.ResourceWebHandler)10 Test (org.junit.Test)8 SimpleUrlHandlerMapping (org.springframework.web.reactive.handler.SimpleUrlHandlerMapping)4 CachingResourceResolver (org.springframework.web.reactive.resource.CachingResourceResolver)4 CachingResourceTransformer (org.springframework.web.reactive.resource.CachingResourceTransformer)4 CssLinkResourceTransformer (org.springframework.web.reactive.resource.CssLinkResourceTransformer)4 PathResourceResolver (org.springframework.web.reactive.resource.PathResourceResolver)4 ResourceResolver (org.springframework.web.reactive.resource.ResourceResolver)4 ResourceTransformer (org.springframework.web.reactive.resource.ResourceTransformer)4 VersionResourceResolver (org.springframework.web.reactive.resource.VersionResourceResolver)4 WebJarsResourceResolver (org.springframework.web.reactive.resource.WebJarsResourceResolver)4 AppCacheManifestTransformer (org.springframework.web.reactive.resource.AppCacheManifestTransformer)2 LinkedHashMap (java.util.LinkedHashMap)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 ConcurrentMapCache (org.springframework.cache.concurrent.ConcurrentMapCache)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)1 WebHandler (org.springframework.web.server.WebHandler)1