KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > WebDAVResponse


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.webdav.client;
20
21 import java.io.*;
22 import java.util.*;
23
24 import javax.xml.parsers.*;
25
26 import org.w3c.dom.*;
27 import org.xml.sax.*;
28
29 import HTTPClient.*;
30
31 /**
32  * Wraps up all the data and methods for dealing with
33  * WebDAV Responses.
34  *
35  * @author Matthew Large
36  * @version $Revision: 1.1 $
37  *
38  */

39 public class WebDAVResponse {
40
41     /**
42      * HTTP connection.
43      */

44     private HTTPResponse m_response = null;
45     
46     /**
47      * List of WebDAV multi-status response root elements.
48      */

49     private List m_aMultiStatusResponses = new ArrayList();
50     
51     /**
52      * URL of request.
53      */

54     private String JavaDoc m_sURL = null;
55
56     /**
57      * Creates a new WebDAV repsonse.
58      *
59      * @param response HTTPResponse used to create this WebDAVResponse
60      * @throws IOException
61      * @throws ModuleException
62      */

63     public WebDAVResponse(HTTPResponse response) throws IOException, ModuleException {
64         super();
65         this.m_response = response;
66         this.processResponse();
67     }
68     
69     /**
70      * Returns the overall status code for the response. If the overall status code
71      * is 207 (MultiStatus) then there will be more detailed status codes in each
72      * of the MultiStatusResponse objects.
73      *
74      * @return int, status code for overall response
75      * @throws IOException
76      * @throws ModuleException
77      */

78     public int getStatusCode() throws IOException, ModuleException {
79         return this.m_response.getStatusCode();
80     }
81     
82     /**
83      * Returns the WebDAV response body.
84      *
85      * @return byte array of the response body
86      * @throws IOException
87      * @throws ModuleException
88      */

89     public byte[] getResponseData() throws IOException, ModuleException {
90         return this.m_response.getData();
91     }
92     
93     /**
94      * XML of the response body.
95      *
96      * @return XML
97      */

98     public Document getResponseXML() throws IOException {
99         Document document = null;
100         try {
101             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
102             factory.setNamespaceAware(true);
103             document = factory.newDocumentBuilder().parse( new InputSource( new InputStreamReader( new ByteArrayInputStream(this.m_response.getData()), "UTF-8" ) ) );
104         } catch (SAXException e) {
105             e.printStackTrace(System.out);
106         } catch (ParserConfigurationException e) {
107             e.printStackTrace(System.out);
108         } catch (FactoryConfigurationError e) {
109             e.printStackTrace(System.out);
110         } catch (ModuleException e) {
111             e.printStackTrace();
112         }
113         
114         return document;
115     }
116     
117     /**
118      * Internal method to check if the response is MultiStatus, if so create all the
119      * MultiStatusResponse objects.
120      *
121      * @throws IOException
122      * @throws ModuleException
123      */

124     private void processResponse() throws IOException, ModuleException {
125         if( this.getStatusCode()==207 ) {
126             this.processMultiStatusResponse();
127         }
128     }
129     
130     /**
131      * Returns list of MultiStatusResponse objects.
132      *
133      * @return List of MultiStatusResponse objects, empty list if there aren't any
134      */

135     public List getMultiStatusResponses() {
136         return this.m_aMultiStatusResponses;
137     }
138
139     /**
140      * Processes the WebDAV response XML to collect the
141      * multi-status response elements.
142      *
143      * @throws IOException
144      */

145     private void processMultiStatusResponse() throws IOException {
146         Document xml = this.getResponseXML();
147         if(xml!=null) {
148             Element elMultiStatus = xml.getDocumentElement();
149         
150             NodeList nl = elMultiStatus.getChildNodes();
151             for(int i=0; i<nl.getLength(); i++) {
152                 if( nl.item(i).getNodeType()==Node.ELEMENT_NODE ) {
153                     Element elTemp = (Element)nl.item(i);
154                     MultiStatusResponse multi = new MultiStatusResponse();
155                     multi.populate(elTemp);
156                     this.m_aMultiStatusResponses.add(multi);
157                 }
158             }
159         }
160         
161     }
162     
163     /**
164      * Returns the URL of the request.
165      *
166      * @return URL
167      */

168     public String JavaDoc getURL() {
169         return this.m_sURL;
170     }
171
172     /**
173      * Sets the URL of the request.
174      *
175      * @param sURL URL
176      */

177     protected void setURL(String JavaDoc sURL) {
178         this.m_sURL = sURL;
179     }
180     
181     /**
182      * Retrieves the value for a given header.
183      *
184      * @param sName Header to return
185      * @return the value for the header, or null if non-existent
186      */

187     public String JavaDoc getHeader(String JavaDoc sName) {
188         String JavaDoc sReturn = null;
189         try {
190             sReturn = this.m_response.getHeader(sName);
191         } catch (IOException e) {
192             e.printStackTrace();
193         } catch (ModuleException e) {
194             e.printStackTrace();
195         }
196         return sReturn;
197     }
198 }
199
Popular Tags