[Bedework-commit] webdav r139 - in trunk/server/src/edu/rpi/cct/webdav/servlet: common shared

svnadmin at bedework.org svnadmin at bedework.org
Thu May 29 10:21:47 EDT 2008


Author: douglm
Date: 2008-05-29 10:21:45 -0400 (Thu, 29 May 2008)
New Revision: 139

Modified:
   trunk/server/src/edu/rpi/cct/webdav/servlet/common/GetMethod.java
   trunk/server/src/edu/rpi/cct/webdav/servlet/common/PutMethod.java
   trunk/server/src/edu/rpi/cct/webdav/servlet/common/WebdavServlet.java
   trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsIntf.java
   trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsNode.java
Log:
Implement resources folders which can be created within a calendar collection. This follows the approach outlined in RFC 4791 and will provide a place to store shareable attachments while ensuring access restrictions are applied.

In addition, implement support for creating and retrieving 'file' resources which may be stored in any non-calendar collection. This will probably be useful when creating an image repository for example, but is also required for full implementation of shared attachments.

Currently, there is no ui support fo any of this, files may be uploaded with DAV explorer and viewed via a browser accessing the caldav server.

Modified: trunk/server/src/edu/rpi/cct/webdav/servlet/common/GetMethod.java
===================================================================
--- trunk/server/src/edu/rpi/cct/webdav/servlet/common/GetMethod.java	2008-05-23 15:14:56 UTC (rev 138)
+++ trunk/server/src/edu/rpi/cct/webdav/servlet/common/GetMethod.java	2008-05-29 14:21:45 UTC (rev 139)
@@ -59,6 +59,8 @@
 import edu.rpi.cct.webdav.servlet.shared.WebdavNsNode;
 
 import java.io.CharArrayReader;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.Reader;
 import java.io.Writer;
 import javax.servlet.http.HttpServletRequest;
@@ -116,15 +118,23 @@
         return;
       }
 
-      Writer out;
+      Writer out = null;
+      Reader in = null;
 
+      /* For binary */
+      OutputStream streamOut = null;
+      InputStream streamIn = null;
+
       /** Get the content now to set up length, type etc.
        */
-      Reader in = null;
       String contentType;
       int contentLength;
 
-      if (node.isCollection()) {
+      if (node.getContentBinary()) {
+        streamIn = getNsIntf().getBinaryContent(node);
+        contentType = node.getContentType();
+        contentLength = node.getContentLen();
+      } else if (node.isCollection()) {
         if (getNsIntf().getDirectoryBrowsingDisallowed()) {
           throw new WebdavException(HttpServletResponse.SC_FORBIDDEN);
         }
@@ -140,9 +150,11 @@
       }
 
       if (doContent) {
-        out = resp.getWriter();
-      } else {
-        out = null;
+        if (node.getContentBinary()) {
+          streamOut = resp.getOutputStream();
+        } else {
+          out = resp.getWriter();
+        }
       }
 
       resp.setHeader("ETag", node.getEtagValue(true));
@@ -155,7 +167,7 @@
       resp.setContentLength(contentLength);
 
       if (doContent) {
-        if (in == null) {
+        if ((in == null) && (streamIn == null)) {
           if (debug) {
             debugMsg("status: " + HttpServletResponse.SC_NO_CONTENT);
           }
@@ -166,7 +178,11 @@
             debugMsg("send content - length=" + node.getContentLen());
           }
 
-          writeContent(in, out);
+          if (node.getContentBinary()) {
+            streamContent(streamIn, streamOut);
+          } else {
+            writeContent(in, out);
+          }
         }
       }
     } catch (WebdavException we) {
@@ -203,6 +219,33 @@
     }
   }
 
