1 23 24 package org.apache.webdav.lib.methods; 25 26 import java.io.IOException ; 27 import java.util.Enumeration ; 28 import java.util.Vector ; 29 import org.apache.commons.httpclient.HttpConnection; 30 import org.apache.commons.httpclient.HttpException; 31 import org.apache.commons.httpclient.HttpState; 32 import org.apache.webdav.lib.PropertyName; 33 import org.apache.webdav.lib.util.XMLPrinter; 34 35 65 public class PropFindMethod extends XMLResponseMethodBase 66 implements DepthSupport { 67 68 69 71 72 75 public static final int BY_NAME = 0; 76 77 78 81 public static final int ALL = 1; 82 83 84 87 public static final int NAMES = 2; 88 89 90 92 93 96 public PropFindMethod() { 97 } 98 99 100 103 public PropFindMethod(String path) { 104 super(path); 105 } 106 107 108 111 public PropFindMethod(String path, int depth) { 112 this(path); 113 setDepth(depth); 114 } 115 116 117 120 public PropFindMethod(String path, int depth, int type) { 121 this(path); 122 setDepth(depth); 123 setType(type); 124 } 125 126 127 130 public PropFindMethod(String path, Enumeration propertyNames) { 131 this(path); 132 setDepth(1); 133 setPropertyNames(propertyNames); 134 setType(BY_NAME); 135 } 136 137 138 141 public PropFindMethod(String path, int depth, Enumeration propertyNames) { 142 this(path); 143 setDepth(depth); 144 setPropertyNames(propertyNames); 145 setType(BY_NAME); 146 } 147 148 149 151 152 155 protected int type = ALL; 156 157 158 161 protected PropertyName[] propertyNames; 162 163 166 protected int depth = DEPTH_INFINITY; 167 168 169 172 protected String prefix = null; 173 174 175 177 178 179 180 187 public void setRequestHeader(String headerName, String headerValue) { 188 if (headerName.equalsIgnoreCase("Depth")){ 189 int depth = -1; 190 if (headerValue.equals("0")){ 191 depth = DEPTH_0; 192 } 193 else if (headerValue.equals("1")){ 194 depth = DEPTH_1; 195 } 196 else if (headerValue.equalsIgnoreCase("infinity")){ 197 depth = DEPTH_INFINITY; 198 } 199 setDepth(depth); 200 } 201 else{ 202 super.setRequestHeader(headerName, headerValue); 203 } 204 } 205 206 207 212 public void setType(int type) { 213 checkNotUsed(); 214 this.type = type; 215 } 216 217 218 223 public int getType() { 224 return type; 225 } 226 227 228 233 public void setDepth(int depth) { 234 checkNotUsed(); 235 this.depth = depth; 236 } 237 238 239 244 public int getDepth() { 245 return depth; 246 } 247 248 249 256 public void setPropertyNames(Enumeration propertyNames) { 257 checkNotUsed(); 258 259 Vector list = new Vector (); 260 while (propertyNames.hasMoreElements()) { 261 262 Object item = propertyNames.nextElement(); 263 264 if (item instanceof PropertyName) 265 { 266 list.add(item); 267 } 268 else if (item instanceof String ) 269 { 270 String propertyName = (String ) item; 271 272 int length = propertyName.length(); 273 boolean found = false; 274 int i = 1; 275 while (!found && (i <= length)) { 276 char chr = propertyName.charAt(length - i); 277 if (!Character.isUnicodeIdentifierPart(chr) 278 && chr!='-' && chr!='_' && chr!='.') { 279 found = true; 280 } else { 281 i++; 282 } 283 } 284 if ((i == 1) || (i >= length)) { 285 list.add(new PropertyName("DAV:",propertyName)); 286 } else { 287 String namespace = propertyName.substring(0, length + 1 - i); 288 String localName = propertyName.substring(length + 1 - i); 289 list.add(new PropertyName(namespace,localName)); 290 } 291 } 292 else 293 { 294 } 297 } 298 299 this.propertyNames = (PropertyName[])list.toArray(new PropertyName[list.size()]); 300 } 301 302 303 305 306 public void recycle() { 307 super.recycle(); 308 prefix = null; 309 } 310 311 public String getName() { 312 return "PROPFIND"; 313 314 } 315 316 317 323 public void addRequestHeaders(HttpState state, HttpConnection conn) 324 throws IOException , HttpException { 325 326 if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 328 super.addRequestHeaders(state, conn); 329 330 switch (depth) { 331 case DEPTH_0: 332 super.setRequestHeader("Depth", "0"); 333 break; 334 case DEPTH_1: 335 super.setRequestHeader("Depth", "1"); 336 break; 337 case DEPTH_INFINITY: 338 super.setRequestHeader("Depth", "infinity"); 339 break; 340 } 341 342 } 343 344 350 protected String generateRequestBody() { 351 352 XMLPrinter printer = new XMLPrinter(); 353 354 printer.writeXMLHeader(); 355 printer.writeElement("D", "DAV:", "propfind", 356 XMLPrinter.OPENING); 357 358 359 360 switch (type) { 361 case ALL: 362 printer.writeElement("D", "allprop", XMLPrinter.NO_CONTENT); 363 break; 364 case NAMES: 365 printer.writeElement("D", "propname", XMLPrinter.NO_CONTENT); 366 break; 367 case BY_NAME: 368 printer.writeElement("D", "prop", XMLPrinter.OPENING); 369 for (int i=0 ; i<propertyNames.length ; i++) 370 { 371 String namespace = propertyNames[i].getNamespaceURI(); 372 String localname = propertyNames[i].getLocalName(); 373 if ("DAV:".equals(namespace)) { 374 printer.writeElement("D", localname, XMLPrinter.NO_CONTENT); 375 } else { 376 if (namespace.length() > 0) { 377 printer.writeElement("ZZ", namespace, localname, 378 XMLPrinter.NO_CONTENT); 379 } else { 380 printer.writeElement(null, null, localname, 381 XMLPrinter.NO_CONTENT); 382 } 383 } 384 } 385 printer.writeElement("D", "prop", XMLPrinter.CLOSING); 386 break; 387 } 388 389 printer.writeElement("D", "propfind", XMLPrinter.CLOSING); 390 391 return printer.toString(); 392 } 393 394 402 public Enumeration getAllResponseURLs() { 403 checkUsed(); 404 return getResponseURLs().elements(); 405 } 406 407 410 public Enumeration getResponseProperties(String urlPath) { 411 checkUsed(); 412 413 Response response = (Response) getResponseHashtable().get(urlPath); 414 if (response != null) { 415 return response.getProperties(); 416 } else { 417 return (new Vector ()).elements(); 418 } 419 420 } 421 } 422 423 | Popular Tags |