You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by bc...@locus.apache.org on 2000/11/27 07:34:20 UTC
cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/util DOMUtils.java
bcholmes 00/11/26 22:34:19
Modified: src/webdav/client/src/org/apache/webdav/lib/methods
LockMethod.java PropFindMethod.java
UnlockMethod.java
src/webdav/client/src/org/apache/webdav/lib/util
DOMUtils.java
Added: src/webdav/client/src/org/apache/webdav/lib/methods
XMLResponseMethodBase.java
Log:
- Implement Lock and Unlock methods
Revision Changes Path
1.2 +317 -17 jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/LockMethod.java
Index: LockMethod.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/LockMethod.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LockMethod.java 2000/11/22 06:19:09 1.1
+++ LockMethod.java 2000/11/27 06:34:16 1.2
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/LockMethod.java,v 1.1 2000/11/22 06:19:09 remm Exp $
- * $Revision: 1.1 $
- * $Date: 2000/11/22 06:19:09 $
+ * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/LockMethod.java,v 1.2 2000/11/27 06:34:16 bcholmes Exp $
+ * $Revision: 1.2 $
+ * $Date: 2000/11/27 06:34:16 $
*
* ====================================================================
*
@@ -63,36 +63,264 @@
package org.apache.webdav.lib.methods;
-import java.io.*;
-import java.util.*;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+
import org.apache.webdav.lib.State;
-import org.apache.webdav.lib.Header;
import org.apache.webdav.lib.WebdavStatus;
+import org.apache.webdav.lib.util.DOMUtils;
+import org.apache.webdav.lib.util.DOMWriter;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+
/**
- * LOCK Method.
+ * Web resources can be locked to ensure that only one user is updating
+ * the resource at a time. Locking helps to prevent the "lost update" problem.
+ * There are two types of lock currently defined by the WebDAV specification:
+ * exclusive locks and shared locks.
+ *
+ * <p> Per the specification, a lock indicates that someone is updating the
+ * resource, (hence the lock is a "write lock"), although the specification
+ * notes that the the syntax is extensible, and permits the eventual creation
+ * of locking for other access types.
+ *
+ * <h3>Shared and Exclusive Locks<h3>
+ *
+ * <p> The most basic form of lock is an <em>exclusive lock</em>. This is a
+ * lock where the access right in question is only granted to a single client.
+ * The need for this arbitration results from a desire to avoid having to merge
+ * results. However, there are times when the goal of a lock is not to exclude
+ * others from exercising an access right but rather to provide a mechanism for
+ * principals to indicate that they intend to exercise their access rights.
+ * <em>Shared locks</em> are provided for this case. A shared lock allows
+ * multiple clients to receive a lock. Hence any user with appropriate
+ * access can get the lock.
+ *
+ * <p> With shared locks there are two trust sets that affect a resource.
+ * The first trust set is created by access permissions. Principals who are
+ * trusted, for example, may have permission to write to the resource. Among
+ * those who have access permission to write to the resource, the set of
+ * principals who have taken out a shared lock also must trust each other,
+ * creating a (typically) smaller trust set within the access permission write
+ * set.
*
+ * <h3>Lock Compatibility</h3>
+ *
+ * <p> The following table indicates what happens if a new lock request
+ * is sent to a resource that is already locked: </p>
+ *
+ * <table border="1">
+ * <!-- ------------------------------------------------------------------- -->
+ * <tr><th> </th><th colspan="2"> Lock Request </th></tr>
+ * <tr><th>Current Lock </th><th>Exclusive Lock </th><th>Shared Lock </th></tr>
+ * <!-- ------------------------------------------------------------------- -->
+ * <tr><td>None </td><td>Success </td><td>Sucess </td></tr>
+ * <tr><td>Shared </td><td>Failure </td><td>Sucess </td></tr>
+ * <tr><td>Exclusive </td><td>Failure </td><td>Failure </td></tr>
+ * <!-- ------------------------------------------------------------------- -->
+ * </table>
+ *
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
+ * @author <a href="mailto:bcholmes@interlog.org">B.C. Holmes</a>
*/
public class LockMethod
- extends WebdavMethodBase {
+ extends XMLResponseMethodBase implements DepthMethod {
+
+ public static final short SCOPE_EXCLUSIVE = 0;
+ public static final short SCOPE_SHARED = 1;
+
+ public static final short TYPE_WRITE = 0;
+
+ public static final long TIMEOUT_INFINITY = Long.MAX_VALUE;
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * The scope of lock we're requesting. The default scope is
+ * SCOPE_EXCLUSIVE.
+ */
+ private short scope = SCOPE_EXCLUSIVE;
+
- // ----------------------------------------------------------- Constructors
+ /**
+ * The type of lock we're requesting. The default type is TYPE_WRITE.
+ */
+ private short type = TYPE_WRITE;
+
+
+ /**
+ * Depth.
+ */
+ private int depth = -1;
+
+ /**
+ * Opaque token of the lock we're trying to refresh.
+ */
+ private String refreshOpaqueToken = null;
+
+ private long timeout = TIMEOUT_INFINITY;
+
+ private String owner = null;
+
+ private State state = null;
+ private String lockToken = null;
+
+ // ----------------------------------------------------------- Constructors
+
/**
* Method constructor.
*/
public LockMethod() {
name = "LOCK";
-
+
}
+
+ // ------------------------------------------------------------- Properties
+
+ /**
+ * Depth setter.
+ *
+ * @param depth New depth value
+ */
+ public void setDepth(int depth) {
+ if (depth != DEPTH_0 && depth != DEPTH_INFINITY) {
+ throw new IllegalArgumentException(
+ "invalid depth value for lock method");
+ }
+ this.depth = depth;
+ }
+
+ /**
+ * Depth getter.
+ *
+ * @return int depth value
+ */
+ public int getDepth() {
+ return depth;
+ }
+
+ public String getLockToken() {
+ return this.lockToken;
+ }
+
+
+
+ public boolean isRefresh() {
+ return !((this.refreshOpaqueToken == null ) ||
+ (this.refreshOpaqueToken.equals("")));
+ }
+
+ public short getScope() {
+ return this.scope;
+ }
+
+
+ /**
+ * Sets the owner of the lock. This method provides only "basic" owner
+ * information. Thus, <code>setOwner("Jezebel Lipshitz")</code> will
+ * produce an <code>owner</code> element in the request document like this:
+ *
+ * <pre>
+ * <D:owner>Jezebel Lipshitz</D:owner>
+ * </pre>
+ *
+ * <p> Examples in the Webdav specification suggest that one can use
+ * e-mail addresses, home page URLs, or other information; this
+ * implementation doesn't handle any of that.
+ */
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
+
+ public void setScope(short scope) {
+ if (scope != SCOPE_SHARED && scope != SCOPE_EXCLUSIVE) {
+ throw new IllegalArgumentException("invalid scope value");
+ }
+ this.scope = scope;
+ }
+
+ public long getTimeout() {
+ return this.timeout;
+ }
+
+ public void setTimeout(long timeout) {
+ if (timeout < 0) {
+ throw new IllegalArgumentException("invalid timeout value: " + timeout);
+ }
+ this.timeout = timeout;
+ }
+
// --------------------------------------------------- WebdavMethod Methods
+
+ public void recycle() {
+ super.recycle();
+ this.refreshOpaqueToken = null;
+ this.depth = -1;
+ this.type = TYPE_WRITE;
+ this.scope = SCOPE_EXCLUSIVE;
+ this.timeout = TIMEOUT_INFINITY;
+ this.state = null;
+ }
+
+
+ /**
+ * Generate additional headers needed by the request.
+ *
+ * @param host the host
+ * @param state State token
+ */
+ public void generateHeaders(String host, State state) {
+
+ super.generateHeaders(host, state);
+
+ switch (depth) {
+ case DEPTH_0:
+ setHeader("Depth", "0");
+ break;
+ case DEPTH_INFINITY:
+ setHeader("Depth", "infinity");
+ break;
+ default:
+ }
+
+ if (timeout == TIMEOUT_INFINITY) {
+ setHeader("Timeout", "Infinity, Second-" + TIMEOUT_INFINITY);
+ } else {
+ setHeader("Timeout", "Second-" + timeout);
+ }
+
+ if (isRefresh()) {
+ setHeader("If", "(<" + refreshOpaqueToken + ">)");
+ }
+
+ this.state = state;
+ }
/**
@@ -101,18 +329,90 @@
* @return String query
*/
public String generateQuery() {
- return null;
+
+ String query = null;
+
+ if (!isRefresh()) {
+ if (this.owner == null || this.owner.equals("")) {
+ throw new IllegalStateException("The owner property has not been set");
+ }
+
+ try {
+ DocumentBuilderFactory factory =
+ DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ Document document = builder.newDocument();
+
+ Element lockinfo = document.createElement("DAV:lockinfo");
+ document.appendChild(lockinfo);
+ lockinfo.setAttribute("xmlns:DAV", "DAV:");
+
+ Element lockscope = document.createElement("DAV:lockscope");
+ lockinfo.appendChild(lockscope);
+
+ if (this.scope == SCOPE_EXCLUSIVE) {
+ Element exclusive = document.createElement("DAV:exclusive");
+ lockscope.appendChild(exclusive);
+ } else {
+ Element shared = document.createElement("DAV:shared");
+ lockscope.appendChild(shared);
+ }
+
+ Element locktype = document.createElement("DAV:locktype");
+ lockinfo.appendChild(locktype);
+
+ Element write = document.createElement("DAV:write");
+ locktype.appendChild(write);
+
+ Element owner = document.createElement("DAV:owner");
+ lockinfo.appendChild(owner);
+
+ Text text = document.createTextNode(this.owner);
+ owner.appendChild(text);
+
+ StringWriter stringWriter = new StringWriter();
+ DOMWriter domWriter = new DOMWriter(stringWriter, true);
+ domWriter.print(document);
+
+ query = stringWriter.getBuffer().toString();
+
+ } catch (DOMException e) {
+ } catch (ParserConfigurationException e) {
+ }
+ }
+ return query;
}
-
-
+
/**
* Parse response.
*
- * @param is Input stream
+ * @param input Input stream
*/
- public void parseResponse(InputStream is)
+ public void parseResponse(InputStream input)
throws IOException {
+ int status = getStatusCode();
+ if (status == WebdavStatus.SC_OK ||
+ status == WebdavStatus.SC_MULTI_STATUS ||
+ status == WebdavStatus.SC_CONFLICT) {
+
+ super.parseResponse(input);
+
+ if (status == WebdavStatus.SC_OK) {
+ String prefix = DOMUtils.findDavPrefix(
+ getResponseDocument());
+ NodeList list = getResponseDocument().getDocumentElement()
+ .getElementsByTagName(prefix + "locktoken");
+
+ if (list.getLength() == 1) {
+ Element locktoken = (Element) list.item(0);
+ NodeList list2 = locktoken.getElementsByTagName(
+ prefix + "href");
+ if (list2.getLength() == 1) {
+ lockToken = DOMUtils.getTextValue(list2.item(0));
+ this.state.addLock(getPath(), lockToken);
+ }
+ }
+ }
+ }
}
-
-
}
1.2 +18 -22 jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PropFindMethod.java
Index: PropFindMethod.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PropFindMethod.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PropFindMethod.java 2000/11/22 06:19:09 1.1
+++ PropFindMethod.java 2000/11/27 06:34:16 1.2
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PropFindMethod.java,v 1.1 2000/11/22 06:19:09 remm Exp $
- * $Revision: 1.1 $
- * $Date: 2000/11/22 06:19:09 $
+ * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PropFindMethod.java,v 1.2 2000/11/27 06:34:16 bcholmes Exp $
+ * $Revision: 1.2 $
+ * $Date: 2000/11/27 06:34:16 $
*
* ====================================================================
*
@@ -120,8 +120,8 @@
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
*/
-public class PropFindMethod
- extends PropMethodBase {
+public class PropFindMethod extends XMLResponseMethodBase
+ implements DepthMethod {
// -------------------------------------------------------------- Constants
@@ -145,23 +145,6 @@
public static final int NAMES = 2;
- /**
- * Request with depth infinity.
- */
- public static final int DEPTH_INFINITY = -1;
-
-
- /**
- * Request with depth 0.
- */
- public static final int DEPTH_0 = 0;
-
-
- /**
- * Request with depth 1.
- */
- public static final int DEPTH_1 = 1;
-
// ----------------------------------------------------------- Constructors
@@ -452,4 +435,17 @@
return tmp.getBuffer().toString();
}
}
+
+ /**
+ * Parse response.
+ *
+ * @param input Input stream
+ */
+ public void parseResponse(InputStream input)
+ throws IOException {
+ if (getStatusCode() == WebdavStatus.SC_MULTI_STATUS) {
+ super.parseResponse(input);
+ }
+ }
+
}
1.2 +32 -12 jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/UnlockMethod.java
Index: UnlockMethod.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/UnlockMethod.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UnlockMethod.java 2000/11/22 06:19:09 1.1
+++ UnlockMethod.java 2000/11/27 06:34:16 1.2
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/UnlockMethod.java,v 1.1 2000/11/22 06:19:09 remm Exp $
- * $Revision: 1.1 $
- * $Date: 2000/11/22 06:19:09 $
+ * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/UnlockMethod.java,v 1.2 2000/11/27 06:34:16 bcholmes Exp $
+ * $Revision: 1.2 $
+ * $Date: 2000/11/27 06:34:16 $
*
* ====================================================================
*
@@ -63,8 +63,9 @@
package org.apache.webdav.lib.methods;
-import java.io.*;
-import java.util.*;
+import java.io.InputStream;
+import java.io.IOException;
+
import org.apache.webdav.lib.State;
import org.apache.webdav.lib.Header;
import org.apache.webdav.lib.WebdavStatus;
@@ -74,10 +75,16 @@
* UNLOCK Method.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
+ * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
*/
public class UnlockMethod
extends WebdavMethodBase {
+ // ----------------------------------------------------- Instance Variables
+
+ private String lockToken = null;
+
+ private State state = null;
// ----------------------------------------------------------- Constructors
@@ -90,21 +97,31 @@
name = "UNLOCK";
}
-
+
+ // ------------------------------------------------------------- Properties
+
+ public void setLockToken(String lockToken) {
+ this.lockToken = lockToken;
+ }
// --------------------------------------------------- WebdavMethod Methods
-
/**
- * Generate the query body.
+ * Generate additional headers needed by the request.
*
- * @return String query
+ * @param host the host
+ * @param state State token
*/
- public String generateQuery() {
- return null;
+ public void generateHeaders(String host, State state) {
+
+ super.generateHeaders(host, state);
+
+ setHeader("Lock-Token", "(<" + lockToken + ">)");
+
+ this.state = state;
}
-
+
/**
* Parse response.
*
@@ -112,6 +129,9 @@
*/
public void parseResponse(InputStream is)
throws IOException {
+ if (getStatusCode() == WebdavStatus.SC_NO_CONTENT) {
+ state.removeLock(getPath(), lockToken);
+ }
}
1.1 jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/XMLResponseMethodBase.java
Index: XMLResponseMethodBase.java
===================================================================
/*
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.webdav.lib.methods;
import java.io.InputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.webdav.lib.WebdavStatus;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLResponseMethodBase
extends WebdavMethodBase {
// ----------------------------------------------------- Instance Variables
/**
* Response document
*/
private Document responseDocument = null;
// ------------------------------------------------------------- Properties
/**
* Response document getter.
*
* @return Document response document
*/
public Document getResponseDocument() {
return this.responseDocument;
}
// --------------------------------------------------- WebdavMethod Methods
/**
* Parse response. This method always assumes that the response is a
* valid XML response; subclasses should probably issue a status code
* check before calling <code>super.parseResponse(input)</code>.
*
* @param input Input stream
*/
public void parseResponse(InputStream input)
throws IOException {
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
responseDocument = builder.parse(new InputSource(input));
} catch (ParserConfigurationException e) {
// throw new IllegalArgumentException("XML Parser Configuration error", e);
} catch (SAXException e) {
throw new IllegalArgumentException(
"XML parsing error; response stream is not valid XML: " +
e.getMessage());
}
}
}
1.2 +3 -2 jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/util/DOMUtils.java
Index: DOMUtils.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/util/DOMUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DOMUtils.java 2000/11/22 06:19:11 1.1
+++ DOMUtils.java 2000/11/27 06:34:19 1.2
@@ -72,6 +72,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
@@ -147,12 +148,12 @@
* <P> In this case, calling this method on the
* <CODE>customer</CODE> element returns "Joe Schmoe".
*/
- public static String getTextValue(Element element) {
+ public static String getTextValue(Node node) {
// I *thought* that I should be able to use element.getNodeValue()...
String text = "";
- NodeList textList = element.getChildNodes();
+ NodeList textList = node.getChildNodes();
for (int i = 0; i < textList.getLength(); i++) {
try {
text += ((Text) textList.item(i)).getData();