001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.method;
024:
025: import org.skunk.dav.client.AbstractDAVMethod;
026: import org.skunk.dav.client.DAVConstants;
027: import org.skunk.dav.client.DAVMethodName;
028: import org.skunk.dav.client.Depth;
029: import org.skunk.dav.client.IfHeader;
030: import org.skunk.dav.client.LockScope;
031: import org.skunk.dav.client.PropertyHandler;
032: import org.skunk.dav.client.Timeout;
033: import org.skunk.minixml.MalformedXMLException;
034: import org.skunk.minixml.XMLDocument;
035: import org.skunk.minixml.XMLElement;
036: import org.skunk.minixml.XMLParser;
037: import org.skunk.minixml.XMLViewer;
038: import org.skunk.trace.Debug;
039: import java.io.IOException;
040: import java.text.MessageFormat;
041: import java.util.ListIterator;
042: import java.util.Map;
043:
044: public class LockMethod extends AbstractDAVMethod {
045: private String lockToken;
046: private Depth depth = Depth.ZERO;
047: private Timeout timeout;
048: private LockScope lockScope;
049: private boolean isRefresh = false;
050: private String userHref;
051: private IfHeader ifHeader = null;
052:
053: private static final String lockRequestXML = createLockRequestXML();
054:
055: public LockMethod(String url, String userHref) {
056: this (url, Depth.ZERO, null, LockScope.EXCLUSIVE, userHref);
057: }
058:
059: public LockMethod(String url, Depth depth, Timeout timeout,
060: LockScope lockScope, String userHref) {
061: super (url);
062: this .depth = depth;
063: this .timeout = timeout;
064: this .lockScope = lockScope;
065: this .userHref = userHref;
066: }
067:
068: public void setDepth(Depth depth) {
069: if (depth == Depth.ONE)
070: throw new IllegalArgumentException(
071: "lock depth must be zero or infinity");
072: else
073: this .depth = depth;
074: }
075:
076: public Depth getDepth() {
077: return depth;
078: }
079:
080: public Timeout getTimeout() {
081: return timeout;
082: }
083:
084: public void setTimeout(Timeout timeout) {
085: this .timeout = timeout;
086: }
087:
088: public LockScope getLockScope() {
089: return this .lockScope;
090: }
091:
092: public void setLockScope(LockScope lockScope) {
093: this .lockScope = lockScope;
094: }
095:
096: private XMLElement getLockScopeElement() {
097: return new XMLElement(
098: (this .lockScope == LockScope.EXCLUSIVE) ? DAVConstants.EXCLUSIVE_ELEM
099: : DAVConstants.SHARED_ELEM);
100: }
101:
102: public void setIfHeader(IfHeader ifHeader) {
103: this .ifHeader = ifHeader;
104: }
105:
106: public IfHeader getIfHeader() {
107: return this .ifHeader;
108: }
109:
110: public void processRequestHeaders() {
111: Map headers = getRequestHeaders();
112: headers.put(DAVConstants.DEPTH_HEADER, depth);
113: if (timeout != null)
114: headers.put(DAVConstants.TIMEOUT_HEADER, timeout);
115: if (!isRefresh())
116: headers.put(DAVConstants.CONTENT_TYPE, "text/xml");
117: if (ifHeader != null)
118: headers.put(DAVConstants.IF_HEADER, ifHeader.toString());
119: }
120:
121: public DAVMethodName getRequestMethodName() {
122: return DAVMethodName.LOCK;
123: }
124:
125: public void processResponseHeaders() {
126: //get lock-token
127: int s = getStatus();
128: if (s >= 200 && s > 300) {
129: Map headers = getResponseHeaders();
130: if (headers.containsKey(DAVConstants.LOCK_TOKEN_HEADER)) {
131: Object lockTokenObj = headers
132: .get(DAVConstants.LOCK_TOKEN_HEADER);
133: this .lockToken = lockTokenObj.toString();
134: }
135: }
136: }
137:
138: public void processResponseBody() throws MalformedXMLException {
139: int status = getStatus();
140: if (status == 207) {
141: super .processResponseBody();
142:
143: } else if (status == 200) {
144: byte[] body = getResponseBody();
145: if (body == null || getStatus() >= 400)
146: return;
147: String s = new String(body).trim();
148: XMLParser parser = new XMLParser();
149: try {
150: XMLDocument doc = parser.parse(s);
151: if (Debug.isDebug(this , Debug.DP8)) {
152: XMLViewer.viewDocumentDialog(doc);
153: }
154: //capture locktoken if we don't already have it from the header
155: if (lockToken == null) {
156: XMLElement elem = doc
157: .getElement("prop/lockdiscovery/activelock/locktoken/href");
158: Object bodyLock = (elem != null) ? elem.getChild(0)
159: : null;
160: if (bodyLock != null)
161: lockToken = bodyLock.toString();
162: }
163: //populate DAVFile
164: XMLElement propElem = doc.getElement("prop");
165: if (propElem == null)
166: return;
167: for (ListIterator lit = propElem.children(); lit
168: .hasNext();) {
169: Object propChile = lit.next();
170: if (propChile instanceof XMLElement)
171: PropertyHandler.handleProperty(getDAVFile(),
172: (XMLElement) propChile);
173: }
174: } catch (IOException oyVeh) {
175: oyVeh.printStackTrace();
176: throw new MalformedXMLException(oyVeh.getMessage());
177: }
178: }
179: }
180:
181: public void setRefresh(boolean isRefresh) {
182: this .isRefresh = isRefresh;
183: }
184:
185: public boolean isRefresh() {
186: return isRefresh;
187: }
188:
189: public void processRequestBody() {
190: if (!isRefresh) //for refreshes, do not include request body
191: {
192: String s = MessageFormat
193: .format(getLockRequestXML(), new Object[] {
194: getLockScopeElement(), getUserHref() });
195: setRequestBody(s.getBytes());
196: }
197: }
198:
199: private static String getLockRequestXML() {
200: return lockRequestXML;
201: }
202:
203: private static String createLockRequestXML() {
204: try {
205: XMLElement lockInfoElem = new XMLElement(
206: DAVConstants.LOCKINFO_ELEM).setAttribute(
207: DAVConstants.XML_NAMESPACE_ATTR,
208: DAVConstants.DAV_NAMESPACE);
209: XMLElement lockScopeElem = new XMLElement(
210: DAVConstants.LOCKSCOPE_ELEM).addChild("{0}"); //new XMLElement(DAVConstants.EXCLUSIVE_ELEM));
211: XMLElement lockTypeElem = new XMLElement(
212: DAVConstants.LOCKTYPE_ELEM)
213: .addChild(new XMLElement(DAVConstants.WRITE_ELEM));
214: XMLElement ownerElem = new XMLElement(
215: DAVConstants.OWNER_ELEM).addChild(new XMLElement(
216: DAVConstants.HREF_ELEM).addChild("{1}")); //message format code
217: lockInfoElem.addChild(lockScopeElem);
218: lockInfoElem.addChild(lockTypeElem);
219: lockInfoElem.addChild(ownerElem);
220: XMLDocument minidoc = new XMLDocument();
221: minidoc.addElement(DAVConstants.XML_BOILERPLATE);
222: minidoc.addElement(lockInfoElem);
223: return minidoc.toString() + DAVConstants.CRLF;
224: } catch (MalformedXMLException malex) //only thrown in development
225: {
226: Debug.trace(LockMethod.class, Debug.DP1, malex);
227: return null;
228: }
229: }
230:
231: public String getLockToken() {
232: return lockToken;
233: }
234:
235: public String getUserHref() {
236: return userHref;
237: }
238: }
239:
240: /* $Log: LockMethod.java,v $
241: /* Revision 1.13 2001/07/18 20:20:53 smulloni
242: /* added IfHeader to LockMethod; fixed peculiar DAVFile bug (in getLock());
243: /* various fixes, new cases to auto.py
244: /*
245: /* Revision 1.12 2001/07/15 18:16:37 smulloni
246: /* fixed some copyright notices.
247: /*
248: /* Revision 1.11 2001/01/02 17:43:38 smulloni
249: /* changed debug level for XMLViewer popups to Debug.DP8.
250: /*
251: /* Revision 1.10 2000/12/03 23:53:26 smulloni
252: /* added license and copyright preamble to java files.
253: /*
254: /* Revision 1.9 2000/11/09 23:35:07 smullyan
255: /* log added to every Java file, with the help of python. Lock stealing
256: /* implemented, and treatment of locks made more robust.
257: /* */
|