1 package com.ibm.webdav; 2 3 17 18 import org.w3c.dom.*; 19 import java.util.*; 20 import javax.xml.parsers.*; 21 22 29 public class MethodResponse extends Response { 30 31 33 private Vector resources = new Vector(); private int status = WebDAVStatus.SC_OK; 36 42 public MethodResponse(Document document, Element element) throws ServerException { 43 super(document); 44 try { 45 NodeList hrefs = element.getElementsByTagNameNS("DAV:", "href"); 46 Element href = (Element) hrefs.item(0); 47 setResource(((Text) href.getFirstChild()).getData()); 48 Element hrefn = null; 49 for (int i = 1; i < hrefs.getLength(); i++) { 50 hrefn = (Element) hrefs.item(i); 51 addResource(((Text) hrefn.getFirstChild()).getData()); 52 } 53 54 Element status = (Element) (element.getElementsByTagNameNS("DAV:", "status").item(0)); 55 String statusText = ((Text) status.getFirstChild()).getData(); 56 StringTokenizer statusFields = new StringTokenizer(statusText, " "); 57 statusFields.nextToken(); 59 int statusCode = Integer.parseInt(statusFields.nextToken()); 60 setStatus(statusCode); 61 62 Element responseDescription = (Element)element.getElementsByTagNameNS("DAV:", "responsedescription").item(0); 63 if (responseDescription != null) { 64 setDescription(((Text) responseDescription.getFirstChild()).getData()); 65 } 66 } catch (Exception exc) { 67 exc.printStackTrace(); 68 throw new ServerException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Invalid MethodResponse"); 69 } 70 } 71 77 public MethodResponse(String url, int status) { 78 super(url); 79 this.status = status; 80 } 81 87 public MethodResponse(String url, WebDAVException exc) { 88 super(url); 89 this.status = exc.getStatusCode(); 90 this.setDescription( exc.getMessage()); 91 } 92 99 public void addResource(String url) throws ServerException { 100 boolean found = url.equals(getResource()); 101 if (!found) { 102 Enumeration urls = getResources(); 103 while (!found && urls.hasMoreElements()) { 104 String aUrl = (String ) urls.nextElement(); 105 found = aUrl.equals(url); 106 } 107 } 108 if (found) { 109 throw new ServerException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Duplicate URL in a Response"); 110 } else { 111 resources.addElement(url); 112 } 113 } 114 117 public Element asXML() { 118 119 Element response = document.createElementNS("DAV:","D:response"); 120 121 Element href = document.createElementNS("DAV:","D:href"); 122 123 href.appendChild(document.createTextNode(getResource())); 124 response.appendChild(href); 125 Enumeration urls = getResources(); 126 while (urls.hasMoreElements()) { 127 String url = (String ) urls.nextElement(); 128 Element hrefn = document.createElementNS("DAV:","D:href"); 129 130 hrefn.appendChild(document.createTextNode(url)); 131 response.appendChild(hrefn); 132 } 133 134 Element status = document.createElementNS("DAV:","D:status"); 135 136 status.appendChild(document.createTextNode(HTTPVersion + " " + getStatus() + " " + WebDAVStatus.getStatusMessage(getStatus()))); 137 response.appendChild(status); 138 139 if (getDescription() != null) { 140 Element description = document.createElementNS("DAV:","D:responsedescription"); 141 142 description.appendChild(document.createTextNode(getDescription())); 143 response.appendChild(description); 144 } 145 return response; 146 } 147 153 public Enumeration getResources() { 154 return resources.elements(); 155 } 156 160 public int getStatus() { 161 return status; 162 } 163 167 public boolean isOK() { 168 return status < 300; 169 } 170 175 public void setStatus(int value) { 176 status = value; 177 } 178 183 public PropertyResponse toPropertyResponse() { 184 PropertyResponse response = new PropertyResponse(getResource()); 185 response.setDescription(getDescription()); 186 try { 187 response.setDocument(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); 188 } catch(Exception e) { 189 e.printStackTrace(System.err); 190 } 191 return response; 192 } 193 } 194 | Popular Tags |