1 package com.ibm.webdav.impl; 2 3 17 import java.io.*; 18 import java.util.*; 19 20 import javax.xml.parsers.*; 21 22 import org.w3c.dom.*; 23 24 import com.ibm.webdav.*; 25 26 27 36 public abstract class CachedPropertiesManager implements PropertiesManager { 37 private static final int set = 0; 38 private static final int remove = 1; 39 protected ResourceImpl resource = null; 40 41 43 public CachedPropertiesManager() { 44 } 45 46 51 public CachedPropertiesManager(ResourceImpl resource, 52 com.ibm.webdav.impl.NamespaceManager namespaceManager) { 53 initialize(resource, namespaceManager); 54 } 55 56 60 public abstract void deleteProperties() throws WebDAVException; 61 62 77 public MultiStatus getProperties() 78 throws WebDAVException { 79 Document propertiesDocument = resource.loadProperties(); 80 81 MultiStatus results = new MultiStatus(); 83 84 PropertyResponse response = null; 86 String urlstring; 87 88 if (false) { 89 urlstring = resource.getURL().toString(); 94 } else { 95 urlstring = resource.getURL().getFile(); 102 } 103 104 response = new PropertyResponse(urlstring); 105 106 NodeList properties = propertiesDocument.getDocumentElement() 108 .getChildNodes(); 109 Node temp = null; 110 111 for (int i = 0; i < properties.getLength(); i++) { 112 temp = properties.item(i); 113 114 if (!(temp.getNodeType() == Node.ELEMENT_NODE)) { 116 continue; 117 } 118 119 Element property = (Element) temp; 120 PropertyName pn = new PropertyName(property); 121 response.addProperty(pn, property, WebDAVStatus.SC_OK); 122 } 123 124 results.addResponse(response); 125 126 return results; 127 } 128 129 135 public MultiStatus getProperties(PropertyName[] names) 136 throws WebDAVException { 137 MultiStatus multiStatus = resource.getProperties(resource.getContext()); 138 MultiStatus newMultiStatus = new MultiStatus(); 139 140 Enumeration responses = multiStatus.getResponses(); 141 142 while (responses.hasMoreElements()) { 143 PropertyResponse response = (PropertyResponse) responses.nextElement(); 144 PropertyResponse newResponse = new PropertyResponse( 145 response.getResource()); 146 newResponse.setDescription(response.getDescription()); 147 newMultiStatus.addResponse(newResponse); 148 149 Hashtable properties = (Hashtable) response.getPropertiesByPropName(); 150 151 for (int i = 0; i < names.length; i++) { 153 if (properties.containsKey(names[i])) { 154 PropertyValue srcval = response.getProperty(names[i]); 155 newResponse.setProperty(names[i], srcval); 156 157 } else { 159 Document factory = null; 160 161 try { 162 factory = DocumentBuilderFactory.newInstance() 163 .newDocumentBuilder() 164 .newDocument(); 165 } catch (Exception e) { 166 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 167 e.getMessage()); 168 } 169 170 Element elTm = factory.createElementNS("X", 183 "X:" + 184 names[i].getLocal()); 185 186 elTm.setAttribute("xmlns:X", names[i].getNamespace()); 187 188 newResponse.addProperty(names[i], elTm, 189 WebDAVStatus.SC_NOT_FOUND); 190 } 191 } 192 } 193 194 return newMultiStatus; 195 } 196 197 204 public MultiStatus getPropertyNames() throws WebDAVException { 205 MultiStatus multiStatus = resource.getProperties(resource.getContext()); 206 Enumeration responses = multiStatus.getResponses(); 207 208 while (responses.hasMoreElements()) { 212 PropertyResponse response = (PropertyResponse) responses.nextElement(); 213 Dictionary properties = response.getPropertiesByPropName(); 214 Enumeration keys = properties.keys(); 215 216 while (keys.hasMoreElements()) { 217 PropertyName key = (PropertyName) keys.nextElement(); 218 Element value = (Element) response.getProperty(key).getValue(); 219 response.setProperty(key, 220 new PropertyValue( 221 (Element) value.cloneNode(false), 222 WebDAVStatus.SC_OK)); 223 } 224 } 225 226 return multiStatus; 227 } 228 229 233 public void initialize(ResourceImpl resource, 234 com.ibm.webdav.impl.NamespaceManager namespaceManager) { 235 this.resource = resource; 236 } 237 238 242 public boolean isLive(String propertyName) { 243 return propertyName.equals("creationdate") || 244 propertyName.equals("getcontentlength") || 245 propertyName.equals("getlastmodified") || 246 propertyName.equals("lockdiscovery") || 247 propertyName.equals("getcontenttype") || 248 propertyName.equals("resourcetype") || 249 propertyName.equals("supportedlock") || 250 propertyName.equals("DAV:creationdate") || 251 propertyName.equals("DAV:getcontentlength") || 252 propertyName.equals("DAV:getlastmodified") || 253 propertyName.equals("DAV:lockdiscovery") || 254 propertyName.equals("DAV:getcontenttype") || 255 propertyName.equals("DAV:resourcetype") || 256 propertyName.equals("DAV:supportedlock"); 257 } 258 259 263 public abstract Document loadProperties() throws WebDAVException; 264 265 266 273 public abstract void removeLiveProperties(Document propertiesDocument); 274 275 279 public abstract void saveProperties(Document propertiesDocument) 280 throws WebDAVException; 281 282 290 public MultiStatus setProperties(Document propertyUpdates) 291 throws WebDAVException { 292 MultiStatus multiStatus = new MultiStatus(); 295 boolean errorsOccurred = false; 296 297 Document propertiesDocument = resource.loadProperties(); 299 Element properties = (Element) propertiesDocument.getDocumentElement(); 300 301 Element propertyupdate = (Element) propertyUpdates.getDocumentElement(); 303 String tagName = propertyupdate.getNamespaceURI() + 304 propertyupdate.getLocalName(); 305 306 if (!tagName.equals("DAV:propertyupdate")) { 307 throw new WebDAVException(WebDAVStatus.SC_UNPROCESSABLE_ENTITY, 308 "missing propertyupdate element"); 309 } 310 311 NodeList updates = propertyupdate.getChildNodes(); 312 313 if (updates.getLength() == 0) { 314 throw new WebDAVException(WebDAVStatus.SC_UNPROCESSABLE_ENTITY, 315 "no updates in request"); 316 } 317 318 Vector propsGood = new Vector(); 320 Node temp = null; 324 325 for (int i = 0; i < updates.getLength(); i++) { 326 temp = updates.item(i); 327 328 if (!(temp.getNodeType() == Node.ELEMENT_NODE)) { 330 continue; 331 } 332 333 Element update = (Element) temp; 334 int updateCommand = -1; 335 tagName = update.getNamespaceURI() + update.getLocalName(); 336 337 if (tagName.equals("DAV:set")) { 338 updateCommand = set; 339 } else if (tagName.equals("DAV:remove")) { 340 updateCommand = remove; 341 } else { 342 throw new WebDAVException(WebDAVStatus.SC_UNPROCESSABLE_ENTITY, 343 update.getTagName() + 344 " is not a valid property update request"); 345 } 346 347 Element prop = (Element) update.getElementsByTagNameNS("DAV:", 350 "prop") 351 .item(0); 352 353 if (prop == null) { 354 throw new WebDAVException(WebDAVStatus.SC_UNPROCESSABLE_ENTITY, 355 "no propeprties in update request"); 356 } 357 358 NodeList propsToUpdate = prop.getChildNodes(); 359 360 for (int j = 0; j < propsToUpdate.getLength(); j++) { 361 temp = propsToUpdate.item(j); 362 363 if (!(temp.getNodeType() == Node.ELEMENT_NODE)) { 365 continue; 366 } 367 368 Element propToUpdate = (Element) temp; 369 370 Element property = null; 372 PropertyName propertyName = new PropertyName(propToUpdate); 373 374 if (((Element) propToUpdate).getNamespaceURI() != null) { 375 property = (Element) properties.getElementsByTagNameNS( 376 propToUpdate.getNamespaceURI(), 377 propToUpdate.getLocalName()) 378 .item(0); 379 } else { 380 property = (Element) properties.getElementsByTagName( 381 propToUpdate.getTagName()) 382 .item(0); 383 } 384 385 boolean liveone = isLive(propertyName.asExpandedString()); 386 387 if (liveone) { 388 errorsOccurred = true; 389 390 PropertyResponse response = new PropertyResponse( 391 resource.getURL() 392 .toString()); 393 response.addProperty(propertyName, propToUpdate, 394 WebDAVStatus.SC_FORBIDDEN); 395 multiStatus.addResponse(response); 396 } 397 398 if (updateCommand == set) { 400 if (property != null) { 401 try { 402 properties.removeChild(property); 403 } catch (DOMException exc) { 404 } 405 } 406 407 if (!liveone) { 408 properties.appendChild(propertiesDocument.importNode( 417 propToUpdate, true)); 418 419 propsGood.addElement(propToUpdate); 420 } 421 } else if (updateCommand == remove) { 422 try { 423 if (property != null) { 424 properties.removeChild(property); 425 propsGood.addElement(propToUpdate); 426 } 427 } catch (DOMException exc) { 428 } 429 } 430 } 431 } 432 433 { 434 Enumeration els = propsGood.elements(); 435 436 for (; els.hasMoreElements();) { 437 Object ob1 = els.nextElement(); 438 Element elProp = (Element) ob1; 439 PropertyName pn = new PropertyName(elProp); 440 PropertyResponse response = new PropertyResponse( 441 resource.getURL() 442 .toString()); 443 response.addProperty(pn, (Element) elProp.cloneNode(false), 444 (errorsOccurred 445 ? WebDAVStatus.SC_FAILED_DEPENDENCY 446 : WebDAVStatus.SC_OK)); 447 448 449 multiStatus.addResponse(response); 451 } 452 } 453 454 if (!errorsOccurred) { 456 resource.saveProperties(propertiesDocument); 457 } 458 459 return multiStatus; 460 } 461 462 468 public void setProperty(String name, Element value) 469 throws WebDAVException { 470 Document propertiesDocument = resource.loadProperties(); 472 Element properties = propertiesDocument.getDocumentElement(); 473 String ns = value.getNamespaceURI(); 474 475 Element property = null; 476 if(ns == null) { 477 property = (Element) ((Element) properties).getElementsByTagName( 478 value.getTagName()).item(0); 479 } else { 480 property = (Element)properties.getElementsByTagNameNS(ns, value.getLocalName()).item(0); 481 } 482 483 484 if (property != null) { 485 try { 486 properties.removeChild(property); 487 } catch (DOMException exc) { 488 } 489 } 490 491 properties.appendChild(propertiesDocument.importNode(value, true)); 492 493 494 resource.saveProperties(propertiesDocument); 496 } 497 498 503 public abstract void updateLiveProperties(Document document) 504 throws WebDAVException; 505 } | Popular Tags |