+  private void streamContent(InputStream in, OutputStream out)
+      throws WebdavException {
+    try {
+      byte[] buff = new byte[bufferSize];
+      int len;
+
+      while (true) {
+        len = in.read(buff);
+
+        if (len < 0) {
+          break;
+        }
+
+        out.write(buff, 0, len);
+      }
+    } catch (Throwable t) {
+      throw new WebdavException(t);
+    } finally {
+      try {
+        in.close();
+      } catch (Throwable t) {}
+      try {
+        out.close();
+      } catch (Throwable t) {}
+    }
+  }
+
   /** Return a String giving an HTML representation of the directory.
    *
    * TODO

Modified: trunk/server/src/edu/rpi/cct/webdav/servlet/common/PutMethod.java
===================================================================
--- trunk/server/src/edu/rpi/cct/webdav/servlet/common/PutMethod.java	2008-05-23 15:14:56 UTC (rev 138)
+++ trunk/server/src/edu/rpi/cct/webdav/servlet/common/PutMethod.java	2008-05-29 14:21:45 UTC (rev 139)
@@ -99,11 +99,21 @@
 
       boolean create = Headers.ifNoneMatchAny(req);
       String ifEtag = Headers.ifMatch(req);
+      WebdavNsIntf.PutContentResult pcr;
 
-      WebdavNsIntf.PutContentResult pcr = intf.putContent(node,
-                                                          getReader(req),
-                                                          create,
-                                                          ifEtag);
+      if (node.getContentBinary()) {
+        pcr = intf.putBinaryContent(node,
+                                    req.getContentType(),
+                                    req.getInputStream(),
+                                    create,
+                                    ifEtag);
+      } else {
+        pcr = intf.putContent(node,
+                              req.getContentType(),
+                              getReader(req),
+                              create,
+                              ifEtag);
+      }
 
       if (pcr.created) {
         resp.setStatus(HttpServletResponse.SC_CREATED);

Modified: trunk/server/src/edu/rpi/cct/webdav/servlet/common/WebdavServlet.java
===================================================================
--- trunk/server/src/edu/rpi/cct/webdav/servlet/common/WebdavServlet.java	2008-05-23 15:14:56 UTC (rev 138)
+++ trunk/server/src/edu/rpi/cct/webdav/servlet/common/WebdavServlet.java	2008-05-29 14:21:45 UTC (rev 139)
@@ -206,17 +206,22 @@
 
       if (debug && dumpContent) {
         CharArrayWrappedResponse wresp = (CharArrayWrappedResponse)resp;
-        String str = wresp.toString();
 
-        debugMsg("------------------------ Dump of response -------------------");
-        debugMsg(str);
-        debugMsg("---------------------- End dump of response -----------------");
+        if (wresp.getUsedOutputStream()) {
+          debugMsg("------------------------ response written to output stream -------------------");
+        } else {
+          String str = wresp.toString();
 
-        byte[] bs = str.getBytes();
-        resp = (HttpServletResponse)wresp.getResponse();
-        debugMsg("contentLength=" + bs.length);
-        resp.setContentLength(bs.length);
-        resp.getOutputStream().write(bs);
+          debugMsg("------------------------ Dump of response -------------------");
+          debugMsg(str);
+          debugMsg("---------------------- End dump of response -----------------");
+
+          byte[] bs = str.getBytes();
+          resp = (HttpServletResponse)wresp.getResponse();
+          debugMsg("contentLength=" + bs.length);
+          resp.setContentLength(bs.length);
+          resp.getOutputStream().write(bs);
+        }
       }
 
       /* WebDAV is stateless - toss away the session */

Modified: trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsIntf.java
===================================================================
--- trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsIntf.java	2008-05-23 15:14:56 UTC (rev 138)
+++ trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsIntf.java	2008-05-29 14:21:45 UTC (rev 139)
@@ -64,6 +64,7 @@
 import edu.rpi.cct.webdav.servlet.common.MethodBase.MethodInfo;
 import edu.rpi.cmt.access.Acl;
 
