1 31 32 package org.opencms.file.types; 33 34 import org.opencms.configuration.CmsConfigurationCopyResource; 35 import org.opencms.configuration.CmsConfigurationException; 36 import org.opencms.db.CmsSecurityManager; 37 import org.opencms.file.CmsFile; 38 import org.opencms.file.CmsObject; 39 import org.opencms.file.CmsProperty; 40 import org.opencms.file.CmsResource; 41 import org.opencms.file.CmsResourceFilter; 42 import org.opencms.file.CmsVfsException; 43 import org.opencms.file.CmsVfsResourceAlreadyExistsException; 44 import org.opencms.lock.CmsLock; 45 import org.opencms.main.CmsException; 46 import org.opencms.main.CmsLog; 47 import org.opencms.main.OpenCms; 48 import org.opencms.security.CmsPermissionSet; 49 import org.opencms.security.CmsSecurityException; 50 import org.opencms.staticexport.CmsLinkManager; 51 import org.opencms.util.CmsFileUtil; 52 import org.opencms.util.CmsMacroResolver; 53 54 import java.util.ArrayList ; 55 import java.util.Collections ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import java.util.Map ; 59 60 import org.apache.commons.logging.Log; 61 62 72 public abstract class A_CmsResourceType implements I_CmsResourceType { 73 74 75 public static final String MACRO_RESOURCE_FOLDER_PATH = "resource.folder.path"; 76 77 78 public static final String MACRO_RESOURCE_NAME = "resource.name"; 79 80 81 public static final String MACRO_RESOURCE_PARENT_PATH = "resource.parent.path"; 82 83 84 public static final String MACRO_RESOURCE_ROOT_PATH = "resource.root.path"; 85 86 87 public static final String MACRO_RESOURCE_SITE_PATH = "resource.site.path"; 88 89 90 private static final Log LOG = CmsLog.getLog(A_CmsResourceType.class); 91 92 93 protected boolean m_addititionalModuleResourceType; 94 95 96 protected String m_className; 97 98 99 protected List m_copyResources; 100 101 102 protected List m_defaultProperties; 103 104 105 protected boolean m_frozen; 106 107 108 protected List m_mappings; 109 110 111 protected int m_typeId; 112 113 114 protected String m_typeName; 115 116 119 public A_CmsResourceType() { 120 121 m_typeId = -1; 122 m_mappings = new ArrayList (); 123 m_defaultProperties = new ArrayList (); 124 m_copyResources = new ArrayList (); 125 } 126 127 130 public void addConfigurationParameter(String paramName, String paramValue) { 131 132 } 134 135 152 public void addCopyResource(String source, String target, String type) throws CmsConfigurationException { 153 154 if (LOG.isDebugEnabled()) { 155 LOG.debug(Messages.get().getBundle().key( 156 Messages.LOG_ADD_COPY_RESOURCE_4, 157 new Object [] {this, source, target, type})); 158 } 159 160 if (m_frozen) { 161 throw new CmsConfigurationException(Messages.get().container( 163 Messages.ERR_CONFIG_FROZEN_3, 164 this.getClass().getName(), 165 getTypeName(), 166 new Integer (getTypeId()))); 167 } 168 169 CmsConfigurationCopyResource copyResource = new CmsConfigurationCopyResource(source, target, type); 171 m_copyResources.add(copyResource); 172 } 173 174 182 public void addDefaultProperty(CmsProperty property) throws CmsConfigurationException { 183 184 if (LOG.isDebugEnabled()) { 185 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_DFLT_PROP_2, this, property)); 186 } 187 188 if (m_frozen) { 189 throw new CmsConfigurationException(Messages.get().container( 191 Messages.ERR_CONFIG_FROZEN_3, 192 this.getClass().getName(), 193 getTypeName(), 194 new Integer (getTypeId()))); 195 } 196 197 m_defaultProperties.add(property); 198 } 199 200 203 public void addMappingType(String mapping) { 204 205 if (LOG.isDebugEnabled()) { 207 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_MAPPING_TYPE_2, mapping, this)); 208 } 209 if (m_mappings == null) { 210 m_mappings = new ArrayList (); 211 } 212 m_mappings.add(mapping); 213 } 214 215 218 public void changeLastModifiedProjectId(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource) 219 throws CmsException { 220 221 securityManager.changeLastModifiedProjectId(cms.getRequestContext(), resource); 222 } 223 224 227 public void changeLock(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource) throws CmsException { 228 229 securityManager.changeLock(cms.getRequestContext(), resource); 230 } 231 232 235 public void chflags(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int flags) 236 throws CmsException { 237 238 securityManager.chflags(cms.getRequestContext(), resource, flags); 239 } 240 241 244 public void chtype(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int type) 245 throws CmsException { 246 247 securityManager.chtype(cms.getRequestContext(), resource, type); 248 } 249 250 253 public void copyResource( 254 CmsObject cms, 255 CmsSecurityManager securityManager, 256 CmsResource source, 257 String destination, 258 int siblingMode) throws CmsException { 259 260 securityManager.copyResource( 261 cms.getRequestContext(), 262 source, 263 cms.getRequestContext().addSiteRoot(destination), 264 siblingMode); 265 } 266 267 270 public void copyResourceToProject(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource) 271 throws CmsException { 272 273 securityManager.copyResourceToProject(cms.getRequestContext(), resource); 274 } 275 276 279 public CmsResource createResource( 280 CmsObject cms, 281 CmsSecurityManager securityManager, 282 String resourcename, 283 byte[] content, 284 List properties) throws CmsException { 285 286 if (cms.existsResource(resourcename, CmsResourceFilter.IGNORE_EXPIRATION)) { 288 289 int todo_v7; 290 292 throw new CmsVfsResourceAlreadyExistsException(org.opencms.db.generic.Messages.get().container( 293 org.opencms.db.generic.Messages.ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1, 294 resourcename)); 295 } 296 297 CmsMacroResolver resolver = getMacroResolver(cms, resourcename); 299 300 List newProperties = processDefaultProperties(properties, resolver); 302 303 CmsResource result = securityManager.createResource( 304 cms.getRequestContext(), 305 cms.getRequestContext().addSiteRoot(resourcename), 306 getTypeId(), 307 content, 308 newProperties); 309 310 processCopyResources(cms, resourcename, resolver); 312 313 return result; 315 } 316 317 320 public void createSibling( 321 CmsObject cms, 322 CmsSecurityManager securityManager, 323 CmsResource source, 324 String destination, 325 List properties) throws CmsException { 326 327 securityManager.createSibling( 328 cms.getRequestContext(), 329 source, 330 cms.getRequestContext().addSiteRoot(destination), 331 properties); 332 } 333 334 337 public void deleteResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int siblingMode) 338 throws CmsException { 339 340 securityManager.deleteResource(cms.getRequestContext(), resource, siblingMode); 341 } 342 343 346 public String getCachePropertyDefault() { 347 348 return null; 349 } 350 351 356 public String getClassName() { 357 358 return m_className; 359 } 360 361 364 public Map getConfiguration() { 365 366 if (LOG.isDebugEnabled()) { 367 LOG.debug(Messages.get().getBundle().key(Messages.LOG_GET_CONFIGURATION_1, this)); 368 } 369 return null; 370 } 371 372 377 public List getConfiguredCopyResources() { 378 379 return m_copyResources; 380 } 381 382 387 public List getConfiguredDefaultProperties() { 388 389 return m_defaultProperties; 390 } 391 392 395 public List getConfiguredMappings() { 396 397 return m_mappings; 398 } 399 400 403 public abstract int getLoaderId(); 404 405 408 public int getTypeId() { 409 410 return m_typeId; 411 } 412 413 416 public String getTypeName() { 417 418 return m_typeName; 419 } 420 421 424 public CmsResource importResource( 425 CmsObject cms, 426 CmsSecurityManager securityManager, 427 String resourcename, 428 CmsResource resource, 429 byte[] content, 430 List properties) throws CmsException { 431 432 resource.setDateLastModified(resource.getDateLastModified()); 435 resource.setState(CmsResource.STATE_NEW); 437 return securityManager.importResource( 438 cms.getRequestContext(), 439 cms.getRequestContext().addSiteRoot(resourcename), 440 resource, 441 content, 442 properties, 443 true); 444 } 445 446 449 public final void initConfiguration() { 450 451 if (LOG.isDebugEnabled()) { 453 LOG.debug(Messages.get().getBundle().key(Messages.LOG_INIT_CONFIGURATION_1, this)); 454 } 455 } 456 457 470 public void initConfiguration(String name, String id) throws CmsConfigurationException { 471 472 initConfiguration(name, id, this.getClass().getName()); 474 } 475 476 479 public void initConfiguration(String name, String id, String className) throws CmsConfigurationException { 480 481 if (LOG.isDebugEnabled()) { 482 LOG.debug(Messages.get().getBundle().key(Messages.LOG_INIT_CONFIGURATION_3, this, name, id)); 483 484 } 485 486 if (m_frozen) { 487 throw new CmsConfigurationException(org.opencms.configuration.Messages.get().container( 489 org.opencms.file.types.Messages.ERR_CONFIG_FROZEN_3, 490 className, 491 getTypeName(), 492 new Integer (getTypeId()))); 493 } 494 495 m_frozen = true; 497 498 if (name != null) { 500 m_typeName = name; 501 } 502 if (id != null) { 503 m_typeId = Integer.valueOf(id).intValue(); 504 } 505 if (className != null) { 506 m_className = className; 507 } 508 509 if ((getTypeId() < 0) || (getTypeName() == null) || (getClassName() == null)) { 511 throw new CmsConfigurationException(Messages.get().container( 512 Messages.ERR_INVALID_RESTYPE_CONFIG_3, 513 className, 514 m_typeName, 515 new Integer (m_typeId))); 516 } 517 518 m_defaultProperties = Collections.unmodifiableList(m_defaultProperties); 519 m_copyResources = Collections.unmodifiableList(m_copyResources); 520 m_mappings = Collections.unmodifiableList(m_mappings); 521 } 522 523 526 public void initialize(CmsObject cms) { 527 528 if (LOG.isDebugEnabled()) { 530 LOG.debug(Messages.get().getBundle().key(Messages.LOG_INITIALIZE_1, this)); 531 } 532 } 533 534 537 public boolean isAdditionalModuleResourceType() { 538 539 return m_addititionalModuleResourceType; 540 } 541 542 545 public boolean isDirectEditable() { 546 547 return false; 548 } 549 550 553 public boolean isFolder() { 554 555 return false; 556 } 557 558 561 public void lockResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int mode) 562 throws CmsException { 563 564 securityManager.lockResource(cms.getRequestContext(), resource, mode); 565 } 566 567 570 public void moveResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, String destination) 571 throws CmsException { 572 573 String dest = cms.getRequestContext().addSiteRoot(destination); 574 575 if (resource.getRootPath().equals(dest)) { 576 throw new CmsVfsException(org.opencms.file.Messages.get().container( 578 org.opencms.file.Messages.ERR_MOVE_SAME_NAME_1, 579 destination)); 580 } 581 582 securityManager.checkPermissions( 585 cms.getRequestContext(), 586 resource, 587 CmsPermissionSet.ACCESS_WRITE, 588 true, 589 CmsResourceFilter.IGNORE_EXPIRATION); 590 591 boolean isNew = resource.getState() == CmsResource.STATE_NEW; 593 594 copyResource(cms, securityManager, resource, destination, CmsResource.COPY_AS_SIBLING); 595 596 deleteResource(cms, securityManager, resource, CmsResource.DELETE_PRESERVE_SIBLINGS); 597 598 CmsResource destinationResource = securityManager.readResource( 600 cms.getRequestContext(), 601 dest, 602 CmsResourceFilter.ALL); 603 604 if (isNew) { 605 securityManager.lockResource(cms.getRequestContext(), destinationResource, CmsLock.COMMON); 607 } else { 608 securityManager.changeLock(cms.getRequestContext(), destinationResource); 610 } 611 } 612 613 public void removeResourceFromProject(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource) 614 throws CmsException { 615 616 securityManager.removeResourceFromProject(cms.getRequestContext(), resource); 617 } 618 619 622 public void replaceResource( 623 CmsObject cms, 624 CmsSecurityManager securityManager, 625 CmsResource resource, 626 int type, 627 byte[] content, 628 List properties) throws CmsException { 629 630 securityManager.replaceResource(cms.getRequestContext(), resource, type, content, properties); 631 } 632 633 636 public void restoreResourceBackup(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int tag) 637 throws CmsException { 638 639 securityManager.restoreResource(cms.getRequestContext(), resource, tag); 640 } 641 642 645 public void setAdditionalModuleResourceType(boolean additionalType) { 646 647 m_addititionalModuleResourceType = additionalType; 648 } 649 650 653 public void setDateExpired( 654 CmsObject cms, 655 CmsSecurityManager securityManager, 656 CmsResource resource, 657 long dateExpired, 658 boolean recursive) throws CmsException { 659 660 securityManager.setDateExpired(cms.getRequestContext(), resource, dateExpired); 661 } 662 663 666 public void setDateLastModified( 667 CmsObject cms, 668 CmsSecurityManager securityManager, 669 CmsResource resource, 670 long dateLastModified, 671 boolean recursive) throws CmsException { 672 673 securityManager.setDateLastModified(cms.getRequestContext(), resource, dateLastModified); 674 } 675 676 679 public void setDateReleased( 680 CmsObject cms, 681 CmsSecurityManager securityManager, 682 CmsResource resource, 683 long dateReleased, 684 boolean recursive) throws CmsException { 685 686 securityManager.setDateReleased(cms.getRequestContext(), resource, dateReleased); 687 } 688 689 692 public String toString() { 693 694 StringBuffer output = new StringBuffer (); 695 output.append("[ResourceType] class="); 696 output.append(getClass().getName()); 697 output.append(" name="); 698 output.append(getTypeName()); 699 output.append(" id="); 700 output.append(getTypeId()); 701 output.append(" loaderId="); 702 output.append(getLoaderId()); 703 return output.toString(); 704 } 705 706 709 public void undoChanges(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, boolean recursive) 710 throws CmsException { 711 712 securityManager.undoChanges(cms.getRequestContext(), resource); 713 } 714 715 718 public void unlockResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource) 719 throws CmsException { 720 721 securityManager.unlockResource(cms.getRequestContext(), resource); 722 } 723 724 727 public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) 728 throws CmsException, CmsVfsException, CmsSecurityException { 729 730 if (resource.isFile()) { 731 return securityManager.writeFile(cms.getRequestContext(), resource); 732 } 733 throw new CmsVfsException(Messages.get().container( 735 Messages.ERR_WRITE_FILE_IS_FOLDER_1, 736 cms.getSitePath(resource))); 737 } 738 739 742 public void writePropertyObject( 743 CmsObject cms, 744 CmsSecurityManager securityManager, 745 CmsResource resource, 746 CmsProperty property) throws CmsException { 747 748 securityManager.writePropertyObject(cms.getRequestContext(), resource, property); 749 } 750 751 754 public void writePropertyObjects( 755 CmsObject cms, 756 CmsSecurityManager securityManager, 757 CmsResource resource, 758 List properties) throws CmsException { 759 760 securityManager.writePropertyObjects(cms.getRequestContext(), resource, properties); 761 } 762 763 771 protected CmsMacroResolver getMacroResolver(CmsObject cms, String resourcename) { 772 773 CmsMacroResolver result = CmsMacroResolver.newInstance().setCmsObject(cms); 774 if (isFolder() && (!CmsResource.isFolder(resourcename))) { 775 resourcename = resourcename.concat("/"); 777 } 778 result.addMacro(MACRO_RESOURCE_ROOT_PATH, cms.getRequestContext().addSiteRoot(resourcename)); 780 result.addMacro(MACRO_RESOURCE_SITE_PATH, resourcename); 781 result.addMacro(MACRO_RESOURCE_FOLDER_PATH, CmsResource.getFolderPath(resourcename)); 782 result.addMacro(MACRO_RESOURCE_PARENT_PATH, CmsResource.getParentFolder(resourcename)); 783 result.addMacro(MACRO_RESOURCE_NAME, CmsResource.getName(resourcename)); 784 785 return result; 786 } 787 788 798 protected I_CmsResourceType getResourceType(int resourceType) throws CmsException { 799 800 return OpenCms.getResourceManager().getResourceType(resourceType); 801 } 802 803 810 protected void processCopyResources(CmsObject cms, String resourcename, CmsMacroResolver resolver) { 811 812 Iterator i = m_copyResources.iterator(); 813 while (i.hasNext()) { 814 CmsConfigurationCopyResource copyResource = (CmsConfigurationCopyResource)i.next(); 815 816 String target = copyResource.getTarget(); 817 if (copyResource.isTargetWasNull() || CmsMacroResolver.isMacro(target, MACRO_RESOURCE_FOLDER_PATH)) { 818 target = target.concat(CmsResource.getName(copyResource.getSource())); 820 } 821 target = resolver.resolveMacros(target); 823 target = CmsFileUtil.normalizePath(CmsLinkManager.getAbsoluteUri(target, resourcename), '/'); 825 826 try { 827 cms.copyResource(copyResource.getSource(), target, copyResource.getType()); 828 } catch (Exception e) { 829 if (LOG.isDebugEnabled()) { 832 LOG.debug(Messages.get().getBundle().key( 834 Messages.LOG_PROCESS_COPY_RESOURCES_3, 835 resourcename, 836 copyResource, 837 target), e); 838 } else { 839 LOG.error(Messages.get().getBundle().key( 840 Messages.LOG_PROCESS_COPY_RESOURCES_3, 841 resourcename, 842 copyResource, 843 target)); 844 } 845 } 846 } 847 } 848 849 860 protected List processDefaultProperties(List properties, CmsMacroResolver resolver) { 861 862 if ((m_defaultProperties == null) || (m_defaultProperties.size() == 0)) { 863 return properties; 865 } 866 867 ArrayList result = new ArrayList (); 870 Iterator i = m_defaultProperties.iterator(); 871 872 while (i.hasNext()) { 873 CmsProperty property = (CmsProperty)((CmsProperty)i.next()).clone(); 875 876 if (property.getResourceValue() != null) { 878 property.setResourceValue(resolver.resolveMacros(property.getResourceValue())); 879 } 880 if (property.getStructureValue() != null) { 881 property.setStructureValue(resolver.resolveMacros(property.getStructureValue())); 882 } 883 884 result.add(property); 886 } 887 888 if (properties != null) { 890 result.addAll(properties); 891 } 892 893 return result; 895 } 896 } | Popular Tags |