1 23 24 package org.apache.slide.common; 25 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.Vector ; 29 import org.apache.slide.content.ContentInterceptor; 30 import org.apache.slide.content.NodeProperty; 31 import org.apache.slide.structure.ActionNode; 32 import org.apache.slide.structure.LinkNode; 33 import org.apache.slide.structure.ObjectNode; 34 import org.apache.slide.structure.SubjectNode; 35 import org.apache.slide.util.Messages; 36 import org.apache.slide.util.conf.Configuration; 37 import org.apache.slide.util.conf.ConfigurationException; 38 import org.apache.slide.util.logger.Logger; 39 40 45 public final class NamespaceConfig { 46 47 48 50 51 54 public static final String NOBODY = "nobody"; 55 public static final String NOBODY_ROLE = ObjectNode.class.getName(); 56 public static final String BASE_ROLE = SubjectNode.class.getName(); 57 public static final String ACTION_ROLE = ActionNode.class.getName(); 58 public static final String LINK_ROLE = LinkNode.class.getName(); 59 60 61 private final static String 62 ACL_INHERIT_TYPE = "acl_inheritance_type", 63 NESTED_ROLES_MAXDEPTH = "nested_roles_maxdepth"; 64 65 private final static int 66 NESTED_ROLES_MAXDEPTH_DEFAULT = 0; 67 68 public final static int 69 ACL_INHERIT_TYPE_NONE = 0, 70 ACL_INHERIT_TYPE_ROOT = 1, 71 ACL_INHERIT_TYPE_PATH = 2, 72 ACL_INHERIT_TYPE_FULL = 3; 73 74 75 77 78 81 protected String name; 82 83 84 87 protected ActionNode readObjectAction; 88 89 90 93 protected ActionNode createObjectAction; 94 95 96 99 protected ActionNode removeObjectAction; 100 101 102 105 protected ActionNode grantPermissionAction; 106 107 108 111 protected ActionNode revokePermissionAction; 112 113 114 117 protected ActionNode readPermissionsAction; 118 protected ActionNode readOwnPermissionsAction; 119 120 121 124 protected ActionNode lockObjectAction; 125 126 127 130 protected ActionNode killLockAction; 131 132 133 136 protected ActionNode readLocksAction; 137 138 139 142 protected ActionNode createRevisionMetadataAction; 143 144 145 148 protected ActionNode modifyRevisionMetadataAction; 149 150 151 154 protected ActionNode removeRevisionMetadataAction; 155 156 157 160 protected ActionNode readRevisionMetadataAction; 161 162 163 166 protected ActionNode readRevisionContentAction; 167 168 169 172 protected ActionNode createRevisionContentAction; 173 174 175 178 protected ActionNode modifyRevisionContentAction; 179 180 181 184 protected ActionNode removeRevisionContentAction; 185 186 187 190 protected ActionNode bindMemberAction; 191 protected ActionNode unbindMemberAction; 192 193 194 197 protected String usersPath = null; 198 protected String groupsPath = null; 199 protected String rolesPath = null; 200 201 private UriPath 202 usersUriPath, 203 groupsUriPath, 204 rolesUriPath; 205 206 207 210 protected String guestPath; 211 212 213 216 protected String filesPath = null; 217 218 219 222 protected String actionsPath = null; 223 224 225 228 protected Hashtable parameters; 229 230 231 235 protected Hashtable roleMappings = new Hashtable (); 236 237 238 243 protected Hashtable roleClassMappings = new Hashtable (); 244 245 246 250 protected Hashtable defaultProperties; 251 252 253 256 protected static Vector emptyVector = new Vector (); 257 258 259 262 protected ContentInterceptor[] contentInterceptors 263 = new ContentInterceptor[0]; 264 265 268 protected boolean autoCreateUsers = false; 269 270 271 274 protected String autoCreateUsersRole = "slideroles.basic.UserRoleImpl"; 275 276 277 279 280 285 public ActionNode getDefaultAction() { 286 return ActionNode.DEFAULT; 287 } 288 289 290 295 public ActionNode getReadObjectAction() { 296 return readObjectAction; 297 } 298 299 300 305 public ActionNode getCreateObjectAction() { 306 return createObjectAction; 307 } 308 309 310 315 public ActionNode getRemoveObjectAction() { 316 return removeObjectAction; 317 } 318 319 320 325 public ActionNode getGrantPermissionAction() { 326 return grantPermissionAction; 327 } 328 329 330 335 public ActionNode getRevokePermissionAction() { 336 return revokePermissionAction; 337 } 338 339 340 345 public ActionNode getReadPermissionsAction() { 346 return readPermissionsAction; 347 } 348 public ActionNode getReadOwnPermissionsAction() { 349 return readOwnPermissionsAction; 350 } 351 352 353 358 public ActionNode getLockObjectAction() { 359 return lockObjectAction; 360 } 361 362 363 368 public ActionNode getKillLockAction() { 369 return killLockAction; 370 } 371 372 373 378 public ActionNode getReadLocksAction() { 379 return readLocksAction; 380 } 381 382 383 388 public ActionNode getCreateRevisionMetadataAction() { 389 return createRevisionMetadataAction; 390 } 391 392 393 398 public ActionNode getReadRevisionMetadataAction() { 399 return readRevisionMetadataAction; 400 } 401 402 403 408 public ActionNode getModifyRevisionMetadataAction() { 409 return modifyRevisionMetadataAction; 410 } 411 412 413 418 public ActionNode getRemoveRevisionMetadataAction() { 419 return removeRevisionMetadataAction; 420 } 421 422 423 428 public ActionNode getReadRevisionContentAction() { 429 return readRevisionContentAction; 430 } 431 432 433 438 public ActionNode getCreateRevisionContentAction() { 439 return createRevisionContentAction; 440 } 441 442 443 448 public ActionNode getModifyRevisionContentAction() { 449 return modifyRevisionContentAction; 450 } 451 452 453 458 public ActionNode getRemoveRevisionContentAction() { 459 return removeRevisionContentAction; 460 } 461 462 463 468 public ActionNode getBindMemberAction() { 469 return bindMemberAction; 470 } 471 472 473 public ActionNode getUnbindMemberAction() { 474 return unbindMemberAction; 475 } 476 477 482 public String getUsersPath() { 483 return usersPath; 484 } 485 486 491 public String getGroupsPath() { 492 return groupsPath; 493 } 494 495 500 public String getRolesPath() { 501 return rolesPath; 502 } 503 504 505 511 public String getGuestPath() { 512 return guestPath; 513 } 514 515 516 521 public String getFilesPath() { 522 return filesPath; 523 } 524 525 526 531 public String getActionsPath() { 532 return actionsPath; 533 } 534 535 536 541 public Enumeration getDefaultProperties(String role) { 542 Vector result = (Vector ) defaultProperties.get(role); 543 if (result == null) 544 return emptyVector.elements(); 545 else 546 return result.elements(); 547 } 548 549 550 553 ContentInterceptor[] getContentInterceptors() { 554 return contentInterceptors; 555 } 556 557 558 560 561 567 public String getParameter(String name) { 568 Object result = parameters.get(name); 569 if (result==null) { 570 return null; 571 } else { 572 return (String ) result; 573 } 574 } 575 576 577 584 public String getRoleMapping(String name) { 585 Object result = roleMappings.get(name); 586 if (result == null) { 587 return (String ) roleClassMappings.get(name); 588 } else { 589 return (String ) result; 590 } 591 } 592 593 594 597 public boolean isAutoCreateUsers() { 598 return autoCreateUsers; 599 } 600 601 public boolean isPrincipal(String uri) { 602 UriPath uriPath = new UriPath(uri); 603 return (usersUriPath != null && usersUriPath.equals(uriPath.parent()) || 604 rolesUriPath != null && rolesUriPath.equals(uriPath.parent()) || 605 groupsUriPath != null && groupsUriPath.equals(uriPath.parent())); 606 } 607 608 public boolean isRole(String uri) { 609 UriPath uriPath = new UriPath(uri); 610 return (rolesUriPath != null && rolesUriPath.equals(uriPath.parent())); 611 } 612 613 public boolean isGroup(String uri) { 614 UriPath uriPath = new UriPath(uri); 615 return (groupsUriPath != null && groupsUriPath.equals(uriPath.parent())); 616 } 617 618 619 623 public String getAutoCreateUsersRole() { 624 return autoCreateUsersRole; 625 } 626 627 public int getAclInheritanceType() { 628 String aclInheritanceTypeStr = getParameter(ACL_INHERIT_TYPE); 629 if ("none".equalsIgnoreCase(aclInheritanceTypeStr)) { 630 return ACL_INHERIT_TYPE_NONE; 631 } 632 else if ("root".equalsIgnoreCase(aclInheritanceTypeStr)) { 633 return ACL_INHERIT_TYPE_ROOT; 634 } 635 else if ("path".equalsIgnoreCase(aclInheritanceTypeStr)) { 636 return ACL_INHERIT_TYPE_PATH; 637 } 638 else if ("full".equalsIgnoreCase(aclInheritanceTypeStr)) { 639 return ACL_INHERIT_TYPE_FULL; 640 } 641 else { 642 return ACL_INHERIT_TYPE_PATH; 643 } 644 } 645 646 public int getNestedRolesMaxDepth() { 647 int result = NESTED_ROLES_MAXDEPTH_DEFAULT; 648 String nestedRolesMaxDepthStr = getParameter(NESTED_ROLES_MAXDEPTH); 649 try { 650 result = Integer.parseInt(nestedRolesMaxDepthStr); 651 } 652 catch (NumberFormatException e) {} 653 return result; 654 } 655 656 658 659 669 void initializeNamespaceConfig(Namespace namespace, Configuration config) 670 throws InvalidNamespaceConfigurationException, SlideException { 671 672 name = namespace.getName(); 673 674 readObjectAction = getConfiguredNode(namespace, config, "read-object"); 675 createObjectAction = getConfiguredNode(namespace, config, "create-object"); 676 removeObjectAction = getConfiguredNode(namespace, config, "remove-object"); 677 grantPermissionAction = getConfiguredNode(namespace, config, "grant-permission"); 678 revokePermissionAction = getConfiguredNode(namespace, config, "revoke-permission"); 679 readPermissionsAction = getConfiguredNode(namespace, config, "read-permissions"); 680 readOwnPermissionsAction = getConfiguredNode(namespace, config, "read-own-permissions"); 681 lockObjectAction = getConfiguredNode(namespace, config, "lock-object"); 682 killLockAction = getConfiguredNode(namespace, config, "kill-lock"); 683 readLocksAction = getConfiguredNode(namespace, config, "read-locks"); 684 readRevisionMetadataAction = getConfiguredNode(namespace, config, "read-revision-metadata"); 685 createRevisionMetadataAction = getConfiguredNode(namespace, config, "create-revision-metadata"); 686 modifyRevisionMetadataAction = getConfiguredNode(namespace, config, "modify-revision-metadata"); 687 removeRevisionMetadataAction = getConfiguredNode(namespace, config, "remove-revision-metadata"); 688 readRevisionContentAction = getConfiguredNode(namespace, config, "read-revision-content"); 689 createRevisionContentAction = getConfiguredNode(namespace, config, "create-revision-content"); 690 modifyRevisionContentAction = getConfiguredNode(namespace, config, "modify-revision-content"); 691 removeRevisionContentAction = getConfiguredNode(namespace, config, "remove-revision-content"); 692 693 bindMemberAction = getConfiguredNode(namespace, config, "bind-member"); 694 unbindMemberAction = getConfiguredNode(namespace, config, "unbind-member"); 695 696 697 setPathsAndConfigValues(config); 698 699 setParameters(config, namespace); 700 701 setRoles(config, namespace); 702 703 setDefaultProperties(config, namespace); 704 705 Enumeration contentInteceptorsDef = 706 config.getConfigurations("content-interceptor"); 707 try { 708 while (contentInteceptorsDef.hasMoreElements()) { 709 Configuration contentInterceptorDef = 710 (Configuration) contentInteceptorsDef.nextElement(); 711 String classname = contentInterceptorDef.getAttribute("class"); 712 713 Enumeration contentInterceptorParametersDef = 715 contentInterceptorDef.getConfigurations("parameter"); 716 Hashtable contentInterceptorParameters = new Hashtable (); 717 while (contentInterceptorParametersDef.hasMoreElements()) { 718 Configuration parameterDefinition = (Configuration) 719 contentInterceptorParametersDef.nextElement(); 720 String parameterName = 721 parameterDefinition.getAttribute("name"); 722 String parameterValue = parameterDefinition.getValue(); 723 contentInterceptorParameters.put(parameterName, 724 parameterValue); 725 } 726 727 try { 728 Class contentInterceptorClass = 729 Class.forName(classname); 730 ContentInterceptor contentInterceptor = 731 (ContentInterceptor) 732 contentInterceptorClass.newInstance(); 733 contentInterceptor.setParameters(contentInterceptorParameters); 734 ContentInterceptor[] tempArray = 735 new ContentInterceptor[contentInterceptors.length + 1]; 736 for (int i = 0; i < contentInterceptors.length; i++) { 737 tempArray[i] = contentInterceptors[i]; 738 } 739 tempArray[contentInterceptors.length] = contentInterceptor; 740 contentInterceptors = tempArray; 741 } catch (Exception e) { 742 namespace.getLogger().log 743 (Messages.format 744 ("org.apache.slide.common.InvalidContentInterceptor", 745 classname, e.getMessage()), Logger.WARNING); 746 } 747 } 748 749 NamespaceAccessToken nat = new NamespaceAccessTokenImpl(namespace); 752 ContentInterceptor[] contentInterceptors = 753 namespace.getContentInterceptors(); 754 for (int i = 0; i < contentInterceptors.length; i++) { 755 contentInterceptors[i].setNamespace(nat); 756 } 757 } catch (ConfigurationException e) { 758 throw new InvalidNamespaceConfigurationException 759 (namespace, e.getMessage()); 760 } 761 762 } 763 764 private void setDefaultProperties(Configuration config, Namespace namespace) throws InvalidNamespaceConfigurationException { 765 defaultProperties = new Hashtable (); 766 Enumeration defaultPropertiesDef = 767 config.getConfigurations("default-property"); 768 try { 769 while (defaultPropertiesDef.hasMoreElements()) { 770 Configuration defaultProperty = 771 (Configuration) defaultPropertiesDef.nextElement(); 772 String name = defaultProperty.getAttribute("name"); 773 String value = defaultProperty.getAttribute("value", ""); 774 String propertyNamespace = defaultProperty.getAttribute 775 ("namespace", NodeProperty.DEFAULT_NAMESPACE); 776 String role = defaultProperty.getAttribute("role"); 777 addDefaultProperty(role, name, value, propertyNamespace); 778 } 779 } catch (ConfigurationException e) { 780 throw new InvalidNamespaceConfigurationException 781 (namespace, e.getMessage()); 782 } 783 } 784 785 private void setParameters(Configuration config, Namespace namespace) throws InvalidNamespaceConfigurationException { 786 parameters = new Hashtable (); 787 Enumeration parametersDef = config.getConfigurations("parameter"); 788 try { 789 while (parametersDef.hasMoreElements()) { 790 Configuration parameter = 791 (Configuration) parametersDef.nextElement(); 792 addParameter(parameter.getAttribute("name"), 793 parameter.getValue()); 794 } 795 } catch (ConfigurationException e) { 796 throw new InvalidNamespaceConfigurationException 797 (namespace, e.getMessage()); 798 } 799 } 800 801 private ActionNode getConfiguredNode(Namespace namespace, Configuration config, String nodeName) throws SlideException { 802 ActionNode result = null; 803 try { 804 result = getActionNode(namespace, config.getConfiguration( nodeName).getValue()); 805 } catch (ConfigurationException e) { 806 result = getDefaultAction(); 807 } 808 return result; 809 } 810 811 private void setPathsAndConfigValues(Configuration config) { 812 try { 813 usersPath = config.getConfiguration("userspath").getValue(); 814 usersUriPath = new UriPath(usersPath); 815 } catch (ConfigurationException e) { 816 usersPath = ""; 817 } 818 819 try { 820 groupsPath = config.getConfiguration("groupspath").getValue(); 821 groupsUriPath = new UriPath(groupsPath); 822 } catch (ConfigurationException e) { 823 groupsPath = ""; 824 } 825 826 try { 827 rolesPath = config.getConfiguration("rolespath").getValue(); 828 rolesUriPath = new UriPath(rolesPath); 829 } catch (ConfigurationException e) { 830 rolesPath = ""; 831 } 832 833 try { 834 guestPath = config.getConfiguration("guestpath").getValue(); 835 } catch (ConfigurationException e) { 836 guestPath = ""; 837 } 838 839 try { 840 filesPath = config.getConfiguration("filespath").getValue(); 841 } catch (ConfigurationException e) { 842 filesPath = ""; 843 } 844 845 try { 846 actionsPath = config.getConfiguration("actionspath").getValue(); 847 } catch (ConfigurationException e) { 848 actionsPath = ""; 849 } 850 851 try { 852 autoCreateUsers = Boolean.valueOf 853 (config.getConfiguration("auto-create-users").getValue()) 854 .booleanValue(); 855 } catch (ConfigurationException e) { 856 autoCreateUsers = false; 857 } 858 859 try { 860 autoCreateUsersRole = 861 config.getConfiguration("auto-create-users-role").getValue(); 862 } catch (ConfigurationException e) { 863 } 864 } 865 866 867 877 void initializeNamespaceParameters 878 (Namespace namespace, Configuration config) 879 throws InvalidNamespaceConfigurationException, SlideException { 880 881 882 setParameters(config, namespace); 883 884 setPathsAndConfigValues(config); 885 886 setRoles(config, namespace); 887 888 setDefaultProperties(config, namespace); 889 890 } 891 892 private void setRoles(Configuration config, Namespace namespace) throws InvalidNamespaceConfigurationException { 893 addRoleMapping(NOBODY, NOBODY_ROLE); 895 addRoleMapping(NOBODY, LINK_ROLE); 896 addRoleMapping(NOBODY, ACTION_ROLE); 897 addRoleMapping(NOBODY, BASE_ROLE); 899 Enumeration roleMappingsDef = config.getConfigurations("role"); 900 try { 901 while (roleMappingsDef.hasMoreElements()) { 902 Configuration roleMappingDef = 903 (Configuration) roleMappingsDef.nextElement(); 904 addRoleMapping(roleMappingDef.getAttribute("name"), 905 roleMappingDef.getValue()); 906 } 907 } catch (ConfigurationException e) { 908 throw new InvalidNamespaceConfigurationException 909 (namespace, e.getMessage()); 910 } 911 } 912 913 914 921 void initializeAsDummyConfig(Namespace namespace) 922 throws InvalidNamespaceConfigurationException { 923 924 name = namespace.getName(); 925 926 try { 927 readObjectAction = getDefaultAction(); 928 createObjectAction = getDefaultAction(); 929 removeObjectAction = getDefaultAction(); 930 grantPermissionAction = getDefaultAction(); 931 revokePermissionAction = getDefaultAction(); 932 readPermissionsAction = getDefaultAction(); 933 readOwnPermissionsAction = getDefaultAction(); 934 lockObjectAction = getDefaultAction(); 935 killLockAction = getDefaultAction(); 936 readLocksAction = getDefaultAction(); 937 readRevisionMetadataAction = getDefaultAction(); 938 createRevisionMetadataAction = getDefaultAction(); 939 modifyRevisionMetadataAction = getDefaultAction(); 940 removeRevisionMetadataAction = getDefaultAction(); 941 readRevisionContentAction = getDefaultAction(); 942 createRevisionContentAction = getDefaultAction(); 943 modifyRevisionContentAction = getDefaultAction(); 944 removeRevisionContentAction = getDefaultAction(); 945 bindMemberAction = getDefaultAction(); 946 unbindMemberAction = getDefaultAction(); 947 } catch (Exception e) { 948 throw new InvalidNamespaceConfigurationException 949 (namespace, e.getMessage()); 950 } 951 } 952 953 954 957 public void initializeForTestPurposesOnly() 958 throws InvalidNamespaceConfigurationException { 959 960 name = "slide"; 961 962 try { 963 readObjectAction = getDefaultAction(); 964 createObjectAction = getDefaultAction(); 965 removeObjectAction = getDefaultAction(); 966 grantPermissionAction = getDefaultAction(); 967 revokePermissionAction = getDefaultAction(); 968 readPermissionsAction = getDefaultAction(); 969 readOwnPermissionsAction = getDefaultAction(); 970 lockObjectAction = getDefaultAction(); 971 killLockAction = getDefaultAction(); 972 readLocksAction = getDefaultAction(); 973 readRevisionMetadataAction = getDefaultAction(); 974 createRevisionMetadataAction = getDefaultAction(); 975 modifyRevisionMetadataAction = getDefaultAction(); 976 removeRevisionMetadataAction = getDefaultAction(); 977 readRevisionContentAction = getDefaultAction(); 978 createRevisionContentAction = getDefaultAction(); 979 modifyRevisionContentAction = getDefaultAction(); 980 removeRevisionContentAction = getDefaultAction(); 981 bindMemberAction = getDefaultAction(); 982 unbindMemberAction = getDefaultAction(); 983 } catch (Exception e) { 984 throw new InvalidNamespaceConfigurationException 985 (null, e.getMessage()); 986 } 987 } 988 989 990 992 993 999 protected void addParameter(String name, String value) { 1000 if ((name!=null) && (value!=null)) { 1001 parameters.put(name, value); 1002 } 1003 } 1004 1005 1006 1014 protected void addDefaultProperty(String role, String name, String value, 1015 String namespace) { 1016 if ((role!=null) && (name!=null) && (value!=null)) { 1017 Vector currentDefaultProperties = 1018 (Vector ) defaultProperties.get(role); 1019 if (currentDefaultProperties == null) { 1020 currentDefaultProperties = new Vector (); 1021 defaultProperties.put(role, currentDefaultProperties); 1022 } 1023 currentDefaultProperties.addElement 1024 (new NodeProperty(name, value, namespace)); 1025 } 1026 } 1027 1028 1029 1035 protected void addRoleMapping(String name, String value) { 1036 if ((name!=null) && (value!=null)) { 1037 roleMappings.put(name, value); 1038 roleClassMappings.put(value, name); 1039 } 1040 } 1041 1042 1043 1049 protected ActionNode getActionNode(Namespace namespace, String actionPath) 1050 throws InvalidNamespaceConfigurationException, SlideException { 1051 1052 ActionNode result = null; 1053 1054 if (actionPath != null) { 1055 Uri actionUri = namespace.getUri(actionPath); 1056 result = (ActionNode) actionUri.getStore() 1057 .retrieveObject(actionUri); 1058 } 1059 else { 1060 result = getDefaultAction(); 1061 } 1062 1063 return result; 1064 1065 } 1066 1067 1068 1070 1071 1074 public String toString() { 1075 return name; 1076 } 1077 1078 1079} 1080 | Popular Tags |