1 23 24 package org.apache.webdav.lib.methods; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import org.apache.commons.httpclient.HttpConnection; 31 import org.apache.commons.httpclient.HttpException; 32 import org.apache.commons.httpclient.HttpState; 33 import org.apache.webdav.lib.util.WebdavStatus; 34 import org.apache.webdav.lib.util.XMLPrinter; 35 36 37 41 public class PropPatchMethod 42 extends XMLResponseMethodBase { 43 44 45 47 48 51 public PropPatchMethod() { 52 } 53 54 55 58 public PropPatchMethod(String path) { 59 super(path); 60 } 61 62 63 65 66 69 protected Hashtable toSet = new Hashtable (); 70 71 72 75 protected Hashtable toRemove = new Hashtable (); 76 77 78 80 81 87 public void addPropertyToSet(String name, String value) { 88 checkNotUsed(); 89 Property propertyToSet = new Property(); 90 if (name != null) { 91 propertyToSet.name = name; 92 if (value != null) 93 propertyToSet.value = value; 94 else 95 propertyToSet.value = ""; 96 toSet.put(name, propertyToSet); 97 } 98 } 99 100 101 109 public void addPropertyToSet(String name, String value, String namespace, 110 String namespaceInfo) { 111 checkNotUsed(); 112 Property propertyToSet = new Property(); 113 if (name != null) { 114 propertyToSet.name = name; 115 if (value != null) 116 propertyToSet.value = value; 117 else 118 propertyToSet.value = ""; 119 propertyToSet.namespace = namespace; 120 propertyToSet.namespaceInfo = namespaceInfo; 121 toSet.put(namespace + name, propertyToSet); 122 } 123 } 124 125 126 131 public void addPropertyToRemove(String name) { 132 checkNotUsed(); 133 Property propertyToRemove = new Property(); 134 if (name != null) { 135 propertyToRemove.name = name; 136 toRemove.put(name, propertyToRemove); 137 } 138 } 139 140 141 148 public void addPropertyToRemove(String name, String namespace, 149 String namespaceInfo) { 150 checkNotUsed(); 151 Property propertyToRemove = new Property(); 152 if (name != null) { 153 propertyToRemove.name = name; 154 propertyToRemove.namespace = namespace; 155 propertyToRemove.namespaceInfo = namespaceInfo; 156 toRemove.put(name, propertyToRemove); 157 } 158 } 159 160 161 163 164 public String getName() { 165 return "PROPPATCH"; 166 } 167 168 174 public void addRequestHeaders(HttpState state, HttpConnection conn) 175 throws IOException , HttpException { 176 177 if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 179 super.addRequestHeaders(state, conn); 180 181 } 182 183 189 protected String generateRequestBody() { 190 XMLPrinter printer = new XMLPrinter(); 191 192 193 printer.writeXMLHeader(); 194 printer.writeElement("D", "DAV:", "propertyupdate", 195 XMLPrinter.OPENING); 196 197 if (toSet.size() > 0) { 198 199 printer.writeElement("D", null, "set", XMLPrinter.OPENING); 200 201 Enumeration toSetList = toSet.elements(); 202 printer.writeElement("D", null, "prop", XMLPrinter.OPENING); 203 while (toSetList.hasMoreElements()) { 204 Property current = (Property) toSetList.nextElement(); 205 if ("DAV:".equals(current.namespaceInfo)) { 206 printer.writeProperty("D", null, current.name, current.value); 207 } 208 else { 209 printer.writeProperty(current.namespace, current.namespaceInfo, 210 current.name, current.value); 211 } 212 } 213 printer.writeElement("D", null, "prop", XMLPrinter.CLOSING); 214 215 printer.writeElement("D", null, "set", XMLPrinter.CLOSING); 216 217 } 218 219 if (toRemove.size() > 0) { 220 221 printer.writeElement("D", null, "remove", XMLPrinter.OPENING); 222 223 Enumeration toRemoveList = toRemove.elements(); 224 printer.writeElement("D", null, "prop", XMLPrinter.OPENING); 225 while (toRemoveList.hasMoreElements()) { 226 Property current = (Property) toRemoveList.nextElement(); 227 printer.writeElement(current.namespace, current.namespaceInfo, 228 current.name, XMLPrinter.NO_CONTENT); 229 } 230 printer.writeElement("D", null, "prop", XMLPrinter.CLOSING); 231 232 printer.writeElement("D", null, "remove", XMLPrinter.CLOSING); 233 234 } 235 236 printer.writeElement("D", "propertyupdate", XMLPrinter.CLOSING); 237 238 return printer.toString(); 239 } 240 241 246 public void parseResponse(InputStream input, HttpState state, HttpConnection conn) 247 throws IOException , HttpException { 248 try 249 { 250 int code = getStatusLine().getStatusCode(); 251 if (code == WebdavStatus.SC_CONFLICT || 252 code == WebdavStatus.SC_MULTI_STATUS || 253 code == WebdavStatus.SC_FORBIDDEN ) { 254 parseXMLResponse(input); 255 } 256 } 257 catch (IOException e) { 258 } 260 } 261 262 263 265 266 private class Property { 267 268 public String name = ""; 269 public String namespace; 270 public String namespaceInfo; 271 public String value; 272 273 } 274 275 276 } 277 | Popular Tags |