From c61f6407642b804b3c25453478d68f3b16f49f58 Mon Sep 17 00:00:00 2001 From: funkman Date: Thu, 13 Sep 2007 15:19:59 +0000 Subject: [PATCH] Allow for Aliases at the resource level. This is to prevent situations of relying on symlinks, or Filters while still make outside directories appear as part of the webapp. (A common feature request seen on the user list) Example usage: If the feature is NOT used - there is no additional overhead. git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@575332 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/naming/resources/FileDirContext.java | 264 +++++++++++++++------ webapps/docs/changelog.xml | 3 + 2 files changed, 200 insertions(+), 67 deletions(-) diff --git a/java/org/apache/naming/resources/FileDirContext.java b/java/org/apache/naming/resources/FileDirContext.java index ddb40feb5..033364c73 100644 --- a/java/org/apache/naming/resources/FileDirContext.java +++ b/java/org/apache/naming/resources/FileDirContext.java @@ -108,6 +108,17 @@ public class FileDirContext extends BaseDirContext { protected boolean allowLinking = false; + /** + * Aliases + */ + protected String aliases; + + + /** + * Aliases in decoded form. + */ + protected Alias[] pathAliases; + // ------------------------------------------------------------- Properties @@ -123,22 +134,22 @@ public class FileDirContext extends BaseDirContext { */ public void setDocBase(String docBase) { - // Validate the format of the proposed document root - if (docBase == null) - throw new IllegalArgumentException - (sm.getString("resources.null")); + // Validate the format of the proposed document root + if (docBase == null) + throw new IllegalArgumentException + (sm.getString("resources.null")); - // Calculate a File object referencing this document base directory - base = new File(docBase); + // Calculate a File object referencing this document base directory + base = new File(docBase); try { base = base.getCanonicalFile(); } catch (IOException e) { // Ignore } - // Validate that the document base is an existing directory - if (!base.exists() || !base.isDirectory() || !base.canRead()) - throw new IllegalArgumentException + // Validate that the document base is an existing directory + if (!base.exists() || !base.isDirectory() || !base.canRead()) + throw new IllegalArgumentException (sm.getString("fileResources.base", docBase)); this.absoluteBase = base.getAbsolutePath(); super.setDocBase(docBase); @@ -177,18 +188,69 @@ public class FileDirContext extends BaseDirContext { return allowLinking; } - - // --------------------------------------------------------- Public Methods + /** + * Get the alias string in use. + */ + public String getAliases(String aliases) { + return aliases; + } /** - * Release any resources allocated for this directory context. + * Set files system aliases. Aliases are of the form + *
prefix=path,prefix2=path2
+ * For example: + *
+     *   /images/=/tmp/images/,
+     *   /pdf/=/usr/local/data/pdfs/
+     * 
*/ - public void release() { - super.release(); + public void setAliases(String aliases) { + this.aliases = aliases; + + pathAliases = null; + if (this.aliases!=null) { + this.aliases = this.aliases.trim(); + } + if (this.aliases==null||this.aliases.length()==0) { + this.aliases=null; + return; + } + + String[] split1 = this.aliases.split(","); + ArrayList aliasList = new ArrayList(); + for (int i=0; split1!=null && i0 && kvp[1].length()>0) { + Alias alias = new Alias(); + alias.prefix=kvp[0]; + alias.basePath= new File(kvp[1]); + alias.absPath=alias.basePath.getAbsolutePath(); + aliasList.add(alias); + } + } + } + + if (aliasList.size()>0) { + pathAliases = new Alias[aliasList.size()]; + for (int i=0; i= 0) - normalized = normalized.replace('\\', '/'); - if (!normalized.startsWith("/")) - normalized = "/" + normalized; - - // Resolve occurrences of "//" in the normalized path - while (true) { - int index = normalized.indexOf("//"); - if (index < 0) - break; - normalized = normalized.substring(0, index) + - normalized.substring(index + 1); - } + String normalized = path; + + // Normalize the slashes and add leading slash if necessary + if (File.separatorChar == '\\' && normalized.indexOf('\\') >= 0) + normalized = normalized.replace('\\', '/'); + if (!normalized.startsWith("/")) + normalized = "/" + normalized; + + // Resolve occurrences of "//" in the normalized path + while (true) { + int index = normalized.indexOf("//"); + if (index < 0) + break; + normalized = normalized.substring(0, index) + + normalized.substring(index + 1); + } - // Resolve occurrences of "/./" in the normalized path - while (true) { - int index = normalized.indexOf("/./"); - if (index < 0) - break; - normalized = normalized.substring(0, index) + - normalized.substring(index + 2); - } + // Resolve occurrences of "/./" in the normalized path + while (true) { + int index = normalized.indexOf("/./"); + if (index < 0) + break; + normalized = normalized.substring(0, index) + + normalized.substring(index + 2); + } - // Resolve occurrences of "/../" in the normalized path - while (true) { - int index = normalized.indexOf("/../"); - if (index < 0) - break; - if (index == 0) - return (null); // Trying to go outside our context - int index2 = normalized.lastIndexOf('/', index - 1); - normalized = normalized.substring(0, index2) + - normalized.substring(index + 3); - } + // Resolve occurrences of "/../" in the normalized path + while (true) { + int index = normalized.indexOf("/../"); + if (index < 0) + break; + if (index == 0) + return (null); // Trying to go outside our context + int index2 = normalized.lastIndexOf('/', index - 1); + normalized = normalized.substring(0, index2) + + normalized.substring(index + 3); + } - // Return the normalized path that we have completed - return (normalized); + // Return the normalized path that we have completed + return (normalized); } @@ -818,12 +928,27 @@ public class FileDirContext extends BaseDirContext { */ protected File file(String name) { - File file = new File(base, name); + File baseDir = base; + String absoluteBaseDir = absoluteBase; + + if (pathAliases!=null) { + for (int i=0; i + + Allow for aliases in FileDirContext. (funkman) + -- 2.11.0