KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > methods > PropPatchMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/PropPatchMethod.java,v 1.6 2004/08/02 15:45:48 unico Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/08/02 15:45:48 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib.methods;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import org.apache.commons.httpclient.HttpConnection;
31 import org.apache.commons.httpclient.HttpException;
32 import org.apache.commons.httpclient.HttpState;
33 import org.apache.webdav.lib.util.WebdavStatus;
34 import org.apache.webdav.lib.util.XMLPrinter;
35
36
37 /**
38  * PROPPATCH Method.
39  *
40  */

41 public class PropPatchMethod
42     extends XMLResponseMethodBase {
43
44
45     // ----------------------------------------------------------- Constructors
46

47
48     /**
49      * Method constructor.
50      */

51     public PropPatchMethod() {
52     }
53
54
55     /**
56      * Method constructor.
57      */

58     public PropPatchMethod(String JavaDoc path) {
59         super(path);
60     }
61
62
63     // ----------------------------------------------------- Instance Variables
64

65
66     /**
67      * Hashtable of the properties to set.
68      */

69     protected Hashtable JavaDoc toSet = new Hashtable JavaDoc();
70
71
72     /**
73      * Hashtable of the properties to remove.
74      */

75     protected Hashtable JavaDoc toRemove = new Hashtable JavaDoc();
76
77
78     // --------------------------------------------------------- Public Methods
79

80
81     /**
82      * Add a new property to set.
83      *
84      * @param name Property name
85      * @param value Property value
86      */

87     public void addPropertyToSet(String JavaDoc name, String JavaDoc value) {
88         checkNotUsed();
89         Property propertyToSet = new Property();
90         if (name != null) {
91             propertyToSet.name = name;
92             if (value != null)
93                 propertyToSet.value = value;
94             else
95                 propertyToSet.value = "";
96             toSet.put(name, propertyToSet);
97         }
98     }
99
100
101     /**
102      * Add a new property to set.
103      *
104      * @param name Property name
105      * @param value Property value
106      * @param namespace Namespace abbreviation
107      * @param namespaceInfo Namespace information
108      */

109     public void addPropertyToSet(String JavaDoc name, String JavaDoc value, String JavaDoc namespace,
110                                  String JavaDoc namespaceInfo) {
111         checkNotUsed();
112         Property propertyToSet = new Property();
113         if (name != null) {
114             propertyToSet.name = name;
115             if (value != null)
116                 propertyToSet.value = value;
117             else
118                 propertyToSet.value = "";
119             propertyToSet.namespace = namespace;
120             propertyToSet.namespaceInfo = namespaceInfo;
121             toSet.put(namespace + name, propertyToSet);
122         }
123     }
124
125
126     /**
127      * Add property to remove.
128      *
129      * @param name Property name
130      */

131     public void addPropertyToRemove(String JavaDoc name) {
132         checkNotUsed();
133         Property propertyToRemove = new Property();
134         if (name != null) {
135             propertyToRemove.name = name;
136             toRemove.put(name, propertyToRemove);
137         }
138     }
139
140
141     /**
142      * Add property to remove.
143      *
144      * @param name Property name
145      * @param namespace Namespace abbreviation
146      * @param namespaceInfo Namespace information
147      */

148     public void addPropertyToRemove(String JavaDoc name, String JavaDoc namespace,
149                                     String JavaDoc namespaceInfo) {
150         checkNotUsed();
151         Property propertyToRemove = new Property();
152         if (name != null) {
153             propertyToRemove.name = name;
154             propertyToRemove.namespace = namespace;
155             propertyToRemove.namespaceInfo = namespaceInfo;
156             toRemove.put(name, propertyToRemove);
157         }
158     }
159
160
161     // --------------------------------------------------- WebdavMethod Methods
162

163
164     public String JavaDoc getName() {
165         return "PROPPATCH";
166     }
167
168     /**
169      * Generate additional headers needed by the request.
170      *
171      * @param state State token
172      * @param conn the connection
173      */

174     public void addRequestHeaders(HttpState state, HttpConnection conn)
175     throws IOException JavaDoc, HttpException {
176
177         // set the default utf-8 encoding, if not already present
178
if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
179         super.addRequestHeaders(state, conn);
180
181     }
182
183     /**
184      * DAV requests that contain a body must override this function to
185      * generate that body.
186      *
187      * <p>The default behavior simply returns an empty body.</p>
188      */

189     protected String JavaDoc generateRequestBody() {
190         XMLPrinter printer = new XMLPrinter();
191
192
193         printer.writeXMLHeader();
194         printer.writeElement("D", "DAV:", "propertyupdate",
195                              XMLPrinter.OPENING);
196
197         if (toSet.size() > 0) {
198
199             printer.writeElement("D", null, "set", XMLPrinter.OPENING);
200
201             Enumeration JavaDoc toSetList = toSet.elements();
202             printer.writeElement("D", null, "prop", XMLPrinter.OPENING);
203             while (toSetList.hasMoreElements()) {
204                 Property current = (Property) toSetList.nextElement();
205                 if ("DAV:".equals(current.namespaceInfo)) {
206                     printer.writeProperty("D", null, current.name, current.value);
207                 }
208                 else {
209                     printer.writeProperty(current.namespace, current.namespaceInfo,
210                                       current.name, current.value);
211                 }
212             }
213             printer.writeElement("D", null, "prop", XMLPrinter.CLOSING);
214
215             printer.writeElement("D", null, "set", XMLPrinter.CLOSING);
216
217         }
218
219         if (toRemove.size() > 0) {
220
221             printer.writeElement("D", null, "remove", XMLPrinter.OPENING);
222
223             Enumeration JavaDoc toRemoveList = toRemove.elements();
224             printer.writeElement("D", null, "prop", XMLPrinter.OPENING);
225             while (toRemoveList.hasMoreElements()) {
226                 Property current = (Property) toRemoveList.nextElement();
227                 printer.writeElement(current.namespace, current.namespaceInfo,
228                                      current.name, XMLPrinter.NO_CONTENT);
229             }
230             printer.writeElement("D", null, "prop", XMLPrinter.CLOSING);
231
232             printer.writeElement("D", null, "remove", XMLPrinter.CLOSING);
233
234         }
235
236         printer.writeElement("D", "propertyupdate", XMLPrinter.CLOSING);
237
238         return printer.toString();
239     }
240
241     /**
242      * Parse response.
243      *
244      * @param input Input stream
245      */

246     public void parseResponse(InputStream JavaDoc input, HttpState state, HttpConnection conn)
247         throws IOException JavaDoc, HttpException {
248         try
249         {
250             int code = getStatusLine().getStatusCode();
251             if (code == WebdavStatus.SC_CONFLICT ||
252                 code == WebdavStatus.SC_MULTI_STATUS ||
253                 code == WebdavStatus.SC_FORBIDDEN ) {
254                 parseXMLResponse(input);
255             }
256         }
257         catch (IOException JavaDoc e) {
258                 // FIX ME: provide a way to deliver non xml data
259
}
260     }
261
262
263     // --------------------------------------------------- Property Inner Class
264

265
266     private class Property {
267
268         public String JavaDoc name = "";
269         public String JavaDoc namespace;
270         public String JavaDoc namespaceInfo;
271         public String JavaDoc value;
272
273     }
274
275
276 }
277
Popular Tags