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.UnsupportedEncodingException;
022: import java.net.URLDecoder;
023:
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.parsers.FactoryConfigurationError;
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032: import org.w3c.dom.Text;
033:
034: /**
035: * This class contains all the methods for creating and dealing with
036: * a WebDAV MultiStatus Response.
037: *
038: * @author Matthew Large
039: * @version $Revision: 1.1 $
040: *
041: */
042: public class MultiStatusResponse {
043:
044: private int m_nStatus = -1;
045: private String m_sURI = null;
046: private String m_sResponseDescription = null;
047:
048: private Element m_elResponseXML = null;
049:
050: public MultiStatusResponse() {
051: super ();
052: }
053:
054: /**
055: * Returns the HTTP status code for this response.
056: *
057: * @return HTTP status code
058: */
059: public int getStatus() {
060: return this .m_nStatus;
061: }
062:
063: /**
064: * Returns that URI for this response.
065: *
066: * @return URI
067: */
068: public String getURI() {
069: try {
070: return URLDecoder.decode(this .m_sURI, "UTF-8");
071: } catch (UnsupportedEncodingException e) {
072: e.printStackTrace();
073: }
074: return null;
075: }
076:
077: /**
078: * Returns the response description or null if one was not present.
079: *
080: * @return Response description or null
081: */
082: public String getResponseDescription() {
083: return this .m_sResponseDescription;
084: }
085:
086: /**
087: * Returns the DAV Response element that this MultiStatus was created from.
088: *
089: * @return Root DOM Element of the response
090: */
091: public Element getResponseXML() {
092: return this .m_elResponseXML;
093: }
094:
095: /**
096: * Populates the MultiStatusResponse from some WebDAV response XML.
097: *
098: * @param elResponse
099: */
100: public void populate(Element elResponse) {
101: this .m_elResponseXML = elResponse;
102: NodeList nl = elResponse.getChildNodes();
103: for (int i = 0; i < nl.getLength(); i++) {
104: if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
105: Element elTemp = (Element) nl.item(i);
106:
107: if (elTemp.getLocalName().equals("href")) {
108: this .m_sURI = elTemp.getFirstChild().getNodeValue();
109: } else if (elTemp.getLocalName().equals("status")) {
110: String sStatus = elTemp.getFirstChild()
111: .getNodeValue();
112: sStatus = sStatus
113: .substring(sStatus.indexOf(" ") + 1);
114: sStatus = sStatus
115: .substring(0, sStatus.indexOf(" "));
116: this .m_nStatus = Integer.parseInt(sStatus);
117: } else if (elTemp.getLocalName().equals("propstat")) {
118: NodeList nl2 = elTemp.getChildNodes();
119: for (int j = 0; j < nl2.getLength(); j++) {
120: if (nl2.item(j).getNodeType() == Node.ELEMENT_NODE) {
121: Element elTemp2 = (Element) nl2.item(j);
122:
123: if (elTemp2.getLocalName().equals("status")) {
124: String sStatus = elTemp2
125: .getFirstChild().getNodeValue();
126: sStatus = sStatus.substring(sStatus
127: .indexOf(" ") + 1);
128: sStatus = sStatus.substring(0, sStatus
129: .indexOf(" "));
130: this .m_nStatus = Integer
131: .parseInt(sStatus);
132: } else if (elTemp2.getLocalName().equals(
133: "responsedescription")) {
134: this .m_sResponseDescription = elTemp2
135: .getFirstChild().getNodeValue();
136: }
137: }
138: }
139: } else if (elTemp.getLocalName().equals(
140: "responsedescription")) {
141: this .m_sResponseDescription = elTemp
142: .getFirstChild().getNodeValue();
143: }
144: }
145: }
146: }
147:
148: public static void main(String[] args) {
149: MultiStatusResponse multi = new MultiStatusResponse();
150:
151: Document xmlDoc = null;
152: try {
153: DocumentBuilderFactory factory = DocumentBuilderFactory
154: .newInstance();
155: factory.setNamespaceAware(true);
156:
157: xmlDoc = factory.newDocumentBuilder().newDocument();
158: } catch (ParserConfigurationException e) {
159: e.printStackTrace();
160: } catch (FactoryConfigurationError e) {
161: e.printStackTrace();
162: }
163: Element elResp = xmlDoc.createElementNS(
164: WebDAVConnection.WEBDAV_NAMESPACE, "response");
165:
166: Element elHREF = xmlDoc.createElementNS(
167: WebDAVConnection.WEBDAV_NAMESPACE, "href");
168: Text txt = xmlDoc.createTextNode("http://www.sim.com");
169: elHREF.appendChild(txt);
170: elResp.appendChild(elHREF);
171:
172: Element elPropStat = xmlDoc.createElementNS(
173: WebDAVConnection.WEBDAV_NAMESPACE, "propstat");
174: elResp.appendChild(elPropStat);
175:
176: Element elStatus = xmlDoc.createElementNS(
177: WebDAVConnection.WEBDAV_NAMESPACE, "status");
178: txt = xmlDoc.createTextNode("HTTP/1.1 200 OK");
179: elStatus.appendChild(txt);
180: elPropStat.appendChild(elStatus);
181:
182: multi.populate(elResp);
183: }
184:
185: }
|