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: import java.util.*;
020: import javax.xml.parsers.*;
021:
022: /** A MethodResponse describes the effect of a method on a resource. A
023: * <code> MultiStatus</code> contains a collection of Response instances,
024: * one for each resource effected by the method sent.
025: * @author Jim Amsden <jamsden@us.ibm.com>
026: * @see com.ibm.webdav.PropertyResponse
027: * @see com.ibm.webdav.MultiStatus
028: */
029: public class MethodResponse extends Response {
030:
031: //------------------------------------------------------------------------
032:
033: private Vector resources = new Vector(); // other resources that have this
034: // same status
035: private int status = WebDAVStatus.SC_OK;
036:
037: /** Construct a MethodResponse from an XML response element
038: *
039: * @param document the document containing the element
040: * @param element the XML response element that is the source
041: * of this response
042: */
043: public MethodResponse(Document document, Element element)
044: throws ServerException {
045: super (document);
046: try {
047: NodeList hrefs = element.getElementsByTagNameNS("DAV:",
048: "href");
049: Element href = (Element) hrefs.item(0);
050: setResource(((Text) href.getFirstChild()).getData());
051: Element hrefn = null;
052: for (int i = 1; i < hrefs.getLength(); i++) {
053: hrefn = (Element) hrefs.item(i);
054: addResource(((Text) hrefn.getFirstChild()).getData());
055: }
056:
057: Element status = (Element) (element.getElementsByTagNameNS(
058: "DAV:", "status").item(0));
059: String statusText = ((Text) status.getFirstChild())
060: .getData();
061: StringTokenizer statusFields = new StringTokenizer(
062: statusText, " ");
063: statusFields.nextToken(); // skip the HTTP version
064:
065: int statusCode = Integer.parseInt(statusFields.nextToken());
066: setStatus(statusCode);
067:
068: Element responseDescription = (Element) element
069: .getElementsByTagNameNS("DAV:",
070: "responsedescription").item(0);
071: if (responseDescription != null) {
072: setDescription(((Text) responseDescription
073: .getFirstChild()).getData());
074: }
075: } catch (Exception exc) {
076: exc.printStackTrace();
077: throw new ServerException(
078: WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
079: "Invalid MethodResponse");
080: }
081: }
082:
083: /** Construct an empty Response for some resource.
084: *
085: * @param url the URL of the resource this is a response for
086: * @param status the HTTP status code for the response.
087: * @see com.ibm.webdav.WebDAVStatus
088: */
089: public MethodResponse(String url, int status) {
090: super (url);
091: this .status = status;
092: }
093:
094: /** Construct an empty Response for some resource.
095: *
096: * @param url the URL of the resource this is a response for
097: * @param status the HTTP status code for the response.
098: * @see com.ibm.webdav.WebDAVStatus
099: */
100: public MethodResponse(String url, WebDAVException exc) {
101: super (url);
102: this .status = exc.getStatusCode();
103: this .setDescription(exc.getMessage());
104: }
105:
106: /** Add a URL to a resource that is effected by the method in the
107: * same way as that returned by <code>getResource()</code>. The
108: * URL can only be added once to this Response.
109: *
110: * @param url the resource URL
111: * @exception com.ibm.webdav.ServerException thrown if the URL is already in the response
112: */
113: public void addResource(String url) throws ServerException {
114: boolean found = url.equals(getResource());
115: if (!found) {
116: Enumeration urls = getResources();
117: while (!found && urls.hasMoreElements()) {
118: String aUrl = (String) urls.nextElement();
119: found = aUrl.equals(url);
120: }
121: }
122: if (found) {
123: throw new ServerException(
124: WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
125: "Duplicate URL in a Response");
126: } else {
127: resources.addElement(url);
128: }
129: }
130:
131: /** Translate this MethodResponse into an XML response element.
132: * @return a DAV:response XML element
133: */
134: public Element asXML() {
135:
136: Element response = document.createElementNS("DAV:",
137: "D:response");
138:
139: Element href = document.createElementNS("DAV:", "D:href");
140:
141: href.appendChild(document.createTextNode(getResource()));
142: response.appendChild(href);
143: Enumeration urls = getResources();
144: while (urls.hasMoreElements()) {
145: String url = (String) urls.nextElement();
146: Element hrefn = document.createElementNS("DAV:", "D:href");
147:
148: hrefn.appendChild(document.createTextNode(url));
149: response.appendChild(hrefn);
150: }
151:
152: Element status = document.createElementNS("DAV:", "D:status");
153:
154: status.appendChild(document.createTextNode(HTTPVersion + " "
155: + getStatus() + " "
156: + WebDAVStatus.getStatusMessage(getStatus())));
157: response.appendChild(status);
158:
159: if (getDescription() != null) {
160: Element description = document.createElementNS("DAV:",
161: "D:responsedescription");
162:
163: description.appendChild(document
164: .createTextNode(getDescription()));
165: response.appendChild(description);
166: }
167: return response;
168: }
169:
170: /** Other resources effected by the method that share the
171: * status response in this MethodResponse
172: *
173: * @return an Enumeration of Strings representing URLs of resources that
174: * responded in the same way to a request.
175: */
176: public Enumeration getResources() {
177: return resources.elements();
178: }
179:
180: /** Get the status of the URLs in this MethodResponse.
181: *
182: * @return the HTTP status code for this response
183: */
184: public int getStatus() {
185: return status;
186: }
187:
188: /** Check to see if this response does not contain an error.
189: *
190: * @return true if all response status code is less that 300, false otherwise.
191: */
192: public boolean isOK() {
193: return status < 300;
194: }
195:
196: /** Set the status of the URLs in this MethodResponse.
197: *
198: * @param value an HTTP status code
199: * @see com.ibm.webdav.WebDAVStatus
200: */
201: public void setStatus(int value) {
202: status = value;
203: }
204:
205: /** Convert this Response to a PropertyResponse.
206: * This method is used to convert MethodResponses to PropertyResponses
207: * when an error occurred accessing the properties of some member.
208: *
209: */
210: public PropertyResponse toPropertyResponse() {
211: PropertyResponse response = new PropertyResponse(getResource());
212: response.setDescription(getDescription());
213: try {
214: response.setDocument(DocumentBuilderFactory.newInstance()
215: .newDocumentBuilder().newDocument());
216: } catch (Exception e) {
217: e.printStackTrace(System.err);
218: }
219: return response;
220: }
221: }
|