1 19 20 package org.openharmonise.dav.server.managers; 21 22 23 import java.io.*; 24 import java.net.*; 25 import java.text.*; 26 import java.util.*; 27 import java.util.logging.*; 28 29 import javax.xml.parsers.*; 30 31 import org.openharmonise.commons.dsi.*; 32 import org.openharmonise.commons.net.MimeTypeMapping; 33 import org.openharmonise.commons.xml.XMLUtils; 34 import org.openharmonise.commons.xml.namespace.NamespaceType; 35 import org.openharmonise.dav.server.adapters.WebPageResourceAdapter; 36 import org.openharmonise.dav.server.apm.*; 37 import org.openharmonise.dav.server.property.domains.DAVDomain; 38 import org.openharmonise.dav.server.property.ranges.*; 39 import org.openharmonise.dav.server.utils.*; 40 import org.openharmonise.rm.*; 41 import org.openharmonise.rm.config.ConfigSettings; 42 import org.openharmonise.rm.dsi.DataStoreInterfaceFactory; 43 import org.openharmonise.rm.factory.HarmoniseFactoryException; 44 import org.openharmonise.rm.metadata.*; 45 import org.openharmonise.rm.resources.*; 46 import org.openharmonise.rm.resources.content.*; 47 import org.openharmonise.rm.resources.lifecycle.Status; 48 import org.openharmonise.rm.resources.metadata.properties.*; 49 import org.openharmonise.rm.resources.metadata.properties.domains.Domain; 50 import org.openharmonise.rm.resources.metadata.properties.ranges.*; 51 import org.openharmonise.rm.resources.metadata.values.Value; 52 import org.openharmonise.rm.resources.publishing.WebPage; 53 import org.openharmonise.rm.resources.users.User; 54 import org.openharmonise.rm.resources.workflow.values.WorkflowStageValue; 55 import org.openharmonise.rm.resources.xml.*; 56 import org.openharmonise.rm.workflow.WorkflowPropertyInstance; 57 import org.w3c.dom.*; 58 59 import com.ibm.webdav.*; 60 import com.ibm.webdav.impl.*; 61 62 71 public class HarmonisePropertiesManager 72 extends CachedPropertiesManager 73 implements com.ibm.webdav.impl.PropertiesManager { 74 75 public static final String TAG_LOCKDISCOVERY = "lockdiscovery"; 76 public static final String TAG_ARCHIVEDATE = "archivedate"; 77 public static final String TAG_HARMONISE_ID = "harmonise-id"; 78 public static final String TAG_PARENT = "parent"; 79 public static final String TAG_DESCRIPTION = "description"; 80 public static final String TAG_TITLE = "title"; 81 public static final String TAG_PARENT_SET = "parent-set"; 82 public static final String TAG_PROPERTY_RESOURCE = "property-resource"; 83 public static final String TAG_RESOURCETYPE = "resourcetype"; 84 public static final String TAG_RESOURCE = "resource"; 85 public static final String TAG_COLLECTION = "collection"; 86 private static String W3C_XMLSCHEMA_URI = 87 "http://www.w3.org/2001/XMLSchema"; 88 public static String propertiesSuffix = ".wdp"; public static final String ATTRIB_TYPE = "type"; 90 public static final String ATTRIB_ITEM_TYPE = "itemType"; 91 public static final String ARRAY_TYPE = "Array"; 92 public static final String ATTRIB_ARRAY_SIZE = "arraySize"; 93 94 private static final int bufferSize = 8192; 95 private static final String TAG_PENDING = NamespaceType.OHRM.getPrefix() + ":pending"; 96 private static final String TAG_RELATED = NamespaceType.OHRM.getPrefix() + ":related"; 97 private static final String TAG_IMAGE = NamespaceType.OHRM.getPrefix() + ":image"; 98 99 private static final String TAG_STRING = "xs:string"; 100 public static final String TAG_HREF = "href"; 101 private static final String ATTRIB_DEFINITION = "definition"; 102 private static final String ATTRIB_DEFINITION_VERSION = 103 "definition-version"; 104 private static final String ATTRIB_ARRAYTYPE = "arrayType"; 105 private static final String TAG_PROP = "prop"; 106 107 private static AuxillaryProcessManager m_auxillary = null; 108 private boolean m_bIsArchived = false; 109 110 protected AbstractDataStoreInterface m_dsi = null; 111 protected Profile resourceProfile = null; 112 protected org.w3c.dom.Document cachedPropertiesDocument = null; 113 protected long cacheTimeStamp = 0; 114 protected AbstractChildObject m_child = null; 115 116 119 private final static Logger m_logger = Logger.getLogger(HarmonisePropertiesManager.class.getName()); 120 121 public HarmonisePropertiesManager() { 122 } 123 124 public HarmonisePropertiesManager( 126 ResourceImpl resource, 127 com.ibm.webdav.impl.NamespaceManager namespaceManager) { 128 super(resource, namespaceManager); 129 } 130 131 134 public void initialize( 135 ResourceImpl resource, 136 com.ibm.webdav.impl.NamespaceManager namespaceManager) { 137 super.initialize(resource, namespaceManager); 138 139 try { 140 141 m_dsi = DataStoreInterfaceFactory.getDataStoreInterface(); 142 143 populateChild(); 144 145 if (m_auxillary == null) { 146 m_auxillary = APMFactory.getAPM(); 147 } 148 } catch (DataStoreException e) { 149 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 150 } catch (WebDAVException e) { 151 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 152 } catch (APMException e) { 153 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 154 } 155 156 if (m_dsi == null) { 157 throw new RuntimeException ("Datastore interface not started properly."); 158 } 159 } 160 161 164 public void deleteProperties() throws WebDAVException { 165 166 171 172 173 cachedPropertiesDocument = null; 174 } 175 176 179 public org.w3c.dom.Document loadProperties() throws WebDAVException { 180 if (cachedPropertiesDocument != null) { 181 return cachedPropertiesDocument; 182 } 183 184 try { 185 186 populateChild(); 187 188 cachedPropertiesDocument = 189 DocumentBuilderFactory 190 .newInstance() 191 .newDocumentBuilder() 192 .newDocument(); 193 194 Element properties = 195 (Element) cachedPropertiesDocument.createElement("properties"); 196 properties.setAttribute("xmlns:D", NamespaceType.DAV.getURI()); 197 cachedPropertiesDocument.appendChild(properties); 198 199 if (m_child != null) { 200 201 Element commentEl = 203 cachedPropertiesDocument.createElementNS( 204 NamespaceType.DAV.getURI(), 205 VersionedPropertiesManager.TAG_COMMENT); 206 commentEl.setPrefix(NamespaceType.DAV.getPrefix()); 207 if (m_child.isPendingVersion() == false) { 208 commentEl.appendChild( 209 cachedPropertiesDocument.createTextNode( 210 m_child.getVersionComment())); 211 } 212 properties.appendChild(commentEl); 213 214 List doneProps = new ArrayList(); 215 List availProps = Domain.getAvailableProperties(m_dsi,m_child); 216 217 Iterator profIter = m_child.getProfiles().iterator(); 219 220 while(profIter.hasNext()) { 221 Profile tmpProf = (Profile) profIter.next(); 222 223 List propInsts = tmpProf.getPropertyInstances(); 224 225 Iterator propIter = propInsts.iterator(); 226 227 while (propIter.hasNext()) { 228 AbstractPropertyInstance propInst = (AbstractPropertyInstance) propIter.next(); 229 doneProps.add(propInst.getName()); 230 NamespaceType ns = HarmoniseNameResolver.getPropertyNamespace(propInst.getProperty()); 231 properties.appendChild(getPropertyInstanceElement( 232 cachedPropertiesDocument, 233 propInst,ns)); 234 } 235 } 236 237 Iterator propIter = availProps.iterator(); 239 while (propIter.hasNext()) { 240 Property tmpProp = (Property) propIter.next(); 241 String sPropName = tmpProp.getName(); 242 if (doneProps.contains(sPropName) == false) { 243 if (m_logger.isLoggable(Level.FINER)) { 244 m_logger.logp(Level.FINER, this.getClass().getName(), "loadProperties", "Adding empty property - " + sPropName); 245 } 246 247 NamespaceType ns = HarmoniseNameResolver.getPropertyNamespace(tmpProp); 248 249 Element propEl = 250 this.getPropertyEmptyElement( 251 cachedPropertiesDocument, 252 tmpProp,ns); 253 254 if(propEl.getLocalName().equals(TAG_TITLE)) { 256 propEl.appendChild(cachedPropertiesDocument 257 .createTextNode(m_child.getDisplayName())); 258 } else if(propEl.getLocalName().equals(TAG_DESCRIPTION)) { 259 propEl.appendChild(cachedPropertiesDocument 260 .createTextNode(m_child.getSummary())); 261 } 262 263 properties.appendChild(propEl); 264 } 265 } 266 267 if (m_child.isLocked() == true) { 268 if (m_logger.isLoggable(Level.FINER)) { 269 m_logger.logp(Level.FINER, this.getClass().getName(), "loadProperties", m_child.getName() + " is locked"); 270 } 271 272 Element lockdiscovery = 273 cachedPropertiesDocument.createElementNS( 274 NamespaceType.DAV.getURI(), 275 "D:lockdiscovery"); 276 277 addLockData(lockdiscovery); 278 properties.appendChild(lockdiscovery); 279 } else { 280 if (m_logger.isLoggable(Level.FINER)) { 281 m_logger.logp(Level.FINER, this.getClass().getName(), "loadProperties", 282 m_child.getName() + " is not locked"); 283 } 284 } 285 286 if (m_child instanceof Property) { 288 Property prop = (Property) m_child; 289 290 properties.appendChild(getRangeElement(cachedPropertiesDocument, prop)); 291 292 DAVDomain davDomain = 293 new DAVDomain(m_dsi, prop.getDomains()); 294 295 properties.appendChild( 296 davDomain.asXML(cachedPropertiesDocument)); 297 } 298 299 } else if(HarmoniseNameResolver.isVirtualResource(resource.getURL())) { 300 try { 301 String sResourcePath = 302 URLDecoder.decode( 303 resource.getURL().getPath(), 304 HarmoniseNameResolver.UTF_8); 305 String sPath = HarmoniseNameResolver.getRealPath(resource.getURL()); 306 307 String sChildName = HarmoniseNameResolver.getChildTypeFromURL(sResourcePath); 308 309 List props = Domain.getAvailableProperties(m_dsi,Class.forName(sChildName),sPath); 310 311 Iterator iter = props.iterator(); 312 313 while (iter.hasNext()) { 314 Property prop = (Property) iter.next(); 315 316 NamespaceType ns = HarmoniseNameResolver.getPropertyNamespace(prop); 317 318 Element propEl = 319 this.getPropertyEmptyElement( 320 cachedPropertiesDocument, 321 prop,ns); 322 323 properties.appendChild(propEl); 324 } 325 326 } catch (UnsupportedEncodingException e) { 327 throw new WebDAVException( 328 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 329 e.getLocalizedMessage()); 330 } catch (ClassNotFoundException e) { 331 throw new WebDAVException( 332 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 333 e.getLocalizedMessage()); 334 } 335 } else if(HarmoniseNameResolver.isVirtualCollection(resource.getURL())) { 336 try { 337 String sResourcePath = 338 URLDecoder.decode( 339 resource.getURL().getPath(), 340 HarmoniseNameResolver.UTF_8); 341 String sPath = HarmoniseNameResolver.getRealPath(resource.getURL()); 342 343 boolean bIsPropCol = HarmoniseNameResolver.getLastSegment(sPath) 344 .equals(HarmoniseNameResolver.VIRTUAL_COLLECTION_NAME); 345 346 if(bIsPropCol == true) { 347 348 String sChildName = HarmoniseNameResolver.getParentTypeFromURL(sResourcePath); 349 350 List props = Domain.getAvailableProperties(m_dsi,Class.forName(sChildName),sPath); 351 352 Iterator iter = props.iterator(); 353 354 while (iter.hasNext()) { 355 Property prop = (Property) iter.next(); 356 357 NamespaceType ns = HarmoniseNameResolver.getPropertyNamespace(prop); 358 359 Element propEl = 360 this.getPropertyEmptyElement( 361 cachedPropertiesDocument, 362 prop,ns); 363 364 properties.appendChild(propEl); 365 } 366 367 } 368 369 } catch (UnsupportedEncodingException e) { 370 throw new WebDAVException( 371 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 372 e.getLocalizedMessage()); 373 } catch (ClassNotFoundException e) { 374 throw new WebDAVException( 375 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 376 e.getLocalizedMessage()); 377 } 378 } 379 380 if (m_logger.isLoggable(Level.FINEST)) { 381 m_logger.logp(Level.FINEST, this.getClass().getName(), "loadProperties", "property results:"); 382 m_logger.log(Level.FINEST, 383 XMLUtility.printNode( 384 cachedPropertiesDocument.getDocumentElement())); 385 386 } 387 } catch (HarmoniseException exc) { 388 m_logger.log(Level.WARNING, exc.getLocalizedMessage(), exc); 389 throw new WebDAVException( 390 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 391 exc.getLocalizedMessage()); 392 } catch (ParserConfigurationException exc) { 393 throw new WebDAVException( 394 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 395 exc.getLocalizedMessage()); 396 } catch (FactoryConfigurationError exc) { 397 throw new WebDAVException( 398 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 399 exc.getLocalizedMessage()); 400 } catch (NameResolverException exc) { 401 m_logger.log(Level.WARNING, exc.getLocalizedMessage(), exc); 402 throw new WebDAVException( 403 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 404 exc.getLocalizedMessage()); 405 } 406 407 return cachedPropertiesDocument; 408 } 409 410 protected Element getRangeElement(org.w3c.dom.Document xmlDoc, Property prop) 411 throws DataAccessException, WebDAVException { 412 Element rangeEl = null; 413 414 Range range = prop.getRange(); 415 416 if (range != null) { 417 DAVRange davRange = 418 DAVRangeFactory.getDAVRange(m_dsi, range); 419 rangeEl = davRange.asXML(xmlDoc); 420 } else { 421 rangeEl = xmlDoc.createElementNS( 422 NamespaceType.DAV.getURI(), 423 NamespaceType.DAV.getPrefix() 424 + ":" 425 + DAVRange.TAG_RANGE); 426 } 427 428 return rangeEl; 429 } 430 431 protected void addLockData(Element lockdiscovery) 432 throws NameResolverException, DataAccessException, WebDAVException { 433 org.w3c.dom.Document xmlDoc = lockdiscovery.getOwnerDocument(); 434 435 String sOwner = HarmoniseNameResolver.getDAVPath(m_child.getLockOwner()); 436 437 Element owner = 438 xmlDoc.createElementNS( 439 NamespaceType.DAV.getURI(), 440 "owner"); 441 owner.setPrefix(NamespaceType.DAV.getPrefix()); 442 443 Element href = 444 xmlDoc.createElementNS( 445 NamespaceType.DAV.getURI(), 446 TAG_HREF); 447 href.setPrefix(NamespaceType.DAV.getPrefix()); 448 href.appendChild(xmlDoc.createTextNode(sOwner)); 449 owner.appendChild(href); 450 451 ActiveLock alock = 452 this.getActiveLockFor( 453 m_child.getLockOwner().getName(), 454 ActiveLock.exclusive, 455 ActiveLock.writeLock, 456 -1, 457 owner); 458 459 lockdiscovery.appendChild(xmlDoc.importNode( alock.asXML(), true)); 460 } 461 462 465 public void removeLiveProperties(org.w3c.dom.Document propertiesDocument) { 466 Element properties = propertiesDocument.getDocumentElement(); 467 Element p = null; 468 p = 469 (Element) ((Element) properties) 470 .getElementsByTagNameNS( 471 NamespaceType.DAV.getURI(), 472 "getcontentlength") 473 .item(0); 474 475 if (p != null) { 476 properties.removeChild(p); 477 } 478 479 p = XMLUtils.getFirstNamedChild(properties, "resourcetype"); 480 481 if (p != null) { 482 properties.removeChild(p); 483 } 484 485 p = 486 (Element) ((Element) properties) 487 .getElementsByTagNameNS( 488 NamespaceType.DAV.getURI(), 489 "getlastmodified") 490 .item(0); 491 492 if (p != null) { 493 properties.removeChild(p); 494 } 495 496 p = 497 (Element) ((Element) properties) 498 .getElementsByTagNameNS( 499 NamespaceType.OHRM.getURI(), 500 TAG_ARCHIVEDATE) 501 .item(0); 502 503 if (p != null) { 504 properties.removeChild(p); 505 } 506 507 p = 508 (Element) ((Element) properties) 509 .getElementsByTagNameNS( 510 NamespaceType.DAV.getURI(), 511 "getcontenttype") 512 .item(0); 513 514 if (p != null) { 515 properties.removeChild(p); 516 } 517 518 p = 519 (Element) ((Element) properties) 520 .getElementsByTagNameNS( 521 NamespaceType.DAV.getURI(), 522 "resourceid") 523 .item(0); 524 525 if (p != null) { 526 properties.removeChild(p); 527 } 528 529 p = 530 (Element) ((Element) properties) 531 .getElementsByTagNameNS( 532 NamespaceType.DAV.getURI(), 533 "displayname") 534 .item(0); 535 536 if (p != null) { 537 properties.removeChild(p); 538 } 539 540 p = (Element) ((Element) properties) 541 .getElementsByTagNameNS( 542 NamespaceType.OHRM.getURI(), 543 TAG_HARMONISE_ID) 544 .item(0); 545 546 if (p != null) { 547 properties.removeChild(p); 548 } 549 550 p = 551 (Element) ((Element) properties) 552 .getElementsByTagNameNS( 553 NamespaceType.DAV.getURI(), 554 TAG_PARENT_SET) 555 .item(0); 556 557 if (p != null) { 558 properties.removeChild(p); 559 } 560 } 561 562 565 public void saveProperties(org.w3c.dom.Document propertiesDocument) 566 throws WebDAVException { 567 try { 568 569 populateChild(); 570 571 boolean bSave = false; 572 573 Element proproot = propertiesDocument.getDocumentElement(); 574 575 boolean bIsLockChanged = false; 577 User usr = ((HarmoniseSessionManager) resource.getUserAuthenticator()) 578 .getUser(resource); 579 if(resource.getContext().getMethodName().equalsIgnoreCase("LOCK") == true 580 || resource.getContext().getMethodName().equalsIgnoreCase("UNLOCK") == true) { 581 Element lockdiscovery = 582 (Element) proproot 583 .getElementsByTagNameNS( 584 NamespaceType.DAV.getURI(), 585 TAG_LOCKDISCOVERY) 586 .item(0); 587 588 589 590 if (m_logger.isLoggable(Level.FINER)) { 591 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "Got user - " + usr.getName()); 592 } 593 594 if (lockdiscovery != null) { 595 NodeList locks = 596 ((Element) lockdiscovery).getElementsByTagNameNS( 597 NamespaceType.DAV.getURI(), 598 "activelock"); 599 Element lock = null; 600 Date now = new Date(); 601 602 if (locks.getLength() > 0) { 603 for (int i = 0; i < locks.getLength(); i++) { 604 lock = (Element) locks.item(i); 605 606 607 608 if ((m_child.isHistorical() == false)) { 609 if (m_child.isLocked() == false) { 610 bIsLockChanged = true; 611 612 CommandWrapper.lock(m_dsi, m_child, usr); 613 614 } 615 } 616 } 617 } else { 618 619 if ((m_child.isLocked() == true) 620 && (m_child.isHistorical() == false)) { 621 bIsLockChanged = true; 622 623 CommandWrapper.unlock(m_dsi, m_child, usr); 624 } 625 } 626 } else { 627 if ((m_child.isLocked() == true) 628 && (m_child.isHistorical() == false)) { 629 bIsLockChanged = true; 630 631 CommandWrapper.unlock(m_dsi, m_child, usr); 632 } 633 } 634 } 635 636 637 639 if (bIsLockChanged == false) { 640 if(CommandWrapper.isSaveAllowed(usr,m_child) == false) { 641 throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN,"User is not allowed to save resource"); 642 } 643 644 Profile prof = m_child.getProfile(); 645 646 if ((m_child.isLiveVersion() == true) 648 && (resource.getContext().getMethodName().equals("PROPPATCH") 649 == true)) { 650 prof.removeProperty(PropertyFactory.getPropertyFromName(m_dsi, HarmoniseNameResolver.START_PROP_NAME)); 651 } 652 653 NodeList propNodes = proproot.getChildNodes(); 654 int nId = -1; 655 boolean bHasPropChanged = false; 656 List invalidProps = new ArrayList(); 657 658 for (int i = 0; i < propNodes.getLength(); i++) { 659 Element propEl = (Element) propNodes.item(i); 660 Profile tmpProf = null; 661 662 if (m_logger.isLoggable(Level.FINER)) { 663 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "Handling prop - " + propEl.getTagName()); 664 } 665 666 NamedNodeMap attribs = propEl.getAttributes(); 667 668 tmpProf = HarmoniseNameResolver.getProfileForNamespaceURI(m_child, propEl.getNamespaceURI()); 669 670 if (tmpProf != null) { 672 bSave = true; 673 674 String propName = propEl.getTagName(); 675 676 if (m_logger.isLoggable(Level.FINER)) { 677 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "trying to save " + propName + " property"); 678 } 679 680 int nIndex = propName.indexOf(":"); 681 682 String localPropName = propName; 683 684 if ((propName.startsWith("DC") == false) 685 && (propName.startsWith("dc") == false)) { 686 localPropName = 687 propName.substring( 688 nIndex + 1, 689 propName.length()); 690 } 691 692 if (localPropName.equalsIgnoreCase("id") == true) { 694 nId = 695 Integer.parseInt( 696 propEl 697 .getChildNodes() 698 .item(0) 699 .getNodeValue()); 700 701 if (m_logger.isLoggable(Level.FINER)) { 702 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "Found id prop " + nId); 703 } 704 } else if(localPropName.equalsIgnoreCase(TAG_DESCRIPTION)) { 705 if(propEl.getChildNodes().getLength() > 0) { 706 m_child.setSummary(propEl 708 .getChildNodes() 709 .item(0) 710 .getNodeValue()); 711 } else { 712 m_child.setSummary(""); 713 } 714 715 } else if(localPropName.equalsIgnoreCase(TAG_TITLE)) { 716 if(propEl.getChildNodes().getLength() > 0) { 717 m_child.setDisplayName(propEl 719 .getChildNodes() 720 .item(0) 721 .getNodeValue()); 722 } else { 723 m_child.setDisplayName(""); 724 } 725 } else { 726 if (m_logger.isLoggable(Level.FINER)) { 727 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "trying to save " 728 + localPropName 729 + " property"); 730 } 731 732 if (tmpProf != null) { 733 if (m_logger.isLoggable(Level.FINER)) { 734 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "trying to save " + localPropName); 735 } 736 737 try { 738 boolean bNewPropInst = false; 739 AbstractPropertyInstance propInst = 740 tmpProf.getPropertyInstance(localPropName); 741 Property prop = null; 742 if (propInst == null) { 745 prop = 746 PropertyFactory 747 .getPropertyFromName( 748 this.m_dsi, 749 localPropName); 750 propInst = 751 PropertyInstanceFactory 752 .getPropertyInstance( 753 this.m_dsi, 754 prop); 755 propInst.setProfile(tmpProf); 756 757 bNewPropInst = true; 758 } else { 759 prop = propInst.getProperty(); 760 } 761 762 if (propInst != null) { 763 bHasPropChanged = 764 populatePropertyInstance( 765 propInst, 766 propEl); 767 768 if(bHasPropChanged == true && bNewPropInst == true) { 769 tmpProf.addPropertyInstance(propInst); 770 771 772 if (m_logger.isLoggable(Level.FINER)) { 773 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "added new PropertyInsatnce:" 774 + localPropName); 775 } 776 } 777 778 if (propInst.hasValues() == false && bNewPropInst == false) { 779 tmpProf.removeProperty( 780 propInst.getProperty()); 781 782 if (m_logger.isLoggable(Level.FINER)) { 783 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "removing propertyinstance " 784 + prop.getName()); 785 } 786 787 bHasPropChanged = true; 788 } 789 790 } 791 792 793 } catch(InvalidPropertyInstanceException e) { 794 if (m_logger.isLoggable(Level.INFO)) { 795 m_logger.log(Level.INFO, e.getLocalizedMessage(), e); 796 } 797 798 invalidProps.add(e); 799 800 801 } catch(InvalidPropertyValueException e) { 802 if (m_logger.isLoggable(Level.INFO)) { 803 m_logger.log(Level.INFO, e.getLocalizedMessage(), e); 804 } 805 806 invalidProps.add(e); 807 } catch (HarmoniseException e) { 808 if (m_logger.isLoggable(Level.INFO)) { 809 m_logger.log(Level.INFO, e.getLocalizedMessage(), e); 810 } 811 throw new WebDAVException( 812 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 813 e.getLocalizedMessage()); 814 } catch (NameResolverException e) { 815 throw new WebDAVException( 816 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 817 e.getLocalizedMessage()); 818 } 819 } 820 } 821 } else { 822 NodeList nodes = propEl.getChildNodes(); 823 824 String sTagName = propEl.getLocalName(); 825 826 if (sTagName 827 .equals( 828 VersionedPropertiesManager.TAG_COMMENT)) { 829 if(nodes.getLength() > 0) { 830 831 String sComment = nodes.item(0).getNodeValue(); 832 m_child.setVersionComment(sComment); 833 if (m_child.isChanged() == true) { 834 bSave = true; 835 } 836 } 837 } else if (m_child instanceof Property) { 838 Property childProp = (Property) m_child; 839 if (sTagName.equals(DAVDomain.TAG_DOMAIN)) { 840 DAVDomain davDomain = new DAVDomain(m_dsi); 841 842 davDomain.populate(propEl); 843 844 childProp.setDomains( 845 davDomain.getHarmoniseDomains()); 846 if (m_child.isChanged() == true) { 847 bSave = true; 848 } 849 850 } else if ( 851 sTagName.equals(DAVRange.TAG_RANGE)) { 852 DAVRange davRange = 853 DAVRangeFactory.getDAVRange( 854 m_dsi,childProp,propEl); 855 856 if (davRange != null) { 857 858 Range range = davRange.getHarmoniseRange(); 859 860 if (range != null) { 861 childProp.setRange(range); 862 if (m_child.isChanged() == true) { 863 bSave = true; 864 } 865 } 866 } 867 } 868 } 869 } 870 } 871 872 873 874 String sMethodName = resource.getContext().getMethodName(); 875 876 if ((bSave == true) 877 && (prof != null || m_child instanceof Property)) { 878 try { 879 880 if (m_logger.isLoggable(Level.FINER)) { 881 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "resource context method - " + sMethodName); 882 } 883 884 if (m_logger.isLoggable(Level.FINER)) { 885 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "before save:" + m_child); 886 } 887 888 m_child = CommandWrapper.save(m_dsi, m_child, usr); 889 890 if (m_logger.isLoggable(Level.FINER)) { 891 m_logger.logp(Level.FINER, this.getClass().getName(), "saveProperties", "after save:" + m_child); 892 } 893 894 895 896 if (m_auxillary != null) { 897 m_auxillary.saveProperties(usr, m_child, prof); 898 } 899 } catch (APMException e) { 900 throw new WebDAVException( 901 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 902 e.getMessage()); 903 } 904 } 905 906 if(invalidProps.size() > 0) { 907 StringBuffer sbuf = new StringBuffer (); 908 909 for (Iterator iter = invalidProps.iterator(); 910 iter.hasNext(); 911 ) { 912 Exception e = (Exception ) iter.next(); 913 sbuf.append("[").append(e.getMessage()).append("]\n"); 914 } 915 916 throw new WebDAVException(WebDAVStatus.SC_CONFLICT,sbuf.toString()); 917 } 918 919 if (m_child instanceof AbstractParentObject 920 && m_child.isPendingVersion() == true 921 && ConfigSettings.getBoolProperty(HarmoniseNameResolver 922 .PNAME_AUTO_APPROVE_COLLECTIONS, "true") == true) { 923 CommandWrapper.changeStatus( 925 m_dsi, 926 m_child, 927 usr, 928 Status.APPROVED); 929 } 930 } 931 } catch (HarmoniseException e) { 932 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 933 934 throw new WebDAVException( 935 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 936 e.getMessage()); 937 } 938 } 939 940 943 public void updateLiveProperties(org.w3c.dom.Document document) 944 throws WebDAVException { 945 populateChild(); 946 boolean bIsVirtCollection = false; 947 boolean bIsVirtualResource = false; 948 949 try { 950 if(m_child == null) { 951 URL url = resource.getURL(); 952 953 bIsVirtCollection = 954 HarmoniseNameResolver.isVirtualCollection(url) 955 || HarmoniseNameResolver.isArchivedVirtualCollection(url); 956 957 958 if(bIsVirtCollection == false) { 959 bIsVirtualResource = HarmoniseNameResolver.isVirtualResource(url); 960 } 961 } 962 963 if (m_child == null && bIsVirtCollection == false && bIsVirtualResource == false) { 964 965 throw new WebDAVException( 966 WebDAVStatus.SC_NOT_FOUND, 967 "Could not find resource at this location"); 968 } 969 970 Element properties = document.getDocumentElement(); 971 972 Element resourceType = 974 document.createElementNS( 975 NamespaceType.DAV.getURI(), 976 "D:resourcetype"); 977 978 if (m_child instanceof AbstractParentObject) { 979 resourceType.appendChild( 980 document.createElementNS( 981 NamespaceType.DAV.getURI(), 982 "D:collection")); 983 } else if (bIsVirtCollection == true) { 984 Element colEl = document.createElementNS( 985 NamespaceType.DAV.getURI(), 986 "D:collection"); 987 colEl.setAttribute(NamespaceType.OHRM.getPrefix() + ":isVirtual", "true"); 988 colEl.setAttribute("xmlns:" + NamespaceType.OHRM.getPrefix(), NamespaceType.OHRM.getURI()); 989 resourceType.appendChild(colEl); 990 } else if (m_child instanceof Property) { 991 resourceType.appendChild( 992 document.createElementNS( 993 NamespaceType.DAV.getURI(), 994 "D:property-resource")); 995 } else if (m_child instanceof Value) { 996 resourceType.appendChild( 997 document.createElementNS( 998 NamespaceType.DAV.getURI(), 999 "D:value")); 1000 } else { 1001 resourceType.appendChild( 1002 document.createElementNS( 1003 NamespaceType.DAV.getURI(), 1004 "D:resource")); 1005 } 1006 1007 properties.appendChild(resourceType); 1008 1009 Element lastModifiedDate = 1010 document.createElementNS( 1011 NamespaceType.DAV.getURI(), 1012 "D:getlastmodified"); 1013 1014 String cdstring = null; 1015 if (m_child != null) { 1016 Date versionDate = m_child.getVersionDate(); 1017 1018 cdstring = new SimpleRFC1123DateFormat().format(versionDate); 1019 lastModifiedDate.appendChild(document.createTextNode(cdstring)); 1020 } 1021 1022 properties.appendChild(lastModifiedDate); 1023 1024 Element creationDate = 1025 document.createElementNS( 1026 NamespaceType.DAV.getURI(), 1027 "D:creationdate"); 1028 1029 if (m_child != null) { 1030 cdstring = 1031 new SimpleISO8601DateFormat().format( 1032 m_child.getVersionDate()); 1033 creationDate.appendChild(document.createTextNode(cdstring)); 1034 1035 if(m_child.isHistorical() == true) { 1036 Element archDateEl = document.createElementNS(NamespaceType.OHRM.getURI(), NamespaceType.OHRM.getPrefix() + ":" + TAG_ARCHIVEDATE); 1037 archDateEl.setAttribute("xmlns:" + NamespaceType.OHRM.getPrefix(), NamespaceType.OHRM.getURI()); 1038 1039 Date archDate = m_child.getHistoryDate(); 1040 1041 String adstring = new SimpleRFC1123DateFormat().format(archDate); 1042 archDateEl.appendChild(document.createTextNode(adstring)); 1043 properties.appendChild(archDateEl); 1044 } 1045 } 1046 1047 1048 1049 properties.appendChild(creationDate); 1050 1051 Element displayname = 1052 document.createElementNS( 1053 NamespaceType.DAV.getURI(), 1054 "D:displayname"); 1055 1056 if (m_child != null) { 1057 displayname.appendChild(document.createTextNode(m_child.getDisplayName())); 1058 } 1059 1060 properties.appendChild(displayname); 1061 1062 Element getContentLength = 1063 document.createElementNS( 1064 NamespaceType.DAV.getURI(), 1065 "D:getcontentlength"); 1066 long length = 0; 1067 1068 if (m_child instanceof Asset) { 1069 Asset asset = (Asset) m_child; 1070 String sType = asset.getContentType(); 1071 1072 if ((m_child instanceof XSLResource) == false && sType.startsWith("link") || sType.startsWith("text")) { 1073 1074 String sURI = asset.getURI(); 1075 if(sURI != null) { 1076 length = sURI.getBytes().length; 1077 } 1078 } else { 1079 File file = asset.getContentFile(); 1080 length = file.length(); 1081 } 1082 } else if (m_child instanceof TextResource) { 1083 TextResource txt = (TextResource) m_child; 1084 String sContent = txt.getContent(); 1085 1086 if (sContent != null) { 1087 length = sContent.getBytes().length; 1088 } 1089 1090 } else if(m_child instanceof WebPage) { 1091 WebPageResourceAdapter page = new WebPageResourceAdapter((WebPage)m_child); 1092 String sContent = page.asString(); 1093 1094 if (sContent != null) { 1095 length = sContent.getBytes().length; 1096 } 1097 } 1098 1099 getContentLength.appendChild( 1100 document.createTextNode(String.valueOf(length))); 1101 1102 properties.appendChild(getContentLength); 1103 1104 Element contentType = 1105 document.createElementNS( 1106 NamespaceType.DAV.getURI(), 1107 "D:getcontenttype"); 1108 1109 String type = ""; 1110 1111 if (m_child instanceof AbstractParentObject 1112 || bIsVirtCollection == true) { 1113 type = "Folder"; 1114 } else if (m_child instanceof TextResource) { 1115 TextResource txt = (TextResource) m_child; 1116 if (txt instanceof XMLResource) { 1117 type = MimeTypeMapping.XML.getMimeType(); 1118 } else { 1119 type = txt.getContentType(); 1120 } 1121 } else if(m_child instanceof Asset) { 1122 type = ((Asset)m_child).getContentType(); 1123 } else if(m_child instanceof WebPage) { 1124 type = MimeTypeMapping.XML.getMimeType(); 1125 } 1126 1127 contentType.appendChild(document.createTextNode(type)); 1128 1129 properties.appendChild(contentType); 1130 1131 if (m_child != null) { 1132 Element resourceId = 1133 document.createElementNS( 1134 NamespaceType.DAV.getURI(), 1135 "D:resourceid"); 1136 1137 String sClassname = m_child.getClass().getName(); 1138 StringBuffer uniqueId = new StringBuffer (); 1139 uniqueId.append(sClassname.substring(sClassname.lastIndexOf(".") + 1)) 1140 .append("#") 1141 .append(m_child.getKey()); 1142 1143 resourceId.appendChild(document.createTextNode(uniqueId.toString())); 1144 1145 properties.appendChild(resourceId); 1146 1147 Element harmoniseId = document.createElementNS( 1148 NamespaceType.OHRM.getURI(), 1149 TAG_HARMONISE_ID); 1150 harmoniseId.setPrefix(NamespaceType.OHRM.getPrefix()); 1151 1152 harmoniseId.setAttribute("xmlns:" + NamespaceType.OHRM.getPrefix(), NamespaceType.OHRM.getURI()); 1153 1154 harmoniseId.appendChild(document.createTextNode(String.valueOf(m_child.getId()))); 1155 1156 properties.appendChild(harmoniseId); 1157 } 1158 1159 if(m_child != null) { 1161 List parents = m_child.getParents(); 1162 1163 if(parents.size() > 1) { 1164 Element parentSetEl = document.createElementNS( 1165 NamespaceType.DAV.getURI(), TAG_PARENT_SET); 1166 parentSetEl.setPrefix(NamespaceType.DAV.getPrefix()); 1167 1168 Iterator iter = parents.iterator(); 1169 1170 while (iter.hasNext()) { 1171 AbstractParentObject parent = (AbstractParentObject) iter.next(); 1172 1173 if(m_child.isRealParent(parent) == false) { 1174 Element parentEl = document.createElementNS( 1175 NamespaceType.DAV.getURI(), TAG_PARENT); 1176 parentEl.setPrefix(NamespaceType.DAV.getPrefix()); 1177 1178 String sHREF = HarmoniseNameResolver.getDAVPath(parent); 1179 1180 Element hrefEl = document.createElementNS( 1181 NamespaceType.DAV.getURI(), TAG_HREF); 1182 hrefEl.setPrefix(NamespaceType.DAV.getPrefix()); 1183 hrefEl.appendChild(document.createTextNode(sHREF)); 1184 parentEl.appendChild(hrefEl); 1185 1186 Element segmentEl = document.createElementNS( 1187 NamespaceType.DAV.getURI(), TAG_HREF); 1188 segmentEl.setPrefix(NamespaceType.DAV.getPrefix()); 1189 1190 String sName = URLEncoder.encode(m_child.getName(), HarmoniseNameResolver.UTF_8); 1191 segmentEl.appendChild(document.createTextNode(sName)); 1192 1193 parentEl.appendChild(segmentEl); 1194 1195 parentSetEl.appendChild(parentEl); 1196 } 1197 } 1198 1199 properties.appendChild(parentSetEl); 1200 } 1201 1202 1203 } 1204 1205 } catch (HarmoniseException e) { 1206 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 1207 throw new WebDAVException( 1208 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1209 e.getMessage()); 1210 } catch (NameResolverException e) { 1211 throw new WebDAVException( 1212 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1213 e.getMessage()); 1214 } catch (UnsupportedEncodingException e) { 1215 throw new WebDAVException( 1216 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1217 e.getMessage()); 1218 } 1219 } 1220 1221 1226 public Node copyNode(org.w3c.dom.Document xmldoc, Node originalEl) 1227 throws Exception { 1228 int i = 0; 1229 1230 Node returnNode = null; 1231 1232 if (originalEl.getNodeType() == Node.ELEMENT_NODE) { 1233 Element el = 1234 xmldoc.createElement(((Element) originalEl).getTagName()); 1235 NamedNodeMap attribs = originalEl.getAttributes(); 1236 1237 for (i = 0; i < attribs.getLength(); i++) { 1238 Attr nextAtt = (Attr) attribs.item(i); 1239 el.setAttribute(nextAtt.getNodeName(), nextAtt.getValue()); 1240 } 1241 1242 NodeList nodes = originalEl.getChildNodes(); 1243 1244 for (i = 0; i < nodes.getLength(); i++) { 1245 el.appendChild(copyNode(xmldoc, nodes.item(i))); 1246 } 1247 1248 returnNode = (Node) el; 1249 } else if (originalEl.getNodeType() == Node.TEXT_NODE) { 1250 Text el = xmldoc.createTextNode(originalEl.getNodeValue()); 1251 1252 returnNode = (Node) el; 1253 } 1254 1255 return returnNode; 1256 } 1257 1258 protected ActiveLock getActiveLockFor( 1259 String sOwner, 1260 String scope, 1261 String type, 1262 int timeout, 1263 Element owner) 1264 throws WebDAVException { 1265 if (sOwner == null) { 1266 throw new WebDAVException( 1267 WebDAVStatus.SC_UNAUTHORIZED, 1268 "missing authorization identification"); 1269 } 1270 1271 if ((scope == null) 1273 || (!scope.equals(ActiveLock.exclusive) 1274 && !scope.equals(ActiveLock.shared))) { 1275 throw new WebDAVException( 1276 WebDAVStatus.SC_BAD_REQUEST, 1277 "unsupported or missing lock scope: " + scope); 1278 } 1279 1280 if ((type == null) || type.equals(ActiveLock.writeLock) == false) { 1281 throw new WebDAVException( 1282 WebDAVStatus.SC_BAD_REQUEST, 1283 "unsupported or missing lock type: " + type); 1284 } 1285 1286 ActiveLock activeLock = new ActiveLock(); 1288 activeLock.setScope(scope); 1289 activeLock.setLockType(type); 1290 activeLock.setDepth(com.ibm.webdav.Collection.shallow); 1291 1292 if (owner != null) { 1293 activeLock.setOwner(owner); 1294 } 1295 1296 if (timeout < 0) { 1297 activeLock.setTimeout("Infinite"); 1298 } else { 1299 activeLock.setTimeout("Second-" + timeout); 1300 } 1301 1302 try { 1303 String lockToken = getLockToken(); 1304 1305 activeLock.setLockToken(lockToken); 1306 } catch (Exception e) { 1307 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 1308 throw new WebDAVException( 1309 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1310 "Trouble creating UUID"); 1311 } 1312 1313 activeLock.setPrincipal(sOwner); 1314 1315 return activeLock; 1316 } 1317 1318 1324 private String getPrefix(String sPropName) { 1325 String prefix = NamespaceType.OHRM.getPrefix(); 1326 int nIndex = sPropName.indexOf(":"); 1327 1328 if (nIndex > 0) { 1329 prefix = sPropName.substring(0, nIndex); 1330 } else if ( 1331 sPropName.equals( 1332 VersionedPropertiesManager.TAG_CREATOR_DISPLAYNAME)) { 1333 prefix = NamespaceType.DAV.getPrefix(); 1334 } 1335 1336 return prefix; 1337 } 1338 1339 1345 private String getNameSpace(String sPropName) { 1346 String ns = NamespaceType.OHRM.getURI(); 1347 int nIndex = sPropName.indexOf(":"); 1348 1349 if (nIndex > 0) { 1350 ns = sPropName.substring(0, nIndex); 1351 } else if ( 1352 sPropName.equals( 1353 VersionedPropertiesManager.TAG_CREATOR_DISPLAYNAME)) { 1354 ns = NamespaceType.DAV.getURI(); 1355 } 1356 1357 return ns; 1358 } 1359 1360 1366 private String getTagname(String sPropName) { 1367 String sTagname = sPropName; 1368 int nIndex = sPropName.indexOf(":"); 1369 1370 if (nIndex > 0) { 1371 sTagname = sPropName.substring(nIndex + 1); 1372 } 1373 1374 return sTagname; 1375 } 1376 1377 1386 private Element getPropertyInstanceElement( 1387 org.w3c.dom.Document xdoc, 1388 AbstractPropertyInstance propInst) throws DataAccessException, NameResolverException, WebDAVException { 1389 return getPropertyInstanceElement(xdoc,propInst, null); 1390 } 1391 1392 1401 private Element getPropertyInstanceElement( 1402 org.w3c.dom.Document xdoc, 1403 AbstractPropertyInstance propInst, NamespaceType nsType) 1404 throws DataAccessException, NameResolverException, WebDAVException { 1405 String sPropName = propInst.getName(); 1406 1407 Property prop = propInst.getProperty(); 1408 1409 Element propEl = getPropertyEmptyElement(xdoc, prop, nsType); 1410 1411 List propValues = propInst.getValues(); 1412 1413 if (propValues != null) { 1414 1415 if (propInst instanceof GeneralPropertyInstance) { 1416 if (propValues.size() == 1) { 1417 String sVal = null; 1418 Range range = prop.getRange(); 1419 1420 if(range instanceof DateRange) { 1422 Date dtVal = (Date)propValues.get(0); 1423 sVal = getDateString((DateRange) range, dtVal); 1424 } else { 1425 sVal = (String ) propValues.get(0); 1426 } 1427 1428 propEl.appendChild(cachedPropertiesDocument.createTextNode(sVal)); 1429 1430 } else if (propValues.size() > 1) { 1431 List davVals = propValues; 1432 1433 Range range = prop.getRange(); 1434 1435 if(range instanceof DateRange) { 1437 davVals = new ArrayList(); 1438 Iterator iter = propValues.iterator(); 1439 1440 while (iter.hasNext()) { 1441 Date dtVal = (Date) iter.next(); 1442 1443 davVals.add(getDateString((DateRange)range,dtVal)); 1444 } 1445 } 1446 1447 try { 1448 addValueArrayToProperty( 1449 xdoc, 1450 propEl, 1451 davVals, 1452 DataType.getDataType(prop.getRange().getObject())); 1453 } catch (ClassNotFoundException e) { 1454 throw new WebDAVException( 1455 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1456 e.getLocalizedMessage()); 1457 } 1458 1459 } 1460 } else if (propInst instanceof ChildObjectPropertyInstance) { 1461 1462 if (propValues.size() == 1) { 1463 Element hrefEl = xdoc.createElementNS(NamespaceType.DAV.getURI(), TAG_HREF); 1464 hrefEl.setPrefix(NamespaceType.DAV.getPrefix()); 1465 String davPath = 1466 HarmoniseNameResolver.getDAVPath( 1467 (AbstractChildObject) propValues.get(0)); 1468 hrefEl.appendChild(xdoc.createTextNode(davPath)); 1469 propEl.appendChild(hrefEl); 1470 } else if (propValues.size() > 1) { 1471 List davVals = new Vector(); 1472 Iterator iter = propValues.iterator(); 1473 1474 while (iter.hasNext()) { 1475 AbstractChildObject child = 1476 (AbstractChildObject) iter.next(); 1477 davVals.add(HarmoniseNameResolver.getDAVPath(child)); 1478 } 1479 1480 addValueArrayToProperty( 1481 xdoc, 1482 propEl, 1483 davVals, 1484 DataType.HREF); 1485 1486 } 1487 } else if (propInst instanceof ProfilePropertyInstance) { 1488 if (propValues.size() == 1) { 1489 Element propPropEl = xdoc.createElementNS(NamespaceType.DAV.getURI(), TAG_PROP); 1490 propPropEl.setPrefix(NamespaceType.DAV.getPrefix()); 1491 1492 Profile prof = (Profile) propValues.get(0); 1493 1494 List propInsts = prof.getPropertyInstances(); 1495 1496 Iterator iter = propInsts.iterator(); 1497 1498 while (iter.hasNext()) { 1499 AbstractPropertyInstance tmpPropInst = (AbstractPropertyInstance) iter.next(); 1500 Element subPropEl = getPropertyInstanceElement(xdoc,tmpPropInst); 1501 if(subPropEl != null) { 1502 propPropEl.appendChild(subPropEl); 1503 } 1504 } 1505 1506 propEl.appendChild(propPropEl); 1507 } else { 1508 1509 List davVals = new Vector(); 1510 Iterator iter = propValues.iterator(); 1511 1512 while (iter.hasNext()) { 1513 Profile prof = 1514 (Profile) iter.next(); 1515 1516 Element tmpPropEl = xdoc.createElementNS(NamespaceType.DAV.getURI(), TAG_PROP); 1517 tmpPropEl.setPrefix(NamespaceType.DAV.getPrefix()); 1518 1519 List propInsts = prof.getPropertyInstances(); 1520 1521 Iterator propInstIter = propInsts.iterator(); 1522 1523 while (propInstIter.hasNext()) { 1524 AbstractPropertyInstance tmpPropInst = (AbstractPropertyInstance) propInstIter.next(); 1525 Element subPropEl = getPropertyInstanceElement(xdoc,tmpPropInst); 1526 tmpPropEl.appendChild(subPropEl); 1527 1528 } 1529 davVals.add(tmpPropEl); 1530 } 1531 1532 addValueArrayToProperty(xdoc, propEl, davVals, DataType.PROP); 1533 } 1534 } 1535 1536 } 1537 1538 return propEl; 1539 } 1540 1541 1551 private Element getPropertyEmptyElement( 1552 org.w3c.dom.Document xdoc, 1553 Property prop, NamespaceType nsType) 1554 throws DataAccessException, DOMException, NameResolverException { 1555 String sPropName = prop.getName(); 1556 1557 String ns = null; 1558 String prefix = null; 1559 1560 if(nsType != null) { 1561 ns = nsType.getURI(); 1562 prefix = nsType.getPrefix(); 1563 } else { 1564 ns = this.getNameSpace(sPropName); 1565 prefix = this.getPrefix(sPropName); 1566 } 1567 1568 1569 Element propEl = 1570 xdoc.createElementNS(ns, prefix + ":" + this.getTagname(sPropName)); 1571 propEl.setAttribute("xmlns:" + prefix, ns); 1572 1573 propEl.setAttributeNS( 1574 NamespaceType.DAV.getURI(), 1575 NamespaceType.DAV.getPrefix() + ":" + ATTRIB_DEFINITION, 1576 HarmoniseNameResolver.getDAVPath(prop)); 1577 1578 propEl.setAttributeNS( 1579 NamespaceType.DAV.getURI(), 1580 NamespaceType.DAV.getPrefix() + ":" + ATTRIB_DEFINITION_VERSION, 1581 "Version " + String.valueOf(prop.getVersionNumber())); 1582 1583 return propEl; 1584 1585 } 1586 1587 1596 private void addValueArrayToProperty( 1597 org.w3c.dom.Document xdoc, 1598 Element propEl, 1599 List vals, 1600 DataType dataType) { 1601 1602 String sTypeTag = dataType.getPrefix() + ":" + dataType.getType(); 1603 propEl.setAttributeNS( 1604 NamespaceType.SOAP_ENCODING.getURI(), 1605 NamespaceType.SOAP_ENCODING.getPrefix() + ":" + ATTRIB_ARRAY_SIZE, 1606 String.valueOf(vals.size())); 1607 propEl.setAttributeNS( 1608 NamespaceType.SOAP_ENCODING.getURI(), 1609 NamespaceType.SOAP_ENCODING.getPrefix() + ":" + ATTRIB_ITEM_TYPE, 1610 sTypeTag); 1611 propEl.setAttribute( 1612 "xmlns:" + NamespaceType.SOAP_ENCODING.getPrefix(), 1613 NamespaceType.SOAP_ENCODING.getURI()); 1614 1615 propEl.setAttribute("xmlns:" + dataType.getPrefix(), dataType.getURI()); 1616 1617 Iterator valIter = vals.iterator(); 1618 1619 while (valIter.hasNext()) { 1620 Object objVal = valIter.next(); 1621 Element valEl = null; 1622 if (objVal instanceof String ) { 1623 String val = (String ) objVal; 1624 valEl = xdoc.createElement(sTypeTag); 1625 1626 valEl.appendChild(xdoc.createTextNode(val)); 1627 } else if (objVal instanceof Element) { 1628 valEl = (Element) objVal; 1629 } 1630 1631 propEl.appendChild(valEl); 1632 } 1633 } 1634 1635 1645 private Element getProfileElement(org.w3c.dom.Document xdoc, Profile prof) 1646 throws WebDAVException, DataAccessException, NameResolverException { 1647 Element profEl = xdoc.createElement(TAG_PROP); 1648 1649 List propInsts = prof.getPropertyInstances(); 1650 1651 Iterator iter = propInsts.iterator(); 1652 1653 while (iter.hasNext()) { 1654 profEl.appendChild( 1655 getPropertyInstanceElement( 1656 xdoc, 1657 (AbstractPropertyInstance) iter.next())); 1658 } 1659 1660 return profEl; 1661 } 1662 1663 1677 private boolean populatePropertyInstance( 1678 AbstractPropertyInstance propInst, 1679 Element propEl) 1680 throws 1681 WebDAVException, HarmoniseFactoryException, ProfileException, PopulateException, InvalidNameException, DataAccessException, NameResolverException { 1682 boolean bHasPropChanged = false; 1683 Property prop = propInst.getProperty(); 1684 Vector newVals = new Vector(); 1685 1686 if (m_logger.isLoggable(Level.FINER)) { 1687 m_logger.logp(Level.FINER, this.getClass().getName(), "populatePropertyInstance", "Populating property instance " 1688 + propInst.getClass() 1689 + " prop " 1690 + prop.getName()); 1691 System.out.println(XMLUtility.printNode(propEl)); 1692 } 1693 1694 String sArraySize = propEl 1695 .getAttributeNS( 1696 NamespaceType.SOAP_ENCODING.getURI(), 1697 ATTRIB_ARRAY_SIZE); 1698 1699 if (sArraySize != null && sArraySize.trim().length() > 0) { 1700 String sItemType = 1701 propEl.getAttributeNS( 1702 NamespaceType.SOAP_ENCODING.getURI(), 1703 ATTRIB_ITEM_TYPE); 1704 1705 if (m_logger.isLoggable(Level.FINER)) { 1706 m_logger.logp(Level.FINER, this.getClass().getName(), "populatePropertyInstance", "looking for array item - " + sItemType); 1707 } 1708 1709 List nodes = XMLUtils.getChildrenByName(propEl,sItemType); 1710 1711 if (m_logger.isLoggable(Level.FINER)) { 1712 m_logger.logp(Level.FINER, this.getClass().getName(), "populatePropertyInstance", "found - " + nodes.size()); 1713 } 1714 1715 for (int i = 0; i < nodes.size(); i++) { 1716 Node node = (Node) nodes.get(i); 1717 1718 Object objVal = getPropertyInstanceValue(propInst, node); 1719 if (objVal != null) { 1720 if (m_logger.isLoggable(Level.FINER)) { 1721 m_logger.logp(Level.FINER, this.getClass().getName(), "populatePropertyInstance", "adding value " + objVal); 1722 } 1723 1724 newVals.add(objVal); 1725 } 1726 } 1727 1728 } else { 1729 newVals.add(this.getPropertyInstanceValue(propInst, propEl)); 1730 } 1731 1732 List currVals = propInst.getValues(); 1733 1734 if (currVals != null) { 1736 currVals = new Vector(currVals); 1737 } 1738 1739 Iterator iter = null; 1740 1741 if (currVals != null) { 1743 iter = currVals.iterator(); 1744 1745 while (iter.hasNext()) { 1746 Object objVal = iter.next(); 1747 1748 if (newVals.contains(objVal) == false) { 1749 if(propInst instanceof WorkflowPropertyInstance) { 1750 WorkflowPropertyInstance wrkflwInst = (WorkflowPropertyInstance) propInst; 1751 1752 User usr = ((HarmoniseSessionManager) resource.getUserAuthenticator()) 1753 .getUser(resource); 1754 1755 wrkflwInst.removeWorkflowStage((WorkflowStageValue) objVal, usr); 1756 1757 } else if(objVal instanceof AbstractChildObject) { 1758 ((ChildObjectPropertyInstance)propInst).removeValue((AbstractChildObject)objVal); 1759 } else { 1760 propInst.removeValue(objVal); 1761 } 1762 1763 if (m_logger.isLoggable(Level.FINER)) { 1764 m_logger.logp(Level.FINER, this.getClass().getName(), "populatePropertyInstance", "removing value " + objVal); 1765 } 1766 1767 bHasPropChanged = true; 1768 } 1769 } 1770 } 1771 1772 iter = newVals.iterator(); 1773 1774 while (iter.hasNext() == true) { 1775 Object tmpValue = iter.next(); 1776 if (tmpValue != null) { 1777 if ((currVals == null) || (currVals.contains(tmpValue) == false)) { 1778 if (m_logger.isLoggable(Level.FINER)) { 1779 m_logger.logp(Level.FINER, this.getClass().getName(), "populatePropertyInstance", "adding value " + tmpValue); 1780 } 1781 1782 if(propInst instanceof WorkflowPropertyInstance) { 1783 WorkflowPropertyInstance wrkflwInst = (WorkflowPropertyInstance) propInst; 1784 1785 User usr = ((HarmoniseSessionManager) resource.getUserAuthenticator()) 1786 .getUser(resource); 1787 1788 wrkflwInst.addWorkflowStage((WorkflowStageValue) tmpValue, usr); 1789 1790 } else if (propInst instanceof GeneralPropertyInstance) { 1791 GeneralPropertyInstance genInst = 1792 (GeneralPropertyInstance) propInst; 1793 1794 if(tmpValue instanceof String ) { 1795 genInst.addValue((String ) tmpValue); 1796 } else if(tmpValue instanceof Date) { 1797 genInst.addValue((Date) tmpValue); 1798 } 1799 1800 1801 } else if (propInst instanceof ChildObjectPropertyInstance) { 1802 ChildObjectPropertyInstance childInst = 1803 (ChildObjectPropertyInstance) propInst; 1804 1805 childInst.addValue((AbstractChildObject) tmpValue); 1806 1807 } else if (propInst instanceof ProfilePropertyInstance) { 1808 ProfilePropertyInstance profPropInst = 1809 (ProfilePropertyInstance) propInst; 1810 1811 profPropInst.addValue((Profile) tmpValue); 1812 1813 } 1814 1815 bHasPropChanged = true; 1816 } 1817 } 1818 1819 } 1820 1821 return bHasPropChanged; 1822 } 1823 1824 1837 private Object getPropertyInstanceValue( 1838 AbstractPropertyInstance propInst, 1839 Node node) 1840 throws 1841 WebDAVException, HarmoniseFactoryException, ProfileException, PopulateException, InvalidNameException, DataAccessException, NameResolverException { 1842 Object objVal = null; 1843 1844 if (propInst instanceof ProfilePropertyInstance 1845 && (node instanceof Element) 1846 && ((Element)node).getLocalName().equals(TAG_PROP)) { 1847 Profile tmpProf = 1848 getProfileFromElement( 1849 (ProfilePropertyInstance) propInst, 1850 (Element) node); 1851 if(tmpProf.getPropertyInstances().size() > 0) { 1852 objVal = tmpProf; 1853 } 1854 1855 1856 } else { 1857 1858 NodeList nl = node.getChildNodes(); 1859 1860 for (int i = 0; i < nl.getLength(); i++) { 1861 Node tmpNode = (Node) nl.item(i); 1862 1863 if (tmpNode instanceof Text) { 1864 String sVal = ((Text) tmpNode).getData().trim(); 1865 1866 if (sVal != null && sVal.length() > 0) { 1867 if (m_logger.isLoggable(Level.FINER)) { 1868 m_logger.logp(Level.FINER, this.getClass().getName(), "getPropertyInstanceValue", 1869 "Adding value '" 1870 + sVal 1871 + "' to property " 1872 + propInst.getName()); 1873 } 1874 1875 if (propInst instanceof GeneralPropertyInstance) { 1876 if(propInst.getProperty().getRange() instanceof DateRange) { 1877 DateRange range = (DateRange) propInst.getProperty().getRange(); 1878 try { 1879 objVal = getDate(range,sVal); 1880 } catch (ParseException e) { 1881 objVal = sVal; 1882 } 1883 } else { 1884 objVal = sVal; 1885 } 1886 1887 } else if ( 1888 propInst instanceof ChildObjectPropertyInstance) { 1889 AbstractChildObject child = 1890 HarmoniseNameResolver.getObjectFromURL(m_dsi, sVal); 1891 objVal = child; 1892 } 1893 } 1894 1895 } else if ( 1896 tmpNode instanceof Element 1897 && ((Element)tmpNode).getLocalName().equals(TAG_HREF) 1898 && propInst instanceof ChildObjectPropertyInstance) { 1899 String sVal = tmpNode.getChildNodes().item(0).getNodeValue(); 1900 1901 if (sVal != null && sVal.trim().length() > 0) { 1902 AbstractChildObject child = 1903 HarmoniseNameResolver.getObjectFromURL(m_dsi, sVal); 1904 objVal = child; 1905 } 1906 } else if (propInst instanceof ProfilePropertyInstance) { 1907 if (tmpNode instanceof Element && ((Element)tmpNode).getLocalName().equals(TAG_PROP)) { 1908 Profile tmpProf = 1909 getProfileFromElement( 1910 (ProfilePropertyInstance) propInst, 1911 (Element) tmpNode); 1912 if(tmpProf.getPropertyInstances().size() > 0) { 1913 objVal = tmpProf; 1914 } 1915 1916 } 1917 } 1918 } 1919 } 1920 1921 return objVal; 1922 1923 } 1924 1925 1937 private Profile getProfileFromElement( 1938 ProfilePropertyInstance parentPropInst, 1939 Element propEl) 1940 throws 1941 WebDAVException, HarmoniseFactoryException, ProfileException, PopulateException, NameResolverException, InvalidNameException, DataAccessException { 1942 1943 if (m_logger.isLoggable(Level.FINER)) { 1944 m_logger.logp(Level.FINER, this.getClass().getName(), "getProfileFromElement", "Creating new profile value"); 1945 } 1946 1947 ProfileValue prof = new ProfileValue(m_dsi); 1948 1949 prof.setName(parentPropInst.getName()); 1950 prof.setProfiledObject(m_child); 1951 prof.setProperty(parentPropInst.getProperty()); 1952 1953 NodeList nl = propEl.getChildNodes(); 1954 1955 for (int i = 0; i < nl.getLength(); i++) { 1956 Node tmpNode = nl.item(i); 1957 1958 if (tmpNode.getNodeType() != Node.ELEMENT_NODE) { 1959 continue; 1960 } 1961 Element el = (Element) tmpNode; 1962 1963 Property prop = 1964 PropertyFactory.getPropertyFromName( 1965 this.m_dsi, 1966 el.getLocalName()); 1967 1968 AbstractPropertyInstance propInst = prof.getPropertyInstance(prop); 1969 boolean bNewProp = false; 1970 if(propInst == null) { 1971 if(m_logger.isLoggable(Level.FINER)) { 1972 m_logger.logp(Level.FINER, this.getClass().getName(), "getProfileFromElement", "Creating new property instance"); 1973 } 1974 1975 propInst = PropertyInstanceFactory.getPropertyInstance(this.m_dsi, prop); 1976 propInst.setProfile(prof); 1977 bNewProp = true; 1978 } 1979 1980 populatePropertyInstance(propInst, el); 1981 1982 if(propInst.hasValues() == true && bNewProp == true) { 1983 prof.addPropertyInstance(propInst); 1984 } else if(bNewProp == false && propInst.hasValues() == false) { 1985 prof.removeProperty(prop); 1986 } 1987 1988 } 1989 1990 return prof; 1991 } 1992 1993 1994 2000 private void populateChild() throws WebDAVException { 2001 try { 2002 2003 URL url = resource.getURL(); 2004 if (m_child == null 2005 && HarmoniseNameResolver.isVirtualCollection(url) 2006 == false 2007 && HarmoniseNameResolver.isVirtualResource(url) == false) { 2008 m_child = 2009 HarmoniseNameResolver.getObjectFromURL(m_dsi, resource.getURL()); 2010 } 2011 } catch (NameResolverException e) { 2012 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 2013 throw new WebDAVException( 2014 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 2015 e.getLocalizedMessage()); 2016 } 2017 } 2018 2019 private String getLockToken() throws WebDAVException { 2020 String sToken = null; 2021 2022 try { 2023 if(m_child.isLocked() == true) { 2024 sToken = HarmoniseLockManager.getLockToken( 2025 resource.getURL().getFile(), 2026 m_child.getLockOwner().getName()); 2027 } 2028 2029 } catch (DataAccessException e) { 2030 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage()); 2031 } catch (NameResolverException e) { 2032 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage()); 2033 } 2034 2035 return sToken; 2036 } 2037 2038 2046 private Date getDate(DateRange range, String sDate) throws ParseException { 2047 Date date = null; 2048 SimpleDateFormat format = null; 2049 if(range.includeTime() == true) { 2050 format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss G"); 2051 } else { 2052 format = new SimpleDateFormat("yyyy-MM-dd G"); 2053 } 2054 2055 2056 date = format.parse(sDate); 2057 return date; 2058 } 2059 2060 2067 private String getDateString(DateRange range, Date date) { 2068 String sDate = null; 2069 2070 SimpleDateFormat format = null; 2071 if(range.includeTime() == true) { 2072 format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss G"); 2073 } else { 2074 format = new SimpleDateFormat("yyyy-MM-dd G"); 2075 } 2076 2077 sDate = format.format(date); 2078 2079 return sDate; 2080 } 2081 2082 2083 2087 protected boolean isAllNonPropertyInstanceProperties(PropertyName[] names) { 2088 boolean bIsAllLive = true; 2089 int i = 0; 2090 2091 while(bIsAllLive == true && i < names.length) { 2092 String sLocalPropName = names[i].getLocal(); 2093 2094 bIsAllLive = false; 2095 2096 bIsAllLive = isLive(sLocalPropName); 2097 2098 if(bIsAllLive == false) { 2099 bIsAllLive = sLocalPropName.equals(TAG_TITLE) || 2100 sLocalPropName.equals(DAVRange.TAG_RANGE) || 2101 sLocalPropName.equals(DAVDomain.TAG_DOMAIN) || 2102 sLocalPropName.equals(TAG_LOCKDISCOVERY) || 2103 sLocalPropName.equals(TAG_HARMONISE_ID) || 2104 sLocalPropName.equals(TAG_DESCRIPTION); 2105 } 2106 2107 i++; 2108 } 2109 2110 return bIsAllLive; 2111 } 2112 2113} | Popular Tags |