KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
19 import org.w3c.dom.*;
20 import javax.xml.parsers.*;
21 import java.io.*;
22
23 /** A MultiStatus contains multiple Response instances resulting from the
24  * invocation of a method on a resource. There will generally be one Response for
25  * each resource effected by the method.
26  * @author Jim Amsden <jamsden@us.ibm.com>
27  * @see com.ibm.webdav.MethodResponse
28  * @see com.ibm.webdav.PropertyResponse
29  */

30 public class MultiStatus extends Object JavaDoc implements Serializable {
31     private Vector responses = new Vector();
32     private String JavaDoc description = null;
33    /** Construct an empty MultiStatus.
34     *
35     */

36    public MultiStatus() throws ServerException
37    {
38    }
39 /** Construct a MultiStatus from an XML DOM Document.
40  *
41  * @param document the XML Document containing a DAV:multistatus element
42  * used to construct a MultiStatus object
43  *
44  */

45 public MultiStatus(Document document) throws ServerException {
46     init(document);
47 }
48 /** Add a Response to this MultiStatus
49  *
50  * @param response the Response to add
51  */

52 public void addResponse(Response response) {
53     responses.addElement(response);
54 }
55 /** Convert a MultiStatus into an XML DOM Document containing a
56  * DAV:multistatus element.
57  * @return an XML document containing a DAV:multistatus representing this MultiStatus
58  */

59 public Document asXML() {
60
61     Document document = null;
62
63         try {
64           document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
65     } catch(Exception JavaDoc e) {
66           e.printStackTrace(System.err);
67           throw new RuntimeException JavaDoc(e.getMessage());
68         }
69         //document.setVersion(Resource.XMLVersion);
70
//document.setEncoding(Resource.defaultXMLEncoding);
71

72     Element multistatus = document.createElementNS("DAV:","D:multistatus");
73
74     multistatus.setAttribute("xmlns:D", "DAV:");
75     document.appendChild(multistatus);
76     Enumeration responses = getResponses();
77     while (responses.hasMoreElements()) {
78         Response response = (Response) responses.nextElement();
79         response.setDocument(document);
80                 
81         multistatus.appendChild(document.importNode(response.asXML(),true));
82     }
83     if (getDescription() != null) {
84         Element description = document.createElementNS("DAV:","D:responsedescription");
85
86                 description.appendChild(document.createTextNode(getDescription()));
87         multistatus.appendChild(description);
88     }
89
90         //System.out.println(Response.printNode(multistatus));
91

92     return document;
93 }
94 /** Get the active lock on this resource owned by the given principal if any.
95  * This is a convenience method to get the active lock granted by a lock request
96  * from the returned MultiStatus. A WebDAVException is raised if the MultiStatus
97  * does not contain a PropertyResponse containing a DAV:lockdiscovery property.
98  * <p>
99  * NOTE: this method cannot be reliably implemented based on the version 10 of
100  * the WebDAV spec as an activelock element in a lockdiscovery does not contain
101  * the authorization credentials of the owner of the lock. For now, this method
102  * relies on an additional principal element in the activelock that contains
103  * the required id that is an IBM EXTENSION.
104  *
105  * @param principal the authorization id of the requesting principal
106  *
107  * @return the active lock owned by that principal or null if the resource is
108  * not locked by that principal.
109  * @exception com.ibm.webdav.WebDAVException raised if the MultiStatus doesn't contain a DAV:lockdiscovery property
110  */

111 public ActiveLock getActiveLockFor(String JavaDoc principal) throws WebDAVException {
112     if (!isOK()) {
113         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Can't access lock information lock method failed");
114     }
115     if (!(responses.elementAt(0) instanceof PropertyResponse)) {
116         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "MultiStatus doesn't contain a property response");
117     }
118     PropertyResponse response = (PropertyResponse) responses.elementAt(0);
119     PropertyValue lockdiscovery = response.getProperty( PropertyName.pnLockdiscovery );
120     if (lockdiscovery == null) {
121         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "MultiStatus doesn't contain a property response containing a lockdiscovery property");
122     }
123
124     // get the active locks from the lockdiscovery and see if one is owned
125
// by this principal. This relies on the principal element added to the
126
// activelock element to identify the owning principal
127
NodeList locks = ((Element) lockdiscovery.value).getElementsByTagNameNS("DAV:","activelock");
128     Element lockElement = null;
129     ActiveLock ownedLock = null;
130     for (int i = 0; i < locks.getLength(); i++) {
131         lockElement = (Element) locks.item(i);
132         ActiveLock lock = new ActiveLock(lockElement);
133         if (lock.getPrincipal().equals(principal)) {
134             ownedLock = lock;
135         }
136     }
137     return ownedLock;
138 }
139 /** Get the overall summary description for this MultiStatus.
140  *
141  * @return a synopsis of the responses in the MultiStatus
142  */

143 public String JavaDoc getDescription() {
144     return description;
145 }
146 /** Get the responses in this MultiStatus
147  *
148  * @return Enumeration of Response instances
149  * @see com.ibm.webdav.MethodResponse
150  * @see com.ibm.webdav.PropertyResponse
151  */

152 public Enumeration getResponses() {
153     return responses.elements();
154 }
155 /** Initialize a MultiStatus from an XML DOM Document.
156  *
157  * @param document an XML Document containing a DAV:multistatus element
158  * @exception com.ibm.webdav.ServerException
159  */

