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

svnadmin at bedework.org svnadmin at bedework.org
Tue Sep 7 22:16:03 EDT 2010


Author: douglm
Date: 2010-09-07 22:15:56 -0400 (Tue, 07 Sep 2010)
New Revision: 238

Modified:
   trunk/server/src/edu/rpi/cct/webdav/servlet/common/MethodBase.java
   trunk/server/src/edu/rpi/cct/webdav/servlet/common/PutMethod.java
   trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsIntf.java
Log:
Handle web service POST for create.

Required some rearrangement of web/caldav

Modified: trunk/server/src/edu/rpi/cct/webdav/servlet/common/MethodBase.java
===================================================================
--- trunk/server/src/edu/rpi/cct/webdav/servlet/common/MethodBase.java	2010-09-08 02:15:42 UTC (rev 237)
+++ trunk/server/src/edu/rpi/cct/webdav/servlet/common/MethodBase.java	2010-09-08 02:15:56 UTC (rev 238)
@@ -44,9 +44,7 @@
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
-import java.io.FilterReader;
 import java.io.IOException;
-import java.io.PushbackReader;
 import java.io.Reader;
 import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
@@ -246,87 +244,6 @@
     resp.addHeader("DAV", getNsIntf().getDavHeader(node));
   }
 
-  private class DebugReader extends FilterReader {
-    StringBuilder sb = new StringBuilder();
-
-    /** Constructor
-     * @param rdr
-     */
-    public DebugReader(final Reader rdr) {
-      super(rdr);
-    }
-
-    @Override
-    public void close() throws IOException {
-      if (sb != null) {
-        trace(sb.toString());
-      }
-
-      super.close();
-    }
-
-    @Override
-    public int read() throws IOException {
-      int c = super.read();
-
-      if (c == -1) {
-        if (sb != null) {
-          trace(sb.toString());
-          sb = null;
-        }
-        return c;
-      }
-
-      if (sb != null) {
-        char ch = (char)c;
-        if (ch == '\n') {
-          trace(sb.toString());
-          sb = new StringBuilder();
-        } else {
-          sb.append(ch);
-        }
-      }
-
-      return c;
-    }
-
-    @Override
-    public int read(final char[] cbuf, final int off, final int len) throws IOException {
-      int res = super.read(cbuf, off, len);
-      if ((res > 0) && (sb != null)) {
-        sb.append(cbuf, off, res);
-      }
-
-      return res;
-    }
-  }
-
-  protected Reader getReader(final HttpServletRequest req) throws Throwable {
-    Reader rdr;
-
-    if (debug) {
-      rdr = new DebugReader(req.getReader());
-    } else {
-      rdr = req.getReader();
-    }
-
-    /* Wrap with a pushback reader and see if there is anything there - some
-     * people are not setting the content length to 0 when there is no body */
-
-    PushbackReader pbr = new PushbackReader(rdr);
-
-    int c = pbr.read();
-
-    if (c == -1) {
-      // No input
-      return null;
-    }
-
-    pbr.unread(c);
-
-    return pbr;
-  }
-
   /** Parse the Webdav request body, and return the DOM representation.
    *
    * @param req        Servlet request object
@@ -348,7 +265,7 @@
 
       DocumentBuilder builder = factory.newDocumentBuilder();
 
-      Reader rdr = getReader(req);
+      Reader rdr = getNsIntf().getReader(req);
 
       if (rdr == null) {
         // No content?

Modified: trunk/server/src/edu/rpi/cct/webdav/servlet/common/PutMethod.java
===================================================================
--- trunk/server/src/edu/rpi/cct/webdav/servlet/common/PutMethod.java	2010-09-08 02:15:42 UTC (rev 237)
+++ trunk/server/src/edu/rpi/cct/webdav/servlet/common/PutMethod.java	2010-09-08 02:15:56 UTC (rev 238)
@@ -1,31 +1,3 @@
-/*
- Copyright (c) 2000-2005 University of Washington.  All rights reserved.
-
- Redistribution and use of this distribution in source and binary forms,
- with or without modification, are permitted provided that:
-
-   The above copyright notice and this permission notice appear in
-   all copies and supporting documentation;
-
-   The name, identifiers, and trademarks of the University of Washington
-   are not used in advertising or publicity without the express prior
-   written permission of the University of Washington;
-
-   Recipients acknowledge that this distribution is made available as a
-   research courtesy, "as is", potentially with defects, without
-   any obligation on the part of the University of Washington to
-   provide support, services, or repair;
-
-   THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR
-   IMPLIED, WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION
-   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
-   PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE UNIVERSITY OF
-   WASHINGTON BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
-   DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
-   PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT (INCLUDING
-   NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
-   THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
 /* **********************************************************************
     Copyright 2005 Rensselaer Polytechnic Institute. All worldwide rights reserved.
 
@@ -55,12 +27,8 @@
 package edu.rpi.cct.webdav.servlet.common;
 
 import edu.rpi.cct.webdav.servlet.shared.WebdavException;
-import edu.rpi.cct.webdav.servlet.shared.WebdavForbidden;
 import edu.rpi.cct.webdav.servlet.shared.WebdavNsIntf;
-import edu.rpi.cct.webdav.servlet.shared.WebdavNsNode;
 
-import java.io.Reader;
-
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -84,82 +52,12 @@
       trace("PutMethod: doMethod");
     }
 
-    try {
-      WebdavNsIntf intf = getNsIntf();
-      WebdavNsNode node = intf.getNode(getResourceUri(req),
-                                       WebdavNsIntf.existanceMay,
-                                       WebdavNsIntf.nodeTypeEntity);
+    boolean create = Headers.ifNoneMatchAny(req);
+    String ifEtag = Headers.ifMatch(req);
 
-      if (node == null) {
-        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
-        return;
-      }
+    WebdavNsIntf intf = getNsIntf();
 
-      if (!node.getAllowsGet() || !intf.canPut(node)) {
-        // If we can't GET - we can't PUT
-        resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
-        return;
-      }
-
-      boolean create = Headers.ifNoneMatchAny(req);
-      String ifEtag = Headers.ifMatch(req);
-      WebdavNsIntf.PutContentResult pcr;
-
-      String[] contentTypePars = null;
-      String contentType = req.getContentType();
-
-      if (contentType != null) {
-        contentTypePars = contentType.split(";");
-      }
-
-      if (node.getContentBinary()) {
-        pcr = intf.putBinaryContent(req, node,
-                                    contentTypePars,
-                                    req.getInputStream(),
-                                    create,
-                                    ifEtag);
-      } else {
-        Reader rdr = getReader(req);
-
-        if (rdr == null) {
-          // No content?
-          resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
-          return;
-        }
-
-        pcr = intf.putContent(req, node,
-                              contentTypePars,
-                              rdr,
-                              create,
-                              ifEtag);
-      }
-
-      if (pcr.created) {
-        resp.setStatus(HttpServletResponse.SC_CREATED);
-      } else {
-        resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
-      }
-      resp.setContentLength(0);
-
-      resp.setHeader("ETag", node.getEtagValue(true));
-      /* Apparently frowned upon
-      resp.setHeader("Location", intf.getLocation(pcr.node));
-      */
-
-    } catch (WebdavForbidden wdf) {
-      resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
-      throw wdf;
-    } catch (WebdavException we) {
-      if (debug) {
-        error(we);
-      }
-      throw we;
-    } catch (Throwable t) {
-      if (debug) {
-        error(t);
-      }
-      throw new WebdavException(t);
-    }
+    intf.putContent(req, resp, false, create, ifEtag);
   }
 }
 

