1 19 20 package com.maverick.http; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Date ; 26 import java.util.Enumeration ; 27 import java.util.NoSuchElementException ; 28 import java.util.Properties ; 29 import java.util.StringTokenizer ; 30 import java.util.Vector ; 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 44 public class MultiStatusResponse { 45 46 static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(MultiStatusResponse.class); 48 50 String href; 51 int status; 52 String version; 53 String reason; 54 Properties properties; 55 boolean collection = false; 56 57 static SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 59 MultiStatusResponse(IXMLElement element) throws IOException { 60 if (element.getFirstChildNamed("href", element.getNamespace()) == null) throw new IOException (Messages.getString("MultiStatusResponse.unexpectedHref")); 63 href = URLUTF8Encoder.decode(element.getFirstChildNamed("href", element.getNamespace()).getContent()); 65 IXMLElement props = element.getFirstChildNamed("propstat", element.getNamespace()); 67 if (props == null) 68 throw new IOException (Messages.getString("MultiStatusResponse.noPropertyElements")); 70 if (props.getFirstChildNamed("status", element.getNamespace()) == null) throw new IOException (Messages.getString("MultiStatusResponse.unexpectedStatusResponse")); 73 String status = props.getFirstChildNamed("status", element.getNamespace()).getContent(); 75 StringTokenizer tokens = new StringTokenizer (status, " ", false); reason = ""; 78 try { 79 version = tokens.nextToken(); 80 this.status = Integer.parseInt(tokens.nextToken()); 81 82 while (tokens.hasMoreTokens()) { 83 reason += tokens.nextToken() + " "; } 85 reason = URLDecoder.decode(reason.trim()); 86 } catch (NoSuchElementException e) { 87 throw new IOException (Messages.getString("MultiStatusResponse.failedToReadHTTPResponseHeader")); } catch (NumberFormatException e) { 89 throw new IOException (Messages.getString("MultiStatusResponse.failedToReadHTTPResponseHeader")); } 91 92 properties = new Properties (); 94 95 if (this.status == 404) 97 return; 98 99 props = props.getFirstChildNamed("prop", props.getNamespace()); 101 if (props == null) 102 throw new IOException (Messages.getString("MultiStatusResponse.noPropElementsInPropStat")); 104 IXMLElement child; 105 106 for (Enumeration e = props.getChildren().elements(); e.hasMoreElements();) { 107 child = (IXMLElement) e.nextElement(); 108 109 if (child.getName().equalsIgnoreCase("resourcetype")) { if (child.getChildrenNamed("collection") != null) collection = true; 112 } else { 113 properties.put(child.getName().toLowerCase(), child.getContent() == null ? "" : child.getContent()); } 115 } 116 117 } 118 119 public long getContentLength() { 120 if (properties.containsKey("getcontentlength")) { return Long.parseLong(properties.getProperty("getcontentlength")); } else 123 return 0; 124 } 125 126 private long processDate(String date) { 127 128 long retval = 0; 129 try { 130 retval = Date.parse(date); 131 } catch (Throwable t) { 132 133 try { 134 139 return 0; } catch (Throwable t2) { 141 } 142 } 143 144 return retval; 145 } 146 147 public long getCreationDate() { 148 149 if (properties.containsKey("creationdate")) { return processDate(properties.getProperty("creationdate")); } else 152 return 0; 153 } 154 155 public long getLastModified() { 156 157 if (properties.containsKey("getlastmodified")) { return processDate(properties.getProperty("getlastmodified")); } else 160 return 0; 161 } 162 163 public String getContentType() { 164 return getProperty("getcontenttype"); } 166 167 public String getDisplayName() { 168 if (properties.containsKey("displayname")) return getProperty("displayname"); 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 getHref() { 184 return href; 185 } 186 187 public boolean isCollection() { 188 return collection; 189 } 190 191 public String getProperty(String property) { 192 return properties.getProperty(property.toLowerCase()); 193 } 194 195 public static MultiStatusResponse[] createResponse(HttpResponse response) throws IOException { 196 197 if (response.getStatus() != 207) { 198 throw new IOException (Messages.getString("MultiStatusResponse.not207")); } 200 201 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 if (log.isDebugEnabled()) 210 log.debug(new String (out.toByteArray())); 211 213 try { 214 IXMLParser parser = XMLParserFactory.createDefaultXMLParser(); 215 IXMLReader reader = StdXMLReader.stringReader(new String (out.toByteArray(), "UTF8")); parser.setReader(reader); 217 IXMLElement rootElement = (IXMLElement) parser.parse(); 218 219 if (!rootElement.getName().equalsIgnoreCase("multistatus")) throw new IOException (Messages.getString("MultiStatusResponse.invalidDavRootElement") + rootElement.getName()); 222 Vector children = rootElement.getChildrenNamed("response", rootElement.getNamespace()); Vector responses = new Vector (); 225 226 for (Enumeration 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 ex) { 235 log.error(Messages.getString("MultiStatusResponse.failedToProcessMultistatusResponse"), ex); throw new IOException (ex.getMessage()); 239 } 240 241 } 242 243 } 244 | Popular Tags |