You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@locus.apache.org on 2000/08/01 09:19:47 UTC

cvs commit: jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets DefaultServlet.java WebdavServlet.java

remm        00/08/01 00:19:46

  Modified:    proposals/catalina/src/share/org/apache/tomcat/resources
                        FileResources.java
               proposals/catalina/src/share/org/apache/tomcat/servlets
                        DefaultServlet.java WebdavServlet.java
  Log:
  - Adds support for PUT and MKCOL. PUT is not working very well
    with Webfolders. I had the same exact problem with Slide, and
    although the problem is now fixed, I never figured out exactly how
    it was fixed ...
  
  Revision  Changes    Path
  1.7       +51 -7     jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/FileResources.java
  
  Index: FileResources.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/FileResources.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- FileResources.java	2000/08/01 03:06:41	1.6
  +++ FileResources.java	2000/08/01 07:19:35	1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/FileResources.java,v 1.6 2000/08/01 03:06:41 remm Exp $
  - * $Revision: 1.6 $
  - * $Date: 2000/08/01 03:06:41 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/FileResources.java,v 1.7 2000/08/01 07:19:35 remm Exp $
  + * $Revision: 1.7 $
  + * $Date: 2000/08/01 07:19:35 $
    *
    * ====================================================================
    *
  @@ -69,6 +69,9 @@
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.InputStream;
  +import java.io.FileOutputStream;
  +import java.io.FileNotFoundException;
  +import java.io.OutputStream;
   import java.io.IOException;
   import java.net.MalformedURLException;
   import java.net.URL;
  @@ -89,7 +92,8 @@
    * since they were cached.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.6 $ $Date: 2000/08/01 03:06:41 $
  + * @author Remy Maucherat
  + * @version $Revision: 1.7 $ $Date: 2000/08/01 07:19:35 $
    */
   
   public final class FileResources extends ResourcesBase {
  @@ -111,6 +115,12 @@
   	"org.apache.tomcat.resources.FileResources/1.0";
   
   
  +    /**
  +     * The descriptive information string for this implementation.
  +     */
  +    protected static final int BUFFER_SIZE = 2048;
  +
  +
       // ------------------------------------------------------------- Properties
   
   
  @@ -452,10 +462,44 @@
        * @param content InputStream to the content to be set
        */
       public boolean setResource(String path, InputStream content) {
  +        
  +        String normalized = normalize(path);
  +	if (normalized == null)
  +	    return (false);
  +	validate(normalized);
  +
  +	File file = new File(base, normalized.substring(1));
  +        //if ((file.exists()) && (file.isDirectory()))
  +        //return (false);
  +        
  +        OutputStream os = null;
  +        
  +        try {
  +            os = new FileOutputStream(file);
  +        } catch (FileNotFoundException e) {
  +            return (false);
  +        }
  +        
  +        try {
  +            byte[] buffer = new byte[BUFFER_SIZE];
  +            while (true) {
  +                int nb = content.read(buffer);
  +                if (nb == -1)
  +                    break;
  +                os.write(buffer, 0, nb);
  +            }
  +        } catch (IOException e) {
  +            return (false);
  +        }
           
  -        // TODO : Implement it !
  -        return false;
  +        try {
  +            os.close();
  +        } catch (IOException e) {
  +            return (false);
  +        }
           
  +        return (true);
  +        
       }
   
   
  @@ -473,7 +517,7 @@
   	    return (false);
   	validate(normalized);
   
  -	File file = file(normalized);
  +	File file = new File(base, normalized.substring(1));
   	if (file != null)
   	    return (file.mkdir());
   	else
  
  
  
  1.17      +6 -8      jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/DefaultServlet.java
  
  Index: DefaultServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/DefaultServlet.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- DefaultServlet.java	2000/08/01 02:41:33	1.16
  +++ DefaultServlet.java	2000/08/01 07:19:39	1.17
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/DefaultServlet.java,v 1.16 2000/08/01 02:41:33 remm Exp $
  - * $Revision: 1.16 $
  - * $Date: 2000/08/01 02:41:33 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/DefaultServlet.java,v 1.17 2000/08/01 07:19:39 remm Exp $
  + * $Revision: 1.17 $
  + * $Date: 2000/08/01 07:19:39 $
    *
    * ====================================================================
    *
  @@ -108,7 +108,7 @@
    *
    * @author Craig R. McClanahan
    * @author Remy Maucherat
  - * @version $Revision: 1.16 $ $Date: 2000/08/01 02:41:33 $
  + * @version $Revision: 1.17 $ $Date: 2000/08/01 07:19:39 $
    */
   
   public class DefaultServlet
  @@ -229,7 +229,7 @@
   	throws IOException, ServletException {
   
   	// Serve the requested resource, without the data content
  -	serveResource(request, response, false);
  +        serveResource(request, response, false);
   
       }
   
  @@ -393,7 +393,6 @@
                       // specified by the client. This is not an error case.
                       response.sendError
                           (HttpServletResponse.SC_NOT_MODIFIED);
  -                    System.out.println("Not modified");
                       return false;
                   }
                   
  @@ -891,7 +890,7 @@
   
           // Parse range specifier
           Vector ranges = null;
  -        if (resourceInfo.collection) {
  +        if (!resourceInfo.collection) {
               parseRange(request, response, resourceInfo);
           
               // Last-Modified header
  @@ -971,7 +970,6 @@
               
           }
           
  -
       }
   
   
  
  
  
  1.2       +124 -11   jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/WebdavServlet.java
  
  Index: WebdavServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/WebdavServlet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WebdavServlet.java	2000/07/31 06:28:01	1.1
  +++ WebdavServlet.java	2000/08/01 07:19:39	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/WebdavServlet.java,v 1.1 2000/07/31 06:28:01 remm Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/07/31 06:28:01 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/WebdavServlet.java,v 1.2 2000/08/01 07:19:39 remm Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/08/01 07:19:39 $
    *
    * ====================================================================
    *
  @@ -118,16 +118,17 @@
    * are handled by the DefaultServlet.
    *
    * @author Remy Maucherat
  - * @version $Revision: 1.1 $ $Date: 2000/07/31 06:28:01 $
  + * @version $Revision: 1.2 $ $Date: 2000/08/01 07:19:39 $
    */
   
  -public final class WebdavServlet
  +public class WebdavServlet
       extends DefaultServlet {
   
   
       // -------------------------------------------------------------- Constants
   
   
  +    private static final String METHOD_HEAD = "HEAD";
       private static final String METHOD_PROPFIND = "PROPFIND";
       private static final String METHOD_PROPPATCH = "PROPPATCH";
       private static final String METHOD_MKCOL = "MKCOL";
  @@ -223,6 +224,9 @@
   	throws ServletException, IOException {
   
   	String method = req.getMethod();
  +
  +	String servletPath = req.getServletPath();
  +        System.out.println("[" + method + "] " + servletPath);
           
   	if (method.equals(METHOD_PROPFIND)) {
   	    doPropfind(req, resp);
  @@ -238,8 +242,10 @@
               doLock(req, resp);
           } else if (method.equals(METHOD_UNLOCK)) {
               doUnlock(req, resp);
  -        } else {
  -            // DefailtServlet processing
  +        }/* else if (method.equals(METHOD_HEAD)) {
  +            doHead(req, resp);
  +        }*/ else {
  +            // DefaultServlet processing
               super.service(req, resp);
           }
           
  @@ -277,9 +283,37 @@
       protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
   	throws ServletException, IOException {
           
  +	String servletPath = req.getServletPath();
  +	if (servletPath == null)
  +	    servletPath = "/";
  +        
   	resp.addHeader("DAV", "1,2");
  -        String methodsAllowed = "OPTIONS, GET, HEAD, POST, DELETE, TRACE, " 
  -            + "PROPFIND, PROPPATCH, COPY, MOVE, LOCK, UNLOCK, PUT";
  +        String methodsAllowed = null;
  +        
  +        // Retrieve the Catalina context
  +        ApplicationContext context = (ApplicationContext) getServletContext();
  +
  +	// Convert the resource path to a URL
  +	URL resourceURL = null;
  +	try {
  +	    resourceURL = context.getResource(servletPath);
  +	} catch (MalformedURLException e) {
  +	    ;
  +	}
  +	if (resourceURL == null) {
  +	    methodsAllowed = "OPTIONS, MKCOL, PUT, LOCK";
  +            resp.addHeader("Allow", methodsAllowed);
  +            return;
  +	}
  +
  +        Resources resources = context.getResources();
  +        
  +        methodsAllowed = "OPTIONS, GET, HEAD, POST, DELETE, TRACE, " 
  +            + "PROPFIND, PROPPATCH, COPY, MOVE, LOCK, UNLOCK";
  +        if (!resources.isCollection(servletPath)) {
  +            methodsAllowed += ", PUT";
  +        }
  +        
           resp.addHeader("Allow", methodsAllowed);
           
       }
  @@ -469,7 +503,41 @@
       protected void doMkcol(HttpServletRequest req, HttpServletResponse resp)
   	throws ServletException, IOException {
   
  +        if (readOnly) {
  +            resp.sendError(WebdavStatus.SC_FORBIDDEN);
  +            return;
  +        }
  +
  +	String servletPath = req.getServletPath();
  +	if (servletPath == null)
  +	    servletPath = "/";
  +        
  +        // Retrieve the Catalina context
  +        ApplicationContext context = (ApplicationContext) getServletContext();
  +
  +	// Convert the resource path to a URL
  +	URL resourceURL = null;
  +	try {
  +	    resourceURL = context.getResource(servletPath);
  +	} catch (MalformedURLException e) {
  +	    ;
  +	}
  +	if (resourceURL != null) {
  +	    resp.sendError(WebdavStatus.SC_METHOD_NOT_ALLOWED);
  +	    return;
  +	}
  +
  +        Resources resources = context.getResources();
  +        
  +        boolean result = resources.createCollection(servletPath);
           
  +        if (!result) {
  +            resp.sendError(WebdavStatus.SC_CONFLICT,
  +                           WebdavStatus.getStatusText
  +                           (WebdavStatus.SC_CONFLICT));
  +        } else {
  +            resp.setStatus(WebdavStatus.SC_CREATED);
  +        }
   
       }
   
  @@ -480,8 +548,13 @@
       protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
   	throws ServletException, IOException {
   
  +        if (readOnly) {
  +            resp.sendError(WebdavStatus.SC_FORBIDDEN);
  +            return;
  +        }
           
  -
  +        
  +        
       }
   
   
  @@ -491,8 +564,34 @@
       protected void doPut(HttpServletRequest req, HttpServletResponse resp)
   	throws ServletException, IOException {
   
  +        if (readOnly) {
  +            resp.sendError(WebdavStatus.SC_FORBIDDEN);
  +            return;
  +        }
           
  -
  +	String servletPath = req.getServletPath();
  +	if (servletPath == null)
  +	    servletPath = "/";
  +        
  +        // Retrieve the Catalina context
  +        ApplicationContext context = (ApplicationContext) getServletContext();
  +        Resources resources = context.getResources();
  +        
  +        boolean exists = (resources.getResourceAsStream(servletPath) != null);
  +        
  +        boolean result = resources.setResource(servletPath, 
  +                                               req.getInputStream());
  +        
  +        if (result) {
  +            if (exists) {
  +                resp.setStatus(WebdavStatus.SC_NO_CONTENT);
  +            } else {
  +                resp.setStatus(WebdavStatus.SC_CREATED);
  +            }
  +        } else {
  +            resp.sendError(WebdavStatus.SC_CONFLICT);
  +        }
  +        
       }
   
   
  @@ -502,6 +601,11 @@
       protected void doCopy(HttpServletRequest req, HttpServletResponse resp)
   	throws ServletException, IOException {
   
  +        if (readOnly) {
  +            resp.sendError(WebdavStatus.SC_FORBIDDEN);
  +            return;
  +        }
  +        
           
   
       }
  @@ -513,6 +617,11 @@
       protected void doMove(HttpServletRequest req, HttpServletResponse resp)
   	throws ServletException, IOException {
   
  +        if (readOnly) {
  +            resp.sendError(WebdavStatus.SC_FORBIDDEN);
  +            return;
  +        }
  +        
           
   
       }
  @@ -556,6 +665,10 @@
       private void parseProperties(Resources resources, XMLWriter generatedXML,
                                    String path, int type, 
                                    Vector propertiesVector) {
  +        
  +        if (path.equalsIgnoreCase("WEB-INF") ||
  +            path.equalsIgnoreCase("META-INF"))
  +            return;
           
           ResourceInfo resourceInfo = new ResourceInfo(path, resources);