KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > webdav > DAVMultiStatus


1 /* ========================================================================== *
2  * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
3  * All rights reserved. *
4  * ========================================================================== *
5  * *
6  * Licensed under the Apache License, Version 2.0 (the "License"). You may *
7  * not use this file except in compliance with the License. You may obtain a *
8  * copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. *
9  * *
10  * Unless required by applicable law or agreed to in writing, software *
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
13  * License for the specific language governing permissions and limitations *
14  * under the License. *
15  * *
16  * ========================================================================== */

17 package com.sslexplorer.vfs.webdav;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import com.sslexplorer.vfs.VFSResource;
26
27
28 /**
29  * <p>A {@link DAVException} representing a
30  * <a HREF="http://www.rfc-editor.org/rfc/rfc2518.txt">WebDAV</a>
31  * <code>207</code> (Multi-Status) response.</p>
32  *
33  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
34  */

35 public class DAVMultiStatus extends DAVException {
36     
37     private Set JavaDoc responses = new HashSet JavaDoc();
38
39     /**
40      * <p>Create a new {@link DAVMultiStatus} instance.</p>
41      */

42     public DAVMultiStatus() {
43         super(207, "Multi-Status response");
44     }
45
46     /**
47      * <p>Write the body of the multi-status response to the specified
48      * {@link DAVTransaction}'s output.</p>
49      */

50     public void write(DAVTransaction transaction)
51     throws IOException JavaDoc {
52         /* What to do on a collection resource */
53         transaction.setStatus(207);
54         transaction.setContentType("text/xml; charset=\"utf-8\"");
55         PrintWriter JavaDoc out = transaction.write("utf-8");
56
57         /* Output the XML declaration and the root document tag */
58         out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
59         out.println("<D:multistatus xmlns:D=\"DAV:\">");
60         
61         Iterator JavaDoc responses = this.responses.iterator();
62         while (responses.hasNext()) {
63             Response response = (Response) responses.next();
64             out.println(" <D:response>");
65             out.print(" <D:href>");
66             out.print(transaction.lookup(response.resource));
67             out.println("</D:href>");
68
69             if (response.status != 0) {
70                 out.print(" <D:status>HTTP/1.1 ");
71                 out.print(DAVUtilities.getStatusMessage(response.status));
72                 out.println("</D:status>");
73             }
74
75             if (response.message != null) {
76                 out.print(" <D:responsedescription>");
77                 out.print(response.message);
78                 out.println("</D:responsedescription>");
79             }
80
81             out.println(" </D:response>");
82         }
83         
84         out.println("</D:multistatus>");
85         out.flush();
86     }
87
88     /**
89      * <p>Return the number of responses held in this instance.</p>
90      */

91     public int size() {
92         return this.responses.size();
93     }
94
95     /**
96      * <p>Merge the responses held into the specified {@link DAVMultiStatus}
97      * into this instance.</p>
98      */

99     public void merge(DAVMultiStatus multistatus) {
100         if (multistatus == null) return;
101         Iterator JavaDoc iterator = multistatus.responses.iterator();
102         while (iterator.hasNext()) this.responses.add(iterator.next());
103     }
104
105     /**
106      * <p>Merge the details held into the specified {@link DAVException}
107      * into this instance.</p>
108      */

109     public void merge(DAVException exception) {
110         VFSResource resource = exception.getResource();
111         if (resource == null) throw exception;
112
113         int status = exception.getStatus();
114         String JavaDoc message = exception.getMessage();
115         this.responses.add(new Response(resource, status, message));
116     }
117
118     private static class Response implements Comparable JavaDoc {
119         private VFSResource resource = null;
120         private int status = 0;
121         private String JavaDoc message = null;
122
123         public Response(Response response) {
124             this(response.resource, response.status, response.message);
125         }
126
127         public Response(VFSResource resource, int status, String JavaDoc message) {
128             if (resource == null) throw new NullPointerException JavaDoc();
129             this.resource = resource;
130             this.status = status;
131             this.message = message;
132         }
133
134         public int hashCode() {
135             return this.resource.hashCode();
136         }
137
138         public int compareTo(Object JavaDoc object) {
139             Response response = (Response) object;
140             return (this.resource.compareTo(response.resource));
141         }
142
143         public boolean equals(Object JavaDoc object) {
144             if (object instanceof Response) {
145                 Response response = (Response) object;
146                 return (this.resource.equals(response.resource));
147             }
148             return false;
149         }
150     }
151 }
152
Popular Tags