home *** CD-ROM | disk | FTP | other *** search
- package org.apache.cocoon.samples.slide;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Date;
- import java.util.Enumeration;
- import java.util.List;
- import org.apache.slide.authenticate.CredentialsToken;
- import org.apache.slide.common.NamespaceAccessToken;
- import org.apache.slide.common.SlideToken;
- import org.apache.slide.common.SlideTokenImpl;
- import org.apache.slide.content.Content;
- import org.apache.slide.content.NodeProperty;
- import org.apache.slide.content.NodeRevisionContent;
- import org.apache.slide.content.NodeRevisionDescriptor;
- import org.apache.slide.content.NodeRevisionDescriptors;
- import org.apache.slide.lock.Lock;
- import org.apache.slide.lock.NodeLock;
- import org.apache.slide.macro.Macro;
- import org.apache.slide.macro.MacroParameters;
- import org.apache.slide.security.NodePermission;
- import org.apache.slide.security.Security;
- import org.apache.slide.structure.ObjectNode;
- import org.apache.slide.structure.ObjectNotFoundException;
- import org.apache.slide.structure.Structure;
- import org.apache.slide.structure.SubjectNode;
-
- public class AdminHelper {
- private static final SlideToken ROOT = new SlideTokenImpl(new CredentialsToken("root"));
-
- public static boolean login(NamespaceAccessToken nat, String userId, String password) throws Exception {
- String usersPath = nat.getNamespaceConfig().getUsersPath();
- String userUri = usersPath + "/" + userId;
- Content content = nat.getContentHelper();
-
- try {
- NodeRevisionDescriptors revisions = content.retrieve(ROOT, userUri);
- NodeRevisionDescriptor revision = content.retrieve(ROOT, revisions);
- NodeProperty property = revision.getProperty("password", "http://jakarta.apache.org/slide/");
- return property.getValue().equals(password);
- } catch (Exception e) {
- e.printStackTrace();
- throw e;
- }
- }
-
- public static void addUser(NamespaceAccessToken nat, String caller, String username, String password) throws Exception {
- String usersPath = nat.getNamespaceConfig().getUsersPath();
- String userUri = usersPath + "/" + username;
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Structure structure = nat.getStructureHelper();
- Content content = nat.getContentHelper();
-
- try {
- nat.begin();
- ObjectNode user = new SubjectNode();
- structure.create(slideToken, user, userUri);
- NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
- descriptor.setCreationDate(new Date());
- descriptor.setLastModified(new Date());
- descriptor.setProperty(new NodeProperty("password", password, "http://jakarta.apache.org/slide/"));
- content.create(slideToken, userUri, descriptor, (NodeRevisionContent)null);
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static void addGroup(NamespaceAccessToken nat, String caller, String groupname) throws Exception {
- String groupsPath = nat.getNamespaceConfig().getGroupsPath();
- String groupUri = groupsPath + "/" + groupname;
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Structure structure = nat.getStructureHelper();
- Content content = nat.getContentHelper();
-
- try {
- nat.begin();
- ObjectNode group = new SubjectNode();
- structure.create(slideToken, group, groupUri);
- NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
- descriptor.setCreationDate(new Date());
- descriptor.setLastModified(new Date());
- content.create(slideToken, groupUri, descriptor, (NodeRevisionContent)null);
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static void addRole(NamespaceAccessToken nat, String caller, String rolename) throws Exception {
- String rolesPath = nat.getNamespaceConfig().getRolesPath();
- String roleUri = rolesPath + "/" + rolename;
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Structure structure = nat.getStructureHelper();
- Content content = nat.getContentHelper();
-
- try {
- nat.begin();
- ObjectNode role = new SubjectNode();
- structure.create(slideToken, role, roleUri);
- NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
- descriptor.setCreationDate(new Date());
- descriptor.setLastModified(new Date());
- content.create(slideToken, roleUri, descriptor, (NodeRevisionContent)null);
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static void removeObject(NamespaceAccessToken nat, String caller, String objectUri) throws Exception {
- String usersPath = nat.getNamespaceConfig().getUsersPath();
- String callerUri = usersPath + "/" + caller;
- if (!callerUri.equals(objectUri)) {
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Macro macro = nat.getMacroHelper();
-
- try {
- nat.begin();
- boolean recursive = true;
- boolean overwrite = false;
- MacroParameters parameters = new MacroParameters(recursive, overwrite);
- macro.delete(slideToken, objectUri, parameters);
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
- }
-
- public static void addMember(NamespaceAccessToken nat, String caller, String objectUri, String subjectUri) throws Exception {
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Structure structure = nat.getStructureHelper();
- Content content = nat.getContentHelper();
-
- try {
- structure.retrieve(slideToken, subjectUri);
- NodeRevisionDescriptors descriptors = content.retrieve(slideToken, objectUri);
- NodeRevisionDescriptor descriptor = content.retrieve(slideToken, descriptors);
- NodeProperty property = descriptor.getProperty("group-member-set", "DAV:");
- String value = null;
- if (property != null) {
- value = (String)property.getValue();
- if (value.indexOf(subjectUri) != -1) {
- return;
- }
- } else {
- value = "";
- }
-
- value = value + "<D:href xmlns:D='DAV:'>" + subjectUri + "</D:href>";
- descriptor.setProperty("group-member-set", "DAV:", value);
- nat.begin();
- content.store(slideToken, objectUri, descriptor, (NodeRevisionContent)null);
- nat.commit();
- } catch (ObjectNotFoundException var12) {
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
-
- }
-
- public static void removeMember(NamespaceAccessToken nat, String caller, String objectUri, String subjectUri) throws Exception {
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Content content = nat.getContentHelper();
-
- try {
- NodeRevisionDescriptors revisions = content.retrieve(slideToken, objectUri);
- NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions);
- NodeProperty property = revision.getProperty("group-member-set", "DAV:");
- if (property == null) {
- return;
- }
-
- String value = (String)property.getValue();
- int index = value.indexOf(subjectUri);
- if (index == -1) {
- return;
- }
-
- int end = index + subjectUri.length();
-
- do {
- ++end;
- } while(value.charAt(end) != '>');
-
- int from = index;
-
- do {
- --from;
- } while(value.charAt(from) != '<');
-
- String before = value.substring(0, from);
- String after = value.substring(end + 1);
- value = before + after;
- revision.setProperty("group-member-set", "DAV:", value);
- nat.begin();
- content.store(slideToken, objectUri, revision, (NodeRevisionContent)null);
- nat.commit();
- } catch (ObjectNotFoundException var16) {
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
-
- }
-
- public static void changePassword(NamespaceAccessToken nat, String caller, String username, String password) throws Exception {
- String usersPath = nat.getNamespaceConfig().getUsersPath();
- String userUri = usersPath + "/" + username;
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Content content = nat.getContentHelper();
-
- try {
- nat.begin();
- NodeRevisionDescriptors revisions = content.retrieve(slideToken, userUri);
- NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions);
- revision.setLastModified(new Date());
- revision.setProperty(new NodeProperty("password", password, "http://jakarta.apache.org/slide/"));
- content.store(slideToken, userUri, revision, (NodeRevisionContent)null);
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static List listPermissions(NamespaceAccessToken nat, String caller, String path) throws Exception {
- String uri = getUriFromPath(nat, path);
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Security security = nat.getSecurityHelper();
- List result = new ArrayList();
-
- try {
- nat.begin();
- Enumeration permissions = security.enumeratePermissions(slideToken, uri, false);
-
- while(permissions.hasMoreElements()) {
- result.add(permissions.nextElement());
- }
-
- nat.commit();
- return result;
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static List listLocks(NamespaceAccessToken nat, String caller, String path) throws Exception {
- String uri = getUriFromPath(nat, path);
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Lock lock = nat.getLockHelper();
- List result = new ArrayList();
-
- try {
- nat.begin();
- Enumeration locks = lock.enumerateLocks(slideToken, uri, false);
-
- while(locks.hasMoreElements()) {
- result.add(locks.nextElement());
- }
-
- nat.commit();
- return result;
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static List listGroups(NamespaceAccessToken nat, String caller, String path) throws Exception {
- List result = new ArrayList();
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Structure structure = nat.getStructureHelper();
- Content content = nat.getContentHelper();
- ObjectNode object = structure.retrieve(slideToken, path);
-
- String uri;
- List members;
- for(Enumeration enum = structure.getChildren(slideToken, object); enum.hasMoreElements(); result.add(new Group(uri, members, (1)null))) {
- uri = ((ObjectNode)enum.nextElement()).getUri();
- NodeRevisionDescriptors revisions = content.retrieve(slideToken, uri);
- NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions);
- NodeProperty property = revision.getProperty("group-member-set", "DAV:");
- if (property != null) {
- String value = (String)property.getValue();
- members = new ArrayList(10);
- int start = value.indexOf(62);
-
- int end;
- for(end = 0; start != -1; start = value.indexOf(62, end + 1)) {
- end = value.indexOf(60, start);
- if (end != -1) {
- members.add(value.substring(start + 1, end));
- }
-
- end = value.indexOf(62, start + 1);
- }
- } else {
- members = Collections.EMPTY_LIST;
- }
- }
-
- return result;
- }
-
- public static List listUsers(NamespaceAccessToken nat, String caller) throws Exception {
- return listObjects(nat, caller, nat.getNamespaceConfig().getUsersPath());
- }
-
- public static List listPrivileges(NamespaceAccessToken nat, String caller) throws Exception {
- return listObjects(nat, caller, nat.getNamespaceConfig().getActionsPath());
- }
-
- private static List listObjects(NamespaceAccessToken nat, String caller, String path) throws Exception {
- List result = new ArrayList();
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Structure structure = nat.getStructureHelper();
- ObjectNode object = structure.retrieve(slideToken, path);
- Enumeration enum = structure.getChildren(slideToken, object);
-
- while(enum.hasMoreElements()) {
- result.add(((ObjectNode)enum.nextElement()).getUri());
- }
-
- return result;
- }
-
- public static void removePermission(NamespaceAccessToken nat, String caller, String path, String subject, String action) throws Exception {
- String uri = getUriFromPath(nat, path);
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Security security = nat.getSecurityHelper();
-
- try {
- NodePermission permission = new NodePermission(uri, subject, action);
- nat.begin();
- security.revokePermission(slideToken, permission);
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static void addPermission(NamespaceAccessToken nat, String caller, String path, String subject, String action, String inheritable, String negative) throws Exception {
- String uri = getUriFromPath(nat, path);
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Security security = nat.getSecurityHelper();
- boolean isInheritable = Boolean.valueOf(inheritable);
- boolean isNegative = Boolean.valueOf(negative);
-
- try {
- NodePermission permission = new NodePermission(uri, subject, action, isInheritable, isNegative);
- nat.begin();
- if (isNegative) {
- security.denyPermission(slideToken, permission);
- } else {
- security.grantPermission(slideToken, permission);
- }
-
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static void removeLock(NamespaceAccessToken nat, String caller, String uri, String lockId) throws Exception {
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Lock lock = nat.getLockHelper();
-
- try {
- nat.begin();
- lock.unlock(slideToken, uri, lockId);
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- public static void addLock(NamespaceAccessToken nat, String caller, String path, String subject, String type, String expiration, String exclusive, String inherit) throws Exception {
- String uri = getUriFromPath(nat, path);
- boolean isExclusive = Boolean.valueOf(exclusive);
- boolean isInherit = Boolean.valueOf(inherit);
- int intExpiration = Integer.valueOf(expiration);
- Date expire = new Date(System.currentTimeMillis() + (long)(intExpiration * 1000 * 60));
- SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
- Lock lock = nat.getLockHelper();
-
- try {
- nat.begin();
- lock.lock(slideToken, new NodeLock(uri, subject, type, expire, isExclusive, isInherit));
- nat.commit();
- } catch (Exception e) {
- try {
- nat.rollback();
- } catch (Exception f) {
- f.printStackTrace();
- }
-
- throw e;
- }
- }
-
- private static String getUriFromPath(NamespaceAccessToken nat, String path) {
- String filesPath = nat.getNamespaceConfig().getFilesPath();
- String uri;
- if (!path.equals("/") && path.length() != 0) {
- uri = filesPath + "/" + path;
- } else {
- uri = filesPath;
- }
-
- return uri;
- }
- }
-