Modified: 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=782945&r1=782944&r2=782945&view=diff ============================================================================== --- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResourceProvider.java (original) +++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableResourceProvider.java Tue Jun 9 10:16:54 2009 @@ -44,177 +44,204 @@ * @scr.component immediate="true" label="%authorizable.resourceprovider.name" * description="authorizable.resourceprovider.description" * @scr.property name="service.description" - * value="Resource provider implementation for UserManager resources" + * 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_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_PATH = SYSTEM_USER_MANAGER_PATH + + "/user"; - 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 + "/"; + public static final String SYSTEM_USER_MANAGER_GROUP_PATH = SYSTEM_USER_MANAGER_PATH + + "/group"; - /* (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) { + 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) { - /* (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); - } - } + // 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(); + } + } - /* (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(); - } - } - }
Modified: 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=782945&r1=782944&r2=782945&view=diff ============================================================================== --- incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableValueMap.java (original) +++ incubator/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/resource/AuthorizableValueMap.java Tue Jun 9 10:16:54 2009 @@ -45,10 +45,15 @@ */ 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 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; + + private Authorizable authorizable; public AuthorizableValueMap(Authorizable authorizable) { this.authorizable = authorizable; @@ -56,17 +61,17 @@ this.fullyRead = false; } - @SuppressWarnings("unchecked") - public <T> T get(String name, Class<T> type) { + @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) { + @SuppressWarnings("unchecked") + public <T> T get(String name, T defaultValue) { if (defaultValue == null) { return (T) get(name); } @@ -81,51 +86,50 @@ } return value; - } + } - public boolean containsKey(Object key) { + public boolean containsKey(Object key) { return get(key) != null; - } + } - public boolean containsValue(Object value) { + public boolean containsValue(Object value) { readFully(); return cache.containsValue(value); - } + } - public Set<java.util.Map.Entry<String, Object>> entrySet() { + public Set<java.util.Map.Entry<String, Object>> entrySet() { readFully(); return cache.entrySet(); - } + } - public Object get(Object key) { + public Object get(Object key) { Object value = cache.get(key); if (value == null) { value = read((String) key); } return value; - } - + } - public Set<String> keySet() { + public Set<String> keySet() { readFully(); return cache.keySet(); - } + } - public int size() { + public int size() { readFully(); return cache.size(); - } + } - public boolean isEmpty() { + public boolean isEmpty() { return size() == 0; - } + } - public Collection<Object> values() { + public Collection<Object> values() { readFully(); return cache.values(); - } - + } + protected Object read(String key) { // if the item has been completely read, we need not check @@ -135,14 +139,14 @@ } if (hiddenProperties.contains(key)) { - return null; + return null; } - + try { if (authorizable.hasProperty(key)) { Value[] property = authorizable.getProperty(key); Object value = valuesToJavaObject(property); - cache.put(key, value); + cache.put(key, value); return value; } } catch (RepositoryException re) { @@ -152,37 +156,38 @@ // property not found or some error accessing it return null; } - - protected Object valuesToJavaObject(Value [] values) throws RepositoryException { + + protected Object valuesToJavaObject(Value[] values) + throws RepositoryException { if (values == null) { - return null; + return null; } else if (values.length == 1) { - return JcrResourceUtil.toJavaObject(values[0]); + 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; + 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() { + protected void readFully() { if (!fullyRead) { try { Iterator pi = authorizable.getPropertyNames(); while (pi.hasNext()) { - String key = (String)pi.next(); + String key = (String) pi.next(); if (hiddenProperties.contains(key)) { - continue; //skip it. + continue; // skip it. } if (!cache.containsKey(key)) { - Value[] property = authorizable.getProperty(key); - Object value = valuesToJavaObject(property); - cache.put(key, value); + Value[] property = authorizable.getProperty(key); + Object value = valuesToJavaObject(property); + cache.put(key, value); } } fullyRead = true; @@ -191,24 +196,25 @@ } } } - + // ---------- Unsupported Modification methods - - public Object remove(Object arg0) { + + public Object remove(Object arg0) { throw new UnsupportedOperationException(); - } - public void clear() { + } + + public void clear() { throw new UnsupportedOperationException(); - } - public Object put(String arg0, Object arg1) { + } + + public Object put(String arg0, Object arg1) { throw new UnsupportedOperationException(); - } + } - public void putAll(Map<? extends String, ? extends Object> arg0) { + public void putAll(Map<? extends String, ? extends Object> arg0) { throw new UnsupportedOperationException(); - } - - + } + // ---------- Implementation helper @SuppressWarnings("unchecked") @@ -220,9 +226,9 @@ Value[] values = authorizable.getProperty(name); if (values == null) { - return null; + return null; } - + boolean multiValue = values.length > 1; boolean array = type.isArray(); @@ -236,8 +242,8 @@ } else { Value value = values[0]; if (array) { - result = (T) convertToArray( - new Value[] { value }, type.getComponentType()); + result = (T) convertToArray(new Value[] { value }, + type.getComponentType()); } else { result = convertToType(-1, value, type); } @@ -254,26 +260,26 @@ // 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); - } - } + 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()); + @SuppressWarnings("unchecked") + T[] result = (T[]) Array.newInstance(type, values.size()); - return values.toArray(result); + return values.toArray(result); } - + @SuppressWarnings("unchecked") - private <T> T convertToType(int index, Value jcrValue, - Class<T> type) throws ValueFormatException, RepositoryException { + private <T> T convertToType(int index, Value jcrValue, Class<T> type) + throws ValueFormatException, RepositoryException { if (String.class == type) { return (T) jcrValue.getString(); @@ -302,7 +308,7 @@ // fallback in case of unsupported type return null; } - + private Class<?> normalizeClass(Class<?> type) { if (Calendar.class.isAssignableFrom(type)) { type = Calendar.class; @@ -315,5 +321,5 @@ } return type; } - + } \ No newline at end of file