You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2009/02/27 10:19:42 UTC
svn commit: r748454 [2/2] - in /incubator/sling/trunk: ./
bundles/jcr/jackrabbit-usermanager/ bundles/jcr/jackrabbit-usermanager/src/
bundles/jcr/jackrabbit-usermanager/src/main/
bundles/jcr/jackrabbit-usermanager/src/main/java/ bundles/jcr/jackrabbit-...
Added: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/post/impl/RequestProperty.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/post/impl/RequestProperty.java?rev=748454&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/post/impl/RequestProperty.java (added)
+++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/post/impl/RequestProperty.java Fri Feb 27 09:19:41 2009
@@ -0,0 +1,257 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.jackrabbit.usermanager.post.impl;
+
+import org.apache.sling.api.request.RequestParameter;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.servlets.post.SlingPostConstants;
+
+/**
+ * This is a copy of the class from 'org.apache.sling.servlets.post.impl.helper' which is not exported.
+ *
+ * Encapsulates all infos from the respective request parameters that are needed
+ * to create the repository property
+ */
+public class RequestProperty {
+
+ private static final RequestParameter[] EMPTY_PARAM_ARRAY = new RequestParameter[0];
+
+ public static final String DEFAULT_IGNORE = SlingPostConstants.RP_PREFIX
+ + "ignore";
+
+ public static final String DEFAULT_NULL = SlingPostConstants.RP_PREFIX
+ + "null";
+
+ private final String path;
+
+ private final String name;
+
+ private final String parentPath;
+
+ private RequestParameter[] values;
+
+ private String[] stringValues;
+
+ private String typeHint;
+
+ private boolean hasMultiValueTypeHint;
+
+ private RequestParameter[] defaultValues = EMPTY_PARAM_ARRAY;
+
+ private boolean isDelete;
+
+ private String repositoryResourcePath;
+
+ private boolean isRepositoryResourceMove;
+
+ public RequestProperty(String path) {
+ assert path.startsWith("/");
+ this.path = ResourceUtil.normalize(path);
+ this.parentPath = ResourceUtil.getParent(path);
+ this.name = ResourceUtil.getName(path);
+ }
+
+ public String getTypeHint() {
+ return typeHint;
+ }
+
+ public boolean hasMultiValueTypeHint() {
+ return this.hasMultiValueTypeHint;
+ }
+
+ public void setTypeHintValue(String typeHint) {
+ if ( typeHint != null && typeHint.endsWith("[]") ) {
+ this.typeHint = typeHint.substring(0, typeHint.length() - 2);
+ this.hasMultiValueTypeHint = true;
+ } else {
+ this.typeHint = typeHint;
+ this.hasMultiValueTypeHint = false;
+ }
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getParentPath() {
+ return parentPath;
+ }
+
+ public boolean hasValues() {
+ return values != null;
+ }
+
+ public RequestParameter[] getValues() {
+ return values;
+ }
+
+ public void setValues(RequestParameter[] values) {
+ this.values = values;
+ }
+
+ public RequestParameter[] getDefaultValues() {
+ return defaultValues;
+ }
+
+ public void setDefaultValues(RequestParameter[] defaultValues) {
+ if (defaultValues == null) {
+ this.defaultValues = EMPTY_PARAM_ARRAY;
+ } else {
+ this.defaultValues = defaultValues;
+ }
+ }
+
+ public boolean isFileUpload() {
+ return !values[0].isFormField();
+ }
+
+ /**
+ * Checks if this property provides any values. this is the case if one of
+ * the values is not empty or if the default handling is not 'ignore'
+ *
+ * @return <code>true</code> if this property provides values
+ */
+ public boolean providesValue() {
+ // should void double creation of string values
+ String[] sv = getStringValues();
+ if (sv == null) {
+ // is missleading return type. but means that property should not
+ // get auto-create values
+ return true;
+ }
+ for (String s : sv) {
+ if (!s.equals("")) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns the assembled string array out of the provided request values and
+ * default values.
+ *
+ * @return a String array or <code>null</code> if the property needs to be
+ * removed.
+ */
+ public String[] getStringValues() {
+ if (stringValues == null) {
+ if (values.length > 1) {
+ // TODO: how the default values work for MV props is not very
+ // clear
+ stringValues = new String[values.length];
+ for (int i = 0; i < stringValues.length; i++) {
+ stringValues[i] = values[i].getString();
+ }
+ } else {
+ String value = values[0].getString();
+ if (value.equals("")) {
+ if (defaultValues.length == 1) {
+ String defValue = defaultValues[0].getString();
+ if (defValue.equals(DEFAULT_IGNORE)) {
+ // ignore means, do not create empty values
+ return new String[0];
+ } else if (defValue.equals(DEFAULT_NULL)) {
+ // null means, remove property if exist
+ return null;
+ }
+ value = defValue;
+ }
+ }
+ stringValues = new String[] { value };
+ }
+ }
+ return stringValues;
+ }
+
+ /**
+ * Specifies whether this property should be deleted before any new content
+ * is to be set according to the values stored.
+ *
+ * @param isDelete <code>true</code> if the repository item described by
+ * this is to be deleted before any other operation.
+ */
+ public void setDelete(boolean isDelete) {
+ this.isDelete = isDelete;
+ }
+
+ /**
+ * Returns <code>true</code> if the repository item described by this is
+ * to be deleted before setting new content to it.
+ */
+ public boolean isDelete() {
+ return isDelete;
+ }
+
+ /**
+ * Sets the path of the repository item from which the content for this
+ * property is to be copied or moved. The path may be relative in which case
+ * it will be resolved relative to the absolute path of this property.
+ *
+ * @param sourcePath The path of the repository item to get the content from
+ * @param isMove <code>true</code> if the source content is to be moved,
+ * otherwise the source content is copied from the repository
+ * item.
+ */
+ public void setRepositorySource(String sourcePath, boolean isMove) {
+
+ // make source path absolute
+ if (!sourcePath.startsWith("/")) {
+ sourcePath = getParentPath() + "/" + sourcePath;
+ sourcePath = ResourceUtil.normalize(sourcePath);
+ }
+
+ this.repositoryResourcePath = sourcePath;
+ this.isRepositoryResourceMove = isMove;
+ }
+
+ /**
+ * Returns <code>true</code> if the content of this property is to be set
+ * by moving content from another repository item.
+ *
+ * @see #getRepositorySource()
+ */
+ public boolean hasRepositoryMoveSource() {
+ return isRepositoryResourceMove;
+ }
+
+ /**
+ * Returns <code>true</code> if the content of this property is to be set
+ * by copying content from another repository item.
+ *
+ * @see #getRepositorySource()
+ */
+ public boolean hasRepositoryCopySource() {
+ return getRepositorySource() != null && !hasRepositoryMoveSource();
+ }
+
+ /**
+ * Returns the absolute path of the repository item from which the content
+ * for this property is to be copied or moved.
+ *
+ * @see #hasRepositoryCopySource()
+ * @see #hasRepositoryMoveSource()
+ * @see #setRepositorySource(String, boolean)
+ */
+ public String getRepositorySource() {
+ return repositoryResourcePath;
+ }
+}
\ No newline at end of file
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/post/impl/RequestProperty.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/post/impl/RequestProperty.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev Url
Added: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResource.java?rev=748454&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResource.java (added)
+++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResource.java Fri Feb 27 09:19:41 2009
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.jackrabbit.usermanager.resource;
+
+
+import java.util.Map;
+
+import javax.jcr.RepositoryException;
+
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.sling.adapter.SlingAdaptable;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+
+/**
+ * Resource implementation for Authorizable
+ */
+public class AuthorizableResource extends SlingAdaptable implements Resource {
+ private Authorizable authorizable = null;
+ private ResourceResolver resourceResolver = null;
+ private final String path;
+ private final String resourceType;
+ private final ResourceMetadata metadata;
+
+ public AuthorizableResource(Authorizable authorizable,
+ ResourceResolver resourceResolver, String path) {
+ super();
+
+ this.resourceResolver = resourceResolver;
+ this.authorizable = authorizable;
+ this.path = path;
+ if (authorizable.isGroup()) {
+ this.resourceType = "sling:group";
+ } else {
+ this.resourceType = "sling:user";
+ }
+
+ this.metadata = new ResourceMetadata();
+ metadata.setResolutionPath(path);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.resource.Resource#getPath()
+ */
+ public String getPath() {
+ return path;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.resource.Resource#getResourceMetadata()
+ */
+ public ResourceMetadata getResourceMetadata() {
+ return metadata;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.resource.Resource#getResourceResolver()
+ */
+ public ResourceResolver getResourceResolver() {
+ return resourceResolver;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.resource.Resource#getResourceSuperType()
+ */
+ public String getResourceSuperType() {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.resource.Resource#getResourceType()
+ */
+ public String getResourceType() {
+ return resourceType;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.adapter.Adaptable#adaptTo(java.lang.Class)
+ */
+ @SuppressWarnings("unchecked")
+ public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+ if (type == Map.class || type == ValueMap.class) {
+ return (AdapterType) new AuthorizableValueMap(authorizable); // unchecked cast
+ } else if (type == Authorizable.class) {
+ return (AdapterType)authorizable;
+ }
+
+ return super.adaptTo(type);
+ }
+
+ public String toString() {
+ String id = null;
+ if (authorizable != null) {
+ try {
+ id = authorizable.getID();
+ } catch (RepositoryException e) {
+ //ignore it.
+ }
+ }
+ return getClass().getSimpleName() + ", id=" + id
+ + ", path=" + getPath();
+ }
+}
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResource.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResource.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev Url
Added: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResourceProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResourceProvider.java?rev=748454&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResourceProvider.java (added)
+++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResourceProvider.java Fri Feb 27 09:19:41 2009
@@ -0,0 +1,220 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.jackrabbit.usermanager.resource;
+
+import java.security.Principal;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.jackrabbit.api.security.principal.PrincipalIterator;
+import org.apache.jackrabbit.api.security.principal.PrincipalManager;
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.UserManager;
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceProvider;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.SyntheticResource;
+import org.apache.sling.jcr.base.util.AccessControlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Resource Provider implementation for jackrabbit UserManager resources.
+ *
+ * @scr.component immediate="true" label="%authorizable.resourceprovider.name"
+ * description="authorizable.resourceprovider.description"
+ * @scr.property name="service.description"
+ * value="Resource provider implementation for UserManager resources"
+ * @scr.property name="service.vendor" value="The Apache Software Foundation"
+ * @scr.property name="provider.roots" value="/system/userManager/"
+ * @scr.service interface="org.apache.sling.api.resource.ResourceProvider"
+ */
+public class AuthorizableResourceProvider implements ResourceProvider {
+
+ /**
+ * default log
+ */
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ public static final String SYSTEM_USER_MANAGER_PATH = "/system/userManager";
+
+ public static final String SYSTEM_USER_MANAGER_USER_PATH = SYSTEM_USER_MANAGER_PATH + "/user";
+ public static final String SYSTEM_USER_MANAGER_GROUP_PATH = SYSTEM_USER_MANAGER_PATH + "/group";
+
+ public static final String SYSTEM_USER_MANAGER_USER_PREFIX = SYSTEM_USER_MANAGER_USER_PATH + "/";
+ public static final String SYSTEM_USER_MANAGER_GROUP_PREFIX = SYSTEM_USER_MANAGER_GROUP_PATH + "/";
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.resource.ResourceProvider#getResource(org.apache.sling.api.resource.ResourceResolver, javax.servlet.http.HttpServletRequest, java.lang.String)
+ */
+ public Resource getResource(ResourceResolver resourceResolver,
+ HttpServletRequest request, String path) {
+ return getResource(resourceResolver, path);
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.resource.ResourceProvider#getResource(org.apache.sling.api.resource.ResourceResolver, java.lang.String)
+ */
+ public Resource getResource(ResourceResolver resourceResolver, String path) {
+
+ //handle resources for the virtual container resources
+ if (path.equals(SYSTEM_USER_MANAGER_PATH)) {
+ return new SyntheticResource(resourceResolver, path, "sling:userManager");
+ } else if (path.equals(SYSTEM_USER_MANAGER_USER_PATH)) {
+ return new SyntheticResource(resourceResolver, path, "sling:users");
+ } else if (path.equals(SYSTEM_USER_MANAGER_GROUP_PATH)) {
+ return new SyntheticResource(resourceResolver, path, "sling:groups");
+ }
+
+ // the principalId should be the first segment after the prefix
+ String pid = null;
+ if (path.startsWith(SYSTEM_USER_MANAGER_USER_PREFIX)) {
+ pid = path.substring(SYSTEM_USER_MANAGER_USER_PREFIX.length());
+ } else if (path.startsWith(SYSTEM_USER_MANAGER_GROUP_PREFIX)) {
+ pid = path.substring(SYSTEM_USER_MANAGER_GROUP_PREFIX.length());
+ }
+
+ if (pid != null) {
+ if (pid.indexOf('/') != -1) {
+ return null; //something bogus on the end of the path so bail out now.
+ }
+ try {
+ Session session = resourceResolver.adaptTo(Session.class);
+ if (session != null) {
+ UserManager userManager = AccessControlUtil.getUserManager(session);
+ if (userManager != null) {
+ Authorizable authorizable = userManager.getAuthorizable(pid);
+ if (authorizable != null) {
+ //found the Authorizable, so return the resource that wraps it.
+ return new AuthorizableResource(authorizable, resourceResolver, path);
+ }
+ }
+ }
+ } catch (RepositoryException re) {
+ throw new SlingException("Error looking up Authorizable for principal: " + pid, re);
+ }
+ }
+ return null;
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.apache.sling.api.resource.ResourceProvider#listChildren(org.apache.sling.api.resource.Resource)
+ */
+ public Iterator<Resource> listChildren(Resource parent) {
+ if (parent == null) {
+ throw new NullPointerException("parent is null");
+ }
+ try {
+ String path = parent.getPath();
+ ResourceResolver resourceResolver = parent.getResourceResolver();
+
+ //handle children of /system/userManager
+ if (SYSTEM_USER_MANAGER_PATH.equals(path)) {
+ List<Resource> resources = new ArrayList<Resource>();
+ if (resourceResolver != null) {
+ resources.add(getResource(resourceResolver, SYSTEM_USER_MANAGER_USER_PATH));
+ resources.add(getResource(resourceResolver, SYSTEM_USER_MANAGER_GROUP_PATH));
+ }
+ return resources.iterator();
+ }
+
+ int searchType = -1;
+ if (SYSTEM_USER_MANAGER_USER_PATH.equals(path)) {
+ searchType = PrincipalManager.SEARCH_TYPE_NOT_GROUP;
+ } else if (SYSTEM_USER_MANAGER_GROUP_PATH.equals(path)) {
+ searchType = PrincipalManager.SEARCH_TYPE_GROUP;
+ }
+ if (searchType != -1) {
+ PrincipalIterator principals = null;
+
+ //TODO: this actually does not work correctly since the jackrabbit findPrincipals API
+ // currently does an exact match of the search filter so it won't match a wildcard
+ Session session = resourceResolver.adaptTo(Session.class);
+ if (session != null) {
+ PrincipalManager principalManager = AccessControlUtil.getPrincipalManager(session);
+ principals = principalManager.findPrincipals(".*", PrincipalManager.SEARCH_TYPE_NOT_GROUP);
+ }
+
+
+ if (principals != null) {
+ return new ChildrenIterator(parent, principals);
+ }
+ }
+ } catch (RepositoryException re) {
+ throw new SlingException("Error listing children of resource: " + parent.getPath(), re);
+ }
+
+ return null;
+ }
+
+
+
+ private final class ChildrenIterator implements Iterator<Resource> {
+ private PrincipalIterator principals;
+ private Resource parent;
+
+ public ChildrenIterator(Resource parent, PrincipalIterator principals) {
+ this.parent = parent;
+ this.principals = principals;
+ }
+
+ public boolean hasNext() {
+ return principals.hasNext();
+ }
+
+ public Resource next() {
+ Principal nextPrincipal = principals.nextPrincipal();
+ try {
+ ResourceResolver resourceResolver = parent.getResourceResolver();
+ if (resourceResolver != null) {
+ Session session = resourceResolver.adaptTo(Session.class);
+ if (session != null) {
+ UserManager userManager = AccessControlUtil.getUserManager(session);
+ if (userManager != null) {
+ Authorizable authorizable = userManager.getAuthorizable(nextPrincipal.getName());
+ if (authorizable != null) {
+ String path;
+ if (authorizable.isGroup()) {
+ path = SYSTEM_USER_MANAGER_GROUP_PREFIX + nextPrincipal.getName();
+ } else {
+ path = SYSTEM_USER_MANAGER_USER_PREFIX + nextPrincipal.getName();
+ }
+ return new AuthorizableResource(authorizable, resourceResolver, path);
+ }
+ }
+ }
+ }
+ } catch (RepositoryException re) {
+ log.error("Exception while looking up authorizable resource.", re);
+ }
+ return null;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+}
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResourceProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResourceProvider.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev Url
Added: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableValueMap.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableValueMap.java?rev=748454&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableValueMap.java (added)
+++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableValueMap.java Fri Feb 27 09:19:41 2009
@@ -0,0 +1,319 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.jackrabbit.usermanager.resource;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.jcr.resource.JcrResourceUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * ValueMap implementation for Authorizable Resources
+ */
+public class AuthorizableValueMap implements ValueMap {
+ private Logger logger = LoggerFactory.getLogger(AuthorizableValueMap.class);
+ private Set<String> hiddenProperties = new HashSet<String>(Arrays.asList(new String[]{"rep:password", "jcr:uuid"}));
+ private boolean fullyRead;
+ private final Map<String, Object> cache;
+ private Authorizable authorizable;
+
+ public AuthorizableValueMap(Authorizable authorizable) {
+ this.authorizable = authorizable;
+ this.cache = new LinkedHashMap<String, Object>();
+ this.fullyRead = false;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> T get(String name, Class<T> type) {
+ if (type == null) {
+ return (T) get(name);
+ }
+
+ return convertToType(name, type);
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> T get(String name, T defaultValue) {
+ if (defaultValue == null) {
+ return (T) get(name);
+ }
+
+ // special handling in case the default value implements one
+ // of the interface types supported by the convertToType method
+ Class<T> type = (Class<T>) normalizeClass(defaultValue.getClass());
+
+ T value = get(name, type);
+ if (value == null) {
+ value = defaultValue;
+ }
+
+ return value;
+ }
+
+ public boolean containsKey(Object key) {
+ return get(key) != null;
+ }
+
+ public boolean containsValue(Object value) {
+ readFully();
+ return cache.containsValue(value);
+ }
+
+ public Set<java.util.Map.Entry<String, Object>> entrySet() {
+ readFully();
+ return cache.entrySet();
+ }
+
+ public Object get(Object key) {
+ Object value = cache.get(key);
+ if (value == null) {
+ value = read((String) key);
+ }
+
+ return value;
+ }
+
+
+ public Set<String> keySet() {
+ readFully();
+ return cache.keySet();
+ }
+
+ public int size() {
+ readFully();
+ return cache.size();
+ }
+
+ public boolean isEmpty() {
+ return size() == 0;
+ }
+
+ public Collection<Object> values() {
+ readFully();
+ return cache.values();
+ }
+
+ protected Object read(String key) {
+
+ // if the item has been completely read, we need not check
+ // again, as we certainly will not find the key
+ if (fullyRead) {
+ return null;
+ }
+
+ if (hiddenProperties.contains(key)) {
+ return null;
+ }
+
+ try {
+ if (authorizable.hasProperty(key)) {
+ Value[] property = authorizable.getProperty(key);
+ Object value = valuesToJavaObject(property);
+ cache.put(key, value);
+ return value;
+ }
+ } catch (RepositoryException re) {
+ // TODO: log !!
+ }
+
+ // property not found or some error accessing it
+ return null;
+ }
+
+ protected Object valuesToJavaObject(Value [] values) throws RepositoryException {
+ if (values == null) {
+ return null;
+ } else if (values.length == 1) {
+ return JcrResourceUtil.toJavaObject(values[0]);
+ } else {
+ Object [] valuesObjs = new Object[values.length];
+ for (int i=0; i < values.length; i++) {
+ valuesObjs[i] = JcrResourceUtil.toJavaObject(values[i]);
+ }
+ return valuesObjs;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void readFully() {
+ if (!fullyRead) {
+ try {
+ Iterator pi = authorizable.getPropertyNames();
+ while (pi.hasNext()) {
+ String key = (String)pi.next();
+
+ if (hiddenProperties.contains(key)) {
+ continue; //skip it.
+ }
+
+ if (!cache.containsKey(key)) {
+ Value[] property = authorizable.getProperty(key);
+ Object value = valuesToJavaObject(property);
+ cache.put(key, value);
+ }
+ }
+ fullyRead = true;
+ } catch (RepositoryException re) {
+ // TODO: log !!
+ }
+ }
+ }
+
+ // ---------- Unsupported Modification methods
+
+ public Object remove(Object arg0) {
+ throw new UnsupportedOperationException();
+ }
+ public void clear() {
+ throw new UnsupportedOperationException();
+ }
+ public Object put(String arg0, Object arg1) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void putAll(Map<? extends String, ? extends Object> arg0) {
+ throw new UnsupportedOperationException();
+ }
+
+
+ // ---------- Implementation helper
+
+ @SuppressWarnings("unchecked")
+ private <T> T convertToType(String name, Class<T> type) {
+ T result = null;
+
+ try {
+ if (authorizable.hasProperty(name)) {
+ Value[] values = authorizable.getProperty(name);
+
+ if (values == null) {
+ return null;
+ }
+
+ boolean multiValue = values.length > 1;
+ boolean array = type.isArray();
+
+ if (multiValue) {
+ if (array) {
+ result = (T) convertToArray(values,
+ type.getComponentType());
+ } else if (values.length > 0) {
+ result = convertToType(-1, values[0], type);
+ }
+ } else {
+ Value value = values[0];
+ if (array) {
+ result = (T) convertToArray(
+ new Value[] { value }, type.getComponentType());
+ } else {
+ result = convertToType(-1, value, type);
+ }
+ }
+ }
+
+ } catch (ValueFormatException vfe) {
+ logger.info("converToType: Cannot convert value of " + name
+ + " to " + type, vfe);
+ } catch (RepositoryException re) {
+ logger.info("converToType: Cannot get value of " + name, re);
+ }
+
+ // fall back to nothing
+ return result;
+ }
+
+ private <T> T[] convertToArray(Value[] jcrValues, Class<T> type)
+ throws ValueFormatException, RepositoryException {
+ List<T> values = new ArrayList<T>();
+ for (int i = 0; i < jcrValues.length; i++) {
+ T value = convertToType(i, jcrValues[i], type);
+ if (value != null) {
+ values.add(value);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ T[] result = (T[]) Array.newInstance(type, values.size());
+
+ return values.toArray(result);
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T convertToType(int index, Value jcrValue,
+ Class<T> type) throws ValueFormatException, RepositoryException {
+
+ if (String.class == type) {
+ return (T) jcrValue.getString();
+ } else if (Byte.class == type) {
+ return (T) new Byte((byte) jcrValue.getLong());
+ } else if (Short.class == type) {
+ return (T) new Short((short) jcrValue.getLong());
+ } else if (Integer.class == type) {
+ return (T) new Integer((int) jcrValue.getLong());
+ } else if (Long.class == type) {
+ return (T) new Long(jcrValue.getLong());
+ } else if (Float.class == type) {
+ return (T) new Float(jcrValue.getDouble());
+ } else if (Double.class == type) {
+ return (T) new Double(jcrValue.getDouble());
+ } else if (Boolean.class == type) {
+ return (T) Boolean.valueOf(jcrValue.getBoolean());
+ } else if (Date.class == type) {
+ return (T) jcrValue.getDate().getTime();
+ } else if (Calendar.class == type) {
+ return (T) jcrValue.getDate();
+ } else if (Value.class == type) {
+ return (T) jcrValue;
+ }
+
+ // fallback in case of unsupported type
+ return null;
+ }
+
+ private Class<?> normalizeClass(Class<?> type) {
+ if (Calendar.class.isAssignableFrom(type)) {
+ type = Calendar.class;
+ } else if (Date.class.isAssignableFrom(type)) {
+ type = Date.class;
+ } else if (Value.class.isAssignableFrom(type)) {
+ type = Value.class;
+ } else if (Property.class.isAssignableFrom(type)) {
+ type = Property.class;
+ }
+ return type;
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableValueMap.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableValueMap.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev Url
Added: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/META-INF/LICENSE
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/META-INF/LICENSE?rev=748454&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/META-INF/LICENSE (added)
+++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/META-INF/LICENSE Fri Feb 27 09:19:41 2009
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
Added: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/META-INF/NOTICE
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/META-INF/NOTICE?rev=748454&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/META-INF/NOTICE (added)
+++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/META-INF/NOTICE Fri Feb 27 09:19:41 2009
@@ -0,0 +1,5 @@
+Apache Sling Jackrabbit UserManager Support
+Copyright 2008-2009 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
Added: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=748454&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/OSGI-INF/metatype/metatype.properties (added)
+++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/OSGI-INF/metatype/metatype.properties Fri Feb 27 09:19:41 2009
@@ -0,0 +1,36 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+
+#
+# This file contains localization strings for configuration labels and
+# descriptions as used in the metatype.xml descriptor generated by the
+# the Sling SCR plugin
+
+authorizable.resourceprovider.name = Resolver for UserManager resources
+authorizable.resourceprovider.description = Handles resolving resources for the \
+ jackrabbit UserManager.
+
+createUser.post.operation.name = Create User Sling Post Operation
+createUser.post.operation.description = The Sling POST Operation to handle create user \
+ requests in Sling.
+
+self.registration.enabled.name = Self-Registration Enabled
+self.registration.enabled.description = When selected, the anonymous user is allowed to \
+ register a new user with the system.
Propchange: incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/resources/OSGI-INF/metatype/metatype.properties
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/sling/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/pom.xml?rev=748454&r1=748453&r2=748454&view=diff
==============================================================================
--- incubator/sling/trunk/pom.xml (original)
+++ incubator/sling/trunk/pom.xml Fri Feb 27 09:19:41 2009
@@ -88,6 +88,7 @@
<module>bundles/jcr/contentloader</module>
<module>bundles/jcr/jackrabbit-api</module>
<module>bundles/jcr/jackrabbit-server</module>
+ <module>bundles/jcr/jackrabbit-usermanager</module>
<module>bundles/jcr/ocm</module>
<module>bundles/jcr/resource</module>
<module>bundles/jcr/webdav</module>