001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.webdav.client;
020:
021: import java.io.*;
022: import java.util.*;
023:
024: import javax.xml.parsers.*;
025:
026: import org.w3c.dom.*;
027: import org.xml.sax.*;
028:
029: import HTTPClient.*;
030:
031: /**
032: * Wraps up all the data and methods for dealing with
033: * WebDAV Responses.
034: *
035: * @author Matthew Large
036: * @version $Revision: 1.1 $
037: *
038: */
039: public class WebDAVResponse {
040:
041: /**
042: * HTTP connection.
043: */
044: private HTTPResponse m_response = null;
045:
046: /**
047: * List of WebDAV multi-status response root elements.
048: */
049: private List m_aMultiStatusResponses = new ArrayList();
050:
051: /**
052: * URL of request.
053: */
054: private String m_sURL = null;
055:
056: /**
057: * Creates a new WebDAV repsonse.
058: *
059: * @param response HTTPResponse used to create this WebDAVResponse
060: * @throws IOException
061: * @throws ModuleException
062: */
063: public WebDAVResponse(HTTPResponse response) throws IOException,
064: ModuleException {
065: super ();
066: this .m_response = response;
067: this .processResponse();
068: }
069:
070: /**
071: * Returns the overall status code for the response. If the overall status code
072: * is 207 (MultiStatus) then there will be more detailed status codes in each
073: * of the MultiStatusResponse objects.
074: *
075: * @return int, status code for overall response
076: * @throws IOException
077: * @throws ModuleException
078: */
079: public int getStatusCode() throws IOException, ModuleException {
080: return this .m_response.getStatusCode();
081: }
082:
083: /**
084: * Returns the WebDAV response body.
085: *
086: * @return byte array of the response body
087: * @throws IOException
088: * @throws ModuleException
089: */
090: public byte[] getResponseData() throws IOException, ModuleException {
091: return this .m_response.getData();
092: }
093:
094: /**
095: * XML of the response body.
096: *
097: * @return XML
098: */
099: public Document getResponseXML() throws IOException {
100: Document document = null;
101: try {
102: DocumentBuilderFactory factory = DocumentBuilderFactory
103: .newInstance();
104: factory.setNamespaceAware(true);
105: document = factory.newDocumentBuilder().parse(
106: new InputSource(new InputStreamReader(
107: new ByteArrayInputStream(this .m_response
108: .getData()), "UTF-8")));
109: } catch (SAXException e) {
110: e.printStackTrace(System.out);
111: } catch (ParserConfigurationException e) {
112: e.printStackTrace(System.out);
113: } catch (FactoryConfigurationError e) {
114: e.printStackTrace(System.out);
115: } catch (ModuleException e) {
116: e.printStackTrace();
117: }
118:
119: return document;
120: }
121:
122: /**
123: * Internal method to check if the response is MultiStatus, if so create all the
124: * MultiStatusResponse objects.
125: *
126: * @throws IOException
127: * @throws ModuleException
128: */
129: private void processResponse() throws IOException, ModuleException {
130: if (this .getStatusCode() == 207) {
131: this .processMultiStatusResponse();
132: }
133: }
134:
135: /**
136: * Returns list of MultiStatusResponse objects.
137: *
138: * @return List of MultiStatusResponse objects, empty list if there aren't any
139: */
140: public List getMultiStatusResponses() {
141: return this .m_aMultiStatusResponses;
142: }
143:
144: /**
145: * Processes the WebDAV response XML to collect the
146: * multi-status response elements.
147: *
148: * @throws IOException
149: */
150: private void processMultiStatusResponse() throws IOException {
151: Document xml = this .getResponseXML();
152: if (xml != null) {
153: Element elMultiStatus = xml.getDocumentElement();
154:
155: NodeList nl = elMultiStatus.getChildNodes();
156: for (int i = 0; i < nl.getLength(); i++) {
157: if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
158: Element elTemp = (Element) nl.item(i);
159: MultiStatusResponse multi = new MultiStatusResponse();
160: multi.populate(elTemp);
161: this .m_aMultiStatusResponses.add(multi);
162: }
163: }
164: }
165:
166: }
167:
168: /**
169: * Returns the URL of the request.
170: *
171: * @return URL
172: */
173: public String getURL() {
174: return this .m_sURL;
175: }
176:
177: /**
178: * Sets the URL of the request.
179: *
180: * @param sURL URL
181: */
182: protected void setURL(String sURL) {
183: this .m_sURL = sURL;
184: }
185:
186: /**
187: * Retrieves the value for a given header.
188: *
189: * @param sName Header to return
190: * @return the value for the header, or null if non-existent
191: */
192: public String getHeader(String sName) {
193: String sReturn = null;
194: try {
195: sReturn = this .m_response.getHeader(sName);
196: } catch (IOException e) {
197: e.printStackTrace();
198: } catch (ModuleException e) {
199: e.printStackTrace();
200: }
201: return sReturn;
202: }
203: }
|