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 47 public class ReportMethod extends XMLResponseMethodBase 48 implements DepthSupport { 49 50 51 53 54 57 public static final int SUB_SET = 0; 58 59 60 63 public static final int ALL = 1; 64 65 public static final int LOCATE_HISTORY = 2; 66 67 public String sVersionHistory =""; 68 69 private String preloadedQuery = null; 70 71 73 74 77 public ReportMethod() { 78 } 79 80 81 84 public ReportMethod(String path) { 85 super(path); 86 } 87 88 89 92 public ReportMethod(String path, int depth) { 93 this(path); 94 setDepth(depth); 95 } 96 97 100 public ReportMethod(String path, Enumeration propertyNames) { 101 this(path); 102 setDepth(1); 103 setPropertyNames(propertyNames); 104 setType(SUB_SET); 105 } 106 107 110 public ReportMethod(String path, int depth, Enumeration propertyNames, Enumeration histUrl) { 111 this(path); 112 setDepth(depth); 113 setPropertyNames(propertyNames); 114 setHistoryURLs(histUrl); 115 setType(LOCATE_HISTORY); 116 } 117 118 121 public ReportMethod(String path, int depth, Enumeration propertyNames) { 122 this(path); 123 setDepth(depth); 124 setPropertyNames(propertyNames); 125 setType(SUB_SET); 126 } 127 128 129 130 public ReportMethod(String path, int depth, String sBody) { 131 this(path); 132 setDepth(depth); 133 setType(SUB_SET); 134 preloadedQuery = sBody; 135 } 136 137 138 140 141 144 protected int type = ALL; 145 146 147 150 protected PropertyName[] propertyNames; 151 152 155 protected int depth = DEPTH_INFINITY; 156 157 158 161 protected String prefix = null; 162 163 164 166 167 168 169 176 public void setRequestHeader(String headerName, String headerValue) { 177 if (headerName.equalsIgnoreCase("Depth")){ 178 int depth = -1; 179 if (headerValue.equals("0")){ 180 depth = DEPTH_0; 181 } 182 if (headerValue.equals("1")){ 183 depth = DEPTH_1; 184 } 185 else if (headerValue.equalsIgnoreCase("infinity")){ 186 depth = DEPTH_INFINITY; 187 } 188 setDepth(depth); 189 } 190 else{ 191 super.setRequestHeader(headerName, headerValue); 192 } 193 } 194 195 196 201 public void setType(int type) { 202 checkNotUsed(); 203 this.type = type; 204 } 205 206 207 212 public int getType() { 213 return type; 214 } 215 216 217 222 public void setDepth(int depth) { 223 checkNotUsed(); 224 this.depth = depth; 225 } 226 227 228 233 public int getDepth() { 234 return depth; 235 } 236 237 238 245 public void setPropertyNames(Enumeration propertyNames) { 246 checkNotUsed(); 247 248 Vector list = new Vector (); 249 while (propertyNames.hasMoreElements()) { 250 251 Object item = propertyNames.nextElement(); 252 253 if (item instanceof PropertyName) 254 { 255 list.add(item); 256 } 257 else if (item instanceof String ) 258 { 259 String propertyName = (String ) item; 260 261 int length = propertyName.length(); 262 boolean found = false; 263 int i = 1; 264 while (!found && (i <= length)) { 265 char chr = propertyName.charAt(length - i); 266 if (!Character.isUnicodeIdentifierPart(chr) 267 && chr!='-' && chr!='_' && chr!='.') { 268 found = true; 269 } else { 270 i++; 271 } 272 } 273 if ((i == 1) || (i >= length)) { 274 list.add(new PropertyName("DAV:",propertyName)); 275 } else { 276 String namespace = propertyName.substring(0, length + 1 - i); 277 String localName = propertyName.substring(length + 1 - i); 278 list.add(new PropertyName(namespace,localName)); 279 } 280 } 281 else 282 { 283 } 286 } 287 288 this.propertyNames = (PropertyName[])list.toArray(new PropertyName[list.size()]); 289 } 290 291 294 public void setHistoryURLs(Enumeration historyURLs) { 295 296 sVersionHistory = "<D:version-history-set>"; 297 298 while (historyURLs.hasMoreElements()) 299 { 300 sVersionHistory += "<D:href>"+ historyURLs.nextElement().toString() + "</D:href>"; 301 } 302 303 sVersionHistory += "</D:version-history-set>"; 304 } 305 306 307 309 public String getName() { 310 return "REPORT"; 311 } 312 313 public void recycle() { 314 super.recycle(); 315 prefix = null; 316 } 317 318 324 public void addRequestHeaders(HttpState state, HttpConnection conn) 325 throws IOException , HttpException { 326 327 if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 329 super.addRequestHeaders(state, conn); 330 331 switch (depth) { 332 case DEPTH_0: 333 super.setRequestHeader("Depth", "0"); 334 break; 335 case DEPTH_1: 336 super.setRequestHeader("Depth", "1"); 337 break; 338 case DEPTH_INFINITY: 339 super.setRequestHeader("Depth", "infinity"); 340 break; 341 } 342 343 } 344 345 351 protected String generateRequestBody() { 352 353 if (preloadedQuery != null) 354 return preloadedQuery; 355 356 XMLPrinter printer = new XMLPrinter(); 357 358 359 printer.writeXMLHeader(); 360 if (type!= LOCATE_HISTORY) 361 printer.writeElement("D", "DAV:", "version-tree", 362 XMLPrinter.OPENING); 363 364 switch (type) { 365 case ALL: 366 printer.writeElement("D", "allprop", XMLPrinter.NO_CONTENT); 367 printer.writeElement("D", "version-tree", XMLPrinter.CLOSING); 368 break; 369 370 case SUB_SET: 371 printer.writeElement("D", "prop", XMLPrinter.OPENING); 372 for (int i=0 ; i<propertyNames.length ; i++) 373 { 374 String namespace = propertyNames[i].getNamespaceURI(); 375 String localname = propertyNames[i].getLocalName(); 376 if ("DAV:".equals(namespace)) { 377 printer.writeElement("D", localname, XMLPrinter.NO_CONTENT); 378 } else { 379 printer.writeElement("ZZ", namespace,localname , XMLPrinter.NO_CONTENT); 380 } 381 } 382 printer.writeElement("D", "prop", XMLPrinter.CLOSING); 383 printer.writeElement("D", "version-tree", XMLPrinter.CLOSING); 384 break; 385 386 case LOCATE_HISTORY: 387 printer.writeElement("D", "DAV:", "locate-by-history", XMLPrinter.OPENING); 388 389 printer.writeText(sVersionHistory); 390 391 printer.writeElement("D", "prop", XMLPrinter.OPENING); 392 for (int i=0 ; i<propertyNames.length ; i++) 393 { 394 String namespace = propertyNames[i].getNamespaceURI(); 395 String localname = propertyNames[i].getLocalName(); 396 if ("DAV:".equals(namespace)) { 397 printer.writeElement("D", localname, XMLPrinter.NO_CONTENT); 398 } else { 399 printer.writeElement("ZZ", namespace,localname , XMLPrinter.NO_CONTENT); 400 } 401 } 402 printer.writeElement("D", "prop", XMLPrinter.CLOSING); 403 printer.writeElement("D", "locate-by-history", XMLPrinter.CLOSING); 404 break; 405 406 } 407 408 return printer.toString(); 410 411 } 412 413 421 public Enumeration getAllResponseURLs() { 422 checkUsed(); 423 return getResponseURLs().elements(); 424 } 425 426 427 430 public Enumeration getResponseProperties(String urlPath) { 431 checkUsed(); 432 433 Response response = (Response) getResponseHashtable().get(urlPath); 434 if (response != null) { 435 return response.getProperties(); 436 } else { 437 return (new Vector ()).elements(); 438 } 439 440 } 441 } 442 443 | Popular Tags |