1 23 package org.apache.slide.webdav.util; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.apache.slide.common.NamespaceAccessToken; 32 import org.apache.slide.common.PropertyName; 33 import org.apache.slide.common.RequestedProperties; 34 import org.apache.slide.common.RequestedPropertiesImpl; 35 import org.apache.slide.common.RequestedProperty; 36 import org.apache.slide.common.RequestedPropertyImpl; 37 import org.apache.slide.common.SlideException; 38 import org.apache.slide.common.SlideToken; 39 import org.apache.slide.common.UriPath; 40 import org.apache.slide.content.Content; 41 import org.apache.slide.content.NodeProperty; 42 import org.apache.slide.content.NodeRevisionDescriptor; 43 import org.apache.slide.content.NodeRevisionDescriptors; 44 import org.apache.slide.content.NodeRevisionNumber; 45 import org.apache.slide.content.RevisionDescriptorNotFoundException; 46 import org.apache.slide.content.NodeProperty.NamespaceCache; 47 import org.apache.slide.lock.Lock; 48 import org.apache.slide.search.RequestedResource; 49 import org.apache.slide.security.AccessDeniedException; 50 import org.apache.slide.security.Security; 51 import org.apache.slide.structure.Structure; 52 import org.apache.slide.util.Configuration; 53 import org.apache.slide.util.XMLValue; 54 import org.apache.slide.webdav.WebdavServletConfig; 55 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind; 56 import org.apache.slide.webdav.util.resourcekind.ResourceKind; 57 import org.jdom.CDATA; 58 import org.jdom.Comment; 59 import org.jdom.Element; 60 import org.jdom.EntityRef; 61 import org.jdom.JDOMException; 62 import org.jdom.Namespace; 63 import org.jdom.ProcessingInstruction; 64 import org.jdom.Text; 65 66 67 77 public class PropertyRetrieverImpl extends AbstractWebdavHelper implements PropertyRetriever, WebdavConstants, DeltavConstants, AclConstants, BindConstants { 78 79 80 83 protected Content content = null; 84 85 88 protected Lock lock = null; 89 90 93 protected Security security = null; 94 95 98 protected Structure structure = null; 99 100 103 protected PropertyHelper propertyHelper = null; 104 105 108 protected WebdavServletConfig config = null; 109 115 public PropertyRetrieverImpl(NamespaceAccessToken token, SlideToken slideToken, WebdavServletConfig sConf) { 116 117 super(slideToken, token); 118 this.structure = token.getStructureHelper(); 119 this.content = token.getContentHelper(); 120 this.security = token.getSecurityHelper(); 121 this.lock = token.getLockHelper(); 122 this.config = sConf; 123 propertyHelper = PropertyHelper.getPropertyHelper(slideToken, token, sConf); 124 } 125 126 127 137 142 protected PropertyHelper getPropertyHelper() { 143 return propertyHelper; 144 } 145 146 147 166 public List getPropertiesOfObject(RequestedProperties requestedProperties, String uri, String servletContextPath, boolean extendedAllprop) throws SlideException, JDOMException { 167 168 NodeRevisionDescriptors revisionDescriptors = null; 169 NodeRevisionDescriptor revisionDescriptor = null; 170 171 173 try { 174 revisionDescriptors = 175 content.retrieve(sToken, uri); 176 177 try { 178 179 revisionDescriptor = content.retrieve(sToken, 180 revisionDescriptors); 181 183 184 } catch (RevisionDescriptorNotFoundException e) { 185 186 revisionDescriptor = new NodeRevisionDescriptor(0); 190 191 if (!Configuration.useBinding(nsaToken.getUri(sToken, uri).getStore())) { 192 revisionDescriptor.setName(new UriPath(uri).lastSegment()); 193 } 194 } 195 196 } catch (AccessDeniedException e) { 197 if (revisionDescriptor == null) { 198 revisionDescriptor = new NodeRevisionDescriptor(0); 199 } 200 } 201 206 return getPropertiesOfObject(requestedProperties, revisionDescriptors, revisionDescriptor, servletContextPath, extendedAllprop); 207 } 208 209 229 public List getPropertiesOfObject(RequestedProperties requestedProperties, String uri, NodeRevisionNumber revisionNumber, String servletContextPath, boolean extendedAllprop) throws SlideException, JDOMException { 230 231 NodeRevisionDescriptors revisionDescriptors = content.retrieve(sToken, uri); 232 NodeRevisionDescriptor revisionDescriptor = content.retrieve(sToken, 233 revisionDescriptors, 234 revisionNumber); 235 return getPropertiesOfObject(requestedProperties, revisionDescriptors, revisionDescriptor, servletContextPath, extendedAllprop); 236 } 237 238 258 public List getPropertiesOfObject(RequestedProperties requestedProperties, NodeRevisionDescriptors revisionDescriptors, NodeRevisionDescriptor revisionDescriptor, String servletContextPath, boolean extendedAllprop) throws SlideException, JDOMException { 259 260 ComputedPropertyProvider propertyProvider = new ComputedPropertyProvider(nsaToken, 261 sToken, 262 propertyHelper, 263 servletContextPath); 264 return getPropertiesOfObject(requestedProperties, 265 new ResourceWithProvidedProperties(revisionDescriptors, 266 revisionDescriptor, 267 propertyProvider), 268 servletContextPath, 269 extendedAllprop); 270 271 } 272 273 291 public List getPropertiesOfObject(RequestedProperties requestedProperties, RequestedResource requestedResource, String servletContextPath, boolean extendedAllprop) throws SlideException, JDOMException { 292 293 List elementList = new ArrayList (); 294 295 299 if (requestedProperties.isAllProp()) { 300 elementList = getAllPropertiesOfObject(requestedResource, servletContextPath, extendedAllprop); 301 } 302 else { 303 elementList = getRequestedPropertiesOfObject(requestedProperties, requestedResource, servletContextPath); 304 } 305 return elementList; 306 } 307 308 322 protected List getRequestedPropertiesOfObject(RequestedProperties requestedProperties, RequestedResource requestedResource, String servletContextPath) throws SlideException { 323 324 List elementList = new ArrayList (); 325 Iterator propertyIterator = requestedProperties.getRequestedProperties(); 326 327 Element propstat = new Element(E_PROPSTAT, NamespaceCache.DEFAULT_NAMESPACE); 328 Element prop = new Element(E_PROP, NamespaceCache.DEFAULT_NAMESPACE); 329 propstat.addContent(prop); 330 Element propertyElement = null; 331 boolean anyPropertyFound = false; 332 333 Map erroneousPropertiesMap = new HashMap (); 336 337 String status = new String ("HTTP/1.1 " + WebdavStatus.SC_OK 338 + " " + WebdavStatus.getStatusText 339 (WebdavStatus.SC_OK)); 340 341 while (propertyIterator.hasNext()) { 342 343 RequestedProperty property = (RequestedProperty)propertyIterator.next(); 344 NodeProperty currentProperty = null; 345 Integer errorCode = null; 346 try { 347 currentProperty = requestedResource.getProperty(property.getName(), 348 property.getNamespace()); 349 if (currentProperty == null) { 350 errorCode = new Integer (WebdavStatus.SC_NOT_FOUND); 351 } 352 } 353 catch (AccessDeniedException e) { 354 errorCode = new Integer (WebdavStatus.SC_FORBIDDEN); 355 } 356 357 if (errorCode != null) { 358 List erroneousPropertiesList = (List )erroneousPropertiesMap.get(errorCode); 359 if (erroneousPropertiesList == null) { 360 erroneousPropertiesList = new ArrayList (); 361 erroneousPropertiesMap.put(errorCode, erroneousPropertiesList); 362 } 363 erroneousPropertiesList.add(new PropertyName(property.getName(), property.getNamespace())); 364 } 365 else { 366 propertyElement = getPropertyElement(currentProperty, servletContextPath); 367 if (propertyElement != null) { 368 anyPropertyFound = true; 369 prop.addContent(propertyElement); 370 } 371 } 372 } 373 374 if (anyPropertyFound) { 375 Element statusElement = new Element(E_STATUS, NamespaceCache.DEFAULT_NAMESPACE); 376 statusElement.setText(status); 377 propstat.addContent(statusElement); 378 elementList.add(propstat); 379 } 380 381 elementList.addAll(getPropstatForErroneousProperties(erroneousPropertiesMap, servletContextPath)); 382 383 return elementList; 384 } 385 386 387 403 protected List getAllPropertiesOfObject(RequestedResource requestedResource, String servletContextPath, boolean extendedAllprop) throws SlideException { 404 405 List elementList = new ArrayList (); 406 Element propstat = new Element(E_PROPSTAT, NamespaceCache.DEFAULT_NAMESPACE); 407 elementList.add(propstat); 408 Element prop = new Element(E_PROP, NamespaceCache.DEFAULT_NAMESPACE); 409 propstat.addContent(prop); 410 Element propertyElement = null; 411 412 Map erroneousPropertiesMap = new HashMap (); 415 416 417 String status = new String ("HTTP/1.1 " + WebdavStatus.SC_OK 418 + " " + WebdavStatus.getStatusText 419 (WebdavStatus.SC_OK)); 420 421 Iterator propertyNameIterator = requestedResource.getAllPropertiesNames(); 422 while (propertyNameIterator.hasNext()) { 423 424 PropertyName currentPropertyName = (PropertyName) propertyNameIterator.next(); 425 if (currentPropertyName != null) { 426 427 if ( !extendedAllprop && S_DAV.equals(currentPropertyName.getNamespace()) && ( 428 DeltavConstants.DELTAV_PROPERTY_LIST.contains(currentPropertyName.getName()) || 429 BindConstants.BIND_PROPERTY_LIST.contains(currentPropertyName.getName()) || 430 AclConstants.ACL_PROPERTY_LIST.contains(currentPropertyName.getName()) 431 ) 432 ) { 433 continue; 435 } 436 437 NodeProperty currentProperty = null; 438 Integer errorCode = null; 439 try { 440 currentProperty = requestedResource.getProperty(currentPropertyName); 441 if (currentProperty == null) { 442 errorCode = new Integer (WebdavStatus.SC_NOT_FOUND); 443 } 444 } 445 catch (AccessDeniedException e) { 446 errorCode = new Integer (WebdavStatus.SC_FORBIDDEN); 447 } 448 449 if (errorCode != null) { 450 List erroneousPropertiesList = (List )erroneousPropertiesMap.get(errorCode); 451 if (erroneousPropertiesList == null) { 452 erroneousPropertiesList = new ArrayList (); 453 erroneousPropertiesMap.put(errorCode, erroneousPropertiesList); 454 } 455 erroneousPropertiesList.add(currentPropertyName); 456 } 457 else { 458 propertyElement = getPropertyElement(currentProperty, servletContextPath); 459 if (propertyElement != null) {prop.addContent(propertyElement);} 460 } 461 } 462 } 463 464 465 Element statusElement = new Element(E_STATUS, NamespaceCache.DEFAULT_NAMESPACE); 466 statusElement.setText(status); 467 propstat.addContent(statusElement); 468 469 elementList.addAll(getPropstatForErroneousProperties(erroneousPropertiesMap, servletContextPath)); 470 471 return elementList; 472 } 473 474 486 protected List getPropstatForErroneousProperties(Map erroneousPropertiesMap, String servletContextPath) { 487 488 List elementList = new ArrayList (); 489 Iterator iterator = erroneousPropertiesMap.keySet().iterator(); 490 while (iterator.hasNext()) { 491 492 Integer errorCode = (Integer )iterator.next(); 493 List erroneousPropertiesList = (List )erroneousPropertiesMap.get(errorCode); 494 Element propstat = new Element(E_PROPSTAT, NamespaceCache.DEFAULT_NAMESPACE); 495 elementList.add(propstat); 496 Element prop = new Element(E_PROP, NamespaceCache.DEFAULT_NAMESPACE); 497 propstat.addContent(prop); 498 499 String status = new String ("HTTP/1.1 " + errorCode.intValue() 500 + " " + WebdavStatus.getStatusText(errorCode.intValue())); 501 502 Iterator propertyIterator = erroneousPropertiesList.iterator(); 503 while (propertyIterator.hasNext()) { 504 505 PropertyName erroneousProperty = 506 (PropertyName) propertyIterator.next(); 507 Element propertyElement = getPropertyElement(erroneousProperty.getNamespace(), 508 erroneousProperty.getName(), 509 null, 510 servletContextPath); 511 if (propertyElement != null) {prop.addContent(propertyElement);} 512 } 513 514 Element statusElement = new Element(E_STATUS, NamespaceCache.DEFAULT_NAMESPACE); 515 statusElement.setText(status); 516 propstat.addContent(statusElement); 517 } 518 519 return elementList; 520 } 521 522 523 524 533 private Element getPropertyElement(NodeProperty property, String servletContextPath) { 534 if (property != null) { 535 return getPropertyElement(property.getNamespace(), 536 property.getName(), 537 property.getValue(), 538 servletContextPath); 539 } 540 return null; 541 } 542 543 554 private Element getPropertyElement(String namespaceString, 555 String propertyName, 556 Object propertyValue, 557 String servletContextPath) { 558 Element property = null; 559 Namespace namespace = Namespace.NO_NAMESPACE; 560 Namespace valueDefaultNamespace = null; 561 if (namespaceString != null) { 562 namespace = NamespaceCache.getNamespace(namespaceString); 563 if (NamespaceCache.DEFAULT_URI.equals(namespace.getURI())) { 564 valueDefaultNamespace = namespace; 567 } 568 } 569 570 property = new Element(propertyName, namespace); 571 if ( (propertyValue != null) && (propertyValue.toString().length() > 0)) { 572 if( propertyValue.toString().indexOf('<') >= 0 ) { 573 try { 574 XMLValue xmlValue = new XMLValue(propertyValue.toString(), valueDefaultNamespace); 575 if (AbstractResourceKind.isLiveProperty(propertyName)) { 576 convertHrefValueToAbsoluteURL (xmlValue, servletContextPath, config); 577 } 578 Iterator iterator = xmlValue.iterator(); 579 while (iterator.hasNext()) { 580 Object o = iterator.next(); 581 if( o instanceof Element ) 582 property.addContent((Element)o); 583 else if( o instanceof Text ) 584 property.addContent((Text)o); 585 else if( o instanceof Comment ) 586 property.addContent((Comment)o); 587 else if( o instanceof ProcessingInstruction ) 588 property.addContent((ProcessingInstruction)o); 589 else if( o instanceof CDATA ) 590 property.addContent((CDATA)o); 591 else if( o instanceof EntityRef ) 592 property.addContent((EntityRef)o); 593 } 594 } 595 catch (JDOMException e) { 596 property.setText(propertyValue.toString()); 597 } 598 } 599 else { 600 property.setText(propertyValue.toString()); 601 } 602 } 603 return property; 604 } 605 606 607 608 609 618 protected static void convertHrefValueToAbsoluteURL (XMLValue xmlValue, 619 String servletContextPath, 620 WebdavServletConfig config) { 621 if (xmlValue != null) { 622 Iterator iterator = xmlValue.iterator(); 623 Element element = null; 624 while (iterator.hasNext()) { 625 Object o = iterator.next(); 626 if( o instanceof Element ) { 627 element = (Element)o; 628 convertHrefValueToAbsoluteURL(element, servletContextPath, config); 629 } 630 } 631 } 632 } 633 634 private static void convertHrefValueToAbsoluteURL (Element element, 635 String servletContextPath, 636 WebdavServletConfig config) { 637 if (element.getChildren().size() > 0) { 638 Iterator i = element.getChildren().iterator(); 639 while (i.hasNext()) { 640 Element child = (Element)i.next(); 641 if (!(element.getName().equals("activelock") && child.getName().equals("owner"))) { 643 convertHrefValueToAbsoluteURL(child, servletContextPath, config); 644 } 645 } 646 } 647 if ( E_HREF.equals(element.getName()) && (element.getText() != null) ) { 648 if ( !PropertyHelper.isAbsoluteURL(servletContextPath, element.getText()) ) { 649 650 element.setText(WebdavUtils.getAbsolutePath (element.getText(), 651 servletContextPath, config)); 652 } 653 } 654 } 655 656 663 public RequestedProperties getAllPropertyNames(String resourcePath, boolean liveOnly) throws SlideException { 664 NodeRevisionDescriptor nrd = 665 content.retrieve(sToken, content.retrieve(sToken, resourcePath)); 666 ResourceKind resourceKind = AbstractResourceKind.determineResourceKind(nsaToken, resourcePath, nrd); 667 RequestedPropertiesImpl result = (RequestedPropertiesImpl)getAllPropertyNames(resourceKind); 668 669 if (!liveOnly) { 670 Enumeration props = nrd.enumerateProperties(); 672 while (props.hasMoreElements()) { 673 NodeProperty np = (NodeProperty)props.nextElement(); 674 if (!result.contains(np)) { 675 result.addProperty(new RequestedPropertyImpl(np.getName(), np.getNamespace())); 676 } 677 } 678 } 679 return result; 680 } 681 682 692 public RequestedProperties getAllPropertyNames(ResourceKind resourceKind) throws SlideException { 693 RequestedPropertiesImpl result = new RequestedPropertiesImpl(); 694 Iterator liveprops = resourceKind.getSupportedLiveProperties().iterator(); 695 while (liveprops.hasNext()) { 696 result.addProperty(new RequestedPropertyImpl((String )liveprops.next(), DNSP.getURI())); 697 } 698 return result; 699 } 700 701 } 702 703 704 | Popular Tags |