Servlet/PortletContextResource's getFile prefers "file:" URL resolution over calling getRealPath (SPR-8461)

This commit is contained in:
Juergen Hoeller 2011-11-28 16:51:42 +00:00
parent bba70a7f12
commit b55040cf02
2 changed files with 30 additions and 10 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.ContextResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.portlet.util.PortletUtils; import org.springframework.web.portlet.util.PortletUtils;
@ -135,14 +136,23 @@ public class PortletContextResource extends AbstractFileResolvingResource implem
} }
/** /**
* This implementation delegates to <code>PortletContext.getRealPath</code>, * This implementation resolves "file:" URLs or alternatively delegates to
* but throws a FileNotFoundException if not found or not resolvable. * <code>PortletContext.getRealPath</code>, throwing a FileNotFoundException
* if not found or not resolvable.
* @see javax.portlet.PortletContext#getResource(String)
* @see javax.portlet.PortletContext#getRealPath(String) * @see javax.portlet.PortletContext#getRealPath(String)
*/ */
@Override @Override
public File getFile() throws IOException { public File getFile() throws IOException {
String realPath = PortletUtils.getRealPath(this.portletContext, this.path); URL url = getURL();
return new File(realPath); 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 @Override

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.ContextResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
@ -135,14 +136,23 @@ public class ServletContextResource extends AbstractFileResolvingResource implem
} }
/** /**
* This implementation delegates to <code>ServletContext.getRealPath</code>, * This implementation resolves "file:" URLs or alternatively delegates to
* but throws a FileNotFoundException if not found or not resolvable. * <code>ServletContext.getRealPath</code>, throwing a FileNotFoundException
* if not found or not resolvable.
* @see javax.servlet.ServletContext#getResource(String)
* @see javax.servlet.ServletContext#getRealPath(String) * @see javax.servlet.ServletContext#getRealPath(String)
*/ */
@Override @Override
public File getFile() throws IOException { public File getFile() throws IOException {
String realPath = WebUtils.getRealPath(this.servletContext, this.path); URL url = getURL();
return new File(realPath); 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);
}
} }
/** /**