Skip to content

Commit b55040c

Browse files
committed
Servlet/PortletContextResource's getFile prefers "file:" URL resolution over calling getRealPath (SPR-8461)
1 parent bba70a7 commit b55040c

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletContextResource.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.core.io.ContextResource;
2929
import org.springframework.core.io.Resource;
3030
import org.springframework.util.Assert;
31+
import org.springframework.util.ResourceUtils;
3132
import org.springframework.util.StringUtils;
3233
import org.springframework.web.portlet.util.PortletUtils;
3334

@@ -135,14 +136,23 @@ public URL getURL() throws IOException {
135136
}
136137

137138
/**
138-
* This implementation delegates to <code>PortletContext.getRealPath</code>,
139-
* but throws a FileNotFoundException if not found or not resolvable.
139+
* This implementation resolves "file:" URLs or alternatively delegates to
140+
* <code>PortletContext.getRealPath</code>, throwing a FileNotFoundException
141+
* if not found or not resolvable.
142+
* @see javax.portlet.PortletContext#getResource(String)
140143
* @see javax.portlet.PortletContext#getRealPath(String)
141144
*/
142145
@Override
143146
public File getFile() throws IOException {
144-
String realPath = PortletUtils.getRealPath(this.portletContext, this.path);
145-
return new File(realPath);
147+
URL url = getURL();
148+
if (ResourceUtils.isFileURL(url)) {
149+
// Proceed with file system resolution...
150+
return super.getFile();
151+
}
152+
else {
153+
String realPath = PortletUtils.getRealPath(this.portletContext, this.path);
154+
return new File(realPath);
155+
}
146156
}
147157

148158
@Override

org.springframework.web/src/main/java/org/springframework/web/context/support/ServletContextResource.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.core.io.ContextResource;
2929
import org.springframework.core.io.Resource;
3030
import org.springframework.util.Assert;
31+
import org.springframework.util.ResourceUtils;
3132
import org.springframework.util.StringUtils;
3233
import org.springframework.web.util.WebUtils;
3334

@@ -135,14 +136,23 @@ public URL getURL() throws IOException {
135136
}
136137

137138
/**
138-
* This implementation delegates to <code>ServletContext.getRealPath</code>,
139-
* but throws a FileNotFoundException if not found or not resolvable.
139+
* This implementation resolves "file:" URLs or alternatively delegates to
140+
* <code>ServletContext.getRealPath</code>, throwing a FileNotFoundException
141+
* if not found or not resolvable.
142+
* @see javax.servlet.ServletContext#getResource(String)
140143
* @see javax.servlet.ServletContext#getRealPath(String)
141144
*/
142145
@Override
143146
public File getFile() throws IOException {
144-
String realPath = WebUtils.getRealPath(this.servletContext, this.path);
145-
return new File(realPath);
147+
URL url = getURL();
148+
if (ResourceUtils.isFileURL(url)) {
149+
// Proceed with file system resolution...
150+
return super.getFile();
151+
}
152+
else {
153+
String realPath = WebUtils.getRealPath(this.servletContext, this.path);
154+
return new File(realPath);
155+
}
146156
}
147157

148158
/**

0 commit comments

Comments
 (0)