1 54 55 package org.apache.jetspeed.services.cms.manager; 56 62 63 import java.util.*; 65 import java.io.FileWriter ; 66 import java.io.FileInputStream ; 67 import java.util.Vector ; 68 69 import org.apache.turbine.services.TurbineServices; 71 import org.apache.turbine.services.resources.TurbineResources; 72 import org.apache.turbine.util.upload.FileItem; 73 import org.apache.turbine.util.Log; 74 75 77 import org.apache.slide.structure.*; 78 import org.apache.slide.content.*; 79 import org.apache.slide.common.*; 80 import org.apache.slide.lock.*; 81 import org.apache.slide.security.*; 82 import org.apache.slide.authenticate.CredentialsToken; 83 import org.apache.slide.authenticate.SecurityToken; 84 import org.apache.slide.security.AccessDeniedException; 85 86 import org.apache.jetspeed.om.cms.*; 88 import org.apache.jetspeed.om.cms.slide.SlideResource; 89 import org.apache.jetspeed.services.cms.repository.slide.Utility; 90 import org.apache.jetspeed.services.cms.CmsService; 91 import org.apache.jetspeed.services.cms.JetspeedCMSException; 92 import org.apache.jetspeed.services.rundata.JetspeedRunDataService; 93 import org.apache.jetspeed.services.rundata.JetspeedRunData; 94 import org.apache.turbine.services.rundata.RunDataService; 95 96 public class CmsManagerSlideImpl implements CmsManager 97 { 98 99 private NamespaceAccessToken token = null; 100 private Structure structure = null; 101 private Security security = null; 102 private Content content = null; 103 private Lock lock = null; 104 private static String PROPERTY_CLASS_NAME = "resourceClassName"; 105 String nameSpace = null; 106 107 112 public void init (String namespace) 113 throws JetspeedCMSException 114 { 115 try 116 { 117 String domainFile =TurbineResources.getString("services.CmsService.CmsManager." + namespace + ".domainfile"); 118 Domain.init(new FileInputStream (domainFile)); 119 } 120 catch (Exception e) 121 { 122 throw new JetspeedCMSException(); 123 } 124 125 token = Domain.accessNamespace(new SecurityToken(new String ()), namespace); 126 127 structure = token.getStructureHelper(); 128 security = token.getSecurityHelper(); 129 content = token.getContentHelper(); 130 lock = token.getLockHelper(); 131 this.nameSpace = namespace; 132 } 133 134 139 140 public void exportRepository (String xmlFileName) 141 throws JetspeedCMSException 142 { 143 try 144 { 145 String userNode = this.getSlideUserNode(); 146 SlideToken slideToken = this.getSlideToken ( userNode ); 147 token.exportData(slideToken, new FileWriter ( xmlFileName )); 148 } 149 catch (Exception e) 150 { 151 throw new JetspeedCMSException("Impossible to export the content respository"); 152 } 153 } 154 155 161 public void populateCatalog (Catalog catalog) 162 throws JetspeedCMSException 163 { 164 this.populateCatalog(catalog, true, true, -1); 165 } 166 167 175 public void populateCatalog (Catalog catalog, 176 boolean withSubCatalog, boolean withContent) 177 throws JetspeedCMSException 178 { 179 this.populateCatalog(catalog, withSubCatalog, 180 withContent, -1); 181 182 } 183 184 193 public void populateCatalog (Catalog catalog, 194 boolean withSubCatalog, boolean withContent, 195 int numberOfLevels ) 196 throws JetspeedCMSException 197 { 198 try 199 { 200 String userNode = this.getSlideUserNode(); 201 int tmpLevel = numberOfLevels ; 202 SlideToken slideToken = this.getSlideToken ( userNode ); 203 ObjectNode objectNode = structure.retrieve(slideToken, catalog.getUri(), true); 204 205 Enumeration childrenUri = objectNode.enumerateChildren(); 208 210 this.getChildren(userNode, childrenUri, catalog, 211 withSubCatalog, withContent, numberOfLevels); 212 213 Enumeration linksUri = objectNode.enumerateLinks(); 215 this.getChildren(userNode, linksUri, catalog, 216 withSubCatalog, withContent, tmpLevel); 217 } 218 catch (Exception e) 219 { 220 throw new JetspeedCMSException("Impossible to get catalog information"); 221 } 222 } 223 224 230 public Vector getUriList (String parentUri) 231 throws JetspeedCMSException 232 { 233 try 234 { 235 236 String userNode = this.getSlideUserNode(); 237 Vector uriList = new Vector (); 238 239 uriList.add(parentUri); 241 243 SlideToken slideToken = this.getSlideToken ( userNode ); 245 ObjectNode objectNode = structure.retrieve(slideToken, parentUri); 246 Enumeration children = objectNode.enumerateChildren(); 247 248 while (children.hasMoreElements()) 250 { 251 String uriChild = (String ) children.nextElement(); 252 Vector v = this.getUriList(uriChild); 253 uriList.addAll(v); 254 } 255 256 return uriList; 257 } 258 catch (Exception e) 259 { 260 throw new JetspeedCMSException("Impossible to get the uri List"); 261 } 262 } 263 264 265 272 public Resource getResource (String uri ) 273 throws JetspeedCMSException 274 { 275 276 SlideResource resource = null; 277 278 try 279 { 280 String userNode = this.getSlideUserNode(); 281 282 SlideToken slideToken = this.getSlideToken( userNode ); 284 285 ObjectNode objectNode = structure.retrieve(slideToken, uri, true); 288 289 NodeRevisionDescriptors revisionDescriptors = 291 content.retrieve(slideToken, objectNode.getUri()); 292 NodeRevisionDescriptor revisionDescriptor = 293 content.retrieve(slideToken, revisionDescriptors); 294 295 NodeProperty p = revisionDescriptor.getProperty(PROPERTY_CLASS_NAME); 298 String className = (String ) p.getValue(); 299 resource = (SlideResource) Class.forName( className ).newInstance(); 300 resource.setUri(uri); 301 resource.setDescriptor(revisionDescriptor); 303 304 resource.setPermissions(this.getSecurity(uri)); 306 return resource; 307 } 308 catch (AccessDeniedException e) 309 { 310 JetspeedCMSException e1 = new JetspeedCMSException("Impossible to get the content resource"); 311 e1.setIsAccessDenied(true); 312 throw e1; 313 } 314 catch (NullPointerException e) 315 { 316 319 Catalog catalog = (Catalog) CmsFactory.getCmsOmInstance("Catalog"); 320 catalog.setUri(uri); 321 catalog.setLogicalName(uri); 322 catalog.setTitle(uri); 323 return catalog; 324 } 325 catch (RevisionDescriptorNotFoundException e) 326 { 327 336 if (uri.equals(TurbineResources.getString("services.CmsService.catalog.root.uri"))) 337 { 338 Catalog catalog = (Catalog) CmsFactory.getCmsOmInstance("Catalog"); 339 catalog.setUri(uri); 340 catalog.setLogicalName(TurbineResources.getString("services.CmsService.catalog.root.logicalname")); 341 catalog.setTitle(TurbineResources.getString("services.CmsService.catalog.root.title")); 342 return catalog; 343 } 344 else 345 { 346 Catalog catalog = (Catalog) CmsFactory.getCmsOmInstance("Catalog"); 348 catalog.setUri(uri); 349 catalog.setLogicalName(uri); 350 catalog.setTitle(uri); 351 return catalog; 352 } 355 } 356 catch ( Exception e) 357 { 358 throw new JetspeedCMSException("Impossible to get the content resource"); 359 } 360 } 361 362 370 public Vector getCatalogs(String fromUri, String toUri) 371 throws JetspeedCMSException 372 { 373 374 Vector catalogs = new Vector (); 375 String currentUri = toUri; 376 377 while ( true ) 378 { 379 try 380 { 381 Catalog catalog = (Catalog) this.getResource(currentUri); 382 catalogs.insertElementAt(catalog, 0); 383 384 if ((currentUri.equals(fromUri)) || 385 (catalog.getParentUri() == null) || 386 (catalog.getParentUri().equals("")) ) 387 { 388 break; 389 } 390 391 currentUri = catalog.getParentUri(); 392 } 393 catch (JetspeedCMSException e) 394 { 395 if (!e.isAccessDenied()) 398 { 399 throw new JetspeedCMSException("Impossible to get the complete catalog path"); 400 } 401 } 402 } 403 404 return catalogs; 405 } 406 407 412 public Vector getSecurity (String uri ) 413 throws JetspeedCMSException 414 { 415 try 416 { 417 String userNode = this.getSlideUserNode(); 418 419 SlideToken slideToken = this.getSlideToken( userNode ); 421 Enumeration e = security.enumeratePermissions(slideToken, uri); 423 Vector permissions = new Vector (); 424 425 while (e.hasMoreElements()) 426 { 427 org.apache.slide.security.NodePermission node = 428 (org.apache.slide.security.NodePermission) e.nextElement(); 429 Permission p = (Permission) CmsFactory.getCmsOmInstance("Permission"); 430 p.setAction(node.getActionUri()); 431 p.setActor(node.getSubjectUri()); 432 p.setInheritable(node.isInheritable()); 433 p.setNegative(node.isNegative()); 434 permissions.add(p); 435 } 436 437 return permissions; 438 } 439 catch (Exception e) 440 { 441 throw new JetspeedCMSException("Impossible to get the security information " + uri); 442 } 443 } 444 445 454 public void grantPermission (String objectUri , String userUri, 455 String actionUri, boolean inheritable, 456 boolean negative) 457 throws JetspeedCMSException 458 { 459 460 try 461 { 462 463 String userNode = this.getSlideUserNode(); 464 SlideToken slideToken = this.getSlideToken( userNode ); 466 467 ObjectNode objectNode = structure.retrieve(slideToken, objectUri, true); 470 471 SubjectNode subjectNode = (SubjectNode) structure.retrieve(slideToken, userUri, true); 474 475 ActionNode actionNode = (ActionNode) structure.retrieve(slideToken, actionUri); 477 478 NodePermission perm = new NodePermission(objectNode, subjectNode, 480 actionNode, inheritable, 481 negative); 482 security.grantPermission(slideToken, perm); 483 484 485 } 486 catch ( Exception e) 487 { 488 throw new JetspeedCMSException("Impossible to grant the permission"); 489 } 490 } 491 492 497 public void createResource (Resource resource) 498 throws JetspeedCMSException 499 { 500 try 501 { 502 503 String userNode = this.getSlideUserNode(); 504 505 SlideToken slideToken = this.getSlideToken( userNode ); 507 508 SubjectNode subject = new SubjectNode(); 510 structure.create(slideToken, subject, resource.getUri()); 511 512 NodeRevisionDescriptor descriptor = (( SlideResource )resource).getDesciptor(); 514 descriptor.setProperty(this.PROPERTY_CLASS_NAME, 515 resource.getClass().getName()); 516 517 if (resource instanceof Catalog) 518 { 519 descriptor.setProperty("getcontentlength", new Long (0)); 521 522 descriptor.setProperty(new NodeProperty("resourcetype", "<collection/>", true)); 524 } 525 else 526 { 527 descriptor.setProperty("getcontentlength", new Long (0)); 529 530 descriptor.setLastModified(new Date()); 532 533 String etag = resource.getUri().hashCode() + "_" 535 + descriptor.getContentLength(); 537 descriptor.setProperty(new NodeProperty("getetag", etag, true)); 538 539 descriptor.setProperty(new NodeProperty("getcontenttype", "", true)); 540 541 descriptor.setProperty(new NodeProperty("resourcetype", "", true)); 543 544 descriptor.setProperty(new NodeProperty("source", "", true)); 546 547 550 554 556 NodeProperty property = new NodeProperty("ishidden", "0", "MICROSOFT"); 558 descriptor.setProperty(property); 559 560 property = new NodeProperty("iscollection", "0", "MICROSOFT"); 562 descriptor.setProperty(property); 563 564 property = new NodeProperty("isreadonly", "0", "MICROSOFT"); 566 descriptor.setProperty(property); 567 568 property = new NodeProperty("lastaccessed", (new Date()).toString(), 570 "MICROSOFT"); 571 descriptor.setProperty(property); 572 } 573 574 content.create(slideToken, resource.getUri(), descriptor, null); 575 576 578 Vector permissions = resource.getPermissions(); 579 580 for (Enumeration e = permissions.elements(); e.hasMoreElements();) 581 { 582 Permission p = (Permission) e.nextElement(); 583 this.grantPermission(resource.getUri(), p.getActor(), 584 p.getAction(), p.isInheritable(), p.isNegative()); 585 } 586 } 587 catch (Exception e) 588 { 589 throw new JetspeedCMSException("Impossible to create the content resource"); 590 } 591 } 592 593 599 public void createResource (Resource resource, FileItem stream) 600 throws JetspeedCMSException 601 { 602 try 603 { 604 605 String userNode = this.getSlideUserNode(); 606 607 SlideToken slideToken = this.getSlideToken( userNode ); 609 610 SubjectNode subject = new SubjectNode(); 612 structure.create(slideToken, subject, resource.getUri()); 613 614 NodeRevisionDescriptor descriptor = ((SlideResource) resource).getDesciptor(); 616 descriptor.setProperty(this.PROPERTY_CLASS_NAME, 617 resource.getClass().getName()); 618 619 descriptor.setProperty("getcontentlength", new Long (stream.getSize())); 621 622 descriptor.setLastModified(new Date()); 624 625 String etag = resource.getUri().hashCode() + "_" 627 + descriptor.getContentLength(); 629 descriptor.setProperty(new NodeProperty("getetag", etag, true)); 630 631 String contentType = stream.getContentType(); 632 descriptor.setProperty(new NodeProperty("getcontenttype", contentType, true)); 633 634 descriptor.setProperty(new NodeProperty("resourcetype", "", true)); 636 637 descriptor.setProperty(new NodeProperty("source", "", true)); 639 640 String displayName = stream.getFileName().substring( stream.getFileName().lastIndexOf("\\")+1 ); 642 643 descriptor.setProperty(new NodeProperty("displayname", 644 "<![CDATA[" + displayName+ "]]>")); 645 646 649 653 655 NodeProperty property = new NodeProperty("ishidden", "0", "MICROSOFT"); 657 descriptor.setProperty(property); 658 659 property = new NodeProperty("iscollection", "0", "MICROSOFT"); 661 descriptor.setProperty(property); 662 663 property = new NodeProperty("isreadonly", "0", "MICROSOFT"); 665 descriptor.setProperty(property); 666 667 property = new NodeProperty("lastaccessed", (new Date()).toString(), 669 "MICROSOFT"); 670 descriptor.setProperty(property); 671 672 NodeRevisionContent revisionContent = new NodeRevisionContent(); 674 revisionContent.setContent(stream.getInputStream()); 675 676 content.create(slideToken, resource.getUri(), descriptor, revisionContent); 678 679 Vector permissions = resource.getPermissions(); 681 682 for (Enumeration e = permissions.elements(); e.hasMoreElements();) 683 { 684 Permission p = (Permission) e.nextElement(); 685 this.grantPermission(resource.getUri(), p.getActor(), 686 p.getAction(), p.isInheritable(), p.isNegative()); 687 } 688 } 689 catch (Exception e) 690 { 691 throw new JetspeedCMSException("Impossible to create the content resource"); 692 } 693 } 694 695 700 public void createLink (Link link) 701 throws JetspeedCMSException 702 { 703 try 704 { 705 String userNode = this.getSlideUserNode(); 706 707 SlideToken slideToken = this.getSlideToken( userNode ); 709 710 ObjectNode resourceNode = (ObjectNode) structure.retrieve(slideToken, 711 link.getTargetResourceUri() ); 712 LinkNode linkNode = new LinkNode(); 713 structure.createLink(slideToken, linkNode, link.getUri(), resourceNode); 714 } 715 catch (Exception e) 716 { 717 throw new JetspeedCMSException("Impossible to get the link"); 718 } 719 } 720 721 722 728 735 private SlideToken getSlideToken (String userNode) 736 { 737 CredentialsToken credToken = new CredentialsToken(new String (userNode)); 738 739 return new SlideTokenImpl(credToken); 740 } 741 742 755 756 private void getChildren(String userNode, Enumeration children, 757 Catalog catalog, boolean withSubCatalog, 758 boolean withContent,int numberOfLevels) 759 throws JetspeedCMSException 760 { 761 762 if (Log.getLogger().isDebugEnabled()) 763 { 764 Log.debug("Slide.getChildren for : " + catalog.getUri()); 765 } 766 767 if (numberOfLevels != -1) 768 { 769 numberOfLevels--; 770 } 771 772 while (children.hasMoreElements()) 773 { 774 String childUri = (String ) children.nextElement(); 775 776 if (Log.getLogger().isDebugEnabled()) 777 { 778 Log.debug("Slide.getChildren - Child found : " + childUri); 779 } 780 781 try 782 { 783 Resource resource = this.getResource(childUri); 784 785 if (resource instanceof Catalog) 786 { 787 if (withSubCatalog) 788 { 789 if ((numberOfLevels == -1) || (numberOfLevels > 0)) 791 { 792 int tmp = numberOfLevels; 793 this.populateCatalog((Catalog) resource, 794 withSubCatalog, 795 withContent, 796 numberOfLevels ); 797 } 798 799 if (Log.getLogger().isDebugEnabled()) 800 { 801 Log.debug("Slide.add Children : " + resource.getKey()); 802 } 803 804 catalog.addResource( resource.getKey(), resource); 805 } 806 } 807 else 808 { 809 if (withContent) 810 { 811 catalog.addResource(resource.getKey(), resource); 812 } 813 } 814 } 815 catch (JetspeedCMSException e) 816 { 817 if (!e.isAccessDenied()) 820 { 821 throw new JetspeedCMSException("Impossible to get the children information "); 822 } 823 } 824 } 825 } 826 827 828 833 private String getSlideUserNode() 834 { 835 836 JetspeedRunData data = ((JetspeedRunDataService) TurbineServices.getInstance() 837 .getService(RunDataService.SERVICE_NAME)) 838 .getCurrentRunData(); 839 return Utility.getUserUri(data.getUser().getUserName()); 840 } 841 } 842 | Popular Tags |