KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > MultiStatusResponse


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import com.maverick.util.URLUTF8Encoder;
33
34 import net.n3.nanoxml.IXMLElement;
35 import net.n3.nanoxml.IXMLParser;
36 import net.n3.nanoxml.IXMLReader;
37 import net.n3.nanoxml.StdXMLReader;
38 import net.n3.nanoxml.XMLParserFactory;
39
40 /**
41  *
42  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
43  */

44 public class MultiStatusResponse {
45
46     // #ifdef DEBUG
47
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(MultiStatusResponse.class);
48     // #endif
49

50     String JavaDoc href;
51     int status;
52     String JavaDoc version;
53     String JavaDoc reason;
54     Properties JavaDoc properties;
55     boolean collection = false;
56
57     static SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
58

59     MultiStatusResponse(IXMLElement element) throws IOException JavaDoc {
60         if (element.getFirstChildNamed("href", element.getNamespace()) == null) //$NON-NLS-1$
61
throw new IOException JavaDoc(Messages.getString("MultiStatusResponse.unexpectedHref")); //$NON-NLS-1$
62

63         href = URLUTF8Encoder.decode(element.getFirstChildNamed("href", element.getNamespace()).getContent()); //$NON-NLS-1$
64

65         IXMLElement props = element.getFirstChildNamed("propstat", element.getNamespace()); //$NON-NLS-1$
66

67         if (props == null)
68             throw new IOException JavaDoc(Messages.getString("MultiStatusResponse.noPropertyElements")); //$NON-NLS-1$
69

70         if (props.getFirstChildNamed("status", element.getNamespace()) == null) //$NON-NLS-1$
71
throw new IOException JavaDoc(Messages.getString("MultiStatusResponse.unexpectedStatusResponse")); //$NON-NLS-1$
72

73         String JavaDoc status = props.getFirstChildNamed("status", element.getNamespace()).getContent(); //$NON-NLS-1$
74

75         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(status, " ", false); //$NON-NLS-1$
76
reason = ""; //$NON-NLS-1$
77

78         try {
79             version = tokens.nextToken();
80             this.status = Integer.parseInt(tokens.nextToken());
81
82             while (tokens.hasMoreTokens()) {
83                 reason += tokens.nextToken() + " "; //$NON-NLS-1$
84
}
85             reason = URLDecoder.decode(reason.trim());
86         } catch (NoSuchElementException JavaDoc e) {
87             throw new IOException JavaDoc(Messages.getString("MultiStatusResponse.failedToReadHTTPResponseHeader")); //$NON-NLS-1$
88
} catch (NumberFormatException JavaDoc e) {
89             throw new IOException JavaDoc(Messages.getString("MultiStatusResponse.failedToReadHTTPResponseHeader")); //$NON-NLS-1$
90
}
91
92         // Create a new set of properties
93
properties = new Properties JavaDoc();
94
95         // Check the status, if its not found then return
96
if (this.status == 404)
97             return;
98
99         props = props.getFirstChildNamed("prop", props.getNamespace()); //$NON-NLS-1$
100

101         if (props == null)
102             throw new IOException JavaDoc(Messages.getString("MultiStatusResponse.noPropElementsInPropStat")); //$NON-NLS-1$
103

104         IXMLElement child;
105
106         for (Enumeration JavaDoc e = props.getChildren().elements(); e.hasMoreElements();) {
107             child = (IXMLElement) e.nextElement();
108
109             if (child.getName().equalsIgnoreCase("resourcetype")) { //$NON-NLS-1$
110
if (child.getChildrenNamed("collection") != null) //$NON-NLS-1$
111
collection = true;
112             } else {
113                 properties.put(child.getName().toLowerCase(), child.getContent() == null ? "" : child.getContent()); //$NON-NLS-1$
114
}
115         }
116
117     }
118
119     public long getContentLength() {
120         if (properties.containsKey("getcontentlength")) { //$NON-NLS-1$
121
return Long.parseLong(properties.getProperty("getcontentlength")); //$NON-NLS-1$
122
} else
123             return 0;
124     }
125
126     private long processDate(String JavaDoc date) {
127
128         long retval = 0;
129         try {
130             retval = Date.parse(date);
131         } catch (Throwable JavaDoc t) {
132
133             try {
134                 /*
135                  * org.joda.time.DateTime dt = new
136                  * org.joda.time.DateTime(properties.getProperty("creationdate"));
137                  * retval = dt.getMillis();
138                  */

139                 return 0; // TODO change
140
} catch (Throwable JavaDoc t2) {
141             }
142         }
143
144         return retval;
145     }
146
147     public long getCreationDate() {
148
149         if (properties.containsKey("creationdate")) { //$NON-NLS-1$
150
return processDate(properties.getProperty("creationdate")); //$NON-NLS-1$
151
} else
152             return 0;
153     }
154
155     public long getLastModified() {
156
157         if (properties.containsKey("getlastmodified")) { //$NON-NLS-1$
158
return processDate(properties.getProperty("getlastmodified")); //$NON-NLS-1$
159
} else
160             return 0;
161     }
162
163     public String JavaDoc getContentType() {
164         return getProperty("getcontenttype"); //$NON-NLS-1$
165
}
166
167     public String JavaDoc getDisplayName() {
168         if (properties.containsKey("displayname")) //$NON-NLS-1$
169
return getProperty("displayname"); //$NON-NLS-1$
170
else {
171             int idx = href.lastIndexOf('/');
172             if (idx > -1) {
173                 return href.substring(idx + 1);
174             } else
175                 return href;
176         }
177     }
178
179     public int getStatus() {
180         return status;
181     }
182
183     public String JavaDoc getHref() {
184         return href;
185     }
186
187     public boolean isCollection() {
188         return collection;
189     }
190
191     public String JavaDoc getProperty(String JavaDoc property) {
192         return properties.getProperty(property.toLowerCase());
193     }
194
195     public static MultiStatusResponse[] createResponse(HttpResponse response) throws IOException JavaDoc {
196
197         if (response.getStatus() != 207) {
198             throw new IOException JavaDoc(Messages.getString("MultiStatusResponse.not207")); //$NON-NLS-1$
199
}
200
201         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
202         int read;
203         byte[] buf = new byte[4096];
204         while ((read = response.getInputStream().read(buf)) > -1) {
205             out.write(buf, 0, read);
206         }
207
208         // #ifdef DEBUG
209
if (log.isDebugEnabled())
210             log.debug(new String JavaDoc(out.toByteArray()));
211         // #endif
212

213         try {
214             IXMLParser parser = XMLParserFactory.createDefaultXMLParser();
215             IXMLReader reader = StdXMLReader.stringReader(new String JavaDoc(out.toByteArray(), "UTF8")); //$NON-NLS-1$
216
parser.setReader(reader);
217             IXMLElement rootElement = (IXMLElement) parser.parse();
218
219             if (!rootElement.getName().equalsIgnoreCase("multistatus")) //$NON-NLS-1$
220
throw new IOException JavaDoc(Messages.getString("MultiStatusResponse.invalidDavRootElement") + rootElement.getName()); //$NON-NLS-1$
221

222             // Now process the responses
223
Vector JavaDoc children = rootElement.getChildrenNamed("response", rootElement.getNamespace()); //$NON-NLS-1$
224
Vector JavaDoc responses = new Vector JavaDoc();
225
226             for (Enumeration JavaDoc e = children.elements(); e.hasMoreElements();) {
227                 responses.addElement(new MultiStatusResponse((IXMLElement) e.nextElement()));
228             }
229
230             MultiStatusResponse[] array = new MultiStatusResponse[responses.size()];
231             responses.copyInto(array);
232             return array;
233
234         } catch (Exception JavaDoc ex) {
235             // #ifdef DEBUG
236
log.error(Messages.getString("MultiStatusResponse.failedToProcessMultistatusResponse"), ex); //$NON-NLS-1$
237
// #endif
238
throw new IOException JavaDoc(ex.getMessage());
239         }
240
241     }
242
243 }
244
Popular Tags