001: package com.ibm.webdav;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017:
018: import org.w3c.dom.*;
019:
020: /** A Response describes the effect of a method on a resource and/or
021: * its properties. See concrete subclasses <code>MethodResponse</code>
022: * and <code>PropertyResponse</code> for additional details. A <code>
023: * MultiStatus</code> contains a collection of Response instances, one
024: * for each resource effected by the method sent.
025: * @author Jim Amsden <jamsden@us.ibm.com>
026: * @see com.ibm.webdav.MethodResponse
027: * @see com.ibm.webdav.PropertyResponse
028: * @see com.ibm.webdav.MultiStatus
029: */
030: public abstract class Response extends Object implements
031: java.io.Serializable {
032:
033: public static String HTTPVersion = "HTTP/1.1";
034:
035: protected Document document = null;
036: private String resource = null; // the URL of the resource this is the response for
037: private String description = null;
038:
039: /** A Response should be constructed with a Document Element
040: */
041: public Response() {
042: }
043:
044: /** Construct a Response from an XML response element
045: *
046: * @param document the document that will contain the Response
047: * when output as XML.
048: * @exception com.ibm.webdav.ServerException thrown if the XML for the response is incorrect
049: */
050: public Response(Document document) throws ServerException {
051: this .document = document;
052: }
053:
054: /** Construct an empty Response for some resource.
055: *
056: * @param url the URL of the resource this is a response for
057: *
058: */
059: public Response(String url) {
060: resource = url;
061: }
062:
063: /** Translate this Response into an XML response element.
064: *
065: */
066: public abstract Element asXML();
067:
068: /** Get the description of this response
069: *
070: * @return the description
071: */
072: public String getDescription() {
073: return description;
074: }
075:
076: /** Get the URL of the resource this response describes
077: * @return the URL of the resource associated with this response
078: */
079: public String getResource() {
080: return resource;
081: }
082:
083: /** Check to see if this response contains any errors.
084: *
085: * @return true if all response does not contain an error, false otherwise.
086: */
087: public abstract boolean isOK();
088:
089: /** Set the description of this response
090: *
091: * @param value the new description
092: */
093: public void setDescription(String value) {
094: description = value;
095: }
096:
097: /** Set the document the response element is placed in when translated to XML
098: *
099: * @param document usually a MultiStatus document
100: */
101: public void setDocument(Document document) {
102: this .document = document;
103: }
104:
105: /** Set the URL of the resource this response describes
106: */
107: protected void setResource(String url) {
108: resource = url;
109: }
110:
111: /** Convert this Response to a PropertyResponse.
112: * This method is used to convert MethodResponses to PropertyResponses
113: * when an error occurred accessing the properties of some member.
114: *
115: */
116: public abstract PropertyResponse toPropertyResponse();
117:
118: /** Convert this Response to a String representation (an XML document).
119: * The String is formatted, and will therefore contain ignorable whitespace.
120: * Use response.asXML().print(pout) if ignorable whitespace is not desired.
121: * @return a string representation of the Response as an XML document
122: *
123: */
124: public String toString() {
125: /*ByteArrayOutputStream os = new ByteArrayOutputStream();
126: PrintWriter pout = new PrintWriter(os);
127: TXDocument document = (TXDocument) asXML();
128: try {
129: document.printWithFormat(pout);
130: } catch (Exception exc) {
131: }
132: return os.toString();*/
133: return printNode(asXML());
134: }
135:
136: /** Takes XML node and prints to String.
137: *
138: * @param node Element to print to String
139: * @return XML node as String
140: */
141: public static String printNode(Node node) {
142: StringBuffer sBuffer = new StringBuffer();
143:
144: if (node.getNodeType() == Node.TEXT_NODE) {
145: sBuffer.append(node.getNodeValue());
146: } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
147: sBuffer.append("<![CDATA[");
148: sBuffer.append(node.getNodeValue());
149: sBuffer.append("]]>\n");
150: } else if (node.getNodeType() == Node.ELEMENT_NODE) {
151: Element el = (Element) node;
152:
153: sBuffer.append("<" + el.getTagName());
154:
155: NamedNodeMap attribs = el.getAttributes();
156: for (int i = 0; i < attribs.getLength(); i++) {
157: Attr nextAtt = (Attr) attribs.item(i);
158: sBuffer.append(" " + nextAtt.getName() + "=\""
159: + nextAtt.getValue() + "\"");
160: }
161:
162: NodeList nodes = node.getChildNodes();
163:
164: if (nodes.getLength() == 0) {
165: sBuffer.append("/>\n");
166: } else {
167: sBuffer.append(">\n");
168:
169: for (int i = 0; i < nodes.getLength(); i++) {
170: sBuffer.append(printNode(nodes.item(i)));
171: }
172: sBuffer.append("</" + el.getTagName() + ">\n");
173: }
174: }
175:
176: return (sBuffer.toString());
177: }
178: }
|