protected List<ResourceResolver> getResourceResolvers() { if (!this.hasPathResolver) { List<ResourceResolver> result = new ArrayList<>(this.resolvers); if (isWebJarsAssetLocatorPresent && !this.hasWebjarsResolver) { result.add(new WebJarsResourceResolver()); } result.add(new PathResourceResolver()); return result; } return this.resolvers; }
@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; }
@Test public void resolveUrlExisting() { this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "/foo/2.3/foo.txt"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(file); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain); assertEquals(file, actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); }
@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 resolveUrlExistingNotInJarFile() { this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "foo/foo.txt"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, never()).resolveUrlPath("foo/2.3/foo.txt", 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); }
protected List<ResourceResolver> getResourceResolvers() { if (!this.hasPathResolver) { List<ResourceResolver> result = new ArrayList<>(this.resolvers); if (isWebJarsAssetLocatorPresent && !this.hasWebjarsResolver) { result.add(new WebJarsResourceResolver()); } result.add(new PathResourceResolver()); return result; } return this.resolvers; }
@Override protected String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) { String path = chain.resolveUrlPath(resourceUrlPath, locations); if (path == null) { String webJarResourcePath = findWebJarResourcePath(resourceUrlPath); if (webJarResourcePath != null) { return chain.resolveUrlPath(webJarResourcePath, locations); } } return path; }
@Test public void resolveUrlWebJarResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, never()).resolveUrlPath(null, 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); }
@Before public void setup() { // for this to work, an actual WebJar must be on the test classpath this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars")); this.resolver = new WebJarsResourceResolver(); this.chain = mock(ResourceResolverChain.class); }
@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; }
@Test public void resolveUrlWebJarResource() { String file = "underscorejs/underscore.js"; String expected = "underscorejs/1.8.3/underscore.js"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null); given(this.chain.resolveUrlPath(expected, this.locations)).willReturn(expected); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain); assertEquals(expected, actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, times(1)).resolveUrlPath(expected, this.locations); }
protected List<ResourceResolver> getResourceResolvers() { if (!this.hasPathResolver) { List<ResourceResolver> result = new ArrayList<ResourceResolver>(this.resolvers); if (isWebJarsAssetLocatorPresent && !this.hasWebjarsResolver) { result.add(new WebJarsResourceResolver()); } result.add(new PathResourceResolver()); return result; } return this.resolvers; }
@Override protected String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) { String path = chain.resolveUrlPath(resourceUrlPath, locations); if (path == null) { String webJarResourcePath = findWebJarResourcePath(resourceUrlPath); if (webJarResourcePath != null) { return chain.resolveUrlPath(webJarResourcePath, locations); } } return path; }
@Override protected Resource resolveResourceInternal(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 String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) { String path = chain.resolveUrlPath(resourceUrlPath, locations); if (path == null) { String webJarResourcePath = findWebJarResourcePath(resourceUrlPath); if (webJarResourcePath != null) { return chain.resolveUrlPath(webJarResourcePath, locations); } } return path; }