refined PathMatchingResourcePatternResolver's treatment of non-readable directories (SPR-6086)

This commit is contained in:
Juergen Hoeller 2009-09-07 12:44:59 +00:00
parent c10342a553
commit c5e49fc6dc
1 changed files with 24 additions and 5 deletions

View File

@ -521,8 +521,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
rootDir = rootDirResource.getFile().getAbsoluteFile(); rootDir = rootDirResource.getFile().getAbsoluteFile();
} }
catch (IOException ex) { catch (IOException ex) {
if (logger.isDebugEnabled()) { if (logger.isWarnEnabled()) {
logger.debug("Cannot search for matching files underneath " + rootDirResource + logger.warn("Cannot search for matching files underneath " + rootDirResource +
" because it does not correspond to a directory in the file system", ex); " because it does not correspond to a directory in the file system", ex);
} }
return Collections.emptySet(); return Collections.emptySet();
@ -563,7 +563,15 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
*/ */
protected Set<File> retrieveMatchingFiles(File rootDir, String pattern) throws IOException { protected Set<File> retrieveMatchingFiles(File rootDir, String pattern) throws IOException {
if (!rootDir.isDirectory()) { if (!rootDir.isDirectory()) {
throw new IllegalArgumentException("Resource path [" + rootDir + "] does not denote a directory"); throw new IllegalStateException(
"Resource path [" + rootDir.getAbsolutePath() + "] does not denote a directory");
}
if (!rootDir.canRead()) {
if (logger.isWarnEnabled()) {
logger.warn("Cannot search for matching files underneath directory [" + rootDir.getAbsolutePath() +
"] because the application is not allowed to read the directory");
}
return Collections.emptySet();
} }
String fullPattern = StringUtils.replace(rootDir.getAbsolutePath(), File.separator, "/"); String fullPattern = StringUtils.replace(rootDir.getAbsolutePath(), File.separator, "/");
if (!pattern.startsWith("/")) { if (!pattern.startsWith("/")) {
@ -591,13 +599,24 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
File[] dirContents = dir.listFiles(); File[] dirContents = dir.listFiles();
if (dirContents == null) { if (dirContents == null) {
throw new IOException("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]"); if (logger.isWarnEnabled()) {
logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
}
return;
} }
for (File content : dirContents) { for (File content : dirContents) {
String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/"); String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) { if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
if (!content.canRead()) {
if (logger.isDebugEnabled()) {
logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() +
"] because the application is not allowed to read the directory");
}
}
else {
doRetrieveMatchingFiles(fullPattern, content, result); doRetrieveMatchingFiles(fullPattern, content, result);
} }
}
if (getPathMatcher().match(fullPattern, currPath)) { if (getPathMatcher().match(fullPattern, currPath)) {
result.add(content); result.add(content);
} }