remm        01/01/11 22:55:35

  Modified:    catalina/src/share/org/apache/catalina/servlets
                        DefaultServlet.java WebdavServlet.java
  Log:
  - Partly rewritten the DefaultServlet and WebdavServlet to use DirContext.
  - Some parts of the code could be simplified to take advantage of the JNDI
    naming conventions. For example, when the member of a collection are
    requested, JNDI does return the names relative to the parent collection's
    context. Before, the Resources were returning the full names of the child
    resources.
  
  Revision  Changes    Path
  1.19      +363 -43   
jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java
  
  Index: DefaultServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- DefaultServlet.java       2001/01/08 16:45:55     1.18
  +++ DefaultServlet.java       2001/01/12 06:55:34     1.19
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java,v
 1.18 2001/01/08 16:45:55 remm Exp $
  - * $Revision: 1.18 $
  - * $Date: 2001/01/08 16:45:55 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java,v
 1.19 2001/01/12 06:55:34 remm Exp $
  + * $Revision: 1.19 $
  + * $Date: 2001/01/12 06:55:34 $
    *
    * ====================================================================
    *
  @@ -66,6 +66,8 @@
   
   
   import java.io.BufferedInputStream;
  +import java.io.ByteArrayOutputStream;
  +import java.io.ByteArrayInputStream;
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.InputStream;
  @@ -95,16 +97,19 @@
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
  +import javax.naming.NamingException;
  +import javax.naming.InitialContext;
  +import javax.naming.Context;
  +import javax.naming.NamingEnumeration;
  +import javax.naming.NameClassPair;
  +import javax.naming.directory.DirContext;
  +import javax.naming.directory.Attribute;
  +import javax.naming.directory.Attributes;
  +import org.apache.naming.resources.Resource;
  +import org.apache.naming.resources.ResourceAttributes;
   import org.apache.catalina.Globals;
  -import org.apache.catalina.Resources;
  -import org.apache.catalina.core.ApplicationContext;
  -import org.apache.catalina.resources.ResourceBean;
  -import org.apache.catalina.resources.DirectoryBean;
   import org.apache.catalina.util.MD5Encoder;
   import org.apache.catalina.util.StringManager;
  -import org.apache.catalina.util.xml.SaxContext;
  -import org.apache.catalina.util.xml.XmlAction;
  -import org.apache.catalina.util.xml.XmlMapper;
   
   
   /**
  @@ -113,7 +118,7 @@
    *
    * @author Craig R. McClanahan
    * @author Remy Maucherat
  - * @version $Revision: 1.18 $ $Date: 2001/01/08 16:45:55 $
  + * @version $Revision: 1.19 $ $Date: 2001/01/12 06:55:34 $
    */
   
   public class DefaultServlet
  @@ -201,6 +206,12 @@
   
   
       /**
  +     * JNDI resources name.
  +     */
  +    protected static final String RESOURCES_JNDI_NAME = "java:/comp/Resources";
  +
  +
  +    /**
        * The string manager for this package.
        */
       protected static StringManager sm =
  @@ -295,6 +306,38 @@
   
   
       /**
  +     * Get resources. This method will try to retrieve the resources through
  +     * JNDI first, then in the servlet context if JNDI has failed (it could be
  +     * disabled). It will return null.
  +     * 
  +     * @return A JNDI DirContext, or null.
  +     */
  +    protected DirContext getResources() {
  +
  +        // First : try JNDI
  +        try {
  +            return 
  +                (DirContext) new InitialContext().lookup(RESOURCES_JNDI_NAME);
  +        } catch (NamingException e) {
  +            // Failed
  +        } catch (ClassCastException e) {
  +            // Failed : Not the right type
  +        }
  +
  +        // If it has failed, try the servlet context
  +        try {
  +            return (DirContext) getServletContext()
  +                .getAttribute(Globals.RESOURCES_ATTR);
  +        } catch (ClassCastException e) {
  +            // Failed : Not the right type
  +        }
  +
  +        return null;
  +
  +    }
  +
  +
  +    /**
        * Show HTTP header information.
        */
       protected void showRequestInfo(HttpServletRequest req) {
  @@ -494,13 +537,29 @@
               resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
           }
           
  -        // Retrieve the Catalina context
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  -        Resources resources = context.getResources();
  +        // Retrieve the resources
  +        DirContext resources = getResources();
  +        
  +        if (resources == null) {
  +            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  +            return;
  +        }
           
  -        boolean exists = resources.exists(path);
  +        boolean exists = true;
  +        try {
  +            resources.lookup(path);
  +        } catch (NamingException e) {
  +            exists = false;
  +        }
           
  -        boolean result = resources.setResource(path, req.getInputStream());
  +        boolean result = true;
  +        try {
  +            Resource newResource = new Resource(req.getInputStream());
  +            // FIXME: Add attributes
  +            resources.bind(path, newResource);
  +        } catch(NamingException e) {
  +            result = false;
  +        }
           
           if (result) {
               if (exists) {
  @@ -535,13 +594,28 @@
           String path = getRelativePath(req);
           
           // Retrieve the Catalina context
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  -        Resources resources = context.getResources();
  +        // Retrieve the resources
  +        DirContext resources = getResources();
  +        
  +        if (resources == null) {
  +            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  +            return;
  +        }
           
  -        boolean exists = resources.exists(path);
  +        boolean exists = true;
  +        try {
  +            resources.lookup(path);
  +        } catch (NamingException e) {
  +            exists = false;
  +        }
           
           if (exists) {
  -            boolean result = resources.deleteResource(path);
  +            boolean result = true;
  +            try {
  +                resources.unbind(path);
  +            } catch (NamingException e) {
  +                result = false;
  +            }
               if (result) {
                   resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
               } else {
  @@ -837,8 +911,7 @@
           IOException exception = null;
               
           // FIXME : i18n ?
  -        InputStream resourceInputStream = 
  -            resourceInfo.resources.getResourceAsStream(resourceInfo.path);
  +        InputStream resourceInputStream = resourceInfo.is;
           InputStream istream = new BufferedInputStream
               (resourceInputStream, input);
           
  @@ -874,8 +947,7 @@
   
           IOException exception = null;
               
  -        InputStream resourceInputStream = 
  -            resourceInfo.resources.getResourceAsStream(resourceInfo.path);
  +        InputStream resourceInputStream = resourceInfo.is;
           // FIXME : i18n ?
           Reader reader = new InputStreamReader(resourceInputStream);
           
  @@ -912,8 +984,7 @@
           
           IOException exception = null;
           
  -        InputStream resourceInputStream = 
  -            resourceInfo.resources.getResourceAsStream(resourceInfo.path);
  +        InputStream resourceInputStream = resourceInfo.is;
           InputStream istream =
               new BufferedInputStream(resourceInputStream, input);
           exception = copyRange(istream, ostream, range.start, range.end);
  @@ -948,8 +1019,7 @@
           
           IOException exception = null;
           
  -        InputStream resourceInputStream = 
  -            resourceInfo.resources.getResourceAsStream(resourceInfo.path);
  +        InputStream resourceInputStream = resourceInfo.is;
           Reader reader = new InputStreamReader(resourceInputStream);
           exception = copyRange(reader, writer, range.start, range.end);
           
  @@ -986,8 +1056,7 @@
           
           while ( (exception == null) && (ranges.hasMoreElements()) ) {
               
  -            InputStream resourceInputStream = 
  -                resourceInfo.resources.getResourceAsStream(resourceInfo.path);
  +            InputStream resourceInputStream = resourceInfo.is;
               InputStream istream =    // FIXME: internationalization???????
                   new BufferedInputStream(resourceInputStream, input);
           
  @@ -1042,8 +1111,7 @@
           
           while ( (exception == null) && (ranges.hasMoreElements()) ) {
               
  -            InputStream resourceInputStream = 
  -                resourceInfo.resources.getResourceAsStream(resourceInfo.path);
  +            InputStream resourceInputStream = resourceInfo.is;
               Reader reader = new InputStreamReader(resourceInputStream);
           
               Range currentRange = (Range) ranges.nextElement();
  @@ -1262,7 +1330,7 @@
        * @param pathname Pathname of the file to be served
        */
       private ResourceInfo checkWelcomeFiles(String pathname, 
  -                                           Resources resources) {
  +                                           DirContext resources) {
           
           String collectionName = pathname;
           if (!pathname.endsWith("/")) {
  @@ -1332,8 +1400,7 @@
        }
   
           // Retrieve the Catalina context and Resources implementation
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  -        Resources resources = context.getResources();
  +        DirContext resources = getResources();
           ResourceInfo resourceInfo = new ResourceInfo(path, resources);
   
           if (!resourceInfo.exists) {
  @@ -1454,6 +1521,15 @@
                   response.setContentLength((int) contentLength);
               }
               
  +            if (resourceInfo.collection) {
  +                
  +                if (content) {
  +                    // Serve the directory browser
  +                    resourceInfo.is = render(resourceInfo);
  +                }
  +                
  +            }
  +            
               // Copy the input stream to our output stream (if requested)
               if (content) {
                   response.setBufferSize(output);
  @@ -1655,6 +1731,210 @@
       }
   
   
  +    /**
  +     * Return an InputStream to an HTML representation of the contents
  +     * of this directory.
  +     *
  +     * @param contextPath Context path to which our internal paths are
  +     *  relative
  +     */
  +    public InputStream render(ResourceInfo resourceInfo) {
  +
  +        String contextPath = resourceInfo.path;
  +        String name = resourceInfo.path;
  +
  +     // Number of characters to trim from the beginnings of filenames
  +     int trim = name.length();
  +     if (!name.endsWith("/"))
  +         trim += 1;
  +     if (name.equals("/"))
  +         trim = 1;
  +
  +     // Prepare a writer to a buffered area
  +     ByteArrayOutputStream stream = new ByteArrayOutputStream();
  +     PrintWriter writer = new PrintWriter(stream);
  +
  +     // FIXME - Currently pays no attention to the user's Locale
  +
  +     // Render the page header
  +     writer.print("<html>\r\n");
  +     writer.print("<head>\r\n");
  +     writer.print("<title>");
  +     writer.print(sm.getString("directory.title", name));
  +     writer.print("</title>\r\n</head>\r\n");
  +     writer.print("<body bgcolor=\"white\">\r\n");
  +     writer.print("<table width=\"90%\" cellspacing=\"0\"" +
  +                  " cellpadding=\"5\" align=\"center\">\r\n");
  +
  +     // Render the in-page title
  +     writer.print("<tr><td colspan=\"3\"><font size=\"+2\">\r\n<strong>");
  +     writer.print(sm.getString("directory.title", name));
  +     writer.print("</strong>\r\n</font></td></tr>\r\n");
  +
  +     // Render the link to our parent (if required)
  +        String parentDirectory = name;
  +        if (parentDirectory.endsWith("/")) {
  +            parentDirectory = 
  +                parentDirectory.substring(0, parentDirectory.length() - 1);
  +        }
  +     int slash = parentDirectory.lastIndexOf("/");
  +     if (slash >= 0) {
  +         String parent = name.substring(0, slash);
  +         writer.print("<tr><td colspan=\"3\" bgcolor=\"#ffffff\">\r\n");
  +         writer.print("<a href=\"");
  +         writer.print(rewriteUrl(contextPath));
  +            if (parent.equals(""))
  +                parent = "/";
  +            if (contextPath.endsWith("/"))
  +                parent = parent.substring(1);
  +         writer.print(parent);
  +         writer.print("\">");
  +         writer.print(sm.getString("directory.parent", parent));
  +         writer.print("</a>\r\n");
  +         writer.print("</td></tr>\r\n");
  +     }
  +
  +     // Render the column headings
  +     writer.print("<tr bgcolor=\"#cccccc\">\r\n");
  +     writer.print("<td align=\"left\"><font size=\"+1\"><strong>");
  +     writer.print(sm.getString("directory.filename"));
  +     writer.print("</strong></font></td>\r\n");
  +     writer.print("<td align=\"center\"><font size=\"+1\"><strong>");
  +     writer.print(sm.getString("directory.size"));
  +     writer.print("</strong></font></td>\r\n");
  +     writer.print("<td align=\"right\"><font size=\"+1\"><strong>");
  +     writer.print(sm.getString("directory.lastModified"));
  +     writer.print("</strong></font></td>\r\n");
  +     writer.print("</tr>\r\n");
  +
  +        try {
  +
  +            // Render the directory entries within this directory
  +            DirContext directory = resourceInfo.directory;
  +            NamingEnumeration enum = 
  +                resourceInfo.resources.list(resourceInfo.path);
  +            boolean shade = false;
  +            while (enum.hasMoreElements()) {
  +
  +                NameClassPair ncPair = (NameClassPair) enum.nextElement();
  +                String resourceName = ncPair.getName();
  +                ResourceInfo childResourceInfo = 
  +                    new ResourceInfo(resourceName, directory);
  +
  +                String trimmed = resourceName.substring(trim);
  +                if (trimmed.equalsIgnoreCase("WEB-INF") ||
  +                    trimmed.equalsIgnoreCase("META-INF"))
  +                    continue;
  +
  +                writer.print("<tr");
  +                if (shade)
  +                    writer.print(" bgcolor=\"eeeeee\"");
  +                writer.print(">\r\n");
  +                shade = !shade;
  +
  +                writer.print("<td align=\"left\">&nbsp;&nbsp;\r\n");
  +                writer.print("<a href=\"");
  +                writer.print(rewriteUrl(contextPath));
  +                resourceName = rewriteUrl(resourceName);
  +                if ((contextPath.endsWith("/")) && (resourceName.length() > 0))
  +                    resourceName = resourceName.substring(1);
  +                writer.print(resourceName);
  +                writer.print("\"><tt>");
  +                writer.print(trimmed);
  +
  +                if (childResourceInfo.collection)
  +                    writer.print("/");
  +                writer.print("</tt></a></td>\r\n");
  +
  +                writer.print("<td align=\"right\"><tt>");
  +                if (childResourceInfo.collection)
  +                    writer.print("&nbsp;");
  +                else
  +                    writer.print(renderSize(childResourceInfo.length));
  +                writer.print("</tt></td>\r\n");
  +
  +                writer.print("<td align=\"right\"><tt>");
  +                writer.print(renderLastModified(childResourceInfo.date));
  +                writer.print("</tt></td>\r\n");
  +
  +                writer.print("</tr>\r\n");
  +            }
  +
  +        } catch (NamingException e) {
  +            // Something went wrong
  +            e.printStackTrace();
  +        }
  +
  +     // Render the page footer
  +     writer.print("<tr><td colspan=\"3\">&nbsp;</td></tr>\r\n");
  +     writer.print("<tr><td colspan=\"3\" bgcolor=\"#cccccc\">");
  +     writer.print("<font size=\"-1\">");
  +     writer.print(Globals.SERVER_INFO);
  +     writer.print("</font></td></tr>\r\n");
  +     writer.print("</table>\r\n");
  +     writer.print("</body>\r\n");
  +     writer.print("</html>\r\n");
  +
  +     // Return an input stream to the underlying bytes
  +     writer.flush();
  +     return (new ByteArrayInputStream(stream.toByteArray()));
  +
  +    }
  +
  +
  +    /**
  +     * Render the last modified date and time for the specified timestamp.
  +     *
  +     * @param lastModified Last modified date and time, in milliseconds since
  +     *  the epoch
  +     */
  +    private String renderLastModified(long lastModified) {
  +
  +     return (formats[0].format(new Date(lastModified)));
  +
  +    }
  +
  +
  +    /**
  +     * Render the specified file size (in bytes).
  +     *
  +     * @param size File size (in bytes)
  +     */
  +    private String renderSize(long size) {
  +
  +     long leftSide = size / 1024;
  +     long rightSide = (size % 1024) / 103;   // Makes 1 digit
  +     if ((leftSide == 0) && (rightSide == 0) && (size > 0))
  +         rightSide = 1;
  +
  +     return ("" + leftSide + "." + rightSide + " kb");
  +
  +    }
  +
  +
  +    /**
  +     * URL rewriter.
  +     * 
  +     * @param path Path which has to be rewiten
  +     */
  +    private String rewriteUrl(String path) {
  +        
  +        String normalized = path;
  +        
  +     // Replace " " with "%20"
  +     while (true) {
  +         int index = normalized.indexOf(" ");
  +         if (index < 0)
  +             break;
  +         normalized = normalized.substring(0, index) + "%20"
  +             + normalized.substring(index + 1);
  +     }
  +
  +        return normalized;
  +        
  +    }
  +    
  +    
       // ------------------------------------------------------ Range Inner Class
   
   
  @@ -1686,22 +1966,62 @@
            * 
            * @param pathname Path name of the file
            */
  -        public ResourceInfo(String path, Resources resources) {
  +        public ResourceInfo(String path, DirContext resources) {
               
               this.path = path;
               this.resources = resources;
  -            this.exists = resources.exists(path);
  +            exists = true;
  +            try {
  +                object = resources.lookup(path);
  +                if (object instanceof Resource) {
  +                    file = (Resource) object;
  +                    is = file.streamContent();
  +                    collection = false;
  +                } else if (object instanceof DirContext) {
  +                    directory = (DirContext) object;
  +                    collection = true;
  +                } else {
  +                    // Don't know how to serve another object type
  +                    exists = false;
  +                }
  +            } catch (NamingException e) {
  +                exists = false;
  +            } catch (IOException e) {
  +                exists = false;
  +            }
               if (exists) {
  -                this.creationDate = resources.getResourceCreated(path);
  -                this.date = resources.getResourceModified(path);
  -                this.httpDate = formats[0].format(new Date(date));
  -                this.length = resources.getResourceLength(path);
  -                this.collection = resources.isCollection(path);
  +                try {
  +                    attributes = resources.getAttributes(path);
  +                    if (attributes instanceof ResourceAttributes) {
  +                        ResourceAttributes tempAttrs =
  +                            (ResourceAttributes) attributes;
  +                        Date tempDate = tempAttrs.getCreationDate();
  +                        if (tempDate != null)
  +                            creationDate = tempDate.getTime();
  +                        tempDate = tempAttrs.getLastModified();
  +                        if (tempDate != null) {
  +                            date = tempDate.getTime();
  +                            httpDate = formats[0].format(tempDate);
  +                        } else {
  +                            httpDate = formats[0].format(new Date());
  +                        }
  +                        length = tempAttrs.getContentLength();
  +                    }
  +                } catch (NamingException e) {
  +                    // Shouldn't happen, the implementation of the DirContext
  +                    // is probably broken
  +                    exists = false;
  +                }
               }
   
           }
   
   
  +        public Object object = null;
  +        public DirContext directory = null;;
  +        public Resource file = null;
  +        public InputStream is = null;
  +        public Attributes attributes = null;
           public String path;
           public long creationDate;
           public String httpDate;
  @@ -1709,7 +2029,7 @@
           public long length;
           public boolean collection;
           public boolean exists;
  -        public Resources resources;
  +        public DirContext resources;
   
   
           /**
  
  
  
  1.8       +228 -88   
jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/WebdavServlet.java
  
  Index: WebdavServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/WebdavServlet.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- WebdavServlet.java        2001/01/08 20:57:11     1.7
  +++ WebdavServlet.java        2001/01/12 06:55:35     1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/WebdavServlet.java,v
 1.7 2001/01/08 20:57:11 remm Exp $
  - * $Revision: 1.7 $
  - * $Date: 2001/01/08 20:57:11 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/WebdavServlet.java,v
 1.8 2001/01/12 06:55:35 remm Exp $
  + * $Revision: 1.8 $
  + * $Date: 2001/01/12 06:55:35 $
    *
    * ====================================================================
    *
  @@ -88,6 +88,11 @@
   import java.text.SimpleDateFormat;
   import java.security.MessageDigest;
   import java.security.NoSuchAlgorithmException;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.NodeList;
  +import org.w3c.dom.Element;
  +import org.w3c.dom.Document;
  +import org.xml.sax.InputSource;
   import javax.servlet.RequestDispatcher;
   import javax.servlet.ServletException;
   import javax.servlet.ServletContext;
  @@ -95,25 +100,23 @@
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
  -import org.w3c.dom.Node;
  -import org.w3c.dom.NodeList;
  -import org.w3c.dom.Element;
  -import org.w3c.dom.Document;
  -import org.xml.sax.InputSource;
   import javax.xml.parsers.DocumentBuilder;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
  -import org.apache.catalina.Resources;
  -import org.apache.catalina.core.ApplicationContext;
  -import org.apache.catalina.resources.ResourceBean;
  -import org.apache.catalina.resources.DirectoryBean;
  +import javax.naming.NamingException;
  +import javax.naming.InitialContext;
  +import javax.naming.Context;
  +import javax.naming.NamingEnumeration;
  +import javax.naming.NameClassPair;
  +import javax.naming.directory.DirContext;
  +import javax.naming.directory.Attribute;
  +import javax.naming.directory.Attributes;
  +import org.apache.naming.resources.Resource;
  +import org.apache.naming.resources.ResourceAttributes;
   import org.apache.catalina.util.MD5Encoder;
   import org.apache.catalina.util.StringManager;
   import org.apache.catalina.util.XMLWriter;
   import org.apache.catalina.util.DOMWriter;
  -import org.apache.catalina.util.xml.SaxContext;
  -import org.apache.catalina.util.xml.XmlAction;
  -import org.apache.catalina.util.xml.XmlMapper;
   
   
   /**
  @@ -121,7 +124,7 @@
    * are handled by the DefaultServlet.
    *
    * @author Remy Maucherat
  - * @version $Revision: 1.7 $ $Date: 2001/01/08 20:57:11 $
  + * @version $Revision: 1.8 $ $Date: 2001/01/12 06:55:35 $
    */
   
   public class WebdavServlet
  @@ -351,12 +354,23 @@
        resp.addHeader("DAV", "1,2");
           String methodsAllowed = null;
           
  -        // Retrieve the Catalina context
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  +        // Retrieve the resources
  +        DirContext resources = getResources();
           
  -        Resources resources = context.getResources();
  +        if (resources == null) {
  +            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  +            return;
  +        }
           
  -     if (!resources.exists(path)) {
  +        boolean exists = true;
  +        Object object = null;
  +        try {
  +            object = resources.lookup(path);
  +        } catch (NamingException e) {
  +            exists = false;
  +        }
  +
  +     if (!exists) {
            methodsAllowed = "OPTIONS, MKCOL, PUT, LOCK";
               resp.addHeader("Allow", methodsAllowed);
               return;
  @@ -364,7 +378,7 @@
           
           methodsAllowed = "OPTIONS, GET, HEAD, POST, DELETE, TRACE, " 
               + "PROPFIND, PROPPATCH, COPY, MOVE, LOCK, UNLOCK";
  -        if (!resources.isCollection(path)) {
  +        if (!(object instanceof DirContext)) {
               methodsAllowed += ", PUT";
           }
           
  @@ -467,12 +481,23 @@
               
           }
           
  -        // Retrieve the Catalina context
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  -
  -        Resources resources = context.getResources();
  +        // Retrieve the resources
  +        DirContext resources = getResources();
           
  -     if (!resources.exists(path)) {
  +        if (resources == null) {
  +            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  +            return;
  +        }
  +        
  +        boolean exists = true;
  +        Object object = null;
  +        try {
  +            object = resources.lookup(path);
  +        } catch (NamingException e) {
  +            exists = false;
  +        }
  +        
  +     if (!exists) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND, path);
            return;
        }
  @@ -504,12 +529,26 @@
                   parseProperties(req, resources, generatedXML, currentPath, 
                                   type, properties);
                   
  -                if (resources.isCollection(currentPath)) {
  -                    String[] children = 
  -                        resources.getCollectionMembers(currentPath);
  -                    
  -                    for (int i=0; i<children.length; i++) {
  -                        stackBelow.push(children[i]);
  +                try {
  +                    object = resources.lookup(currentPath);
  +                } catch (NamingException e) {
  +                    resp.sendError
  +                        (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
  +                    return;
  +                }
  +                if (object instanceof DirContext) {
  +                    try {
  +                        NamingEnumeration enum = resources.list(currentPath);
  +                        while (enum.hasMoreElements()) {
  +                            NameClassPair ncPair = 
  +                                (NameClassPair) enum.nextElement();
  +                            stackBelow.push(ncPair.getName());
  +                        }
  +                    } catch (NamingException e) {
  +                        resp.sendError
  +                            (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 
  +                             path);
  +                        return;
                       }
                       
                       if (depth > 0) {
  @@ -590,19 +629,35 @@
           
           String path = getRelativePath(req);
           
  -        // Retrieve the Catalina context
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  -
  -        Resources resources = context.getResources();
  +        // Retrieve the resources
  +        DirContext resources = getResources();
           
  +        if (resources == null) {
  +            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  +            return;
  +        }
  +        
  +        boolean exists = true;
  +        Object object = null;
  +        try {
  +            object = resources.lookup(path);
  +        } catch (NamingException e) {
  +            exists = false;
  +        }
  +
        // Can't create a collection if a resource already exists at the given
           // path
  -     if (resources.exists(path)) {
  +     if (exists) {
            resp.sendError(WebdavStatus.SC_METHOD_NOT_ALLOWED);
            return;
        }
   
  -        boolean result = resources.createCollection(path);
  +        boolean result = true;
  +        try {
  +            resources.createSubcontext(path);
  +        } catch (NamingException e) {
  +            result = false;
  +        }
           
           if (!result) {
               resp.sendError(WebdavStatus.SC_CONFLICT,
  @@ -910,11 +965,22 @@
           
           lock.path = path;
           
  -        // Retrieve the Catalina context
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  +        // Retrieve the resources
  +        DirContext resources = getResources();
           
  -        Resources resources = context.getResources();
  +        if (resources == null) {
  +            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  +            return;
  +        }
           
  +        boolean exists = true;
  +        Object object = null;
  +        try {
  +            object = resources.lookup(path);
  +        } catch (NamingException e) {
  +            exists = false;
  +        }
  +        
           Enumeration locksList = null;
           
           if (lockRequestType == LOCK_CREATION) {
  @@ -928,7 +994,7 @@
               String lockToken = 
                   md5Encoder.encode(md5Helper.digest(lockTokenStr.getBytes()));
               
  -            if ( (resources.exists(path)) && (resources.isCollection(path)) && 
  +            if ( (exists) && (object instanceof DirContext) && 
                    (lock.depth == INFINITY) ) {
                   
                   // Locking a collection (and all its member resources)
  @@ -1054,7 +1120,7 @@
                   LockInfo presentLock = (LockInfo) resourceLocks.get(lock.path);
                   if (presentLock != null) {
                       
  -                    if ( (presentLock.isExclusive()) || (lock.isExclusive()) ) {
  +                    if ((presentLock.isExclusive()) || (lock.isExclusive())) {
                           // If either lock is exclusive, the lock can't be 
                           // granted
                           resp.sendError(WebdavStatus.SC_PRECONDITION_FAILED);
  @@ -1070,7 +1136,13 @@
                       resourceLocks.put(lock.path, lock);
                       
                       // Checking if a resource exists at this path
  -                    if (!resources.exists(lock.path)) {
  +                    exists = true;
  +                    try {
  +                        object = resources.lookup(path);
  +                    } catch (NamingException e) {
  +                        exists = false;
  +                    }
  +                    if (!exists) {
                           
                           // "Creating" a lock-null resource
                           int slash = lock.path.lastIndexOf('/');
  @@ -1413,15 +1485,25 @@
           
           // Overwriting the destination
           
  -        // Retrieve the Catalina context
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  +        // Retrieve the resources
  +        DirContext resources = getResources();
           
  -        Resources resources = context.getResources();
  +        if (resources == null) {
  +            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  +            return false;
  +        }
  +        
  +        boolean exists = true;
  +        try {
  +            resources.lookup(destinationPath);
  +        } catch (NamingException e) {
  +            exists = false;
  +        }
           
           if (overwrite) {
               
               // Delete destination resource, if it exists
  -            if (resources.exists(destinationPath)) {
  +            if (exists) {
                   if (!deleteResource(destinationPath, req, resp)) {
                       return false;
                   } else {
  @@ -1434,7 +1516,7 @@
           } else {
               
               // If the destination exists, then it's a conflict
  -            if (resources.exists(destinationPath)) {
  +            if (exists) {
                   resp.sendError(WebdavStatus.SC_PRECONDITION_FAILED);
                   return false;
               }
  @@ -1473,28 +1555,51 @@
        * @param source Path of the resource to be copied
        * @param dest Destination path
        */
  -    private boolean copyResource(Resources resources, Hashtable errorList,
  +    private boolean copyResource(DirContext resources, Hashtable errorList,
                                    String source, String dest) {
           
  -        if (resources.isCollection(source)) {
  +        Object object = null;
  +        try {
  +            object = resources.lookup(source);
  +        } catch (NamingException e) {
  +        }
  +        
  +        if (object instanceof DirContext) {
               
  -            if (!resources.createCollection(dest)) {
  +            try {
  +                resources.createSubcontext(dest);
  +            } catch (NamingException e) {
                   errorList.put
  -                    (dest, 
  -                     new Integer(WebdavStatus.SC_CONFLICT));
  +                    (dest, new Integer(WebdavStatus.SC_CONFLICT));
                   return false;
               }
  -            String[] members = resources.getCollectionMembers(source);
  -            for (int i=0; i<members.length; i++) {
  -                String childDest = dest + 
  -                    members[i].substring(source.length());
  -                copyResource(resources, errorList, members[i], childDest);
  +            
  +            try {
  +                NamingEnumeration enum = resources.list(source);
  +                while (enum.hasMoreElements()) {
  +                    NameClassPair ncPair = (NameClassPair) enum.nextElement();
  +                    String childDest = dest + ncPair.getName();
  +                    copyResource(resources, errorList, 
  +                                 source + ncPair.getName(), childDest);
  +                }
  +            } catch (NamingException e) {
  +                errorList.put
  +                    (dest, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
  +                return false;
               }
               
           } else {
               
  -            InputStream is = resources.getResourceAsStream(source);
  -            if (!resources.setResource(dest, is)) {
  +            if (object instanceof Resource) {
  +                try {
  +                    resources.bind(dest, object);
  +                } catch (NamingException e) {
  +                    errorList.put
  +                        (source, 
  +                         new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
  +                    return false;
  +                }
  +            } else {
                   errorList.put
                       (source, 
                        new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
  @@ -1550,20 +1655,33 @@
               return false;
           }
           
  -        // Retrieve the Catalina context
  -        ApplicationContext context = (ApplicationContext) getServletContext();
  -
  -        Resources resources = context.getResources();
  +        // Retrieve the resources
  +        DirContext resources = getResources();
  +        
  +        if (resources == null) {
  +            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  +            return false;
  +        }
           
  -     if (!resources.exists(path)) {
  +        boolean exists = true;
  +        Object object = null;
  +        try {
  +            object = resources.lookup(path);
  +        } catch (NamingException e) {
  +            exists = false;
  +        }
  +        
  +     if (!exists) {
               resp.sendError(WebdavStatus.SC_NOT_FOUND);
            return false;
        }
           
  -        boolean collection = resources.isCollection(path);
  +        boolean collection = (object instanceof DirContext);
           
           if (!collection) {
  -            if (!resources.deleteResource(path)) {
  +            try {
  +                resources.unbind(path);
  +            } catch (NamingException e) {
                   resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                   return false;
               }
  @@ -1572,7 +1690,12 @@
               Hashtable errorList = new Hashtable();
               
               deleteCollection(req, resources, path, errorList);
  -            resources.deleteResource(path);
  +            try {
  +                resources.unbind(path);
  +            } catch (NamingException e) {
  +                errorList.put(path, new Integer
  +                    (WebdavStatus.SC_INTERNAL_SERVER_ERROR));
  +            }
               
               if (!errorList.isEmpty()) {
                   
  @@ -1596,7 +1719,8 @@
        * @param path Path to the collection to be deleted
        * @param errorList Contains the list of the errors which occured
        */
  -    private void deleteCollection(HttpServletRequest req, Resources resources, 
  +    private void deleteCollection(HttpServletRequest req, 
  +                                  DirContext resources, 
                                     String path, Hashtable errorList) {
           
           String ifHeader = req.getHeader("If");
  @@ -1607,31 +1731,47 @@
           if (lockTokenHeader == null)
               lockTokenHeader = "";
           
  -        String[] members = resources.getCollectionMembers(path);
  +        Enumeration enum = null;
  +        try {
  +            enum = resources.list(path);
  +        } catch (NamingException e) {
  +            errorList.put(path, new Integer
  +                (WebdavStatus.SC_INTERNAL_SERVER_ERROR));
  +            return;
  +        }
           
  -        for (int i=0; i<members.length; i++) {
  +        while (enum.hasMoreElements()) {
  +            NameClassPair ncPair = (NameClassPair) enum.nextElement();
  +            String childName = path + ncPair.getName();
               
  -            if (isLocked(members[i], ifHeader + lockTokenHeader)) {
  +            if (isLocked(childName, ifHeader + lockTokenHeader)) {
                   
  -                errorList.put(members[i], new Integer(WebdavStatus.SC_LOCKED));
  +                errorList.put(childName, new Integer(WebdavStatus.SC_LOCKED));
                   
               } else {
                   
  -                if (resources.isCollection(members[i])) {
  -                    deleteCollection(req, resources, members[i], errorList);
  -                }
  -                
  -                boolean result = resources.deleteResource(members[i]);
  -                if (!result) {
  -                    if (!resources.isCollection(members[i])) {
  -                        // If it's not a collection, then it's an unknown
  -                        // error
  -                        errorList.put
  -                            (members[i], new Integer
  -                                (WebdavStatus.SC_INTERNAL_SERVER_ERROR));
  +                try {
  +                    Object object = resources.lookup(childName);
  +                    if (object instanceof DirContext) {
  +                        deleteCollection(req, resources, childName, errorList);
                       }
  +                    
  +                    try {
  +                        resources.unbind(childName);
  +                    } catch (NamingException e) {
  +                        if (!(object instanceof DirContext)) {
  +                            // If it's not a collection, then it's an unknown
  +                            // error
  +                            errorList.put
  +                                (childName, new Integer
  +                                    (WebdavStatus.SC_INTERNAL_SERVER_ERROR));
  +                        }
  +                    }
  +                } catch (NamingException e) {
  +                    errorList.put
  +                        (childName, new Integer
  +                            (WebdavStatus.SC_INTERNAL_SERVER_ERROR));
                   }
  -                
               }
               
           }
  @@ -1707,7 +1847,7 @@
        * name, then this Vector contains those properties
        */
       private void parseProperties(HttpServletRequest req,
  -                                 Resources resources, XMLWriter generatedXML,
  +                                 DirContext resources, XMLWriter generatedXML,
                                    String path, int type, 
                                    Vector propertiesVector) {
           
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to