/** * Overridden version which checks for ServletContextResource * and uses {@code ServletContext.getResourcePaths} to find * matching resources below the web application root directory. * In case of other resources, delegates to the superclass version. * @see #doRetrieveMatchingServletContextResources * @see ServletContextResource * @see javax.servlet.ServletContext#getResourcePaths */ @Override protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { if (rootDirResource instanceof ServletContextResource) { ServletContextResource scResource = (ServletContextResource) rootDirResource; ServletContext sc = scResource.getServletContext(); String fullPattern = scResource.getPath() + subPattern; Set<Resource> result = new LinkedHashSet<>(8); doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result); return result; } else { return super.doFindPathMatchingFileResources(rootDirResource, subPattern); } }
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else if (resource instanceof ServletContextResource) { resourcePath = ((ServletContextResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
/** * Overridden version which checks for ServletContextResource * and uses {@code ServletContext.getResourcePaths} to find * matching resources below the web application root directory. * In case of other resources, delegates to the superclass version. * @see #doRetrieveMatchingServletContextResources * @see ServletContextResource * @see javax.servlet.ServletContext#getResourcePaths */ @Override protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { if (rootDirResource instanceof ServletContextResource) { ServletContextResource scResource = (ServletContextResource) rootDirResource; ServletContext sc = scResource.getServletContext(); String fullPattern = scResource.getPath() + subPattern; Set<Resource> result = new LinkedHashSet<>(8); doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result); return result; } else { return super.doFindPathMatchingFileResources(rootDirResource, subPattern); } }
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else if (resource instanceof ServletContextResource) { resourcePath = ((ServletContextResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
@Test public void testServletContextResourcePatternResolverWithAbsolutePaths() throws IOException { final Set<String> paths = new HashSet<>(); paths.add("C:/webroot/WEB-INF/context1.xml"); paths.add("C:/webroot/WEB-INF/context2.xml"); paths.add("C:/webroot/someOtherDirThatDoesntContainPath"); MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") { @Override public Set<String> getResourcePaths(String path) { if ("/WEB-INF/".equals(path)) { return paths; } return null; } }; ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc); Resource[] found = rpr.getResources("/WEB-INF/*.xml"); Set<String> foundPaths = new HashSet<>(); for (Resource resource : found) { foundPaths.add(((ServletContextResource) resource).getPath()); } assertEquals(2, foundPaths.size()); assertTrue(foundPaths.contains("/WEB-INF/context1.xml")); assertTrue(foundPaths.contains("/WEB-INF/context2.xml")); }
@Test public void testServletContextResourcePatternResolver() throws IOException { final Set<String> paths = new HashSet<>(); paths.add("/WEB-INF/context1.xml"); paths.add("/WEB-INF/context2.xml"); MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") { @Override public Set<String> getResourcePaths(String path) { if ("/WEB-INF/".equals(path)) { return paths; } return null; } }; ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc); Resource[] found = rpr.getResources("/WEB-INF/*.xml"); Set<String> foundPaths = new HashSet<>(); for (Resource resource : found) { foundPaths.add(((ServletContextResource) resource).getPath()); } assertEquals(2, foundPaths.size()); assertTrue(foundPaths.contains("/WEB-INF/context1.xml")); assertTrue(foundPaths.contains("/WEB-INF/context2.xml")); }
@Test public void testServletContextResourcePatternResolverWithPatternPath() throws IOException { final Set<String> dirs = new HashSet<>(); dirs.add("/WEB-INF/mydir1/"); dirs.add("/WEB-INF/mydir2/"); MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") { @Override public Set<String> getResourcePaths(String path) { if ("/WEB-INF/".equals(path)) { return dirs; } if ("/WEB-INF/mydir1/".equals(path)) { return Collections.singleton("/WEB-INF/mydir1/context1.xml"); } if ("/WEB-INF/mydir2/".equals(path)) { return Collections.singleton("/WEB-INF/mydir2/context2.xml"); } return null; } }; ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc); Resource[] found = rpr.getResources("/WEB-INF/*/*.xml"); Set<String> foundPaths = new HashSet<>(); for (Resource resource : found) { foundPaths.add(((ServletContextResource) resource).getPath()); } assertEquals(2, foundPaths.size()); assertTrue(foundPaths.contains("/WEB-INF/mydir1/context1.xml")); assertTrue(foundPaths.contains("/WEB-INF/mydir2/context2.xml")); }
Set<String> foundPaths = new HashSet<>(); for (Resource resource : found) { foundPaths.add(((ServletContextResource) resource).getPath());
/** * Overridden version which checks for ServletContextResource * and uses {@code ServletContext.getResourcePaths} to find * matching resources below the web application root directory. * In case of other resources, delegates to the superclass version. * @see #doRetrieveMatchingServletContextResources * @see ServletContextResource * @see javax.servlet.ServletContext#getResourcePaths */ @Override protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { if (rootDirResource instanceof ServletContextResource) { ServletContextResource scResource = (ServletContextResource) rootDirResource; ServletContext sc = scResource.getServletContext(); String fullPattern = scResource.getPath() + subPattern; Set<Resource> result = new LinkedHashSet<>(8); doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result); return result; } else { return super.doFindPathMatchingFileResources(rootDirResource, subPattern); } }
/** * Overridden version which checks for ServletContextResource * and uses {@code ServletContext.getResourcePaths} to find * matching resources below the web application root directory. * In case of other resources, delegates to the superclass version. * @see #doRetrieveMatchingServletContextResources * @see ServletContextResource * @see javax.servlet.ServletContext#getResourcePaths */ @Override protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { if (rootDirResource instanceof ServletContextResource) { ServletContextResource scResource = (ServletContextResource) rootDirResource; ServletContext sc = scResource.getServletContext(); String fullPattern = scResource.getPath() + subPattern; Set<Resource> result = new LinkedHashSet<>(8); doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result); return result; } else { return super.doFindPathMatchingFileResources(rootDirResource, subPattern); } }
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else if (resource instanceof ServletContextResource) { resourcePath = ((ServletContextResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
/** * Overridden version which checks for ServletContextResource * and uses <code>ServletContext.getResourcePaths</code> to find * matching resources below the web application root directory. * In case of other resources, delegates to the superclass version. * @see #doRetrieveMatchingServletContextResources * @see ServletContextResource * @see javax.servlet.ServletContext#getResourcePaths */ protected Set doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { if (rootDirResource instanceof ServletContextResource) { ServletContextResource scResource = (ServletContextResource) rootDirResource; ServletContext sc = scResource.getServletContext(); if (sc.getMajorVersion() > 2 || (sc.getMajorVersion() == 2 && sc.getMinorVersion() > 2)) { // Only try the following on Servlet containers >= 2.3: // ServletContext.getResourcePaths() is not available before that version. String fullPattern = scResource.getPath() + subPattern; Set result = CollectionFactory.createLinkedSetIfPossible(8); doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result); return result; } } return super.doFindPathMatchingFileResources(rootDirResource, subPattern); }