1515 */
1616package org .ocpsoft .rewrite .transform .resolve ;
1717
18- import java .io .InputStream ;
18+ import java .io .File ;
19+
20+ import javax .servlet .ServletContext ;
1921
2022import org .ocpsoft .rewrite .context .EvaluationContext ;
2123import org .ocpsoft .rewrite .event .Rewrite ;
2224import org .ocpsoft .rewrite .servlet .http .event .HttpServletRewrite ;
25+ import org .ocpsoft .rewrite .transform .resource .FileResource ;
26+ import org .ocpsoft .rewrite .transform .resource .Resource ;
2327
2428public class WebResourceResolver implements ResourceResolver
2529{
@@ -42,7 +46,7 @@ private WebResourceResolver(String fileType)
4246 }
4347
4448 @ Override
45- public InputStream getResource (Rewrite event , EvaluationContext context )
49+ public Resource getResource (Rewrite event , EvaluationContext context )
4650 {
4751
4852 // will only work for HTTP requests
@@ -51,13 +55,22 @@ public InputStream getResource(Rewrite event, EvaluationContext context)
5155
5256 // the name of the requested resource
5357 String path = httpServletRewrite .getRequestPath ();
54-
55- if (fileType != null ) {
58+
59+ if (fileType != null ) {
5660 path = path .replaceAll ("\\ .\\ w+$" , fileType );
5761 }
5862
59- // try to load the underlying resource
60- return httpServletRewrite .getRequest ().getServletContext ().getResourceAsStream (path );
63+ // obtain the file system path of the resource
64+ ServletContext servletContext = httpServletRewrite .getRequest ().getServletContext ();
65+ String realPath = servletContext .getRealPath (path );
66+
67+ // wrap the file in a FileResource
68+ if (realPath != null ) {
69+ File resource = new File (realPath );
70+ if (resource .isFile () && resource .canRead ()) {
71+ return new FileResource (resource );
72+ }
73+ }
6174
6275 }
6376
0 commit comments