KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/PropFindMethod.java,v 1.7 2004/08/02 15:45:48 unico Exp $
3  * $Revision: 1.7 $
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.util.Enumeration JavaDoc;
28 import java.util.Vector JavaDoc;
29 import org.apache.commons.httpclient.HttpConnection;
30 import org.apache.commons.httpclient.HttpException;
31 import org.apache.commons.httpclient.HttpState;
32 import org.apache.webdav.lib.PropertyName;
33 import org.apache.webdav.lib.util.XMLPrinter;
34
35 /**
36  * This class implements the WebDAV PROPFIND Method.
37  *
38  * <P> The PROPFIND method retrieves properties defined on the resource
39  * identified by the Request-URI, if the resource does not have any internal
40  * members, or on the resource identified by the Request-URI and potentially
41  * its member resources, if the resource is a collection that has internal
42  * member URIs.
43  *
44  * <P> A typical request looks like this:
45  *
46  * <PRE>
47  *
48  * PROPFIND /file HTTP/1.1
49  * Host: www.foo.bar
50  * Content-type: text/xml; charset="utf-8"
51  * Content-Length: xxxx
52  *
53  * &lt;?xml version="1.0" encoding="utf-8" ?&gt;
54  * &lt;D:propfind xmlns:D="DAV:"&gt;
55  * &lt;D:prop xmlns:R="http://www.foo.bar/boxschema/"&gt;
56  * &lt;R:bigbox/&gt;
57  * &lt;R:author/&gt;
58  * &lt;R:DingALing/&gt;
59  * &lt;R:Random/&gt;
60  * &lt;/D:prop&gt;
61  * &lt;/D:propfind&gt;
62  * </PRE>
63  *
64  */

