KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/ReportMethod.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.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 REPORT Method.
37  *
38  * <P> The REPORT 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  */

47 public class ReportMethod extends XMLResponseMethodBase
48     implements DepthSupport {
49
50
51     // -------------------------------------------------------------- Constants
52

53
54     /**
55      * Request specified properties.
56      */

57     public static final int SUB_SET = 0;
58
59
60     /**
61      * Request of all properties name and value.
62      */

63     public static final int ALL = 1;
64
65     public static final int LOCATE_HISTORY = 2;
66
67     public String JavaDoc sVersionHistory ="";
68
69     private String JavaDoc preloadedQuery = null;
70
71     // ----------------------------------------------------------- Constructors
72

73
74     /**
75      * Method constructor.
76      */

77     public ReportMethod() {
78     }
79
80
81     /**
82      * Method constructor.
83      */

84     public ReportMethod(String JavaDoc path) {
85         super(path);
86     }
87
88
89     /**
90      * Method constructor.
91      */

92     public ReportMethod(String JavaDoc path, int depth) {
93         this(path);
94         setDepth(depth);
95     }
96
97     /**
98      * Method constructor.
99      */

100     public ReportMethod(String JavaDoc path, Enumeration JavaDoc propertyNames) {
101         this(path);
102         setDepth(1);
103         setPropertyNames(propertyNames);
104         setType(SUB_SET);
105     }
106
107 /**
108      * Method constructor.
109      */

110     public ReportMethod(String JavaDoc path, int depth, Enumeration JavaDoc propertyNames, Enumeration JavaDoc histUrl) {
111         this(path);
112         setDepth(depth);
113         setPropertyNames(propertyNames);
114         setHistoryURLs(histUrl);
115         setType(LOCATE_HISTORY);
116     }
117
118     /**
119      * Method constructor.
120      */

121     public ReportMethod(String JavaDoc path, int depth, Enumeration JavaDoc propertyNames) {
122         this(path);
123         setDepth(depth);
124         setPropertyNames(propertyNames);
125         setType(SUB_SET);
126     }
127
128
129
130     public ReportMethod(String JavaDoc path, int depth, String JavaDoc sBody) {
131         this(path);
132         setDepth(depth);
133         setType(SUB_SET);
134         preloadedQuery = sBody;
135     }
136
137
138     // ----------------------------------------------------- Instance Variables
139

140
141     /**
142      * Type of the Propfind.
143      */

144     protected int type = ALL;
145
146
147     /**
148      * Property name list.
149      */

150     protected PropertyName[] propertyNames;
151
152     /**
153      * Depth.
154      */

155     protected int depth = DEPTH_INFINITY;
156
157
158     /**
159      * The namespace abbreviation that prefixes DAV tags
160      */

161     protected String JavaDoc prefix = null;
162
163
164     // ------------------------------------------------------------- Properties
165

166
167
168
169     /**
170      * Set a header value, redirecting attempts to set the "Depth" header to
171      * a {@link #setDepth} call.
172      *
173      * @param headerName Header name
174      * @param headerValue Header value
175      */

176     public void setRequestHeader(String JavaDoc headerName, String JavaDoc headerValue) {
177         if (headerName.equalsIgnoreCase("Depth")){
178             int depth = -1;
179             if (headerValue.equals("0")){
180                 depth = DEPTH_0;
181             }
182             if (headerValue.equals("1")){
183                 depth = DEPTH_1;
184             }
185             else if (headerValue.equalsIgnoreCase("infinity")){
186                 depth = DEPTH_INFINITY;
187             }
188             setDepth(depth);
189         }
190         else{
191             super.setRequestHeader(headerName, headerValue);
192         }
193     }
194
195
196     /**
197      * Type setter.
198      *
199      * @param type New type value
200      */

201     public void setType(int type) {
202         checkNotUsed();
203         this.type = type;
204     }
205
206
207     /**
208      * Type getter.
209      *
210      * @return int type value
211      */

212     public int getType() {
213         return type;
214     }
215
216
217     /**
218      * Depth setter.
219      *
220      * @param depth New depth value
221      */

222     public void setDepth(int depth) {
223         checkNotUsed();
224         this.depth = depth;
225     }
226
227
228     /**
229      * Depth getter.
230      *
231      * @return int depth value
232      */

233     public int getDepth() {
234         return depth;
235     }
236
237
238     /**
239      * Property names setter.
240      * The enumeration may contain strings with or without a namespace prefix
241      * but the preferred way is to provide PropertyName objects.
242      *
243      * @param propertyNames List of the property names
244      */

245     public void setPropertyNames(Enumeration JavaDoc propertyNames) {
246         checkNotUsed();
247
248         Vector JavaDoc list = new Vector JavaDoc();
249         while (propertyNames.hasMoreElements()) {
250
251             Object JavaDoc item = propertyNames.nextElement();
252
253             if (item instanceof PropertyName)
254             {
255                 list.add(item);
256             }
257             else if (item instanceof String JavaDoc)
258             {
259                 String JavaDoc propertyName = (String JavaDoc) item;
260
261                 int length = propertyName.length();
262                 boolean found = false;
263                 int i = 1;
264                 while (!found && (i <= length)) {
265                     char chr = propertyName.charAt(length - i);
266                     if (!Character.isUnicodeIdentifierPart(chr)
267                         && chr!='-' && chr!='_' && chr!='.') {
268                         found = true;
269                     } else {
270                         i++;
271                     }
272                 }
273                 if ((i == 1) || (i >= length)) {
274                     list.add(new PropertyName("DAV:",propertyName));
275                 } else {
276                     String JavaDoc namespace = propertyName.substring(0, length + 1 - i);
277                     String JavaDoc localName = propertyName.substring(length + 1 - i);
278                     list.add(new PropertyName(namespace,localName));
279                 }
280             }
281             else
282             {
283                 // unknown type
284
// ignore
285
}
286         }
287
288         this.propertyNames = (PropertyName[])list.toArray(new PropertyName[list.size()]);
289     }
290
291     /**
292      * sets History URL for locate by history Report
293      */

294     public void setHistoryURLs(Enumeration JavaDoc historyURLs) {
295
296         sVersionHistory = "<D:version-history-set>";
297
298         while (historyURLs.hasMoreElements())
299         {
300             sVersionHistory += "<D:href>"+ historyURLs.nextElement().toString() + "</D:href>";
301         }
302
303         sVersionHistory += "</D:version-history-set>";
304     }
305
306
307     // --------------------------------------------------- WebdavMethod Methods
308

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

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

351     protected String JavaDoc generateRequestBody() {
352
353         if (preloadedQuery != null)
354             return preloadedQuery;
355
356         XMLPrinter printer = new XMLPrinter();
357
358
359         printer.writeXMLHeader();
360         if (type!= LOCATE_HISTORY)
361             printer.writeElement("D", "DAV:", "version-tree",
362                              XMLPrinter.OPENING);
363
364         switch (type) {
365         case ALL:
366             printer.writeElement("D", "allprop", XMLPrinter.NO_CONTENT);
367             printer.writeElement("D", "version-tree", XMLPrinter.CLOSING);
368             break;
369
370         case SUB_SET:
371             printer.writeElement("D", "prop", XMLPrinter.OPENING);
372             for (int i=0 ; i<propertyNames.length ; i++)
373             {
374                 String JavaDoc namespace = propertyNames[i].getNamespaceURI();
375                 String JavaDoc localname = propertyNames[i].getLocalName();
376                 if ("DAV:".equals(namespace)) {
377                     printer.writeElement("D", localname, XMLPrinter.NO_CONTENT);
378                 } else {
379                     printer.writeElement("ZZ", namespace,localname , XMLPrinter.NO_CONTENT);
380                 }
381             }
382             printer.writeElement("D", "prop", XMLPrinter.CLOSING);
383             printer.writeElement("D", "version-tree", XMLPrinter.CLOSING);
384             break;
385
386         case LOCATE_HISTORY:
387             printer.writeElement("D", "DAV:", "locate-by-history", XMLPrinter.OPENING);
388
389             printer.writeText(sVersionHistory);
390
391             printer.writeElement("D", "prop", XMLPrinter.OPENING);
392             for (int i=0 ; i<propertyNames.length ; i++)
393             {
394                 String JavaDoc namespace = propertyNames[i].getNamespaceURI();
395                 String JavaDoc localname = propertyNames[i].getLocalName();
396                 if ("DAV:".equals(namespace)) {
397                     printer.writeElement("D", localname, XMLPrinter.NO_CONTENT);
398                 } else {
399                     printer.writeElement("ZZ", namespace,localname , XMLPrinter.NO_CONTENT);
400                 }
401             }
402             printer.writeElement("D", "prop", XMLPrinter.CLOSING);
403             printer.writeElement("D", "locate-by-history", XMLPrinter.CLOSING);
404             break;
405
406         }
407
408         //System.out.println(query);
409
return printer.toString();
410
411     }
412
413     /**
414      * This method returns an enumeration of URL paths. If the ReportMethod
415      * was sent to the URL of a collection, then there will be multiple URLs.
416      * The URLs are picked out of the <code>&lt;D:href&gt;</code> elements
417      * of the response.
418      *
419      * @return an enumeration of URL paths as Strings
420      */

421     public Enumeration JavaDoc getAllResponseURLs() {
422         checkUsed();
423         return getResponseURLs().elements();
424     }
425
426
427     /**
428      * Returns an enumeration of <code>Property</code> objects.
429      */

430             public Enumeration JavaDoc getResponseProperties(String JavaDoc urlPath) {
431         checkUsed();
432
433         Response response = (Response) getResponseHashtable().get(urlPath);
434         if (response != null) {
435             return response.getProperties();
436         } else {
437             return (new Vector JavaDoc()).elements();
438         }
439
440          }
441 }
442
443
Popular Tags