+import java.io.InputStream;
 import java.io.Reader;
 import java.io.Serializable;
 import java.net.URI;
@@ -489,7 +490,7 @@
   public abstract WebdavNsNode getParent(WebdavNsNode node)
       throws WebdavException;
 
-  /** Returns an InputStream for the content.
+  /** Returns a Reader for the content.
    *
    * @param node             node in question
    * @return Reader          A reader for the content.
@@ -498,6 +499,15 @@
   public abstract Reader getContent(WebdavNsNode node)
       throws WebdavException;
 
+  /** Returns an InputStream for the binary content.
+   *
+   * @param node             node in question
+   * @return inputStream     A stream for the content.
+   * @throws WebdavException
+   */
+  public abstract InputStream getBinaryContent(WebdavNsNode node)
+      throws WebdavException;
+
   /** Result for putContent
    */
   public static class PutContentResult {
@@ -511,6 +521,7 @@
   /** Set the content from a Reader
    *
    * @param node              node in question.
+   * @param contentType       null or value from content-type header
    * @param contentRdr        Reader for content
    * @param create            true if this is a probably creation
    * @param ifEtag            if non-null etag must match
@@ -518,11 +529,29 @@
    * @throws WebdavException
    */
   public abstract PutContentResult putContent(WebdavNsNode node,
+                                              String contentType,
                                               Reader contentRdr,
                                               boolean create,
                                               String ifEtag)
       throws WebdavException;
 
+  /** Set the content from a Stream
+   *
+   * @param node              node in question.
+   * @param contentType       null or value from content-type header
+   * @param contentStream     Stream for content
+   * @param create            true if this is a probably creation
+   * @param ifEtag            if non-null etag must match
+   * @return PutContentResult result of creating
+   * @throws WebdavException
+   */
+  public abstract PutContentResult putBinaryContent(WebdavNsNode node,
+                                              String contentType,
+                                              InputStream contentStream,
+                                              boolean create,
+                                              String ifEtag)
+      throws WebdavException;
+
   /** Create a new node.
    *
    * @param node             node to create with new uri set

Modified: trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsNode.java
===================================================================
--- trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsNode.java	2008-05-23 15:14:56 UTC (rev 138)
+++ trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsNode.java	2008-05-29 14:21:45 UTC (rev 139)
@@ -62,6 +62,8 @@
 import edu.rpi.sss.util.xml.XmlUtil;
 import edu.rpi.sss.util.xml.tagdefs.WebdavTags;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 import java.io.Reader;
 import java.io.Serializable;
 import java.io.StringReader;
@@ -920,7 +922,7 @@
     return new ArrayList<WebdavProperty>();
   }
 
-  /** Returns an InputStream for the content.
+  /** Returns a Reader for the content.
    *
    * @return Reader       A reader for the content.
    * @throws WebdavException
@@ -935,6 +937,21 @@
     return new StringReader(cont);
   }
 
+  /** Returns an InputStream for the content.
+   *
+   * @return InputStream       A reader for the content.
+   * @throws WebdavException
+   */
+  public InputStream getContentStream() throws WebdavException {
+    byte[] cont = getBinaryContent();
+
+    if (cont == null) {
+      return null;
+    }
+
+    return new ByteArrayInputStream(cont);
+  }
+
   /** Return string content
    *
    * @return String       content.
@@ -944,11 +961,26 @@
     return null;
   }
 
+  /** Return binary content
+   *
+   * @return byte[]       content.
+   * @throws WebdavException
+   */
+  public byte[] getBinaryContent() throws WebdavException {
+    return null;
+  }
+
   /* ====================================================================
    *                   Required webdav properties
    * ==================================================================== */
 
   /**
+   * @return boolean true if this is binary content
+   * @throws WebdavException
+   */
+  public abstract boolean getContentBinary() throws WebdavException;
+
+  /**
    * @return String lang
    * @throws WebdavException
    */



More information about the Bedework-commit mailing list