changeset 74:e5ff09208c28

search files in webapp in WEB-INF folder.
author casties
date Wed, 12 Mar 2014 12:42:28 +0100
parents 4b8c909cabf3
children 25eb2e1df106
files src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java
diffstat 1 files changed, 13 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java	Wed Mar 12 11:54:07 2014 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java	Wed Mar 12 12:42:28 2014 +0100
@@ -336,7 +336,10 @@
     }
 
     /**
-     * returns resource from path (in webapp) as InputStream.
+     * returns resource from path as InputStream.
+     * 
+     * Tries path in webapp first, then uses classpath loader.
+     * Relative paths in webapp start in /WEB-INF/.
      * 
      * @param sc
      * @param path
@@ -347,15 +350,17 @@
         if (sc == null) {
             // no servlet context -> use class loader
             ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
-            /* Request request = new Request(Method.GET, "riap://component/ping");
-               Response response = getContext().getClientDispatcher().handle(request);
-               Representation repr = response.getEntity(); */
         } else {
             // try path in webapp first
-            ps = sc.getResourceAsStream(path);
+            String webPath = path;
+            if (!webPath.startsWith("/")) {
+                // relative path in webapp starts in WEB-INF
+                webPath = "WEB-INF/" + webPath;
+            }
+            ps = sc.getResourceAsStream(webPath);
             if (ps == null) {
                 // try as file
-                File pf = new File(sc.getRealPath(path));
+                File pf = new File(sc.getRealPath(webPath));
                 if (pf.canRead()) {
                     logger.debug("trying file for: " + pf);
                     try {
@@ -376,7 +381,7 @@
      * get a real file name for a web app file pathname.
      * 
      * If filename starts with "/" its treated as absolute else the path is
-     * appended to the base directory of the web-app.
+     * appended to the /WEB-INF/ directory in the web-app.
      * 
      * @param filename
      * @param sc
@@ -387,7 +392,7 @@
         // is the filename absolute?
         if (!f.isAbsolute() && sc != null) {
             // relative path -> use getRealPath to resolve in webapp
-            filename = sc.getRealPath(filename);
+            filename = sc.getRealPath("WEB-INF/" + filename);
         }
         return filename;
     }