/** * Find the resource under the given location. * <p>The default implementation checks if there is a readable * {@code Resource} for the given path relative to the location. * @param resourcePath the path to the resource * @param location the location to check * @return the resource, or {@code null} if none found */ @Nullable protected Resource getResource(String resourcePath, Resource location) throws IOException { Resource resource = location.createRelative(resourcePath); if (resource.isReadable()) { if (checkResource(resource, location)) { return resource; } else if (logger.isWarnEnabled()) { Resource[] allowedLocations = getAllowedLocations(); logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " + "but resource \"" + resource.getURL() + "\" is neither under the " + "current location \"" + location.getURL() + "\" nor under any of the " + "allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]")); } } return null; }
/** * Find the resource under the given location. * <p>The default implementation checks if there is a readable * {@code Resource} for the given path relative to the location. * @param resourcePath the path to the resource * @param location the location to check * @return the resource, or {@code null} if none found */ @Nullable protected Resource getResource(String resourcePath, Resource location) throws IOException { Resource resource = location.createRelative(resourcePath); if (resource.isReadable()) { if (checkResource(resource, location)) { return resource; } else if (logger.isWarnEnabled()) { Resource[] allowedLocations = getAllowedLocations(); logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " + "but resource \"" + resource.getURL() + "\" is neither under the " + "current location \"" + location.getURL() + "\" nor under any of the " + "allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]")); } } return null; }
@Test public void checkServletContextResource() throws Exception { Resource classpathLocation = new ClassPathResource("test/", PathResourceResolver.class); MockServletContext context = new MockServletContext(); ServletContextResource servletContextLocation = new ServletContextResource(context, "/webjars/"); ServletContextResource resource = new ServletContextResource(context, "/webjars/webjar-foo/1.0/foo.js"); assertFalse(this.resolver.checkResource(resource, classpathLocation)); assertTrue(this.resolver.checkResource(resource, servletContextLocation)); }
@Test public void checkFileLocation() throws Exception { Resource resource = getResource("main.css"); assertTrue(this.resolver.checkResource(resource, resource)); }
/** * Find the resource under the given location. * <p>The default implementation checks if there is a readable * {@code Resource} for the given path relative to the location. * @param resourcePath the path to the resource * @param location the location to check * @return the resource, or {@code null} if none found */ protected Resource getResource(String resourcePath, Resource location) throws IOException { Resource resource = location.createRelative(resourcePath); if (resource.exists() && resource.isReadable()) { if (checkResource(resource, location)) { return resource; } else if (logger.isTraceEnabled()) { Resource[] allowedLocations = getAllowedLocations(); logger.trace("Resource path \"" + resourcePath + "\" was successfully resolved " + "but resource \"" + resource.getURL() + "\" is neither under the " + "current location \"" + location.getURL() + "\" nor under any of the " + "allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]")); } } return null; }