1 package com.ibm.webdav; 2 3 17 18 import java.util.*; 19 import org.w3c.dom.*; 20 import javax.xml.parsers.*; 21 import java.io.*; 22 23 30 public class MultiStatus extends Object implements Serializable { 31 private Vector responses = new Vector(); 32 private String description = null; 33 36 public MultiStatus() throws ServerException 37 { 38 } 39 45 public MultiStatus(Document document) throws ServerException { 46 init(document); 47 } 48 52 public void addResponse(Response response) { 53 responses.addElement(response); 54 } 55 59 public Document asXML() { 60 61 Document document = null; 62 63 try { 64 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 65 } catch(Exception e) { 66 e.printStackTrace(System.err); 67 throw new RuntimeException (e.getMessage()); 68 } 69 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 92 return document; 93 } 94 111 public ActiveLock getActiveLockFor(String 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 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 143 public String getDescription() { 144 return description; 145 } 146 152 public Enumeration getResponses() { 153 return responses.elements(); 154 } 155 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 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 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 209 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { 210 int size = in.readInt(); 211 byte[] buffer = new byte[size]; 212 in.readFully(buffer); 213 ByteArrayInputStream is = new ByteArrayInputStream(buffer); 214 215 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 (is)); 224 init(contents); 225 } catch (Exception exc) { 226 System.err.println(exc); 227 throw new IOException(exc.getMessage()); 228 } 229 } 230 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 247 public void removeResponse(Response response) { 248 try { 249 responses.removeElement(response); 250 } catch (Exception exc) { 251 } 252 } 253 257 public void setDescription(String value) { 258 description = value; 259 } 260 266 public String toString() { 267 276 277 Document document = null; 278 279 try { 280 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 281 } catch(Exception e) { 282 e.printStackTrace(System.err); 283 } 284 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 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 311 private void writeObject(java.io.ObjectOutputStream 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 out.writeInt(os.size()); 318 out.write(os.toByteArray()); 319 } 320 } 321 | Popular Tags |