1 17 package com.sslexplorer.vfs.webdav; 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import com.sslexplorer.vfs.VFSResource; 26 27 28 35 public class DAVMultiStatus extends DAVException { 36 37 private Set responses = new HashSet (); 38 39 42 public DAVMultiStatus() { 43 super(207, "Multi-Status response"); 44 } 45 46 50 public void write(DAVTransaction transaction) 51 throws IOException { 52 53 transaction.setStatus(207); 54 transaction.setContentType("text/xml; charset=\"utf-8\""); 55 PrintWriter out = transaction.write("utf-8"); 56 57 58 out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 59 out.println("<D:multistatus xmlns:D=\"DAV:\">"); 60 61 Iterator 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 91 public int size() { 92 return this.responses.size(); 93 } 94 95 99 public void merge(DAVMultiStatus multistatus) { 100 if (multistatus == null) return; 101 Iterator iterator = multistatus.responses.iterator(); 102 while (iterator.hasNext()) this.responses.add(iterator.next()); 103 } 104 105 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 message = exception.getMessage(); 115 this.responses.add(new Response(resource, status, message)); 116 } 117 118 private static class Response implements Comparable { 119 private VFSResource resource = null; 120 private int status = 0; 121 private String 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 message) { 128 if (resource == null) throw new NullPointerException (); 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 object) { 139 Response response = (Response) object; 140 return (this.resource.compareTo(response.resource)); 141 } 142 143 public boolean equals(Object 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 |