From 2baceac5ff03489628e058c76a35306519e6cbb2 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 24 Mar 2017 16:09:14 +0100 Subject: [PATCH] Resolve ResourceUrlProvider from current request This commit changes `ResourceTransformerSupport` to look for the `ResourceUrlProvider` in the current request if none is configured on the resource transformer itself. Issue: SPR-15369 --- .../resource/AppCacheManifestTransformer.java | 4 ++-- .../resource/CssLinkResourceTransformer.java | 4 ++-- .../resource/ResourceTransformerSupport.java | 8 ++++---- .../resource/ResourceTransformerSupport.java | 15 ++++----------- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/AppCacheManifestTransformer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/AppCacheManifestTransformer.java index ab4414ee861..ac3ccd3df8a 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/AppCacheManifestTransformer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/AppCacheManifestTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -144,7 +144,7 @@ public class AppCacheManifestTransformer extends ResourceTransformerSupport { return Mono.just(new LineOutput(info.getLine(), null)); } - String link = toAbsolutePath(info.getLine(), exchange.getRequest()); + String link = toAbsolutePath(info.getLine(), exchange); Mono pathMono = resolveUrlPath(link, exchange, resource, chain) .doOnNext(path -> { if (logger.isTraceEnabled()) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java index 287f57cc2c3..d7c7b2b6a4f 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -103,7 +103,7 @@ public class CssLinkResourceTransformer extends ResourceTransformerSupport { .concatMap(segment -> { String segmentContent = segment.getContent(fullContent); if (segment.isLink() && !hasScheme(segmentContent)) { - String link = toAbsolutePath(segmentContent, exchange.getRequest()); + String link = toAbsolutePath(segmentContent, exchange); return resolveUrlPath(link, exchange, newResource, transformerChain) .defaultIfEmpty(segmentContent); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceTransformerSupport.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceTransformerSupport.java index 5dc1e1b491a..287896f42df 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceTransformerSupport.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceTransformerSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -89,11 +89,11 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer * The resulting path is also cleaned from sequences like "path/..". * * @param path the relative path to transform - * @param request the referer request + * @param exchange the current exchange * @return the absolute request path for the given resource path */ - protected String toAbsolutePath(String path, ServerHttpRequest request) { - String requestPath = request.getURI().getPath(); + protected String toAbsolutePath(String path, ServerWebExchange exchange) { + String requestPath = exchange.getRequest().getURI().getPath(); String absolutePath = StringUtils.applyRelativePath(requestPath, path); return StringUtils.cleanPath(absolutePath); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceTransformerSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceTransformerSupport.java index ebf5254a7e0..de6b7758373 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceTransformerSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceTransformerSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,15 +38,8 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer /** * Configure a {@link ResourceUrlProvider} to use when resolving the public * URL of links in a transformed resource (e.g. import links in a CSS file). - * This is required only for links expressed as full paths, i.e. including - * context and servlet path, and not for relative links. - *

By default this property is not set. In that case if a - * {@code ResourceUrlProvider} is needed an attempt is made to find the - * {@code ResourceUrlProvider} exposed through the - * {@link org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor - * ResourceUrlProviderExposingInterceptor} (configured by default in the MVC - * Java config and XML namespace). Therefore explicitly configuring this - * property should not be needed in most cases. + * This is required only for links expressed as full paths and not for + * relative links. * @param resourceUrlProvider the URL provider to use */ public void setResourceUrlProvider(ResourceUrlProvider resourceUrlProvider) { @@ -96,7 +89,7 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer * @return the absolute request path for the given resource path */ protected String toAbsolutePath(String path, HttpServletRequest request) { - String requestPath = this.getResourceUrlProvider().getUrlPathHelper().getRequestUri(request); + String requestPath = this.findResourceUrlProvider(request).getUrlPathHelper().getRequestUri(request); String absolutePath = StringUtils.applyRelativePath(requestPath, path); return StringUtils.cleanPath(absolutePath); }