Author: remy.maucherat(a)jboss.com
Date: 2007-08-31 12:57:27 -0400 (Fri, 31 Aug 2007)
New Revision: 249
Modified:
   trunk/java/org/apache/catalina/servlets/WebdavServlet.java
Log:
- Webdav servlet cleanup.
- Add some relative path tweaks.
Modified: trunk/java/org/apache/catalina/servlets/WebdavServlet.java
===================================================================
--- trunk/java/org/apache/catalina/servlets/WebdavServlet.java	2007-08-31 14:18:01 UTC
(rev 248)
+++ trunk/java/org/apache/catalina/servlets/WebdavServlet.java	2007-08-31 16:57:27 UTC
(rev 249)
@@ -44,6 +44,7 @@
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
+import org.apache.catalina.Globals;
 import org.apache.catalina.util.DOMWriter;
 import org.apache.catalina.util.MD5Encoder;
 import org.apache.catalina.util.RequestUtil;
@@ -66,7 +67,7 @@
  * are handled by the DefaultServlet.
  *
  * @author Remy Maucherat
- * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
+ * @version $Revision: 561186 $ $Date: 2007-07-31 01:37:47 +0200 (mar., 31 juil. 2007) $
  */
 
 public class WebdavServlet
@@ -174,7 +175,8 @@
      * Key : path <br>
      * Value : LockInfo
      */
-    private Hashtable resourceLocks = new Hashtable();
+    private Hashtable<String,LockInfo> resourceLocks =
+        new Hashtable<String,LockInfo>();
 
 
     /**
@@ -185,7 +187,8 @@
      * collection. Each element of the Vector is the path associated with
      * the lock-null resource.
      */
-    private Hashtable lockNullResources = new Hashtable();
+    private Hashtable<String,Vector<String>> lockNullResources =
+        new Hashtable<String,Vector<String>>();
 
 
     /**
@@ -194,7 +197,7 @@
      * Key : path <br>
      * Value : LockInfo
      */
-    private Vector collectionLocks = new Vector();
+    private Vector<LockInfo> collectionLocks = new Vector<LockInfo>();
 
 
     /**
@@ -310,6 +313,35 @@
 
 
     /**
+     * Override the DefaultServlet implementation and only use the PathInfo. If
+     * the ServletPath is non-null, it will be because the WebDAV servlet has
+     * been mapped to a url other than /* to configure editing at different url
+     * than normal viewing.
+     *
+     * @param request The servlet request we are processing
+     */
+    protected String getRelativePath(HttpServletRequest request) {
+
+        // Are we being processed by a RequestDispatcher.include()?
+        if (request.getAttribute(Globals.INCLUDE_REQUEST_URI_ATTR) != null) {
+            String result = (String) request.getAttribute(
+                                            Globals.INCLUDE_PATH_INFO_ATTR);
+            if ((result == null) || (result.equals("")))
+                result = "/";
+            return (result);
+        }
+
+        // No, extract the desired path directly from the request
+        String result = request.getPathInfo();
+        if ((result == null) || (result.equals(""))) {
+            result = "/";
+        }
+        return (result);
+
+    }
+
+
+    /**
      * OPTIONS Method.
      *
      * @param req The request
@@ -358,7 +390,7 @@
         }
 
         // Properties which are to be displayed.
-        Vector properties = null;
+        Vector<String> properties = null;
         // Propfind depth
         int depth = INFINITY;
         // Propfind type
@@ -379,44 +411,46 @@
         }
 
         Node propNode = null;
-
-        DocumentBuilder documentBuilder = getDocumentBuilder();
-
-        try {
-            Document document = documentBuilder.parse
-                (new InputSource(req.getInputStream()));
-
-            // Get the root element of the document
-            Element rootElement = document.getDocumentElement();
-            NodeList childList = rootElement.getChildNodes();
-
-            for (int i=0; i < childList.getLength(); i++) {
-                Node currentNode = childList.item(i);
-                switch (currentNode.getNodeType()) {
-                case Node.TEXT_NODE:
-                    break;
-                case Node.ELEMENT_NODE:
-                    if (currentNode.getNodeName().endsWith("prop")) {
-                        type = FIND_BY_PROPERTY;
-                        propNode = currentNode;
+        
+        if (req.getInputStream().available() > 0) {
+            DocumentBuilder documentBuilder = getDocumentBuilder();
+    
+            try {
+                Document document = documentBuilder.parse
+                    (new InputSource(req.getInputStream()));
+    
+                // Get the root element of the document
+                Element rootElement = document.getDocumentElement();
+                NodeList childList = rootElement.getChildNodes();
+    
+                for (int i=0; i < childList.getLength(); i++) {
+                    Node currentNode = childList.item(i);
+                    switch (currentNode.getNodeType()) {
+                    case Node.TEXT_NODE:
+                        break;
+                    case Node.ELEMENT_NODE:
+                        if (currentNode.getNodeName().endsWith("prop")) {
+                            type = FIND_BY_PROPERTY;
+                            propNode = currentNode;
+                        }
+                        if (currentNode.getNodeName().endsWith("propname")) {
+                            type = FIND_PROPERTY_NAMES;
+                        }
+                        if (currentNode.getNodeName().endsWith("allprop")) {
+                            type = FIND_ALL_PROP;
+                        }
+                        break;
                     }
-                    if (currentNode.getNodeName().endsWith("propname")) {
-                        type = FIND_PROPERTY_NAMES;
-                    }
-                    if (currentNode.getNodeName().endsWith("allprop")) {
-                        type = FIND_ALL_PROP;
-                    }
-                    break;
                 }
+            } catch (SAXException e) {
+                // Something went wrong - use the defaults.
+            } catch (IOException e) {
+                // Something went wrong - use the defaults.
             }
-        } catch (SAXException e) {
-            // Most likely there was no content : we use the defaults.
-        } catch (IOException e) {
-            // Most likely there was no content : we use the defaults.
         }
 
         if (type == FIND_BY_PROPERTY) {
-            properties = new Vector();
+            properties = new Vector<String>();
             NodeList childList = propNode.getChildNodes();
 
             for (int i=0; i < childList.getLength(); i++) {
@@ -504,11 +538,11 @@
                             properties);
         } else {
             // The stack always contains the object of the current level
-            Stack stack = new Stack();
+            Stack<String> stack = new Stack<String>();
             stack.push(path);
 
             // Stack of the objects one level below
-            Stack stackBelow = new Stack();
+            Stack<String> stackBelow = new Stack<String>();
 
             while ((!stack.isEmpty()) && (depth >= 0)) {
 
@@ -567,7 +601,7 @@
                 if (stack.isEmpty()) {
                     depth--;
                     stack = stackBelow;
-                    stackBelow = new Stack();
+                    stackBelow = new Stack<String>();
                 }
 
                 generatedXML.sendData();
@@ -1017,7 +1051,7 @@
 
                 // Checking if a child resource of this collection is
                 // already locked
-                Vector lockPaths = new Vector();
+                Vector<String> lockPaths = new Vector<String>();
                 locksList = collectionLocks.elements();
                 while (locksList.hasMoreElements()) {
                     LockInfo currentLock = (LockInfo) locksList.nextElement();
@@ -1164,10 +1198,10 @@
                         int slash = lock.path.lastIndexOf('/');
                         String parentPath = lock.path.substring(0, slash);
 
-                        Vector lockNulls =
-                            (Vector) lockNullResources.get(parentPath);
+                        Vector<String> lockNulls =
+                            lockNullResources.get(parentPath);
                         if (lockNulls == null) {
-                            lockNulls = new Vector();
+                            lockNulls = new Vector<String>();
                             lockNullResources.put(parentPath, lockNulls);
                         }
 
@@ -1636,7 +1670,7 @@
 
         // Copying source to destination
 
-        Hashtable errorList = new Hashtable();
+        Hashtable<String,Integer> errorList = new
Hashtable<String,Integer>();
 
         boolean result = copyResource(resources, errorList,
                                       path, destinationPath);
@@ -1666,8 +1700,8 @@
      * @param source Path of the resource to be copied
      * @param dest Destination path
      */
