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.AbstractWriteDAVMethod;
026: import org.skunk.dav.client.DAVConstants;
027: import org.skunk.dav.client.DAVMethodName;
028: import org.skunk.dav.client.DAVProperty;
029: import org.skunk.dav.client.IfHeader;
030: import org.skunk.minixml.MalformedXMLException;
031: import org.skunk.minixml.XMLDocument;
032: import org.skunk.minixml.XMLElement;
033: import org.skunk.minixml.XMLViewer;
034: import org.skunk.trace.Debug;
035: import java.io.IOException;
036: import java.util.ArrayList;
037: import java.util.Iterator;
038:
039: public class PropPatchMethod extends AbstractWriteDAVMethod {
040: private ArrayList patchProps = new ArrayList();
041: private ArrayList unpatchProps = new ArrayList();
042: private String lockToken;
043:
044: public PropPatchMethod(String url) {
045: super (url);
046: }
047:
048: public final DAVMethodName getRequestMethodName() {
049: return DAVMethodName.PROPPATCH;
050: }
051:
052: public void processRequestBody() {
053: String NScode = "skunk";
054: XMLElement propertyUpdateElem = new XMLElement(
055: DAVConstants.PROPERTYUPDATE_ELEM, NScode,
056: DAVConstants.DAV_NAMESPACE);
057: if (patchProps.size() > 0) {
058: XMLElement setElem = new XMLElement(DAVConstants.SET_ELEM,
059: NScode);
060: for (Iterator it = patchProps.iterator(); it.hasNext();)
061: setElem.addChild(new XMLElement(DAVConstants.PROP_ELEM,
062: NScode).addChild((DAVProperty) it.next()));
063: propertyUpdateElem.addChild(setElem);
064: }
065: if (unpatchProps.size() > 0) {
066: XMLElement removeElem = new XMLElement(
067: DAVConstants.REMOVE_ELEM, NScode);
068: for (Iterator it = unpatchProps.iterator(); it.hasNext();)
069: removeElem.addChild(new XMLElement(
070: DAVConstants.PROP_ELEM, NScode)
071: .addChild((DAVProperty) it.next()));
072: propertyUpdateElem.addChild(removeElem);
073: }
074: XMLDocument doc = new XMLDocument();
075: try {
076: doc.addElement(DAVConstants.XML_BOILERPLATE);
077: doc.addElement(propertyUpdateElem);
078: } catch (MalformedXMLException malex) {
079: Debug.trace(this , Debug.DP2, malex);
080: }
081: if (Debug.isDebug(this , Debug.DP8)) {
082: XMLViewer.viewDocumentDialog(doc);
083: }
084: setRequestBody((doc.toString() + DAVConstants.CRLF).getBytes());
085: }
086:
087: public void putProperty(DAVProperty property, Object value) {
088: property.addChild(value);
089: patchProps.add(property);
090: }
091:
092: public void removeProperty(DAVProperty property) {
093: unpatchProps.add(property);
094: }
095:
096: /**
097: * convenience method for the case when there is only one
098: * lock token; for more flexibility, create an IfHeader.
099: * Note that using this will create a new IfHeader at request header
100: * processing time, destroying any IfHeader that has been manually
101: * created!! This method exists solely for backwards compatibility;
102: * it will probably be deprecated in the future.
103: */
104: public void setLockToken(String lockToken) {
105: this .lockToken = lockToken;
106: }
107:
108: /**
109: * accesses the locktoken property set with the setLockToken() method.
110: */
111: public String getLockToken() {
112: return lockToken;
113: }
114:
115: public void processRequestHeaders() {
116: if (this .lockToken != null) {
117: // blow away any if headers that are lying around
118: IfHeader iffy = new IfHeader(true);
119: iffy.addStateToken(getRequestURL(), lockToken);
120: this .setIfHeader(iffy);
121: }
122: super .processRequestHeaders();
123: }
124: }
125:
126: /* $Log: PropPatchMethod.java,v $
127: /* Revision 1.10 2001/07/17 04:41:32 smulloni
128: /* integrated IfHeader more closely into method package; added lock test.
129: /*
130: /* Revision 1.9 2001/07/15 18:16:37 smulloni
131: /* fixed some copyright notices.
132: /*
133: /* Revision 1.8 2001/01/02 17:43:38 smulloni
134: /* changed debug level for XMLViewer popups to Debug.DP8.
135: /*
136: /* Revision 1.7 2000/12/03 23:53:27 smulloni
137: /* added license and copyright preamble to java files.
138: /*
139: /* Revision 1.6 2000/11/14 23:18:32 smullyan
140: /* fix for responsedescription property in multistatus
141: /*
142: /* Revision 1.5 2000/11/09 23:35:09 smullyan
143: /* log added to every Java file, with the help of python. Lock stealing
144: /* implemented, and treatment of locks made more robust.
145: /* */
|