@Override protected Resource resolveResourceInternal(@Nullable HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) { Resource resolved = chain.resolveResource(request, requestPath, locations); if (resolved == null) { String webJarResourcePath = findWebJarResourcePath(requestPath); if (webJarResourcePath != null) { return chain.resolveResource(request, webJarResourcePath, locations); } } return resolved; }
@Override protected Resource resolveResourceInternal(@Nullable HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) { Resource resource = chain.resolveResource(request, requestPath, locations); if (resource == null || (request != null && !isGzipAccepted(request))) { return resource; } try { Resource gzipped = new GzippedResource(resource); if (gzipped.exists()) { return gzipped; } } catch (IOException ex) { logger.trace("No gzip resource for [" + resource.getFilename() + "]", ex); } return resource; }
@Override protected Resource resolveResourceInternal(@Nullable HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) { String key = computeKey(request, requestPath); Resource resource = this.cache.get(key, Resource.class); if (resource != null) { if (logger.isTraceEnabled()) { logger.trace("Resource resolved from cache"); } return resource; } resource = chain.resolveResource(request, requestPath, locations); if (resource != null) { this.cache.put(key, resource); } return resource; }
@Test public void resolveResourceInternalNoMatch() { assertNull(this.chain.resolveResource(null, "invalid.css", this.locations)); }
@Test public void resolveResourceInternal() { Resource expected = new ClassPathResource("test/bar.css", getClass()); Resource actual = this.chain.resolveResource(null, "bar.css", this.locations); assertNotSame(expected, actual); assertEquals(expected, actual); }
private LineOutput processLine(LineInfo info, HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) { if (!info.isLink()) { return new LineOutput(info.getLine(), null); } Resource appCacheResource = transformerChain.getResolverChain() .resolveResource(null, info.getLine(), Collections.singletonList(resource)); String path = info.getLine(); String absolutePath = toAbsolutePath(path, request); String newPath = resolveUrlPath(absolutePath, request, resource, transformerChain); return new LineOutput((newPath != null ? newPath : path), appCacheResource); }
@Test public void resolveResourceExisting() { Resource expected = mock(Resource.class); this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "foo/2.3/foo.txt"; given(this.chain.resolveResource(this.request, file, this.locations)).willReturn(expected); Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain); assertEquals(expected, actual); verify(this.chain, times(1)).resolveResource(this.request, file, this.locations); }
@Test public void resolveResourceWebJar() { Resource expected = mock(Resource.class); String file = "underscorejs/underscore.js"; String expectedPath = "underscorejs/1.8.3/underscore.js"; this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); given(this.chain.resolveResource(this.request, expectedPath, this.locations)).willReturn(expected); Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain); assertEquals(expected, actual); verify(this.chain, times(1)).resolveResource(this.request, file, this.locations); }
@Test public void resolveResourceInternalFromCache() { Resource expected = Mockito.mock(Resource.class); this.cache.put(resourceKey("bar.css"), expected); Resource actual = this.chain.resolveResource(null, "bar.css", this.locations); assertSame(expected, actual); }
@Test public void resolveResourceNoVersionStrategy() throws Exception { String file = "missing.css"; given(this.chain.resolveResource(null, file, this.locations)).willReturn(null); this.resolver.setStrategyMap(Collections.emptyMap()); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveResource(null, file, this.locations); }
@Test public void resolveResourceExisting() throws Exception { String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); assertEquals(expected, actual); verify(this.chain, times(1)).resolveResource(null, file, this.locations); verify(this.versionStrategy, never()).extractVersion(file); }
@Test // SPR-13149 public void resolveWithNullRequest() { String file = "js/foo.js"; Resource resolved = this.resolver.resolveResource(null, file, this.locations); assertEquals(getResource(file).getDescription(), resolved.getDescription()); assertEquals(getResource(file).getFilename(), resolved.getFilename()); }
@Test public void resolveResourceMatchingEncoding() { Resource resource = Mockito.mock(Resource.class); Resource gzipped = Mockito.mock(Resource.class); this.cache.put(resourceKey("bar.css"), resource); this.cache.put(resourceKey("bar.css+encoding=gzip"), gzipped); MockHttpServletRequest request = new MockHttpServletRequest("GET", "bar.css"); assertSame(resource, this.chain.resolveResource(request,"bar.css", this.locations)); request = new MockHttpServletRequest("GET", "bar.css"); request.addHeader("Accept-Encoding", "gzip"); assertSame(gzipped, this.chain.resolveResource(request, "bar.css", this.locations)); }
@Test public void resolveResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null); Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveResource(this.request, file, this.locations); verify(this.chain, never()).resolveResource(this.request, null, this.locations); }
@Test public void resolveResourceNoVersionInPath() throws Exception { String file = "bar.css"; given(this.chain.resolveResource(null, file, this.locations)).willReturn(null); given(this.versionStrategy.extractVersion(file)).willReturn(""); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveResource(null, file, this.locations); verify(this.versionStrategy, times(1)).extractVersion(file); }
@Test public void resolveGzippedWithVersion() { String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css"; MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept-Encoding", "gzip"); Resource resolved = this.resolver.resolveResource(request, file, this.locations); assertEquals(getResource("foo.css.gz").getDescription(), resolved.getDescription()); assertEquals(getResource("foo.css").getFilename(), resolved.getFilename()); assertTrue(resolved instanceof HttpResource); }
@Test public void resolveResourceNoAcceptEncoding() { String file = "bar.css"; MockHttpServletRequest request = new MockHttpServletRequest("GET", file); Resource expected = this.chain.resolveResource(request, file, this.locations); String cacheKey = resourceKey(file); Object actual = this.cache.get(cacheKey).get(); assertEquals(expected, actual); }
@Test public void resolveResourceVersionDoesNotMatch() throws Exception { String versionFile = "bar-version.css"; String version = "version"; String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); given(this.chain.resolveResource(null, versionFile, this.locations)).willReturn(null); given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected); given(this.versionStrategy.extractVersion(versionFile)).willReturn(version); given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file); given(this.versionStrategy.getResourceVersion(expected)).willReturn("newer-version"); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, versionFile, this.locations, this.chain); assertNull(actual); verify(this.versionStrategy, times(1)).getResourceVersion(expected); }
@Test public void resolveResourceNoResourceAfterVersionRemoved() throws Exception { String versionFile = "bar-version.css"; String version = "version"; String file = "bar.css"; given(this.chain.resolveResource(null, versionFile, this.locations)).willReturn(null); given(this.chain.resolveResource(null, file, this.locations)).willReturn(null); given(this.versionStrategy.extractVersion(versionFile)).willReturn(version); given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, versionFile, this.locations, this.chain); assertNull(actual); verify(this.versionStrategy, times(1)).removeVersion(versionFile, version); }
@Test public void resolveGzipped() { String file = "js/foo.js"; MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept-Encoding", "gzip"); Resource actual = this.resolver.resolveResource(request, file, this.locations); assertEquals(getResource(file + ".gz").getDescription(), actual.getDescription()); assertEquals(getResource(file).getFilename(), actual.getFilename()); assertTrue(actual instanceof HttpResource); HttpHeaders headers = ((HttpResource) actual).getResponseHeaders(); assertEquals("gzip", headers.getFirst(HttpHeaders.CONTENT_ENCODING)); assertEquals("Accept-Encoding", headers.getFirst(HttpHeaders.VARY)); }