KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > support > ServletContextResourcePatternResolver


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.context.support;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
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 /**
32  * ServletContext-aware subclass of PathMatchingResourcePatternResolver,
33  * able to find matching resources below the web application root directory
34  * via Servlet 2.3's <code>ServletContext.getResourcePaths</code>.
35  * Falls back to the superclass' file system checking for other resources.
36  *
37  * <p>The advantage of using <code>ServletContext.getResourcePaths</code> to
38  * find matching files is that it will work in a WAR file which has not been
39  * expanded too. For Servlet containers that do not support Servlet 2.3 or
40  * above, this resolver will always fall back to file system checking,
41  * which requires an expanded WAR file.
42  *
43  * @author Juergen Hoeller
44  * @since 1.1.2
45  */

46 public class ServletContextResourcePatternResolver extends PathMatchingResourcePatternResolver {
47
48     /**
49      * Create a new ServletContextResourcePatternResolver.
50      * @param servletContext the ServletContext to load resources with
51      * @see ServletContextResourceLoader#ServletContextResourceLoader(javax.servlet.ServletContext)
52      */

53     public ServletContextResourcePatternResolver(ServletContext JavaDoc servletContext) {
54         super(new ServletContextResourceLoader(servletContext));
55     }
56
57     /**
58      * Create a new ServletContextResourcePatternResolver.
59      * @param resourceLoader the ResourceLoader to load root directories and
60      * actual resources with
61      */

62     public ServletContextResourcePatternResolver(ResourceLoader resourceLoader) {
63         super(resourceLoader);
64     }
65
66     /**
67      * Overridden version which checks for ServletContextResource
68      * and uses <code>ServletContext.getResourcePaths</code> to find
69      * matching resources below the web application root directory.
70      * In case of other resources, delegates to the superclass version.
71      * @see #doRetrieveMatchingServletContextResources
72      * @see ServletContextResource
73      * @see javax.servlet.ServletContext#getResourcePaths
74      */

75     protected Set JavaDoc doFindPathMatchingFileResources(Resource rootDirResource, String JavaDoc subPattern) throws IOException JavaDoc {
76         if (rootDirResource instanceof ServletContextResource) {
77             ServletContextResource scResource = (ServletContextResource) rootDirResource;
78             ServletContext JavaDoc sc = scResource.getServletContext();
79             if (sc.getMajorVersion() > 2 || (sc.getMajorVersion() == 2 && sc.getMinorVersion() > 2)) {
80                 // Only try the following on Servlet containers >= 2.3:
81
// ServletContext.getResourcePaths() is not available before that version.
82
String JavaDoc fullPattern = scResource.getPath() + subPattern;
83                 Set JavaDoc 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     /**
92      * Recursively retrieve ServletContextResources that match the given pattern,
93      * adding them to the given result set.
94      * @param servletContext the ServletContext to work on
95      * @param fullPattern the pattern to match against,
96      * with preprended root directory path
97      * @param dir the current directory
98      * @param result the Set of matching Resources to add to
99      * @throws IOException if directory contents could not be retrieved
100      * @see ServletContextResource
101      * @see javax.servlet.ServletContext#getResourcePaths
102      */

103     protected void doRetrieveMatchingServletContextResources(
104             ServletContext JavaDoc servletContext, String JavaDoc fullPattern, String JavaDoc dir, Set JavaDoc result) throws IOException JavaDoc {
105
106         Set JavaDoc candidates = servletContext.getResourcePaths(dir);
107         if (candidates != null) {
108             boolean dirDepthNotFixed = (fullPattern.indexOf("**") != -1);
109             for (Iterator JavaDoc it = candidates.iterator(); it.hasNext();) {
110                 String JavaDoc currPath = (String JavaDoc) it.next();
111                 if (!currPath.startsWith(dir)) {
112                     // Returned resource path does not start with relative directory:
113
// assuming absolute path returned -> strip absolute path.
114
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                     // Search subdirectories recursively: ServletContext.getResourcePaths
123
// only returns entries for one directory level.
124
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