comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java @ 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
comparison
equal deleted inserted replaced
73:4b8c909cabf3 74:e5ff09208c28
334 } 334 }
335 return retString; 335 return retString;
336 } 336 }
337 337
338 /** 338 /**
339 * returns resource from path (in webapp) as InputStream. 339 * returns resource from path as InputStream.
340 *
341 * Tries path in webapp first, then uses classpath loader.
342 * Relative paths in webapp start in /WEB-INF/.
340 * 343 *
341 * @param sc 344 * @param sc
342 * @param path 345 * @param path
343 * @return 346 * @return
344 */ 347 */
345 protected InputStream getResourceAsStream(ServletContext sc, String path) { 348 protected InputStream getResourceAsStream(ServletContext sc, String path) {
346 InputStream ps = null; 349 InputStream ps = null;
347 if (sc == null) { 350 if (sc == null) {
348 // no servlet context -> use class loader 351 // no servlet context -> use class loader
349 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); 352 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
350 /* Request request = new Request(Method.GET, "riap://component/ping");
351 Response response = getContext().getClientDispatcher().handle(request);
352 Representation repr = response.getEntity(); */
353 } else { 353 } else {
354 // try path in webapp first 354 // try path in webapp first
355 ps = sc.getResourceAsStream(path); 355 String webPath = path;
356 if (!webPath.startsWith("/")) {
357 // relative path in webapp starts in WEB-INF
358 webPath = "WEB-INF/" + webPath;
359 }
360 ps = sc.getResourceAsStream(webPath);
356 if (ps == null) { 361 if (ps == null) {
357 // try as file 362 // try as file
358 File pf = new File(sc.getRealPath(path)); 363 File pf = new File(sc.getRealPath(webPath));
359 if (pf.canRead()) { 364 if (pf.canRead()) {
360 logger.debug("trying file for: " + pf); 365 logger.debug("trying file for: " + pf);
361 try { 366 try {
362 ps = new FileInputStream(pf); 367 ps = new FileInputStream(pf);
363 } catch (FileNotFoundException e) { 368 } catch (FileNotFoundException e) {
374 379
375 /** 380 /**
376 * get a real file name for a web app file pathname. 381 * get a real file name for a web app file pathname.
377 * 382 *
378 * If filename starts with "/" its treated as absolute else the path is 383 * If filename starts with "/" its treated as absolute else the path is
379 * appended to the base directory of the web-app. 384 * appended to the /WEB-INF/ directory in the web-app.
380 * 385 *
381 * @param filename 386 * @param filename
382 * @param sc 387 * @param sc
383 * @return 388 * @return
384 */ 389 */
385 public static String getResourcePath(ServletContext sc, String filename) { 390 public static String getResourcePath(ServletContext sc, String filename) {
386 File f = new File(filename); 391 File f = new File(filename);
387 // is the filename absolute? 392 // is the filename absolute?
388 if (!f.isAbsolute() && sc != null) { 393 if (!f.isAbsolute() && sc != null) {
389 // relative path -> use getRealPath to resolve in webapp 394 // relative path -> use getRealPath to resolve in webapp
390 filename = sc.getRealPath(filename); 395 filename = sc.getRealPath("WEB-INF/" + filename);
391 } 396 }
392 return filename; 397 return filename;
393 } 398 }
394 399
395 /* 400 /*