1 16 17 package org.springframework.web.context.support; 18 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.Set ; 22 23 import javax.servlet.ServletContext ; 24 25 import org.springframework.core.CollectionFactory; 26 import org.springframework.core.io.Resource; 27 import org.springframework.core.io.ResourceLoader; 28 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 29 import org.springframework.util.StringUtils; 30 31 46 public class ServletContextResourcePatternResolver extends PathMatchingResourcePatternResolver { 47 48 53 public ServletContextResourcePatternResolver(ServletContext servletContext) { 54 super(new ServletContextResourceLoader(servletContext)); 55 } 56 57 62 public ServletContextResourcePatternResolver(ResourceLoader resourceLoader) { 63 super(resourceLoader); 64 } 65 66 75 protected Set doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { 76 if (rootDirResource instanceof ServletContextResource) { 77 ServletContextResource scResource = (ServletContextResource) rootDirResource; 78 ServletContext sc = scResource.getServletContext(); 79 if (sc.getMajorVersion() > 2 || (sc.getMajorVersion() == 2 && sc.getMinorVersion() > 2)) { 80 String fullPattern = scResource.getPath() + subPattern; 83 Set result = CollectionFactory.createLinkedSetIfPossible(8); 84 doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result); 85 return result; 86 } 87 } 88 return super.doFindPathMatchingFileResources(rootDirResource, subPattern); 89 } 90 91 103 protected void doRetrieveMatchingServletContextResources( 104 ServletContext servletContext, String fullPattern, String dir, Set result) throws IOException { 105 106 Set candidates = servletContext.getResourcePaths(dir); 107 if (candidates != null) { 108 boolean dirDepthNotFixed = (fullPattern.indexOf("**") != -1); 109 for (Iterator it = candidates.iterator(); it.hasNext();) { 110 String currPath = (String ) it.next(); 111 if (!currPath.startsWith(dir)) { 112 int dirIndex = currPath.indexOf(dir); 115 if (dirIndex != -1) { 116 currPath = currPath.substring(dirIndex); 117 } 118 } 119 if (currPath.endsWith("/") && 120 (dirDepthNotFixed || 121 StringUtils.countOccurrencesOf(currPath, "/") < StringUtils.countOccurrencesOf(fullPattern, "/"))) { 122 doRetrieveMatchingServletContextResources(servletContext, fullPattern, currPath, result); 125 } 126 if (getPathMatcher().match(fullPattern, currPath)) { 127 result.add(new ServletContextResource(servletContext, currPath)); 128 } 129 } 130 } 131 } 132 133 } 134 | Popular Tags |