160 private void init(Document document) throws ServerException {
161     responses = new Vector();
162     Element multistatus = (Element) document.getDocumentElement();
163     NodeList responses = multistatus.getElementsByTagNameNS("DAV:","response");
164     Element response = null;
165     for (int i = 0; i < responses.getLength(); i++) {
166         response = (Element) responses.item(i);
167         Response aResponse = null;
168         if (response.getElementsByTagNameNS("DAV:","propstat").item(0) != null) {
169             aResponse = new PropertyResponse((Document) document, response);
170         } else {
171             aResponse = new MethodResponse((Document) document, response);
172         }
173         this.responses.addElement(aResponse);
174     }
175     Element responseDescription = (Element) multistatus.getElementsByTagNameNS("DAV:","responsedescription");
176     if (responseDescription != null) {
177         setDescription(((Text) responseDescription.getFirstChild()).getData());
178     }
179 }
180 /** Check to see if all responses in this MultiStatus were successful.
181  *
182  * @return true if all method response status codes are successful, false otherwise.
183  */

184 public boolean isOK() {
185     boolean isok = true;
186     Enumeration responses = this.getResponses();
187     while (isok && responses.hasMoreElements()) {
188         Response response = (Response) responses.nextElement();
189         if (response instanceof MethodResponse) {
190             isok = isok && ((MethodResponse) response).isOK();
191         }
192     }
193     return isok;
194 }
195 /** Merge the responses in childMultiStatus into this MultiStatus.
196  *
197  * @param childMultiStatus contains the responses to merge
198  */

199 public void mergeWith(MultiStatus childMultiStatus) {
200     Enumeration responses = childMultiStatus.getResponses();
201     while (responses.hasMoreElements()) {
202         Response response = (Response) responses.nextElement();
203         addResponse(response);
204     }
205 }
206 /** De-serialize a MultiStatus as an XML document.
207  * @param in the stream to read from
208  */

209 private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException, ClassNotFoundException JavaDoc {
210     int size = in.readInt();
211     byte[] buffer = new byte[size];
212     in.readFully(buffer);
213     ByteArrayInputStream is = new ByteArrayInputStream(buffer);
214
215     // TODO: the parser closes the stream with it is done reading. This closes
216
// the rmi socket!
217
Document contents = null;
218
219     try {
220                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
221                 factory.setNamespaceAware(true);
222                 DocumentBuilder docbuilder = factory.newDocumentBuilder();
223                 contents = docbuilder.parse(new org.xml.sax.InputSource JavaDoc(is));
224         init(contents);
225     } catch (Exception JavaDoc exc) {
226         System.err.println(exc);
227         throw new IOException(exc.getMessage());
228     }
229 }
230 /** Remove responses that are OK from this MultiStatus. This method is used
231  * for those situations where the MultiStatus may contain responses that are
232  * not necessary to return to a client.
233  */

234 public void removeOKResponses() {
235     for (int i = responses.size() - 1; i >= 0; i--) {
236         Response response = (Response) responses.elementAt(i);
237         if (response.isOK()) {
238             responses.removeElementAt(i);
239         }
240     }
241 }
242 /** Remove a Response from this MultiStatus. Ignores errors if the response is
243  * not in the MultiStatus.
244  *
245  * @param response the Response to remove
246  */

247 public void removeResponse(Response response) {
248     try {
249         responses.removeElement(response);
250     } catch (Exception JavaDoc exc) {
251     }
252 }
253 /** Set the overall summary description for this MultiStatus.
254 *
255 * @param value a synopsis of the responses in the MultiStatus
256 */

257 public void setDescription(String JavaDoc value) {
258     description = value;
259 }
260 /** Convert this MultiStatus to a String representation (an XML document).
261 * The String is formatted, and will therefore contain ignorable whitespace.
262 * Use multiStatus.asXML().print(pout) if ignorable whitespace is not desired.
263 * @return a string representation of a MultiStatus as an XML document
264 *
265 */

266 public String JavaDoc toString() {
267     /*ByteArrayOutputStream os = new ByteArrayOutputStream();
268     PrintWriter pout = new PrintWriter(os);
269     TXDocument document = (TXDocument) asXML();
270     try {
271         document.printWithFormat(pout);
272     } catch (Exception exc) {
273     }
274     pout.close();
275     return os.toString();*/

276
277         Document document = null;
278
279         try {
280           document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
281         } catch(Exception JavaDoc e) {
282           e.printStackTrace(System.err);
283         }
284         //document.setVersion(Resource.XMLVersion);
285
//document.setEncoding(Resource.defaultXMLEncoding);
286

287     Element multistatus = document.createElementNS("DAV:","D:multistatus");
288
289     multistatus.setAttribute("xmlns:D", "DAV:");
290     document.appendChild(multistatus);
291     Enumeration responses = getResponses();
292     while (responses.hasMoreElements()) {
293         Response response = (Response) responses.nextElement();
294         response.setDocument(document);
295                 //System.out.println(response.toString());
296
multistatus.appendChild(response.asXML());
297     }
298     if (getDescription() != null) {
299         Element description = document.createElementNS("DAV:","D:responsedescription");
300
301         description.appendChild(document.createTextNode(getDescription()));
302         multistatus.appendChild(description);
303     }
304
305         return Response.printNode(multistatus);
306
307 }
308 /** Serialize a MultiStatus as an XML document.
309  * @param out the stream to write to
310  */

311 private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws IOException {
312     Document document = (Document) asXML();
313     ByteArrayOutputStream os = new ByteArrayOutputStream();
314     PrintWriter pw = new PrintWriter(os, false);
315     pw.print(XMLUtility.printNode(document.getDocumentElement()));
316         //document.print(pw);
317
out.writeInt(os.size());
318     out.write(os.toByteArray());
319 }
320 }
321
Popular Tags