KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > MethodResponse


1 package com.ibm.webdav;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17
18 import org.w3c.dom.*;
19 import java.util.*;
20 import javax.xml.parsers.*;
21
22 /** A MethodResponse describes the effect of a method on a resource. A
23  * <code> MultiStatus</code> contains a collection of Response instances,
24  * one for each resource effected by the method sent.
25  * @author Jim Amsden &lt;jamsden@us.ibm.com&gt;
26  * @see com.ibm.webdav.PropertyResponse
27  * @see com.ibm.webdav.MultiStatus
28  */

29 public class MethodResponse extends Response {
30
31    //------------------------------------------------------------------------
32

33    private Vector resources = new Vector(); // other resources that have this
34
// same status
35
private int status = WebDAVStatus.SC_OK;
36 /** Construct a MethodResponse from an XML response element
37  *
38  * @param document the document containing the element
39  * @param element the XML response element that is the source
40  * of this response
41  */

42 public MethodResponse(Document document, Element element) throws ServerException {
43     super(document);
44     try {
45         NodeList hrefs = element.getElementsByTagNameNS("DAV:", "href");
46         Element href = (Element) hrefs.item(0);
47         setResource(((Text) href.getFirstChild()).getData());
48         Element hrefn = null;
49         for (int i = 1; i < hrefs.getLength(); i++) {
50             hrefn = (Element) hrefs.item(i);
51             addResource(((Text) hrefn.getFirstChild()).getData());
52         }
53
54         Element status = (Element) (element.getElementsByTagNameNS("DAV:", "status").item(0));
55         String JavaDoc statusText = ((Text) status.getFirstChild()).getData();
56         StringTokenizer statusFields = new StringTokenizer(statusText, " ");
57         statusFields.nextToken(); // skip the HTTP version
58

59         int statusCode = Integer.parseInt(statusFields.nextToken());
60         setStatus(statusCode);
61
62         Element responseDescription = (Element)element.getElementsByTagNameNS("DAV:", "responsedescription").item(0);
63         if (responseDescription != null) {
64             setDescription(((Text) responseDescription.getFirstChild()).getData());
65         }
66     } catch (Exception JavaDoc exc) {
67         exc.printStackTrace();
68         throw new ServerException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Invalid MethodResponse");
69     }
70 }
71 /** Construct an empty Response for some resource.
72  *
73  * @param url the URL of the resource this is a response for
74  * @param status the HTTP status code for the response.
75  * @see com.ibm.webdav.WebDAVStatus
76  */

77 public MethodResponse(String JavaDoc url, int status) {
78     super(url);
79     this.status = status;
80 }
81 /** Construct an empty Response for some resource.
82  *
83  * @param url the URL of the resource this is a response for
84  * @param status the HTTP status code for the response.
85  * @see com.ibm.webdav.WebDAVStatus
86  */

87 public MethodResponse(String JavaDoc url, WebDAVException exc) {
88     super(url);
89     this.status = exc.getStatusCode();
90     this.setDescription( exc.getMessage());
91 }
92 /** Add a URL to a resource that is effected by the method in the
93  * same way as that returned by <code>getResource()</code>. The
94  * URL can only be added once to this Response.
95  *
96  * @param url the resource URL
97  * @exception com.ibm.webdav.ServerException thrown if the URL is already in the response
98  */

99 public void addResource(String JavaDoc url) throws ServerException {
100     boolean found = url.equals(getResource());
101     if (!found) {
102         Enumeration urls = getResources();
103         while (!found && urls.hasMoreElements()) {
104             String JavaDoc aUrl = (String JavaDoc) urls.nextElement();
105             found = aUrl.equals(url);
106         }
107     }
108     if (found) {
109         throw new ServerException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Duplicate URL in a Response");
110     } else {
111         resources.addElement(url);
112     }
113 }
114 /** Translate this MethodResponse into an XML response element.
115  * @return a DAV:response XML element
116  */

117 public Element asXML() {
118
119     Element response = document.createElementNS("DAV:","D:response");
120
121     Element href = document.createElementNS("DAV:","D:href");
122
123     href.appendChild(document.createTextNode(getResource()));
124     response.appendChild(href);
125     Enumeration urls = getResources();
126     while (urls.hasMoreElements()) {
127         String JavaDoc url = (String JavaDoc) urls.nextElement();
128         Element hrefn = document.createElementNS("DAV:","D:href");
129
130         hrefn.appendChild(document.createTextNode(url));
131         response.appendChild(hrefn);
132     }
133
134     Element status = document.createElementNS("DAV:","D:status");
135
136     status.appendChild(document.createTextNode(HTTPVersion + " " + getStatus() + " " + WebDAVStatus.getStatusMessage(getStatus())));
137     response.appendChild(status);
138
139     if (getDescription() != null) {
140         Element description = document.createElementNS("DAV:","D:responsedescription");
141
142         description.appendChild(document.createTextNode(getDescription()));
143         response.appendChild(description);
144     }
145     return response;
146 }
147 /** Other resources effected by the method that share the
148  * status response in this MethodResponse
149  *
150  * @return an Enumeration of Strings representing URLs of resources that
151  * responded in the same way to a request.
152  */

153 public Enumeration getResources() {
154     return resources.elements();
155 }
156 /** Get the status of the URLs in this MethodResponse.
157  *
158  * @return the HTTP status code for this response
159  */

160 public int getStatus() {
161     return status;
162 }
163 /** Check to see if this response does not contain an error.
164  *
165  * @return true if all response status code is less that 300, false otherwise.
166  */

167 public boolean isOK() {
168     return status < 300;
169 }
170 /** Set the status of the URLs in this MethodResponse.
171  *
172  * @param value an HTTP status code
173  * @see com.ibm.webdav.WebDAVStatus
174  */

175 public void setStatus(int value) {
176     status = value;
177 }
178 /** Convert this Response to a PropertyResponse.
179  * This method is used to convert MethodResponses to PropertyResponses
180  * when an error occurred accessing the properties of some member.
181  *
182  */

183 public PropertyResponse toPropertyResponse() {
184     PropertyResponse response = new PropertyResponse(getResource());
185     response.setDescription(getDescription());
186     try {
187           response.setDocument(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
188     } catch(Exception JavaDoc e) {
189           e.printStackTrace(System.err);
190         }
191         return response;
192 }
193 }
194
Popular Tags