-    private boolean copyResource(DirContext resources, Hashtable errorList,
-                                 String source, String dest) {
+    private boolean copyResource(DirContext resources,
+            Hashtable<String,Integer> errorList, String source, String dest) {
 
         if (debug > 1)
             log("Copy: " + source + " To: " + dest);
@@ -1807,7 +1841,8 @@
             }
         } else {
 
-            Hashtable errorList = new Hashtable();
+            Hashtable<String,Integer> errorList =
+                new Hashtable<String,Integer>();
 
             deleteCollection(req, resources, path, errorList);
             try {
@@ -1842,7 +1877,8 @@
      */
     private void deleteCollection(HttpServletRequest req,
                                   DirContext resources,
-                                  String path, Hashtable errorList) {
+                                  String path,
+                                  Hashtable<String,Integer> errorList) {
 
         if (debug > 1)
             log("Delete:" + path);
@@ -1983,7 +2019,7 @@
     private void parseProperties(HttpServletRequest req,
                                  XMLWriter generatedXML,
                                  String path, int type,
-                                 Vector propertiesVector) {
+                                 Vector<String> propertiesVector) {
 
         // Exclude any resource in the /WEB-INF and /META-INF subdirectories
         // (the "toUpperCase()" avoids problems on Windows systems)
@@ -2119,14 +2155,14 @@
 
         case FIND_BY_PROPERTY :
 
-            Vector propertiesNotFound = new Vector();
+            Vector<String> propertiesNotFound = new Vector<String>();
 
             // Parse the list of properties
 
             generatedXML.writeElement(null, "propstat", XMLWriter.OPENING);
             generatedXML.writeElement(null, "prop", XMLWriter.OPENING);
 
-            Enumeration properties = propertiesVector.elements();
+            Enumeration<String> properties = propertiesVector.elements();
 
             while (properties.hasMoreElements()) {
 
@@ -2396,7 +2432,7 @@
 
         case FIND_BY_PROPERTY :
 
-            Vector propertiesNotFound = new Vector();
+            Vector<String> propertiesNotFound = new Vector<String>();
 
             // Parse the list of properties
 
@@ -2645,7 +2681,7 @@
         String scope = "exclusive";
         int depth = 0;
         String owner = "";
-        Vector tokens = new Vector();
+        Vector<String> tokens = new Vector<String>();
         long expiresAt = 0;
         Date creationDate = new Date();
 
@@ -2742,20 +2778,6 @@
     }
 
 
-    // --------------------------------------------------- Property Inner Class
-
-
-    private class Property {
-
-        public String name;
-        public String value;
-        public String namespace;
-        public String namespaceAbbrev;
-        public int status = WebdavStatus.SC_OK;
-
-    }
-
-
 };
 
 
@@ -2782,7 +2804,8 @@
      * status codes to descriptive text.  This is a static
      * variable.
      */
-    private static Hashtable mapStatusCodes = new Hashtable();
+    private static Hashtable<Integer,String> mapStatusCodes =
+        new Hashtable<Integer,String>();
 
 
     // ------------------------------------------------------ HTTP Status Codes