001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.context.support;
018:
019: import java.io.IOException;
020: import java.util.Iterator;
021: import java.util.Set;
022:
023: import javax.servlet.ServletContext;
024:
025: import org.springframework.core.CollectionFactory;
026: import org.springframework.core.io.Resource;
027: import org.springframework.core.io.ResourceLoader;
028: import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
029: import org.springframework.util.StringUtils;
030:
031: /**
032: * ServletContext-aware subclass of PathMatchingResourcePatternResolver,
033: * able to find matching resources below the web application root directory
034: * via Servlet 2.3's <code>ServletContext.getResourcePaths</code>.
035: * Falls back to the superclass' file system checking for other resources.
036: *
037: * <p>The advantage of using <code>ServletContext.getResourcePaths</code> to
038: * find matching files is that it will work in a WAR file which has not been
039: * expanded too. For Servlet containers that do not support Servlet 2.3 or
040: * above, this resolver will always fall back to file system checking,
041: * which requires an expanded WAR file.
042: *
043: * @author Juergen Hoeller
044: * @since 1.1.2
045: */
046: public class ServletContextResourcePatternResolver extends
047: PathMatchingResourcePatternResolver {
048:
049: /**
050: * Create a new ServletContextResourcePatternResolver.
051: * @param servletContext the ServletContext to load resources with
052: * @see ServletContextResourceLoader#ServletContextResourceLoader(javax.servlet.ServletContext)
053: */
054: public ServletContextResourcePatternResolver(
055: ServletContext servletContext) {
056: super (new ServletContextResourceLoader(servletContext));
057: }
058:
059: /**
060: * Create a new ServletContextResourcePatternResolver.
061: * @param resourceLoader the ResourceLoader to load root directories and
062: * actual resources with
063: */
064: public ServletContextResourcePatternResolver(
065: ResourceLoader resourceLoader) {
066: super (resourceLoader);
067: }
068:
069: /**
070: * Overridden version which checks for ServletContextResource
071: * and uses <code>ServletContext.getResourcePaths</code> to find
072: * matching resources below the web application root directory.
073: * In case of other resources, delegates to the superclass version.
074: * @see #doRetrieveMatchingServletContextResources
075: * @see ServletContextResource
076: * @see javax.servlet.ServletContext#getResourcePaths
077: */
078: protected Set doFindPathMatchingFileResources(
079: Resource rootDirResource, String subPattern)
080: throws IOException {
081: if (rootDirResource instanceof ServletContextResource) {
082: ServletContextResource scResource = (ServletContextResource) rootDirResource;
083: ServletContext sc = scResource.getServletContext();
084: if (sc.getMajorVersion() > 2
085: || (sc.getMajorVersion() == 2 && sc
086: .getMinorVersion() > 2)) {
087: // Only try the following on Servlet containers >= 2.3:
088: // ServletContext.getResourcePaths() is not available before that version.
089: String fullPattern = scResource.getPath() + subPattern;
090: Set result = CollectionFactory
091: .createLinkedSetIfPossible(8);
092: doRetrieveMatchingServletContextResources(sc,
093: fullPattern, scResource.getPath(), result);
094: return result;
095: }
096: }
097: return super .doFindPathMatchingFileResources(rootDirResource,
098: subPattern);
099: }
100:
101: /**
102: * Recursively retrieve ServletContextResources that match the given pattern,
103: * adding them to the given result set.
104: * @param servletContext the ServletContext to work on
105: * @param fullPattern the pattern to match against,
106: * with preprended root directory path
107: * @param dir the current directory
108: * @param result the Set of matching Resources to add to
109: * @throws IOException if directory contents could not be retrieved
110: * @see ServletContextResource
111: * @see javax.servlet.ServletContext#getResourcePaths
112: */
113: protected void doRetrieveMatchingServletContextResources(
114: ServletContext servletContext, String fullPattern,
115: String dir, Set result) throws IOException {
116:
117: Set candidates = servletContext.getResourcePaths(dir);
118: if (candidates != null) {
119: boolean dirDepthNotFixed = (fullPattern.indexOf("**") != -1);
120: for (Iterator it = candidates.iterator(); it.hasNext();) {
121: String currPath = (String) it.next();
122: if (!currPath.startsWith(dir)) {
123: // Returned resource path does not start with relative directory:
124: // assuming absolute path returned -> strip absolute path.
125: int dirIndex = currPath.indexOf(dir);
126: if (dirIndex != -1) {
127: currPath = currPath.substring(dirIndex);
128: }
129: }
130: if (currPath.endsWith("/")
131: && (dirDepthNotFixed || StringUtils
132: .countOccurrencesOf(currPath, "/") < StringUtils
133: .countOccurrencesOf(fullPattern, "/"))) {
134: // Search subdirectories recursively: ServletContext.getResourcePaths
135: // only returns entries for one directory level.
136: doRetrieveMatchingServletContextResources(
137: servletContext, fullPattern, currPath,
138: result);
139: }
140: if (getPathMatcher().match(fullPattern, currPath)) {
141: result.add(new ServletContextResource(
142: servletContext, currPath));
143: }
144: }
145: }
146: }
147:
148: }
|