remm        01/01/13 13:24:48

  Modified:    catalina/src/share/org/apache/catalina/servlets
                        DefaultServlet.java WebdavServlet.java
  Log:
  - Fixes all known issues with file serving and WebDAV.
  - COPY, MOVE, DELETE now work again.
  - All the resources served / browsed / written are now accessed
    through JNDI. The initial DirContext itself is retrieved inside the naming
    environment (java:/comp/Resources), or inside the ServletContext if
    naming is disabled.
  - There are still some dependencies with Catalina, though : four classes from
    the util package (which are already independent from Catalina), and one
    constant from the Globals interface. It takes two minutes to remove them,
    although I anticipate that these shared servlets will be moved to a separate
    Tomcat subproject.
  
  Revision  Changes    Path
  1.21      +87 -36    
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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- DefaultServlet.java       2001/01/13 05:20:05     1.20
  +++ DefaultServlet.java       2001/01/13 21:24:48     1.21
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java,v
 1.20 2001/01/13 05:20:05 remm Exp $
  - * $Revision: 1.20 $
  - * $Date: 2001/01/13 05:20:05 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java,v
 1.21 2001/01/13 21:24:48 remm Exp $
  + * $Revision: 1.21 $
  + * $Date: 2001/01/13 21:24:48 $
    *
    * ====================================================================
    *
  @@ -118,7 +118,7 @@
    *
    * @author Craig R. McClanahan
    * @author Remy Maucherat
  - * @version $Revision: 1.20 $ $Date: 2001/01/13 05:20:05 $
  + * @version $Revision: 1.21 $ $Date: 2001/01/13 21:24:48 $
    */
   
   public class DefaultServlet
  @@ -444,7 +444,7 @@
           }
   
           // No, extract the desired path directly from the request
  -        String result = request.getPathInfo();
  +        String result = normalize(request.getPathInfo());
           if (result == null) {
               result = request.getServletPath();
           }
  @@ -818,6 +818,9 @@
        */
       protected String normalize(String path) {
   
  +        if (path == null)
  +            return null;
  +
        String normalized = path;
   
        // Resolve encoded characters in the normalized path,
  @@ -911,7 +914,7 @@
           IOException exception = null;
               
           // FIXME : i18n ?
  -        InputStream resourceInputStream = resourceInfo.is;
  +        InputStream resourceInputStream = resourceInfo.getStream();
           InputStream istream = new BufferedInputStream
               (resourceInputStream, input);
           
  @@ -947,7 +950,7 @@
   
           IOException exception = null;
               
  -        InputStream resourceInputStream = resourceInfo.is;
  +        InputStream resourceInputStream = resourceInfo.getStream();
           // FIXME : i18n ?
           Reader reader = new InputStreamReader(resourceInputStream);
           
  @@ -984,7 +987,7 @@
           
           IOException exception = null;
           
  -        InputStream resourceInputStream = resourceInfo.is;
  +        InputStream resourceInputStream = resourceInfo.getStream();
           InputStream istream =
               new BufferedInputStream(resourceInputStream, input);
           exception = copyRange(istream, ostream, range.start, range.end);
  @@ -1019,7 +1022,7 @@
           
           IOException exception = null;
           
  -        InputStream resourceInputStream = resourceInfo.is;
  +        InputStream resourceInputStream = resourceInfo.getStream();
           Reader reader = new InputStreamReader(resourceInputStream);
           exception = copyRange(reader, writer, range.start, range.end);
           
  @@ -1056,7 +1059,7 @@
           
           while ( (exception == null) && (ranges.hasMoreElements()) ) {
               
  -            InputStream resourceInputStream = resourceInfo.is;
  +            InputStream resourceInputStream = resourceInfo.getStream();
               InputStream istream =    // FIXME: internationalization???????
                   new BufferedInputStream(resourceInputStream, input);
           
  @@ -1111,7 +1114,7 @@
           
           while ( (exception == null) && (ranges.hasMoreElements()) ) {
               
  -            InputStream resourceInputStream = resourceInfo.is;
  +            InputStream resourceInputStream = resourceInfo.getStream();
               Reader reader = new InputStreamReader(resourceInputStream);
           
               Range currentRange = (Range) ranges.nextElement();
  @@ -1391,10 +1394,9 @@
   
        // Exclude any resource in the /WEB-INF and /META-INF subdirectories
        // (the "toUpperCase()" avoids problems on Windows systems)
  -        String normalizedPath = normalize(path);
  -     if ((normalizedPath == null) ||
  -            normalizedPath.toUpperCase().startsWith("/WEB-INF") ||
  -         normalizedPath.toUpperCase().startsWith("/META-INF")) {
  +     if ((path == null) ||
  +            path.toUpperCase().startsWith("/WEB-INF") ||
  +         path.toUpperCase().startsWith("/META-INF")) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, path);
            return;
        }
  @@ -1519,8 +1521,8 @@
                   
                   if (content) {
                       // Serve the directory browser
  -                    resourceInfo.is = 
  -                        render(request.getContextPath(), resourceInfo);
  +                    resourceInfo.setStream
  +                        (render(request.getContextPath(), resourceInfo));
                   }
                   
               }
  @@ -1944,6 +1946,12 @@
                        && (start <= end) && (end < length) );
           }
           
  +        public void recycle() {
  +            start = 0;
  +            end = 0;
  +            length = 0;
  +        }
  +        
       }
   
   
  @@ -1959,7 +1967,46 @@
            * @param pathname Path name of the file
            */
           public ResourceInfo(String path, DirContext resources) {
  +            set(path, resources);
  +        }
  +
  +
  +        public Object object;
  +        public DirContext directory;
  +        public Resource file;
  +        public Attributes attributes;
  +        public String path;
  +        public long creationDate;
  +        public String httpDate;
  +        public long date;
  +        public long length;
  +        public boolean collection;
  +        public boolean exists;
  +        public DirContext resources;
  +        protected InputStream is;
  +
  +
  +        public void recycle() {
  +            object = null;
  +            directory = null;
  +            file = null;
  +            attributes = null;
  +            path = null;
  +            creationDate = 0;
  +            httpDate = null;
  +            date = 0;
  +            length = -1;
  +            collection = true;
  +            exists = false;
  +            resources = null;
  +            is = null;
  +        }
  +
  +
  +        public void set(String path, DirContext resources) {
               
  +            recycle();
  +            
               this.path = path;
               this.resources = resources;
               exists = true;
  @@ -1967,7 +2014,6 @@
                   object = resources.lookup(path);
                   if (object instanceof Resource) {
                       file = (Resource) object;
  -                    is = file.streamContent();
                       collection = false;
                   } else if (object instanceof DirContext) {
                       directory = (DirContext) object;
  @@ -1978,8 +2024,6 @@
                   }
               } catch (NamingException e) {
                   exists = false;
  -            } catch (IOException e) {
  -                exists = false;
               }
               if (exists) {
                   try {
  @@ -2005,25 +2049,10 @@
                       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;
  -        public long date;
  -        public long length;
  -        public boolean collection;
  -        public boolean exists;
  -        public DirContext resources;
  -
  -
           /**
            * Test if the associated resource exists.
            */
  @@ -2037,6 +2066,28 @@
            */
           public String toString() {
               return path;
  +        }
  +
  +
  +        /**
  +         * Set IS.
  +         */
  +        public void setStream(InputStream is) {
  +            this.is = is;
  +        }
  +
  +
  +        /**
  +         * Get IS from resource.
  +         */
  +        public InputStream getStream()
  +            throws IOException {
  +            if (is != null)
  +                return is;
  +            if (file != null)
  +                return (file.streamContent());
  +            else
  +                return null;
           }
   
   
  
  
  
  1.9       +35 -17    
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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- WebdavServlet.java        2001/01/12 06:55:35     1.8
  +++ WebdavServlet.java        2001/01/13 21:24:48     1.9
  @@ -1,7 +1,7 @@
   /*
  - * $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 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/catalina/servlets/WebdavServlet.java,v
 1.9 2001/01/13 21:24:48 remm Exp $
  + * $Revision: 1.9 $
  + * $Date: 2001/01/13 21:24:48 $
    *
    * ====================================================================
    *
  @@ -124,7 +124,7 @@
    * are handled by the DefaultServlet.
    *
    * @author Remy Maucherat
  - * @version $Revision: 1.8 $ $Date: 2001/01/12 06:55:35 $
  + * @version $Revision: 1.9 $ $Date: 2001/01/13 21:24:48 $
    */
   
   public class WebdavServlet
  @@ -532,6 +532,7 @@
                   try {
                       object = resources.lookup(currentPath);
                   } catch (NamingException e) {
  +                    e.printStackTrace();
                       resp.sendError
                           (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
                       return;
  @@ -542,9 +543,14 @@
                           while (enum.hasMoreElements()) {
                               NameClassPair ncPair = 
                                   (NameClassPair) enum.nextElement();
  -                            stackBelow.push(ncPair.getName());
  +                            String newPath = currentPath;
  +                            if (!newPath.equals("/"))
  +                                newPath += "/";
  +                            newPath += ncPair.getName();
  +                            stackBelow.push(newPath);
                           }
                       } catch (NamingException e) {
  +                        e.printStackTrace();
                           resp.sendError
                               (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 
                                path);
  @@ -1557,6 +1563,9 @@
        */
       private boolean copyResource(DirContext resources, Hashtable errorList,
                                    String source, String dest) {
  +
  +        if (debug > 1)
  +            System.out.println("Copy: " + source + " To: " + dest);
           
           Object object = null;
           try {
  @@ -1578,9 +1587,15 @@
                   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);
  +                    String childDest = dest;
  +                    if (!childDest.equals("/"))
  +                        childDest += "/";
  +                    childDest += ncPair.getName();
  +                    String childSrc = source;
  +                    if (!childSrc.equals("/"))
  +                        childSrc += "/";
  +                    childSrc += ncPair.getName();
  +                    copyResource(resources, errorList, childSrc, childDest);
                   }
               } catch (NamingException e) {
                   errorList.put
  @@ -1742,7 +1757,10 @@
           
           while (enum.hasMoreElements()) {
               NameClassPair ncPair = (NameClassPair) enum.nextElement();
  -            String childName = path + ncPair.getName();
  +            String childName = path;
  +            if (!childName.equals("/"))
  +                childName += "/";
  +            childName += ncPair.getName();
               
               if (isLocked(childName, ifHeader + lockTokenHeader)) {
                   
  @@ -1867,15 +1885,15 @@
           // Generating href element
           generatedXML.writeElement(null, "href", XMLWriter.OPENING);
           
  -        String absoluteUri = req.getRequestURI();
  -        String relativePath = normalize(getRelativePath(req));
  -        String toAppend = path.substring(relativePath.length());
  -        if ((!toAppend.startsWith("/")) && (!absoluteUri.endsWith("/")))
  -            toAppend = "/" + toAppend;
  -        if (toAppend.equals("/"))
  -            toAppend = "";
  +        String href = req.getContextPath();
  +        if ((href.endsWith("/")) && (path.startsWith("/")))
  +            href += path.substring(1);
  +        else
  +            href += path;
  +        if ((resourceInfo.collection) && (!href.endsWith("/")))
  +            href += "/";
           
  -        generatedXML.writeText(absoluteUri + toAppend);
  +        generatedXML.writeText(href);
           
           generatedXML.writeElement(null, "href", XMLWriter.CLOSING);
           
  
  
  

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

Reply via email to