1 package com.ibm.webdav; 2 3 17 18 import java.util.*; 19 import java.util.logging.*; 20 21 import javax.xml.parsers.*; 22 23 import org.w3c.dom.*; 24 25 33 public class ActiveLock extends Object { 34 35 39 public static final String exclusive = "exclusive"; 40 41 47 public static final String shared = "shared"; 48 49 53 public static final String writeLock = "write"; 54 55 57 private String timeout = "Infinite"; 59 60 private String depth = Collection.deep; 61 62 private String scope = ActiveLock.exclusive; 64 65 private String lockType = ActiveLock.writeLock; 66 67 private Element owner = null; 68 69 private String sOwner = null; 70 71 private String lockToken = null; 73 74 private Date expiration = null; 77 private String principal = null; 79 82 private static final Logger m_logger = Logger.getLogger(ActiveLock.class.getName()); 83 84 88 public ActiveLock() { 89 } 90 91 101 public ActiveLock(Element activeLock) throws WebDAVException { 102 103 Element lockScope = (Element) activeLock.getElementsByTagNameNS("DAV:", 104 "lockscope").item(0); 105 setScope(((Element) lockScope.getFirstChild()).getLocalName()); 106 107 Element lockType = (Element) activeLock.getElementsByTagNameNS("DAV:", 108 "locktype").item(0); 109 setLockType(((Element) lockType.getFirstChild()).getLocalName()); 110 111 Element depth = (Element) activeLock.getElementsByTagNameNS("DAV:", 112 "depth").item(0); 113 setDepth(((Text) depth.getFirstChild()).getData()); 114 115 Element owner = (Element) activeLock.getElementsByTagNameNS("DAV:", 116 "owner").item(0); 117 if (owner != null) { 118 setOwner(owner); 119 } 120 Element timeout = (Element) activeLock.getElementsByTagNameNS("DAV:", 121 "timeout").item(0); 122 if (timeout != null) { 123 this.timeout = ((Text) timeout.getFirstChild()).getData(); 124 } 125 126 Element lockToken = (Element) activeLock.getElementsByTagNameNS("DAV:", 128 "locktoken").item(0); 129 if (lockToken != null) { 130 Element href = (Element) lockToken.getElementsByTagNameNS("DAV:", 131 "href").item(0); 132 if (href != null) { 133 this.lockToken = ((Text) href.getFirstChild()).getData(); 134 } 135 } 136 137 139 Element principal = (Element) activeLock.getElementsByTagNameNS("DAV:", 140 "principal").item(0); 141 if (principal != null) { 142 setPrincipal(((Text) principal.getFirstChild()).getData()); 143 } 144 145 151 } 164 165 173 public Element asXML() { 174 String sPrefix = "D"; 175 Document document = null; 177 178 try { 179 document = DocumentBuilderFactory.newInstance() 180 .newDocumentBuilder().newDocument(); 181 } catch (Exception e) { 182 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 183 } 184 187 Element activelock = document.createElementNS("DAV:", "D:activelock"); 188 189 activelock.setAttribute("xmlns:D", "DAV:"); 190 191 Element lockscope = document.createElementNS("DAV:", "D:lockscope"); 192 193 Element scopeEl = document.createElementNS("DAV:", "D:" + this.scope); 194 195 lockscope.appendChild(scopeEl); 196 activelock.appendChild(lockscope); 197 198 Element locktype = document.createElementNS("DAV:", "D:locktype"); 199 200 Element locktypeEl = document.createElementNS("DAV:", "D:" 201 + this.lockType); 202 203 locktype.appendChild(locktypeEl); 204 activelock.appendChild(locktype); 205 206 Element depth = document.createElementNS("DAV:", "D:depth"); 207 208 depth.appendChild(document.createTextNode(this.depth)); 209 activelock.appendChild(depth); 210 211 if (this.owner != null) { 212 if (owner.getPrefix() == null 213 || owner.getPrefix().equals("D") == false) { 214 215 Element ownerEl = document.createElementNS("DAV:", "D:owner"); 216 Element hrefEl = document.createElementNS("DAV:", "D:href"); 217 218 String sOwnerVal = owner.getChildNodes().item(0) 219 .getChildNodes().item(0).getNodeValue(); 220 221 Text txtOwnerVal = document.createTextNode(sOwnerVal); 222 223 hrefEl.appendChild(txtOwnerVal); 224 225 ownerEl.appendChild(hrefEl); 226 activelock.appendChild(ownerEl); 227 owner = ownerEl; 228 } else { 229 activelock.appendChild(document.importNode(owner, true)); 230 } 231 } else if (sOwner != null) { 232 Element ownerEl = document.createElementNS("DAV:", "D:owner"); 233 Element hrefEl = document.createElementNS("DAV:", "D:href"); 234 235 hrefEl.appendChild(document.createTextNode(sOwner)); 236 ownerEl.appendChild(hrefEl); 237 activelock.appendChild(ownerEl); 238 } 239 240 Element timeout = document.createElementNS("DAV:", "D:timeout"); 241 242 timeout.appendChild(document.createTextNode(this.timeout)); 243 activelock.appendChild(timeout); 244 245 if (this.lockToken != null) { 246 Element locktoken = document.createElementNS("DAV:", "D:locktoken"); 247 248 Element href = document.createElementNS("DAV:", "D:href"); 249 250 href.appendChild(document.createTextNode(this.lockToken)); 251 locktoken.appendChild(href); 252 activelock.appendChild(locktoken); 253 } 254 255 if (getPrincipal() != null) { 256 Element principal = (Element) document.createElementNS("DAV:", 257 "D:principal"); 258 principal.appendChild(document.createTextNode(getPrincipal())); 259 activelock.appendChild(principal); 260 } 261 262 268 274 return activelock; 275 } 276 277 287 public String getDepth() { 288 return depth; 289 } 290 291 297 public Date getExpiration() { 298 return expiration; 299 } 300 301 311 public String getLockToken() { 312 return lockToken; 313 } 314 315 320 public String getLockType() { 321 return lockType; 322 } 323 324 332 public Element getOwner() { 333 return owner; 334 } 335 336 343 public String getPrincipal() { 344 return principal; 345 } 346 347 352 public String getScope() { 353 return scope; 354 } 355 356 361 public String getTimeout() { 362 return timeout; 363 } 364 365 371 public long getTimeRemaining() { 372 long now = new Date().getTime(); 373 long t = expiration.getTime() - now; 374 if (t < 0) { 375 t = 0; 376 } 377 return t; 378 } 379 380 393 public void setDepth(String depth) throws ClientException { 394 if (!(depth.equals(Collection.shallow) || depth.equals(Collection.deep))) { 395 throw new ClientException(400, "invalid lock depth"); 396 } 397 this.depth = depth; 398 } 399 400 404 public void setExpiration(Date value) { 405 expiration = value; 406 } 407 408 414 public void setLockToken(String lockToken) { 415 this.lockToken = lockToken; 416 } 417 418 427 public void setLockType(String lockType) throws ClientException { 428 if (!(lockType.equals(writeLock))) { 429 throw new ClientException(400, "invalid lock type: " + lockType); 430 } 431 this.lockType = lockType; 432 } 433 434 444 public void setOwner(Element owner) { 445 this.owner = owner; 446 } 447 448 public void setOwner(String sOwner) { 449 this.sOwner = sOwner; 450 } 451 452 456 public void setPrincipal(String value) { 457 principal = value; 458 } 459 460 468 public void setScope(String scope) throws ClientException { 469 if (!(scope.equals(exclusive) || scope.equals(shared))) { 470 throw new ClientException(400, "invalid lock scope: " + scope); 471 } 472 this.scope = scope; 473 } 474 475 481 public void setTimeout(int timeout) { 482 if (timeout < 0) { 483 setTimeout("Infinite"); 484 } else { 485 setTimeout("Second-" + new Integer (timeout).toString()); 486 } 487 } 488 489 496 public void setTimeout(String timeout) { 497 this.timeout = timeout; 498 if (timeout.equals("Infinite")) { 499 expiration = null; 500 } else { 501 if (timeout.startsWith("Second-")) { 502 long t = new Long (timeout.substring(7)).longValue(); 503 expiration = new Date(new Date().getTime() + t * 1000); 504 } 505 } 506 } 507 508 516 public String toString() { 517 523 return XMLUtility.printNode(asXML()); 524 } 525 } | Popular Tags |