From ace0753eeda3a5fd118b54eab354cb75c3a31cc8 Mon Sep 17 00:00:00 2001 From: markt Date: Tue, 9 Mar 2010 20:33:27 +0000 Subject: [PATCH] Make StandardContext aliases work with ServletContext.getResourcePaths() git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@921110 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/naming/resources/BaseDirContext.java | 19 ++++- .../apache/naming/resources/FileDirContext.java | 2 +- .../org/apache/naming/resources/WARDirContext.java | 45 ++--------- .../naming/resources/TestBaseDirContext.java | 89 ++++++++++++++++++++++ 4 files changed, 112 insertions(+), 43 deletions(-) create mode 100644 test/org/apache/naming/resources/TestBaseDirContext.java diff --git a/java/org/apache/naming/resources/BaseDirContext.java b/java/org/apache/naming/resources/BaseDirContext.java index f95c3de13..f8b34e5ad 100644 --- a/java/org/apache/naming/resources/BaseDirContext.java +++ b/java/org/apache/naming/resources/BaseDirContext.java @@ -366,7 +366,7 @@ public abstract class BaseDirContext implements DirContext { * @return the object bound to name * @exception NamingException if a naming exception is encountered */ - public Object lookup(Name name) + public final Object lookup(Name name) throws NamingException { return lookup(name.toString()); } @@ -566,7 +566,7 @@ public abstract class BaseDirContext implements DirContext { * Each element of the enumeration is of type Binding. * @exception NamingException if a naming exception is encountered */ - public NamingEnumeration listBindings(Name name) + public final NamingEnumeration listBindings(Name name) throws NamingException { return listBindings(name.toString()); } @@ -581,8 +581,16 @@ public abstract class BaseDirContext implements DirContext { * Each element of the enumeration is of type Binding. * @exception NamingException if a naming exception is encountered */ - public abstract NamingEnumeration listBindings(String name) - throws NamingException; + public final NamingEnumeration listBindings(String name) + throws NamingException { + if (!aliases.isEmpty()) { + AliasResult result = findAlias(name); + if (result.dirContext != null) { + return result.dirContext.listBindings(result.aliasName); + } + } + return doListBindings(name); + } /** @@ -1365,6 +1373,9 @@ public abstract class BaseDirContext implements DirContext { protected abstract Object doLookup(String name) throws NamingException; + protected abstract NamingEnumeration doListBindings(String name) + throws NamingException; + protected abstract String doGetRealPath(String name); // -------------------------------------------------------- Private Methods diff --git a/java/org/apache/naming/resources/FileDirContext.java b/java/org/apache/naming/resources/FileDirContext.java index 6ef0d4e63..1f2c872da 100644 --- a/java/org/apache/naming/resources/FileDirContext.java +++ b/java/org/apache/naming/resources/FileDirContext.java @@ -320,7 +320,7 @@ public class FileDirContext extends BaseDirContext { * @exception NamingException if a naming exception is encountered */ @Override - public NamingEnumeration listBindings(String name) + protected NamingEnumeration doListBindings(String name) throws NamingException { File file = file(name); diff --git a/java/org/apache/naming/resources/WARDirContext.java b/java/org/apache/naming/resources/WARDirContext.java index 4702b9501..efaa6c9be 100644 --- a/java/org/apache/naming/resources/WARDirContext.java +++ b/java/org/apache/naming/resources/WARDirContext.java @@ -187,30 +187,16 @@ public class WARDirContext extends BaseDirContext { /** * Retrieves the named object. * - * @param name the name of the object to look up + * @param strName the name of the object to look up * @return the object bound to name * @exception NamingException if a naming exception is encountered */ @Override - protected Object doLookup(String name) + protected Object doLookup(String strName) throws NamingException { - return lookup(new CompositeName(name)); - } + Name name = new CompositeName(strName); - /** - * Retrieves the named object. If name is empty, returns a new instance - * of this context (which represents the same naming context as this - * context, but its environment may be modified independently and it may - * be accessed concurrently). - * - * @param name the name of the object to look up - * @return the object bound to name - * @exception NamingException if a naming exception is encountered - */ - @Override - public Object lookup(Name name) - throws NamingException { if (name.isEmpty()) return this; Entry entry = treeLookup(name); @@ -318,34 +304,17 @@ public class WARDirContext extends BaseDirContext { * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * - * @param name the name of the context to list + * @param strName the name of the context to list * @return an enumeration of the bindings in this context. * Each element of the enumeration is of type Binding. * @exception NamingException if a naming exception is encountered */ @Override - public NamingEnumeration listBindings(String name) + protected NamingEnumeration doListBindings(String strName) throws NamingException { - return listBindings(new CompositeName(name)); - } - + + Name name = new CompositeName(strName); - /** - * Enumerates the names bound in the named context, along with the - * objects bound to them. The contents of any subcontexts are not - * included. - *

- * If a binding is added to or removed from this context, its effect on - * an enumeration previously returned is undefined. - * - * @param name the name of the context to list - * @return an enumeration of the bindings in this context. - * Each element of the enumeration is of type Binding. - * @exception NamingException if a naming exception is encountered - */ - @Override - public NamingEnumeration listBindings(Name name) - throws NamingException { if (name.isEmpty()) return new NamingContextBindingsEnumeration(list(entries).iterator(), this); diff --git a/test/org/apache/naming/resources/TestBaseDirContext.java b/test/org/apache/naming/resources/TestBaseDirContext.java new file mode 100644 index 000000000..7d937810e --- /dev/null +++ b/test/org/apache/naming/resources/TestBaseDirContext.java @@ -0,0 +1,89 @@ +package org.apache.naming.resources; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.Set; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.catalina.core.StandardContext; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.util.buf.ByteChunk; + +public class TestBaseDirContext extends TomcatBaseTest { + + public void testDirContextAliases() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // Must have a real docBase - just use temp + StandardContext ctx = (StandardContext) + tomcat.addContext("/", System.getProperty("java.io.tmpdir")); + + File lib = new File("webapps/examples/WEB-INF/lib"); + ctx.setAliases("/WEB-INF/lib=" + lib.getCanonicalPath()); + + Tomcat.addServlet(ctx, "test", new TestServlet()); + ctx.addServletMapping("/", "test"); + + tomcat.start(); + + ByteChunk res = getUrl("http://localhost:" + getPort() + "/"); + + String result = res.toString(); + + assertTrue(result.indexOf("00-PASS") > -1); + assertTrue(result.indexOf("01-PASS") > -1); + assertTrue(result.indexOf("02-PASS") > -1); + } + + + /** + * Looks for the JSTL JARs in WEB-INF/lib. + */ + public static class TestServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + resp.setContentType("text/plain"); + + ServletContext context = getServletContext(); + + // Check resources individually + URL url = context.getResource("/WEB-INF/lib/jstl.jar"); + if (url != null) { + resp.getWriter().write("00-PASS\n"); + } + + url = context.getResource("/WEB-INF/lib/standard.jar"); + if (url != null) { + resp.getWriter().write("01-PASS\n"); + } + + // Check a directory listing + Set libs = context.getResourcePaths("/WEB-INF/lib"); + if (libs == null) { + return; + } + + if (!libs.contains("/WEB-INF/lib/jstl.jar")) { + return; + } + if (!libs.contains("/WEB-INF/lib/standard.jar")) { + return; + } + + resp.getWriter().write("02-PASS\n"); + } + + } +} -- 2.11.0