/** * A transformer can use this method when a resource being transformed * contains links to other resources. Such links need to be replaced with the * public facing link as determined by the resource resolver chain (e.g. the * public URL may have a version inserted). * @param resourcePath the path to a resource that needs to be re-written * @param request the current request * @param resource the resource being transformed * @param transformerChain the transformer chain * @return the resolved URL, or {@code} if not resolvable */ @Nullable protected String resolveUrlPath(String resourcePath, HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) { if (resourcePath.startsWith("/")) { // full resource path ResourceUrlProvider urlProvider = findResourceUrlProvider(request); return (urlProvider != null ? urlProvider.getForRequestUrl(request, resourcePath) : null); } else { // try resolving as relative path return transformerChain.getResolverChain().resolveUrlPath( resourcePath, Collections.singletonList(resource)); } }
/** * A transformer can use this method when a resource being transformed * contains links to other resources. Such links need to be replaced with the * public facing link as determined by the resource resolver chain (e.g. the * public URL may have a version inserted). * @param resourcePath the path to a resource that needs to be re-written * @param request the current request * @param resource the resource being transformed * @param transformerChain the transformer chain * @return the resolved URL, or {@code} if not resolvable */ @Nullable protected String resolveUrlPath(String resourcePath, HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) { if (resourcePath.startsWith("/")) { // full resource path ResourceUrlProvider urlProvider = findResourceUrlProvider(request); return (urlProvider != null ? urlProvider.getForRequestUrl(request, resourcePath) : null); } else { // try resolving as relative path return transformerChain.getResolverChain().resolveUrlPath( resourcePath, Collections.singletonList(resource)); } }
@Test // SPR-13374 public void getStaticResourceUrlRequestWithQueryOrHash() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/"); request.setRequestURI("/"); String url = "/resources/foo.css?foo=bar&url=http://example.org"; String resolvedUrl = this.urlProvider.getForRequestUrl(request, url); assertEquals("/resources/foo.css?foo=bar&url=http://example.org", resolvedUrl); url = "/resources/foo.css#hash"; resolvedUrl = this.urlProvider.getForRequestUrl(request, url); assertEquals("/resources/foo.css#hash", resolvedUrl); }
@Test // SPR-16526 public void getStaticResourceWithMissingContextPath() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/contextpath-longer-than-request-path"); request.setRequestURI("/contextpath-longer-than-request-path/style.css"); String url = "/resources/foo.css"; String resolvedUrl = this.urlProvider.getForRequestUrl(request, url); assertNull(resolvedUrl); }
/** * A transformer can use this method when a resource being transformed * contains links to other resources. Such links need to be replaced with the * public facing link as determined by the resource resolver chain (e.g. the * public URL may have a version inserted). * @param resourcePath the path to a resource that needs to be re-written * @param request the current request * @param resource the resource being transformed * @param transformerChain the transformer chain * @return the resolved URL, or {@code} if not resolvable */ protected String resolveUrlPath(String resourcePath, HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) { if (resourcePath.startsWith("/")) { // full resource path ResourceUrlProvider urlProvider = findResourceUrlProvider(request); return (urlProvider != null ? urlProvider.getForRequestUrl(request, resourcePath) : null); } else { // try resolving as relative path return transformerChain.getResolverChain().resolveUrlPath( resourcePath, Collections.singletonList(resource)); } }