1 23 24 package org.apache.webdav.lib.methods; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.StringWriter ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import org.apache.commons.httpclient.HttpConnection; 33 import org.apache.commons.httpclient.HttpException; 34 import org.apache.commons.httpclient.HttpState; 35 import org.apache.commons.httpclient.HttpStatus; 36 import org.apache.commons.httpclient.util.URIUtil; 37 38 import org.apache.webdav.lib.WebdavState; 39 import org.apache.webdav.lib.properties.LockEntryProperty; 40 import org.apache.webdav.lib.util.DOMUtils; 41 import org.apache.webdav.lib.util.DOMWriter; 42 import org.w3c.dom.DOMException ; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.NodeList ; 46 import org.w3c.dom.Text ; 47 48 93 public class LockMethod 94 extends XMLResponseMethodBase implements DepthSupport { 95 96 97 99 100 public static final short SCOPE_EXCLUSIVE = 101 LockEntryProperty.SCOPE_EXCLUSIVE; 102 public static final short SCOPE_SHARED = LockEntryProperty.SCOPE_SHARED; 103 104 public static final short TYPE_WRITE = LockEntryProperty.TYPE_WRITE; 105 106 public static final int TIMEOUT_INFINITY = Integer.MAX_VALUE; 108 109 110 112 113 117 private short scope = SCOPE_EXCLUSIVE; 118 119 122 private int depth = DEPTH_INFINITY; 123 124 125 128 private String refreshOpaqueToken = null; 129 130 131 134 private int timeout = TIMEOUT_INFINITY; 135 136 137 140 private String owner = null; 141 142 143 146 private String lockToken = null; 147 148 private boolean typeTransaction = false; 149 150 151 153 154 172 public LockMethod(String path, String owner, int timeout, boolean isTransaction) { 173 this(path); 174 setOwner(owner); 175 setTimeout(timeout); 176 setTypeTransaction(isTransaction); 177 } 178 179 182 public LockMethod() { 183 } 184 185 186 189 public LockMethod(String path) { 190 super(path); 191 } 192 193 194 197 public LockMethod(String path, String refreshOpaqueToken, int timeout) { 198 this(path); 199 this.refreshOpaqueToken = refreshOpaqueToken; 200 setTimeout(timeout); 201 } 202 203 204 207 public LockMethod(String path, String owner, short scope, int timeout) { 208 this(path); 209 setOwner(owner); 210 setScope(scope); 211 setTimeout(timeout); 212 } 213 214 218 public LockMethod(String path, String refreshOpaqueToken, long timeout) { 219 this(path, refreshOpaqueToken, (int) timeout); 220 } 221 222 223 227 public LockMethod(String path, String owner, short scope, long timeout) { 228 this(path, owner, scope, (int) timeout); 229 } 230 231 233 234 235 236 243 public void setRequestHeader(String headerName, String headerValue) { 244 if (headerName.equalsIgnoreCase("Depth")){ 245 int depth = -1; 246 if (headerValue.equals("0")){ 247 depth = DEPTH_0; 248 } 249 if (headerValue.equals("1")){ 250 depth = DEPTH_1; 251 } 252 else if (headerValue.equalsIgnoreCase("infinity")){ 253 depth = DEPTH_INFINITY; 254 } 255 setDepth(depth); 256 } 257 else if(headerName.equalsIgnoreCase("Timeout")){ 258 if (headerValue.startsWith("Second-")) 259 headerValue = headerValue.substring("Second-".length()); 260 try { 261 setTimeout(Integer.parseInt(headerValue)); 262 } catch (NumberFormatException e) { 263 } 264 } 265 else if(headerName.equalsIgnoreCase("Owner")){ 266 setOwner(headerValue); 267 } 268 else{ 269 super.setRequestHeader(headerName, headerValue); 270 } 271 } 272 273 public boolean isTypeTransaction() { 274 return typeTransaction; 275 } 276 277 public void setTypeTransaction(boolean typeTransaction) { 278 this.typeTransaction = typeTransaction; 279 } 280 281 286 public void setDepth(int depth) { 287 checkNotUsed(); 288 if (depth != DEPTH_0 && depth != DEPTH_INFINITY) { 289 throw new IllegalArgumentException 290 ("invalid depth value for lock method " + depth); 291 } 292 this.depth = depth; 293 } 294 295 296 301 public int getDepth() { 302 return depth; 303 } 304 305 306 public String getLockToken() { 307 checkUsed(); 308 return this.lockToken; 309 } 310 311 312 313 public boolean isRefresh() { 314 return !((this.refreshOpaqueToken == null ) || 315 (this.refreshOpaqueToken.equals(""))); 316 } 317 318 319 public short getScope() { 320 return this.scope; 321 } 322 323 324 337 public void setOwner(String owner) { 338 checkNotUsed(); 339 this.owner = owner; 340 } 341 342 343 public String getOwner() { 344 return owner; 345 } 346 347 348 public void setScope(short scope) { 349 checkNotUsed(); 350 if (scope != SCOPE_SHARED && scope != SCOPE_EXCLUSIVE) { 351 throw new IllegalArgumentException ("invalid scope value"); 352 } 353 this.scope = scope; 354 } 355 356 357 362 public int getTimeout() { 363 return this.timeout; 364 } 365 366 367 370 public void setTimeout(int timeout) { 371 checkNotUsed(); 372 if (timeout < 0) { 373 throw new IllegalArgumentException ("invalid timeout value: " + 374 timeout); 375 } 376 this.timeout = timeout; 377 } 378 379 383 public void setTimeout(long timeout) { 384 setTimeout((int) timeout); 385 } 386 387 388 390 public String getName() { 391 return "LOCK"; 392 } 393 394 public void recycle() { 395 super.recycle(); 396 this.refreshOpaqueToken = null; 397 this.depth = DEPTH_INFINITY; 398 this.scope = SCOPE_EXCLUSIVE; 399 this.timeout = TIMEOUT_INFINITY; 400 this.typeTransaction = false; 401 } 402 403 404 410 public void addRequestHeaders(HttpState state, HttpConnection conn) 411 throws IOException , HttpException { 412 413 if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 415 super.addRequestHeaders(state, conn); 416 417 switch (depth) { 418 case DEPTH_0: 419 super.setRequestHeader("Depth", "0"); 420 break; 421 case DEPTH_INFINITY: 422 super.setRequestHeader("Depth", "infinity"); 423 break; 424 default: 425 } 426 427 if (timeout == TIMEOUT_INFINITY) { 428 super.setRequestHeader("Timeout", "Infinite, Second-" + TIMEOUT_INFINITY); 429 } else { 430 super.setRequestHeader("Timeout", "Second-" + timeout); 431 } 432 433 if (isRefresh()) { 434 super.setRequestHeader("If", "(<" + refreshOpaqueToken + ">)"); 435 } 436 437 } 438 439 445 protected String generateRequestBody() { 446 447 String result = ""; 448 449 if (!isRefresh()) { 450 451 if (this.owner == null || this.owner.equals("")) { 452 throw new IllegalStateException 453 ("The owner property has not been set"); 454 } 455 456 try { 457 458 DocumentBuilderFactory factory = 459 DocumentBuilderFactory.newInstance(); 460 DocumentBuilder builder = factory.newDocumentBuilder(); 461 Document document = builder.newDocument(); 462 463 Element lockinfo = document.createElement("DAV:lockinfo"); 464 document.appendChild(lockinfo); 465 lockinfo.setAttribute("xmlns:DAV", "DAV:"); 466 467 Element lockscope = document.createElement("DAV:lockscope"); 468 lockinfo.appendChild(lockscope); 469 470 if (this.scope == SCOPE_EXCLUSIVE) { 471 Element exclusive = 472 document.createElement("DAV:exclusive"); 473 lockscope.appendChild(exclusive); 474 } else { 475 Element shared = document.createElement("DAV:shared"); 476 lockscope.appendChild(shared); 477 } 478 479 Element locktype = document.createElement("DAV:locktype"); 480 lockinfo.appendChild(locktype); 481 482 if (typeTransaction) { 483 Element transaction = document.createElement("DAV:transaction"); 484 locktype.appendChild(transaction); 485 } else { 486 Element write = document.createElement("DAV:write"); 487 locktype.appendChild(write); 488 } 489 490 Element owner = document.createElement("DAV:owner"); 491 lockinfo.appendChild(owner); 492 493 Text text = document.createTextNode(this.owner); 494 owner.appendChild(text); 495 496 StringWriter stringWriter = new StringWriter (); 497 DOMWriter domWriter = new DOMWriter(stringWriter, false); 498 domWriter.print(document); 499 500 result = stringWriter.getBuffer().toString(); 501 502 } catch (DOMException e) { 503 } catch (ParserConfigurationException e) { 504 } 505 506 } 507 508 return result; 509 } 510 511 516 public void parseResponse(InputStream input, HttpState state, HttpConnection conn) 517 throws IOException , HttpException { 518 int status = getStatusLine().getStatusCode(); 519 if (status == HttpStatus.SC_OK || 520 status == HttpStatus.SC_CREATED || 521 status == HttpStatus.SC_MULTI_STATUS ) { 522 523 parseXMLResponse(input); 524 525 NodeList list = getResponseDocument().getDocumentElement() 526 .getElementsByTagNameNS("DAV:", "locktoken"); 527 528 if (list.getLength() == 1) { 529 Element locktoken = (Element ) list.item(0); 530 NodeList list2 = locktoken.getElementsByTagNameNS("DAV:", "href"); 531 if (list2.getLength() == 1) { 532 this.lockToken = DOMUtils.getTextValue(list2.item(0)); 533 if (state instanceof WebdavState) { 534 539 ((WebdavState) state).addLock(URIUtil.decode(getPath()), 540 this.lockToken); 541 } 542 } 543 } 544 545 list = getResponseDocument().getDocumentElement() 546 .getElementsByTagNameNS("DAV:", "owner"); 547 548 if (list.getLength() == 1) { 549 Element owner = (Element )list.item(0); 550 551 this.owner = DOMUtils.getTextValue(owner); 552 } 553 } 554 } 555 } 556 557 | Popular Tags |