/** * method to return the Localized version of the file whose name is passed as an argument. The * localization is done based on localization subdirectories under the docBase. * * <p>The method performs a resource lookup in a manner similar to the one used for JavaHelp * resources. * * <p>Search for localized versions of the file are looked for: * * <p><docBase> + "/" + language1 + "_" + country1 + "_" + variant1 + file <docBase> + "/" + * language1 + "_" + country1 + file <docBase> + "/" + language1 + file <docBase> + "/" + * language2 + "_" + country2 + "_" + variant1 + file <docBase> + "/" + language2 + "_" + country2 * + file <docBase> + "/" + language2 + file <docBase> + file * * <p>Where language1, country1, variant1 are associated with the Locale passed as an argument and * language2, country2, variant are associated with the fallback Locale passed as argument. * * @param path the pathname for the resource whose localized version we are seeking * @param loc the Locale we are interested in. * @param fbLoc the fallback Locale to use if unsuccessful * @param locType the type of localization required "file", "docbase" * @return a String with the path of the "best localized match" for the file whose path has been * passed as argument. */ public String getRealPath(String path, Locale reqLocale, Locale fbLocale, String locType) { String base = getAbsolutePath(); if (path == null) path = ""; String realPath = null; if ("file".equals(locType)) realPath = FileUtil.getLocalizedFile(base, path, reqLocale, fbLocale); else if ("docbase".equals(locType)) realPath = FileUtil.getDocBaseLocalizedFile(base, path, reqLocale, fbLocale); if (debug > 5) { log( "Get real path " + path + " " + realPath + " " + base + reqLocale.toString() + " " + fbLocale.toString()); } return realPath; }
/** * Implements getResource() See getRealPath(), it have to be local to the current Context - and * can't go to a sub-context. That means we don't need any overhead. */ public URL getResource(String rpath) throws MalformedURLException { if (rpath == null) return null; if (URLUtil.hasEscape(rpath)) return null; URL url = null; String absPath = getAbsolutePath(); if ("".equals(rpath)) return new URL("file", null, 0, absPath); if (!rpath.startsWith("/")) rpath = "/" + rpath; String realPath = FileUtil.safePath(absPath, rpath); if (realPath == null) { log("Unsafe path " + absPath + " " + rpath); return null; } try { url = new URL("file", null, 0, realPath); if (debug > 9) log("getResourceURL=" + url + " request=" + rpath); return url; } catch (IOException ex) { ex.printStackTrace(); return null; } }
/** Return the absolute path for the docBase, if we are file-system based, null otherwise. */ public String getAbsolutePath() { if (absPath != null) return absPath; if (FileUtil.isAbsolute(docBase)) absPath = docBase; else absPath = contextM.getHome() + File.separator + docBase; try { absPath = new File(absPath).getCanonicalPath(); } catch (IOException npe) { } return absPath; }
/** * According to Servlet 2.2 the real path is interpreted as relative to the current web app and * _cannot_ go outside the box. If your intention is different or want the "other" behavior you'll * have to first call getContext(path) and call getRealPath() on the result context ( if any - the * server may disable that from security reasons !). XXX find out how can we find the context path * in order to remove it from the path - that's the only way a user can do that unless he have * prior knowledge of the mappings ! */ public String getRealPath(String path) { String base = getAbsolutePath(); if (path == null) path = ""; String realPath = FileUtil.safePath(base, path); // No need for a sub-request, that's a great simplification // in servlet space. // Important: that's different from what some people might // expect and how other server APIs work, but that's how it's // specified in 2.2. From a security point of view that's very // good, it keeps inter-webapp communication under control. if (debug > 5) { log("Get real path " + path + " " + realPath + " " + base); } return realPath; }
/** * @deprecated - use getDocBase and URLUtil if you need it as URL NOT USED INSIDE TOMCAT - ONLY IN * OLD J2EE CONNECTORS ! */ public URL getDocumentBase() { if (documentBase == null) { if (docBase == null) return null; try { String absPath = docBase; // detect absolute path ( use the same logic in all tomcat ) if (FileUtil.isAbsolute(docBase)) absPath = docBase; else absPath = contextM.getHome() + File.separator + docBase; try { absPath = new File(absPath).getCanonicalPath(); } catch (IOException npe) { } documentBase = new URL("file", "", absPath); } catch (MalformedURLException ex) { ex.printStackTrace(); } } return documentBase; }