home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / AdminHelper.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  14.9 KB  |  484 lines

  1. package org.apache.cocoon.samples.slide;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Date;
  6. import java.util.Enumeration;
  7. import java.util.List;
  8. import org.apache.slide.authenticate.CredentialsToken;
  9. import org.apache.slide.common.NamespaceAccessToken;
  10. import org.apache.slide.common.SlideToken;
  11. import org.apache.slide.common.SlideTokenImpl;
  12. import org.apache.slide.content.Content;
  13. import org.apache.slide.content.NodeProperty;
  14. import org.apache.slide.content.NodeRevisionContent;
  15. import org.apache.slide.content.NodeRevisionDescriptor;
  16. import org.apache.slide.content.NodeRevisionDescriptors;
  17. import org.apache.slide.lock.Lock;
  18. import org.apache.slide.lock.NodeLock;
  19. import org.apache.slide.macro.Macro;
  20. import org.apache.slide.macro.MacroParameters;
  21. import org.apache.slide.security.NodePermission;
  22. import org.apache.slide.security.Security;
  23. import org.apache.slide.structure.ObjectNode;
  24. import org.apache.slide.structure.ObjectNotFoundException;
  25. import org.apache.slide.structure.Structure;
  26. import org.apache.slide.structure.SubjectNode;
  27.  
  28. public class AdminHelper {
  29.    private static final SlideToken ROOT = new SlideTokenImpl(new CredentialsToken("root"));
  30.  
  31.    public static boolean login(NamespaceAccessToken nat, String userId, String password) throws Exception {
  32.       String usersPath = nat.getNamespaceConfig().getUsersPath();
  33.       String userUri = usersPath + "/" + userId;
  34.       Content content = nat.getContentHelper();
  35.  
  36.       try {
  37.          NodeRevisionDescriptors revisions = content.retrieve(ROOT, userUri);
  38.          NodeRevisionDescriptor revision = content.retrieve(ROOT, revisions);
  39.          NodeProperty property = revision.getProperty("password", "http://jakarta.apache.org/slide/");
  40.          return property.getValue().equals(password);
  41.       } catch (Exception e) {
  42.          e.printStackTrace();
  43.          throw e;
  44.       }
  45.    }
  46.  
  47.    public static void addUser(NamespaceAccessToken nat, String caller, String username, String password) throws Exception {
  48.       String usersPath = nat.getNamespaceConfig().getUsersPath();
  49.       String userUri = usersPath + "/" + username;
  50.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  51.       Structure structure = nat.getStructureHelper();
  52.       Content content = nat.getContentHelper();
  53.  
  54.       try {
  55.          nat.begin();
  56.          ObjectNode user = new SubjectNode();
  57.          structure.create(slideToken, user, userUri);
  58.          NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
  59.          descriptor.setCreationDate(new Date());
  60.          descriptor.setLastModified(new Date());
  61.          descriptor.setProperty(new NodeProperty("password", password, "http://jakarta.apache.org/slide/"));
  62.          content.create(slideToken, userUri, descriptor, (NodeRevisionContent)null);
  63.          nat.commit();
  64.       } catch (Exception e) {
  65.          try {
  66.             nat.rollback();
  67.          } catch (Exception f) {
  68.             f.printStackTrace();
  69.          }
  70.  
  71.          throw e;
  72.       }
  73.    }
  74.  
  75.    public static void addGroup(NamespaceAccessToken nat, String caller, String groupname) throws Exception {
  76.       String groupsPath = nat.getNamespaceConfig().getGroupsPath();
  77.       String groupUri = groupsPath + "/" + groupname;
  78.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  79.       Structure structure = nat.getStructureHelper();
  80.       Content content = nat.getContentHelper();
  81.  
  82.       try {
  83.          nat.begin();
  84.          ObjectNode group = new SubjectNode();
  85.          structure.create(slideToken, group, groupUri);
  86.          NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
  87.          descriptor.setCreationDate(new Date());
  88.          descriptor.setLastModified(new Date());
  89.          content.create(slideToken, groupUri, descriptor, (NodeRevisionContent)null);
  90.          nat.commit();
  91.       } catch (Exception e) {
  92.          try {
  93.             nat.rollback();
  94.          } catch (Exception f) {
  95.             f.printStackTrace();
  96.          }
  97.  
  98.          throw e;
  99.       }
  100.    }
  101.  
  102.    public static void addRole(NamespaceAccessToken nat, String caller, String rolename) throws Exception {
  103.       String rolesPath = nat.getNamespaceConfig().getRolesPath();
  104.       String roleUri = rolesPath + "/" + rolename;
  105.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  106.       Structure structure = nat.getStructureHelper();
  107.       Content content = nat.getContentHelper();
  108.  
  109.       try {
  110.          nat.begin();
  111.          ObjectNode role = new SubjectNode();
  112.          structure.create(slideToken, role, roleUri);
  113.          NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
  114.          descriptor.setCreationDate(new Date());
  115.          descriptor.setLastModified(new Date());
  116.          content.create(slideToken, roleUri, descriptor, (NodeRevisionContent)null);
  117.          nat.commit();
  118.       } catch (Exception e) {
  119.          try {
  120.             nat.rollback();
  121.          } catch (Exception f) {
  122.             f.printStackTrace();
  123.          }
  124.  
  125.          throw e;
  126.       }
  127.    }
  128.  
  129.    public static void removeObject(NamespaceAccessToken nat, String caller, String objectUri) throws Exception {
  130.       String usersPath = nat.getNamespaceConfig().getUsersPath();
  131.       String callerUri = usersPath + "/" + caller;
  132.       if (!callerUri.equals(objectUri)) {
  133.          SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  134.          Macro macro = nat.getMacroHelper();
  135.  
  136.          try {
  137.             nat.begin();
  138.             boolean recursive = true;
  139.             boolean overwrite = false;
  140.             MacroParameters parameters = new MacroParameters(recursive, overwrite);
  141.             macro.delete(slideToken, objectUri, parameters);
  142.             nat.commit();
  143.          } catch (Exception e) {
  144.             try {
  145.                nat.rollback();
  146.             } catch (Exception f) {
  147.                f.printStackTrace();
  148.             }
  149.  
  150.             throw e;
  151.          }
  152.       }
  153.    }
  154.  
  155.    public static void addMember(NamespaceAccessToken nat, String caller, String objectUri, String subjectUri) throws Exception {
  156.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  157.       Structure structure = nat.getStructureHelper();
  158.       Content content = nat.getContentHelper();
  159.  
  160.       try {
  161.          structure.retrieve(slideToken, subjectUri);
  162.          NodeRevisionDescriptors descriptors = content.retrieve(slideToken, objectUri);
  163.          NodeRevisionDescriptor descriptor = content.retrieve(slideToken, descriptors);
  164.          NodeProperty property = descriptor.getProperty("group-member-set", "DAV:");
  165.          String value = null;
  166.          if (property != null) {
  167.             value = (String)property.getValue();
  168.             if (value.indexOf(subjectUri) != -1) {
  169.                return;
  170.             }
  171.          } else {
  172.             value = "";
  173.          }
  174.  
  175.          value = value + "<D:href xmlns:D='DAV:'>" + subjectUri + "</D:href>";
  176.          descriptor.setProperty("group-member-set", "DAV:", value);
  177.          nat.begin();
  178.          content.store(slideToken, objectUri, descriptor, (NodeRevisionContent)null);
  179.          nat.commit();
  180.       } catch (ObjectNotFoundException var12) {
  181.       } catch (Exception e) {
  182.          try {
  183.             nat.rollback();
  184.          } catch (Exception f) {
  185.             f.printStackTrace();
  186.          }
  187.  
  188.          throw e;
  189.       }
  190.  
  191.    }
  192.  
  193.    public static void removeMember(NamespaceAccessToken nat, String caller, String objectUri, String subjectUri) throws Exception {
  194.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  195.       Content content = nat.getContentHelper();
  196.  
  197.       try {
  198.          NodeRevisionDescriptors revisions = content.retrieve(slideToken, objectUri);
  199.          NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions);
  200.          NodeProperty property = revision.getProperty("group-member-set", "DAV:");
  201.          if (property == null) {
  202.             return;
  203.          }
  204.  
  205.          String value = (String)property.getValue();
  206.          int index = value.indexOf(subjectUri);
  207.          if (index == -1) {
  208.             return;
  209.          }
  210.  
  211.          int end = index + subjectUri.length();
  212.  
  213.          do {
  214.             ++end;
  215.          } while(value.charAt(end) != '>');
  216.  
  217.          int from = index;
  218.  
  219.          do {
  220.             --from;
  221.          } while(value.charAt(from) != '<');
  222.  
  223.          String before = value.substring(0, from);
  224.          String after = value.substring(end + 1);
  225.          value = before + after;
  226.          revision.setProperty("group-member-set", "DAV:", value);
  227.          nat.begin();
  228.          content.store(slideToken, objectUri, revision, (NodeRevisionContent)null);
  229.          nat.commit();
  230.       } catch (ObjectNotFoundException var16) {
  231.       } catch (Exception e) {
  232.          try {
  233.             nat.rollback();
  234.          } catch (Exception f) {
  235.             f.printStackTrace();
  236.          }
  237.  
  238.          throw e;
  239.       }
  240.  
  241.    }
  242.  
  243.    public static void changePassword(NamespaceAccessToken nat, String caller, String username, String password) throws Exception {
  244.       String usersPath = nat.getNamespaceConfig().getUsersPath();
  245.       String userUri = usersPath + "/" + username;
  246.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  247.       Content content = nat.getContentHelper();
  248.  
  249.       try {
  250.          nat.begin();
  251.          NodeRevisionDescriptors revisions = content.retrieve(slideToken, userUri);
  252.          NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions);
  253.          revision.setLastModified(new Date());
  254.          revision.setProperty(new NodeProperty("password", password, "http://jakarta.apache.org/slide/"));
  255.          content.store(slideToken, userUri, revision, (NodeRevisionContent)null);
  256.          nat.commit();
  257.       } catch (Exception e) {
  258.          try {
  259.             nat.rollback();
  260.          } catch (Exception f) {
  261.             f.printStackTrace();
  262.          }
  263.  
  264.          throw e;
  265.       }
  266.    }
  267.  
  268.    public static List listPermissions(NamespaceAccessToken nat, String caller, String path) throws Exception {
  269.       String uri = getUriFromPath(nat, path);
  270.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  271.       Security security = nat.getSecurityHelper();
  272.       List result = new ArrayList();
  273.  
  274.       try {
  275.          nat.begin();
  276.          Enumeration permissions = security.enumeratePermissions(slideToken, uri, false);
  277.  
  278.          while(permissions.hasMoreElements()) {
  279.             result.add(permissions.nextElement());
  280.          }
  281.  
  282.          nat.commit();
  283.          return result;
  284.       } catch (Exception e) {
  285.          try {
  286.             nat.rollback();
  287.          } catch (Exception f) {
  288.             f.printStackTrace();
  289.          }
  290.  
  291.          throw e;
  292.       }
  293.    }
  294.  
  295.    public static List listLocks(NamespaceAccessToken nat, String caller, String path) throws Exception {
  296.       String uri = getUriFromPath(nat, path);
  297.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  298.       Lock lock = nat.getLockHelper();
  299.       List result = new ArrayList();
  300.  
  301.       try {
  302.          nat.begin();
  303.          Enumeration locks = lock.enumerateLocks(slideToken, uri, false);
  304.  
  305.          while(locks.hasMoreElements()) {
  306.             result.add(locks.nextElement());
  307.          }
  308.  
  309.          nat.commit();
  310.          return result;
  311.       } catch (Exception e) {
  312.          try {
  313.             nat.rollback();
  314.          } catch (Exception f) {
  315.             f.printStackTrace();
  316.          }
  317.  
  318.          throw e;
  319.       }
  320.    }
  321.  
  322.    public static List listGroups(NamespaceAccessToken nat, String caller, String path) throws Exception {
  323.       List result = new ArrayList();
  324.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  325.       Structure structure = nat.getStructureHelper();
  326.       Content content = nat.getContentHelper();
  327.       ObjectNode object = structure.retrieve(slideToken, path);
  328.  
  329.       String uri;
  330.       List members;
  331.       for(Enumeration enum = structure.getChildren(slideToken, object); enum.hasMoreElements(); result.add(new Group(uri, members, (1)null))) {
  332.          uri = ((ObjectNode)enum.nextElement()).getUri();
  333.          NodeRevisionDescriptors revisions = content.retrieve(slideToken, uri);
  334.          NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions);
  335.          NodeProperty property = revision.getProperty("group-member-set", "DAV:");
  336.          if (property != null) {
  337.             String value = (String)property.getValue();
  338.             members = new ArrayList(10);
  339.             int start = value.indexOf(62);
  340.  
  341.             int end;
  342.             for(end = 0; start != -1; start = value.indexOf(62, end + 1)) {
  343.                end = value.indexOf(60, start);
  344.                if (end != -1) {
  345.                   members.add(value.substring(start + 1, end));
  346.                }
  347.  
  348.                end = value.indexOf(62, start + 1);
  349.             }
  350.          } else {
  351.             members = Collections.EMPTY_LIST;
  352.          }
  353.       }
  354.  
  355.       return result;
  356.    }
  357.  
  358.    public static List listUsers(NamespaceAccessToken nat, String caller) throws Exception {
  359.       return listObjects(nat, caller, nat.getNamespaceConfig().getUsersPath());
  360.    }
  361.  
  362.    public static List listPrivileges(NamespaceAccessToken nat, String caller) throws Exception {
  363.       return listObjects(nat, caller, nat.getNamespaceConfig().getActionsPath());
  364.    }
  365.  
  366.    private static List listObjects(NamespaceAccessToken nat, String caller, String path) throws Exception {
  367.       List result = new ArrayList();
  368.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  369.       Structure structure = nat.getStructureHelper();
  370.       ObjectNode object = structure.retrieve(slideToken, path);
  371.       Enumeration enum = structure.getChildren(slideToken, object);
  372.  
  373.       while(enum.hasMoreElements()) {
  374.          result.add(((ObjectNode)enum.nextElement()).getUri());
  375.       }
  376.  
  377.       return result;
  378.    }
  379.  
  380.    public static void removePermission(NamespaceAccessToken nat, String caller, String path, String subject, String action) throws Exception {
  381.       String uri = getUriFromPath(nat, path);
  382.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  383.       Security security = nat.getSecurityHelper();
  384.  
  385.       try {
  386.          NodePermission permission = new NodePermission(uri, subject, action);
  387.          nat.begin();
  388.          security.revokePermission(slideToken, permission);
  389.          nat.commit();
  390.       } catch (Exception e) {
  391.          try {
  392.             nat.rollback();
  393.          } catch (Exception f) {
  394.             f.printStackTrace();
  395.          }
  396.  
  397.          throw e;
  398.       }
  399.    }
  400.  
  401.    public static void addPermission(NamespaceAccessToken nat, String caller, String path, String subject, String action, String inheritable, String negative) throws Exception {
  402.       String uri = getUriFromPath(nat, path);
  403.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  404.       Security security = nat.getSecurityHelper();
  405.       boolean isInheritable = Boolean.valueOf(inheritable);
  406.       boolean isNegative = Boolean.valueOf(negative);
  407.  
  408.       try {
  409.          NodePermission permission = new NodePermission(uri, subject, action, isInheritable, isNegative);
  410.          nat.begin();
  411.          if (isNegative) {
  412.             security.denyPermission(slideToken, permission);
  413.          } else {
  414.             security.grantPermission(slideToken, permission);
  415.          }
  416.  
  417.          nat.commit();
  418.       } catch (Exception e) {
  419.          try {
  420.             nat.rollback();
  421.          } catch (Exception f) {
  422.             f.printStackTrace();
  423.          }
  424.  
  425.          throw e;
  426.       }
  427.    }
  428.  
  429.    public static void removeLock(NamespaceAccessToken nat, String caller, String uri, String lockId) throws Exception {
  430.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  431.       Lock lock = nat.getLockHelper();
  432.  
  433.       try {
  434.          nat.begin();
  435.          lock.unlock(slideToken, uri, lockId);
  436.          nat.commit();
  437.       } catch (Exception e) {
  438.          try {
  439.             nat.rollback();
  440.          } catch (Exception f) {
  441.             f.printStackTrace();
  442.          }
  443.  
  444.          throw e;
  445.       }
  446.    }
  447.  
  448.    public static void addLock(NamespaceAccessToken nat, String caller, String path, String subject, String type, String expiration, String exclusive, String inherit) throws Exception {
  449.       String uri = getUriFromPath(nat, path);
  450.       boolean isExclusive = Boolean.valueOf(exclusive);
  451.       boolean isInherit = Boolean.valueOf(inherit);
  452.       int intExpiration = Integer.valueOf(expiration);
  453.       Date expire = new Date(System.currentTimeMillis() + (long)(intExpiration * 1000 * 60));
  454.       SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
  455.       Lock lock = nat.getLockHelper();
  456.  
  457.       try {
  458.          nat.begin();
  459.          lock.lock(slideToken, new NodeLock(uri, subject, type, expire, isExclusive, isInherit));
  460.          nat.commit();
  461.       } catch (Exception e) {
  462.          try {
  463.             nat.rollback();
  464.          } catch (Exception f) {
  465.             f.printStackTrace();
  466.          }
  467.  
  468.          throw e;
  469.       }
  470.    }
  471.  
  472.    private static String getUriFromPath(NamespaceAccessToken nat, String path) {
  473.       String filesPath = nat.getNamespaceConfig().getFilesPath();
  474.       String uri;
  475.       if (!path.equals("/") && path.length() != 0) {
  476.          uri = filesPath + "/" + path;
  477.       } else {
  478.          uri = filesPath;
  479.       }
  480.  
  481.       return uri;
  482.    }
  483. }
  484.