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