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.portlet.context;
018:
019: import java.io.IOException;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Set;
023:
024: import javax.portlet.PortletContext;
025:
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: * PortletContext-aware subclass of PathMatchingResourcePatternResolver,
033: * able to find matching resources below the web application root directory
034: * via Portlet API's <code>PortletContext.getResourcePaths</code>.
035: * Falls back to the superclass' file system checking for other resources.
036: *
037: * <p>The advantage of using <code>PortletContext.getResourcePaths</code> to
038: * find matching files is that it will work in a WAR file which has not been
039: * expanded too.
040: *
041: * @author Juergen Hoeller
042: * @author John A. Lewis
043: * @since 2.0
044: */
045: public class PortletContextResourcePatternResolver extends
046: PathMatchingResourcePatternResolver {
047:
048: /**
049: * Create a new PortletContextResourcePatternResolver.
050: * @param portletContext the PortletContext to load resources with
051: * @see PortletContextResourceLoader#PortletContextResourceLoader(javax.portlet.PortletContext)
052: */
053: public PortletContextResourcePatternResolver(
054: PortletContext portletContext) {
055: super (new PortletContextResourceLoader(portletContext));
056: }
057:
058: /**
059: * Create a new PortletContextResourcePatternResolver.
060: * @param resourceLoader the ResourceLoader to load root directories and
061: * actual resources with
062: */
063: public PortletContextResourcePatternResolver(
064: ResourceLoader resourceLoader) {
065: super (resourceLoader);
066: }
067:
068: /**
069: * Overridden version which checks for PortletContextResource
070: * and uses <code>PortletContext.getResourcePaths</code> to find
071: * matching resources below the web application root directory.
072: * In case of other resources, delegates to the superclass version.
073: * @see #doRetrieveMatchingPortletContextResources
074: * @see org.springframework.web.portlet.context.PortletContextResource
075: * @see javax.portlet.PortletContext#getResourcePaths
076: */
077: protected Set doFindPathMatchingFileResources(
078: Resource rootDirResource, String subPattern)
079: throws IOException {
080: if (rootDirResource instanceof PortletContextResource) {
081: PortletContextResource pcResource = (PortletContextResource) rootDirResource;
082: PortletContext pc = pcResource.getPortletContext();
083: String fullPattern = pcResource.getPath() + subPattern;
084: Set result = new HashSet();
085: doRetrieveMatchingPortletContextResources(pc, fullPattern,
086: pcResource.getPath(), result);
087: return result;
088: }
089: return super .doFindPathMatchingFileResources(rootDirResource,
090: subPattern);
091: }
092:
093: /**
094: * Recursively retrieve PortletContextResources that match the given pattern,
095: * adding them to the given result set.
096: * @param portletContext the PortletContext to work on
097: * @param fullPattern the pattern to match against,
098: * with preprended root directory path
099: * @param dir the current directory
100: * @param result the Set of matching Resources to add to
101: * @throws IOException if directory contents could not be retrieved
102: * @see org.springframework.web.portlet.context.PortletContextResource
103: * @see javax.portlet.PortletContext#getResourcePaths
104: */
105: protected void doRetrieveMatchingPortletContextResources(
106: PortletContext portletContext, String fullPattern,
107: String dir, Set result) throws IOException {
108:
109: Set candidates = portletContext.getResourcePaths(dir);
110: if (candidates != null) {
111: boolean dirDepthNotFixed = (fullPattern.indexOf("**") != -1);
112: for (Iterator it = candidates.iterator(); it.hasNext();) {
113: String currPath = (String) it.next();
114: if (currPath.endsWith("/")
115: && (dirDepthNotFixed || StringUtils
116: .countOccurrencesOf(currPath, "/") < StringUtils
117: .countOccurrencesOf(fullPattern, "/"))) {
118: doRetrieveMatchingPortletContextResources(
119: portletContext, fullPattern, currPath,
120: result);
121: }
122: if (getPathMatcher().match(fullPattern, currPath)) {
123: result.add(new PortletContextResource(
124: portletContext, currPath));
125: }
126: }
127: }
128: }
129:
130: }
|