From b55040cf026c968078c9f56a3f9df3f0bc012536 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 28 Nov 2011 16:51:42 +0000 Subject: [PATCH] Servlet/PortletContextResource's getFile prefers "file:" URL resolution over calling getRealPath (SPR-8461) --- .../context/PortletContextResource.java | 20 ++++++++++++++----- .../support/ServletContextResource.java | 20 ++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletContextResource.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletContextResource.java index 365dad1d380..599c9e3e54e 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletContextResource.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletContextResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -28,6 +28,7 @@ import org.springframework.core.io.AbstractFileResolvingResource; import org.springframework.core.io.ContextResource; import org.springframework.core.io.Resource; import org.springframework.util.Assert; +import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; import org.springframework.web.portlet.util.PortletUtils; @@ -135,14 +136,23 @@ public class PortletContextResource extends AbstractFileResolvingResource implem } /** - * This implementation delegates to PortletContext.getRealPath, - * but throws a FileNotFoundException if not found or not resolvable. + * This implementation resolves "file:" URLs or alternatively delegates to + * PortletContext.getRealPath, throwing a FileNotFoundException + * if not found or not resolvable. + * @see javax.portlet.PortletContext#getResource(String) * @see javax.portlet.PortletContext#getRealPath(String) */ @Override public File getFile() throws IOException { - String realPath = PortletUtils.getRealPath(this.portletContext, this.path); - return new File(realPath); + URL url = getURL(); + if (ResourceUtils.isFileURL(url)) { + // Proceed with file system resolution... + return super.getFile(); + } + else { + String realPath = PortletUtils.getRealPath(this.portletContext, this.path); + return new File(realPath); + } } @Override diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/support/ServletContextResource.java b/org.springframework.web/src/main/java/org/springframework/web/context/support/ServletContextResource.java index 284f9bf52ff..99a028b2dc2 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/context/support/ServletContextResource.java +++ b/org.springframework.web/src/main/java/org/springframework/web/context/support/ServletContextResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -28,6 +28,7 @@ import org.springframework.core.io.AbstractFileResolvingResource; import org.springframework.core.io.ContextResource; import org.springframework.core.io.Resource; import org.springframework.util.Assert; +import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; import org.springframework.web.util.WebUtils; @@ -135,14 +136,23 @@ public class ServletContextResource extends AbstractFileResolvingResource implem } /** - * This implementation delegates to ServletContext.getRealPath, - * but throws a FileNotFoundException if not found or not resolvable. + * This implementation resolves "file:" URLs or alternatively delegates to + * ServletContext.getRealPath, throwing a FileNotFoundException + * if not found or not resolvable. + * @see javax.servlet.ServletContext#getResource(String) * @see javax.servlet.ServletContext#getRealPath(String) */ @Override public File getFile() throws IOException { - String realPath = WebUtils.getRealPath(this.servletContext, this.path); - return new File(realPath); + URL url = getURL(); + if (ResourceUtils.isFileURL(url)) { + // Proceed with file system resolution... + return super.getFile(); + } + else { + String realPath = WebUtils.getRealPath(this.servletContext, this.path); + return new File(realPath); + } } /**