Modified: trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsIntf.java
===================================================================
--- trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsIntf.java	2010-09-08 02:15:42 UTC (rev 237)
+++ trunk/server/src/edu/rpi/cct/webdav/servlet/shared/WebdavNsIntf.java	2010-09-08 02:15:56 UTC (rev 238)
@@ -42,8 +42,10 @@
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
+import java.io.FilterReader;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.PushbackReader;
 import java.io.Reader;
 import java.io.Serializable;
 import java.net.URI;
@@ -562,6 +564,109 @@
     public boolean created;
   }
 
+  /** Put content for the PUT or POST methods
+   *
+   * @param req
+   * @param resp
+   * @param fromPost          POST style - create
+   * @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 PutContentResult putContent(final HttpServletRequest req,
+                                     final HttpServletResponse resp,
+                                     final boolean fromPost,
+                                     final boolean create,
+                                     final String ifEtag) throws WebdavException {
+    try {
+      /* We get a node to represent the entity we are creating or updating. */
+      int existance;
+
+      if (create) {
+        existance = existanceNot;
+      } if (!fromPost) {
+        existance = existanceMay;
+      } else {
+        existance = existanceMust;
+      }
+
+      WebdavNsNode node = getNode(getResourceUri(req),
+                                  existance,
+                                  nodeTypeEntity);
+
+      if (node == null) {
+        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
+        return null;
+      }
+
+      if (!node.getAllowsGet() || !canPut(node)) {
+        // If we can't GET - we can't PUT
+        resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+        return null;
+      }
+
+      PutContentResult pcr;
+
+      String[] contentTypePars = null;
+      String contentType = req.getContentType();
+
+      if (contentType != null) {
+        contentTypePars = contentType.split(";");
+      }
+
+      if (node.getContentBinary()) {
+        pcr = putBinaryContent(req, node,
+                               contentTypePars,
+                               req.getInputStream(),
+                               create,
+                               ifEtag);
+      } else {
+        Reader rdr = getReader(req);
+
+        if (rdr == null) {
+          // No content?
+          resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+          return null;
+        }
+
+        pcr = putContent(req, node,
+                         contentTypePars,
+                         rdr,
+                         create,
+                         ifEtag);
+      }
+
+      if (pcr.created) {
+        resp.setStatus(HttpServletResponse.SC_CREATED);
+      } else {
+        resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
+      }
+      resp.setContentLength(0);
+
+      resp.setHeader("ETag", node.getEtagValue(true));
+
+      if (fromPost && pcr.created) {
+        resp.setHeader("Location", getLocation(pcr.node));
+      }
+
+      return pcr;
+    } catch (WebdavForbidden wdf) {
+      resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
+      throw wdf;
+    } catch (WebdavException we) {
+      if (debug) {
+        error(we);
+      }
+      throw we;
+    } catch (Throwable t) {
+      if (debug) {
+        error(t);
+      }
+      throw new WebdavException(t);
+    }
+  }
+
   /** Set the content from a Reader
    *
    * @param req
@@ -1125,6 +1230,92 @@
     return sb.toString();
   }
 
+  private class DebugReader extends FilterReader {
+    StringBuilder sb = new StringBuilder();
+
+    /** Constructor
+     * @param rdr
+     */
+    public DebugReader(final Reader rdr) {
+      super(rdr);
+    }
+
+    @Override
+    public void close() throws IOException {
+      if (sb != null) {
+        trace(sb.toString());
+      }
+
+      super.close();
+    }
+
+    @Override
+    public int read() throws IOException {
+      int c = super.read();
+
+      if (c == -1) {
+        if (sb != null) {
+          trace(sb.toString());
+          sb = null;
+        }
+        return c;
+      }
+
+      if (sb != null) {
+        char ch = (char)c;
+        if (ch == '\n') {
+          trace(sb.toString());
+          sb = new StringBuilder();
+        } else {
+          sb.append(ch);
+        }
+      }
+
+      return c;
+    }
+
+    @Override
+    public int read(final char[] cbuf, final int off, final int len) throws IOException {
+      int res = super.read(cbuf, off, len);
+      if ((res > 0) && (sb != null)) {
+        sb.append(cbuf, off, res);
+      }
+
+      return res;
+    }
+  }
+
+  /**
+   * @param req
+   * @return possibly wrapped reader
+   * @throws Throwable
+   */
+  public Reader getReader(final HttpServletRequest req) throws Throwable {
+    Reader rdr;
+
+    if (debug) {
+      rdr = new DebugReader(req.getReader());
+    } else {
+      rdr = req.getReader();
+    }
+
+    /* Wrap with a pushback reader and see if there is anything there - some
+     * people are not setting the content length to 0 when there is no body */
+
+    PushbackReader pbr = new PushbackReader(rdr);
+
+    int c = pbr.read();
+
+    if (c == -1) {
+      // No input
+      return null;
+    }
+
+    pbr.unread(c);
+
+    return pbr;
+  }
+
   /* ====================================================================
    *                   XmlUtil wrappers
    * ==================================================================== */



More information about the Bedework-commit mailing list