Skip to content

ServletContextResource getFile should not rely on getRealPath (for WebLogic 10 compatibility) [SPR-8461] #13107

@spring-projects-issues

Description

@spring-projects-issues

Jason Arndt opened SPR-8461 and commented

When using the ResourceHttpRequestHandler to serve static resources the ServletContextResource's getFile() method that is used doesn't work when not deploying exploded.

Here is the scenario:

  • wire up in spring config
    <mvc:resources mapping="/resources/**" location="/resources/" />

  • access http://localhost:7001/myapp/resources/images/img.gif

  • DispatcherServlet gets the request and delegates to the ResourceHttpRequestHandler.getResource()

  • ResourceHttpRequestHandler uses the ServletContextResource from it's list of locations to check "exists" and "readable"

  • ServletContextResource says yes it exists

  • isReadable() hops to AbstractFileResolvingResource, which determines that is is a "file system resolution" and calls getFile() on ServletContextResource. NOTE: the url that is returned by Weblogic is file:c:/weblogic/tmp/blah/blah/blah/resources/images/img.gif

  • The ServletContextResource.getFile() uses WebUtils.getRealPath(), which is returning /resources/images/img.gif and it tries to then create a File object.

I was able to work around this issue by creating and plugging in a custom ServletContextResource class that overrides the getFile() method and does this:

@Override
public File getFile() throws IOException {
////////////////////////////////////////////////////
//TODO: this fixes a Bug in spring...they are using file path resolution, so use it here too...
// @see AbstractFileResolvingResource.isReadable()
//String realPath = WebUtils.getRealPath(this.servletContext, this.path);
//return new File(realPath);
////////////////////////////////////////////////////////
URL url = getServletContext().getResource(getPath());
String realPath = url.getPath();
return new File(realPath);
}

I have tested this using Weblogic 10.3 deployed exploded, not exploded, and using the eclipse plugin which is basically exploded.


Affects: 3.0.5, 3.1 M1, 3.1 M2

4 votes, 6 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions