Treat InvalidPathException like an IOException in MockServletContext
Prior to this commit, if MockServletContext was configured with a FileSystemResourceLoader, invocations of the following methods on a Microsoft Windows operating system resulted in an InvalidPathException if the supplied path contained a colon (such as "C:\\temp"). This is inconsistent with the behavior on non-Windows operating systems. In addition, for comparable errors resulting in an IOException, those methods (except getRealPath()) return null instead of throwing the exception. - getResourcePaths() - getResource() - getResourceAsStream() - getRealPath() This commit makes handling of InvalidPathException and IOException consistent for these methods: both exceptions now result in null be returned by these methods. Closes gh-23717
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.EventListener;
|
||||
@@ -294,8 +295,10 @@ public class MockServletContext implements ServletContext {
|
||||
@Nullable
|
||||
public Set<String> getResourcePaths(String path) {
|
||||
String actualPath = (path.endsWith("/") ? path : path + "/");
|
||||
Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
|
||||
String resourceLocation = getResourceLocation(actualPath);
|
||||
Resource resource = null;
|
||||
try {
|
||||
resource = this.resourceLoader.getResource(resourceLocation);
|
||||
File file = resource.getFile();
|
||||
String[] fileList = file.list();
|
||||
if (ObjectUtils.isEmpty(fileList)) {
|
||||
@@ -311,9 +314,10 @@ public class MockServletContext implements ServletContext {
|
||||
}
|
||||
return resourcePaths;
|
||||
}
|
||||
catch (IOException ex) {
|
||||
catch (InvalidPathException | IOException ex ) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Could not get resource paths for " + resource, ex);
|
||||
logger.warn("Could not get resource paths for " +
|
||||
(resource != null ? resource : resourceLocation), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -322,19 +326,22 @@ public class MockServletContext implements ServletContext {
|
||||
@Override
|
||||
@Nullable
|
||||
public URL getResource(String path) throws MalformedURLException {
|
||||
Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
|
||||
if (!resource.exists()) {
|
||||
return null;
|
||||
}
|
||||
String resourceLocation = getResourceLocation(path);
|
||||
Resource resource = null;
|
||||
try {
|
||||
resource = this.resourceLoader.getResource(resourceLocation);
|
||||
if (!resource.exists()) {
|
||||
return null;
|
||||
}
|
||||
return resource.getURL();
|
||||
}
|
||||
catch (MalformedURLException ex) {
|
||||
throw ex;
|
||||
}
|
||||
catch (IOException ex) {
|
||||
catch (InvalidPathException | IOException ex) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Could not get URL for " + resource, ex);
|
||||
logger.warn("Could not get URL for resource " +
|
||||
(resource != null ? resource : resourceLocation), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -343,16 +350,19 @@ public class MockServletContext implements ServletContext {
|
||||
@Override
|
||||
@Nullable
|
||||
public InputStream getResourceAsStream(String path) {
|
||||
Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
|
||||
if (!resource.exists()) {
|
||||
return null;
|
||||
}
|
||||
String resourceLocation = getResourceLocation(path);
|
||||
Resource resource = null;
|
||||
try {
|
||||
resource = this.resourceLoader.getResource(resourceLocation);
|
||||
if (!resource.exists()) {
|
||||
return null;
|
||||
}
|
||||
return resource.getInputStream();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
catch (InvalidPathException | IOException ex) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Could not open InputStream for " + resource, ex);
|
||||
logger.warn("Could not open InputStream for resource " +
|
||||
(resource != null ? resource : resourceLocation), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -459,13 +469,16 @@ public class MockServletContext implements ServletContext {
|
||||
@Override
|
||||
@Nullable
|
||||
public String getRealPath(String path) {
|
||||
Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
|
||||
String resourceLocation = getResourceLocation(path);
|
||||
Resource resource = null;
|
||||
try {
|
||||
resource = this.resourceLoader.getResource(resourceLocation);
|
||||
return resource.getFile().getAbsolutePath();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
catch (InvalidPathException | IOException ex) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Could not determine real path of resource " + resource, ex);
|
||||
logger.warn("Could not determine real path of resource " +
|
||||
(resource != null ? resource : resourceLocation), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user