65 public class PropFindMethod extends XMLResponseMethodBase
66     implements DepthSupport {
67
68
69     // -------------------------------------------------------------- Constants
70

71
72     /**
73      * Request of named properties.
74      */

75     public static final int BY_NAME = 0;
76
77
78     /**
79      * Request of all properties name and value.
80      */

81     public static final int ALL = 1;
82
83
84     /**
85      * Request of all properties name.
86      */

87     public static final int NAMES = 2;
88
89
90     // ----------------------------------------------------------- Constructors
91

92
93     /**
94      * Method constructor.
95      */

96     public PropFindMethod() {
97     }
98
99
100     /**
101      * Method constructor.
102      */

103     public PropFindMethod(String JavaDoc path) {
104         super(path);
105     }
106
107
108     /**
109      * Method constructor.
110      */

111     public PropFindMethod(String JavaDoc path, int depth) {
112         this(path);
113         setDepth(depth);
114     }
115
116
117     /**
118      * Method constructor.
119      */

120     public PropFindMethod(String JavaDoc path, int depth, int type) {
121         this(path);
122         setDepth(depth);
123         setType(type);
124     }
125
126
127     /**
128      * Method constructor.
129      */

130     public PropFindMethod(String JavaDoc path, Enumeration JavaDoc propertyNames) {
131         this(path);
132         setDepth(1);
133         setPropertyNames(propertyNames);
134         setType(BY_NAME);
135     }
136
137
138     /**
139      * Method constructor.
140      */

141     public PropFindMethod(String JavaDoc path, int depth, Enumeration JavaDoc propertyNames) {
142         this(path);
143         setDepth(depth);
144         setPropertyNames(propertyNames);
145         setType(BY_NAME);
146     }
147
148
149     // ----------------------------------------------------- Instance Variables
150

151
152     /**
153      * Type of the Propfind.
154      */

155     protected int type = ALL;
156
157
158     /**
159      * Property name list.
160      */

161     protected PropertyName[] propertyNames;
162
163     /**
164      * Depth.
165      */

166     protected int depth = DEPTH_INFINITY;
167
168
169     /**
170      * The namespace abbreviation that prefixes DAV tags
171      */

172     protected String JavaDoc prefix = null;
173
174
175     // ------------------------------------------------------------- Properties
176

177
178
179
180     /**
181      * Set a request header value, redirecting the special case of the "Depth" header
182      * to invoke {@link #setDepth} instead.
183      *
184      * @param headerName Header name
185      * @param headerValue Header value
186      */

187     public void setRequestHeader(String JavaDoc headerName, String JavaDoc headerValue) {
188         if (headerName.equalsIgnoreCase("Depth")){
189             int depth = -1;
190             if (headerValue.equals("0")){
191                 depth = DEPTH_0;
192             }
193             else if (headerValue.equals("1")){
194                 depth = DEPTH_1;
195             }
196             else if (headerValue.equalsIgnoreCase("infinity")){
197                 depth = DEPTH_INFINITY;
198             }
199             setDepth(depth);
200         }
201         else{
202             super.setRequestHeader(headerName, headerValue);
203         }
204     }
205
206
207     /**
208      * Type setter.
209      *
210      * @param type New type value
211      */

212     public void setType(int type) {
213         checkNotUsed();
214         this.type = type;
215     }
216
217
218     /**
219      * Type getter.
220      *
221      * @return int type value
222      */

223     public int getType() {
224         return type;
225     }
226
227
228     /**
229      * Depth setter.
230      *
231      * @param depth New depth value
232      */

233     public void setDepth(int depth) {
234         checkNotUsed();
235         this.depth = depth;
236     }
237
238
239     /**
240      * Depth getter.
241      *
242      * @return int depth value
243      */

244     public int getDepth() {
245         return depth;
246     }
247
248
249     /**
250      * Property names setter.
251      * The enumeration may contain strings with or without a namespace prefix
252      * but the preferred way is to provide PropertyName objects.
253      *
254      * @param propertyNames List of the property names
255      */

256     public void setPropertyNames(Enumeration JavaDoc propertyNames) {
257         checkNotUsed();
258
259         Vector JavaDoc list = new Vector JavaDoc();
260         while (propertyNames.hasMoreElements()) {
261
262             Object JavaDoc item = propertyNames.nextElement();
263
264             if (item instanceof PropertyName)
265             {
266                 list.add(item);
267             }
268             else if (item instanceof String JavaDoc)
269             {
270                 String JavaDoc propertyName = (String JavaDoc) item;
271
272                 int length = propertyName.length();
273                 boolean found = false;
274                 int i = 1;
275                 while (!found && (i <= length)) {
276                     char chr = propertyName.charAt(length - i);
277                     if (!Character.isUnicodeIdentifierPart(chr)
278                         && chr!='-' && chr!='_' && chr!='.') {
279                         found = true;
280                     } else {
281                         i++;
282                     }
283                 }
284                 if ((i == 1) || (i >= length)) {
285                     list.add(new PropertyName("DAV:",propertyName));
286                 } else {
287                     String JavaDoc namespace = propertyName.substring(0, length + 1 - i);
288                     String JavaDoc localName = propertyName.substring(length + 1 - i);
289                     list.add(new PropertyName(namespace,localName));
290                 }
291             }
292             else
293             {
294                 // unknown type
295
// ignore
296
}
297         }
298
299         this.propertyNames = (PropertyName[])list.toArray(new PropertyName[list.size()]);
300     }
301
302
303     // --------------------------------------------------- WebdavMethod Methods
304

305
306     public void recycle() {
307         super.recycle();
308         prefix = null;
309     }
310
311     public String JavaDoc getName() {
312         return "PROPFIND";
313
314     }
315
316
317     /**
318      * Generate additional headers needed by the request.
319      *
320      * @param state State token
321      * @param conn The connection being used to make the request.
322      */

323     public void addRequestHeaders(HttpState state, HttpConnection conn)
324     throws IOException JavaDoc, HttpException {
325
326         // set the default utf-8 encoding, if not already present
327
if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
328         super.addRequestHeaders(state, conn);
329
330         switch (depth) {
331         case DEPTH_0:
332             super.setRequestHeader("Depth", "0");
333             break;
334         case DEPTH_1:
335             super.setRequestHeader("Depth", "1");
336             break;
337         case DEPTH_INFINITY:
338             super.setRequestHeader("Depth", "infinity");
339             break;
340         }
341
342     }
343
344     /**
345      * DAV requests that contain a body must override this function to
346      * generate that body.
347      *
348      * <p>The default behavior simply returns an empty body.</p>
349      */

350     protected String JavaDoc generateRequestBody() {
351
352         XMLPrinter printer = new XMLPrinter();
353
354         printer.writeXMLHeader();
355         printer.writeElement("D", "DAV:", "propfind",
356                              XMLPrinter.OPENING);
357                              
358                              
359
360         switch (type) {
361         case ALL:
362             printer.writeElement("D", "allprop", XMLPrinter.NO_CONTENT);
363             break;
364         case NAMES:
365             printer.writeElement("D", "propname", XMLPrinter.NO_CONTENT);
366             break;
367         case BY_NAME:
368             printer.writeElement("D", "prop", XMLPrinter.OPENING);
369             for (int i=0 ; i<propertyNames.length ; i++)
370             {
371                 String JavaDoc namespace = propertyNames[i].getNamespaceURI();
372                 String JavaDoc localname = propertyNames[i].getLocalName();
373                 if ("DAV:".equals(namespace)) {
374                     printer.writeElement("D", localname, XMLPrinter.NO_CONTENT);
375                 } else {
376                     if (namespace.length() > 0) {
377                         printer.writeElement("ZZ", namespace, localname,
378                                 XMLPrinter.NO_CONTENT);
379                     } else {
380                         printer.writeElement(null, null, localname,
381                                 XMLPrinter.NO_CONTENT);
382                     }
383                 }
384             }
385             printer.writeElement("D", "prop", XMLPrinter.CLOSING);
386             break;
387         }
388
389         printer.writeElement("D", "propfind", XMLPrinter.CLOSING);
390
391         return printer.toString();
392     }
393
394     /**
395      * This method returns an enumeration of URL paths. If the PropFindMethod
396      * was sent to the URL of a collection, then there will be multiple URLs.
397      * The URLs are picked out of the <code>&lt;D:href&gt;</code> elements
398      * of the response.
399      *
400      * @return an enumeration of URL paths as Strings
401      */

402     public Enumeration JavaDoc getAllResponseURLs() {
403         checkUsed();
404         return getResponseURLs().elements();
405     }
406
407     /**
408      * Returns an enumeration of <code>Property</code> objects.
409      */

410     public Enumeration JavaDoc getResponseProperties(String JavaDoc urlPath) {
411         checkUsed();
412
413         Response response = (Response) getResponseHashtable().get(urlPath);
414         if (response != null) {
415             return response.getProperties();
416         } else {
417             return (new Vector JavaDoc()).elements();
418         }
419
420     }
421 }
422
423
Popular Tags