KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/OptionsMethod.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.ByteArrayInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import java.util.Vector JavaDoc;
34 import javax.xml.parsers.DocumentBuilder JavaDoc;
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37 import org.apache.commons.httpclient.Header;
38 import org.apache.commons.httpclient.HttpConnection;
39 import org.apache.commons.httpclient.HttpException;
40 import org.apache.commons.httpclient.HttpState;
41 import org.apache.webdav.lib.util.WebdavStatus;
42 import org.apache.webdav.lib.util.XMLPrinter;
43 import org.w3c.dom.Document JavaDoc;
44 import org.xml.sax.InputSource JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46
47
48 /**
49  * OPTIONS Method.
50  *
51  */

52 public class OptionsMethod
53     extends XMLResponseMethodBase {
54
55
56     // -------------------------------------------------------------- Constants
57

58
59     /**
60      * DAV level 1. Mandatory.
61      */

62     public static final String JavaDoc DAV_LEVEL1 = "1";
63
64
65     /**
66      * DAV level 2.
67      */

68     public static final String JavaDoc DAV_LEVEL2 = "2";
69
70
71     /**
72      * Advanced collections.
73      */

74     public static final String JavaDoc ADVANCED_COLLECTIONS = "3";
75
76
77     /**
78      * Delta V.
79      */

80     public static final String JavaDoc DELTAV = "4";
81
82
83     /**
84      * ACL.
85      */

86     public static final String JavaDoc ACL = "5";
87
88
89     /**
90      * DASL.
91      */

92     public static final String JavaDoc DASL = "6";
93
94     /**
95      *
96      */

97     public static final int OPTIONS_WORKSPACE = 8;
98
99     /**
100      *
101      */

102     public static final int OPTIONS_VERSION_HISTORY = 9;
103
104
105
106     // ----------------------------------------------------------- Constructors
107

108
109     /**
110      * Method constructor.
111      */

112     public OptionsMethod() {
113     }
114
115
116     /**
117      * Method constructor.
118      */

119     public OptionsMethod(String JavaDoc path) {
120         super(path);
121     }
122
123     /**
124      * Method constructor.
125      */

126     public OptionsMethod(String JavaDoc path, int type) {
127         super(path);
128         this.type = type;
129     }
130
131     // ----------------------------------------------------- Instance Variables
132
/**
133      * DAV Capabilities.
134      */

135     private Vector JavaDoc davCapabilities = new Vector JavaDoc();
136
137
138     /**
139      * Methods allowed.
140      */

141     private Vector JavaDoc methodsAllowed = new Vector JavaDoc();
142
143     private int type = 0;
144
145     private boolean hasXMLBody = false;
146
147
148     // --------------------------------------------------------- Public Methods
149

150
151     /**
152      * Is the specified method allowed ?
153      */

154     public boolean isAllowed(String JavaDoc method) {
155         checkUsed();
156         return methodsAllowed.contains(method);
157     }
158
159
160     /**
161      * Get a list of allowed methods.
162      */

163     public Enumeration JavaDoc getAllowedMethods() {
164         checkUsed();
165         return methodsAllowed.elements();
166     }
167
168
169     /**
170      * Is DAV capability supported ?
171      */

172     public boolean isSupported(String JavaDoc capability) {
173         checkUsed();
174         return davCapabilities.contains(capability);
175     }
176
177
178     /**
179      * Get a list of supported DAV capabilities.
180      */

181     public Enumeration JavaDoc getDavCapabilities() {
182         checkUsed();
183         return davCapabilities.elements();
184     }
185
186     /**
187      * Parse response.
188      *
189      * @param input Input stream
190      */

191     public void parseResponse(InputStream JavaDoc input, HttpState state, HttpConnection conn)
192         throws IOException JavaDoc, HttpException {
193         try
194         {
195             if (getStatusLine().getStatusCode() == WebdavStatus.SC_OK && hasXMLBody) {
196                 parseXMLResponse(input);
197             }
198         }
199         catch (IOException JavaDoc e) {
200                 // FIX ME: provide a way to deliver non xml data
201
}
202     }
203
204
205     // --------------------------------------------------- WebdavMethod Methods
206

207
208
209
210
211     /**
212      * Process response headers. The contract of this method is that it only
213      * parses the response headers.
214      *
215      * @param state the state
216      * @param conn the connection
217      */

218     public void processResponseHeaders(HttpState state,
219         HttpConnection conn) {
220
221         Header davHeader = getResponseHeader("dav");
222         if (davHeader != null) {
223             String JavaDoc davHeaderValue = davHeader.getValue();
224             StringTokenizer JavaDoc tokenizer =
225                 new StringTokenizer JavaDoc(davHeaderValue, ",");
226             while (tokenizer.hasMoreElements()) {
227                 String JavaDoc davCapability = tokenizer.nextToken().trim();
228                 davCapabilities.addElement(davCapability);
229             }
230         }
231
232         Header allowHeader = getResponseHeader("allow");
233         if (allowHeader != null) {
234             String JavaDoc allowHeaderValue = allowHeader.getValue();
235             StringTokenizer JavaDoc tokenizer =
236                 new StringTokenizer JavaDoc(allowHeaderValue, ",");
237             while (tokenizer.hasMoreElements()) {
238                 String JavaDoc methodAllowed =
239                     tokenizer.nextToken().trim().toUpperCase();
240                 methodsAllowed.addElement(methodAllowed);
241             }
242         }
243
244         Header lengthHeader = getResponseHeader("content-length");
245         Header typeHeader = getResponseHeader("content-type");
246         if(
247                 (lengthHeader != null &&
248                  Integer.parseInt(lengthHeader.getValue()) > 0) ||
249                 (typeHeader != null &&
250                  typeHeader.getValue().startsWith("text/xml")))
251             hasXMLBody = true;
252
253         super.processResponseHeaders(state, conn);
254     }
255
256     /**
257      * DAV requests that contain a body must override this function to
258      * generate that body.
259      *
260      * <p>The default behavior simply returns an empty body.</p>
261      */

262     protected String JavaDoc generateRequestBody() {
263
264         if (type != 0){
265             XMLPrinter printer = new XMLPrinter();
266
267             printer.writeXMLHeader();
268             //System.out.println(printer.toString());
269
printer.writeElement("D", "DAV:", "options",
270                                  XMLPrinter.OPENING);
271
272             if (type == OPTIONS_VERSION_HISTORY)
273                 printer.writeElement("D", "version-history-collection-set", XMLPrinter.NO_CONTENT);
274             if (type == OPTIONS_WORKSPACE)
275                 printer.writeElement("D", "workspace-collection-set", XMLPrinter.NO_CONTENT);
276
277             printer.writeElement("D", "options", XMLPrinter.CLOSING);
278
279             return printer.toString();
280         }
281
282         return null;
283     }
284
285     public String JavaDoc getName() {
286         return "OPTIONS";
287     }
288
289         //get and set header
290
public void addRequestHeaders(HttpState state, HttpConnection conn)
291      throws IOException JavaDoc, HttpException {
292
293          if (type!= 0){
294             // set the default utf-8 encoding, if not already present
295
if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
296          }
297
298          super.addRequestHeaders(state, conn);
299      }
300
301 /**
302      * This method returns an enumeration of URL paths. If the PropFindMethod
303      * was sent to the URL of a collection, then there will be multiple URLs.
304      * The URLs are picked out of the <code>&lt;D:href&gt;</code> elements
305      * of the response.
306      *
307      * @return an enumeration of URL paths as Strings
308      */

309     public Enumeration JavaDoc getAllResponseURLs() {
310         checkUsed();
311         return getResponseURLs().elements();
312     }
313
314     public Enumeration JavaDoc getResponseProperties(){
315         Vector JavaDoc result = new Vector JavaDoc();
316         return (Enumeration JavaDoc) result;
317     }
318
319
320     protected Document JavaDoc parseResponseContent(InputStream JavaDoc is)
321         throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
322
323         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
324         factory.setNamespaceAware(true);
325
326         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
327
328         byte[] chunk;
329         byte[] all;
330         int chunkLen;
331         int allLen;
332         List JavaDoc chunks;
333         int i;
334         int max;
335         int ofs;
336
337         allLen = 0;
338         chunk = new byte[1024*4];
339         chunkLen = is.read(chunk);
340         chunks = new ArrayList JavaDoc();
341         while (chunkLen != -1) {
342             chunks.add(new Integer JavaDoc(chunkLen));
343             chunks.add(chunk);
344             allLen += chunkLen;
345             chunk = new byte[1024*4];
346             chunkLen = is.read(chunk);
347         }
348
349         all = new byte[allLen];
350         ofs = 0;
351         max = chunks.size();
352         for (i = 0; i < max; i += 2) {
353             chunkLen = ((Integer JavaDoc) chunks.get(i)).intValue();
354             chunk = (byte[]) chunks.get(i + 1);
355             System.arraycopy(chunk, 0, all, ofs, chunkLen);
356             ofs += chunkLen;
357         }
358
359         if (all.length == 0) return null;
360         return builder.parse(new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(all)));
361
362     }
363
364 }
365
366
367
368
369
Popular Tags