Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.impl.resource;
 
 import java.util.List;
 
 
Resource Provider implementation for jackrabbit UserManager resources.
 
 @Component (immediate=true, metatype=true,
 		label="%authorizable.resourceprovider.name",
 		description="%authorizable.resourceprovider.description")
 @Service (value=ResourceProvider.class)
 	@Property (name="service.description"
 			value="Resource provider implementation for UserManager resources"),
 	@Property (name="service.vendor",
 			value="The Apache Software Foundation"),
 	@Property (name="provider.roots",
 			value="/system/userManager/")			
 })
 public class AuthorizableResourceProvider implements ResourceProvider {

    
default log
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     public static final String SYSTEM_USER_MANAGER_PATH = "/system/userManager";
 
         + "/user";
 
         + "/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 requestString path) {
         return getResource(resourceResolverpath);
     }
 
     /*
      * (non-Javadoc)
      * @see
      * org.apache.sling.api.resource.ResourceProvider#getResource(org.apache
      * .sling.api.resource.ResourceResolver, java.lang.String)
      */
     public Resource getResource(ResourceResolver resourceResolverString path) {
        // handle resources for the virtual container resources
        if (path.equals()) {
            return new SyntheticResource(resourceResolverpath,
                "sling/userManager");
        } else if (path.equals()) {
            return new SyntheticResource(resourceResolverpath"sling/users");
        } else if (path.equals()) {
            return new SyntheticResource(resourceResolverpath"sling/groups");
        }
        // the principalId should be the first segment after the prefix
        String pid = null;
        if (path.startsWith()) {
            pid = path.substring(.length());
        } else if (path.startsWith()) {
            pid = path.substring(.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,
                                resourceResolverpath);
                        }
                    }
                }
            } catch (RepositoryException re) {
                throw new SlingException(
                    "Error looking up Authorizable for principal: " + pidre);
            }
        }
        return null;
    }
    /*
     * (non-Javadoc)
     * @see
     * org.apache.sling.api.resource.ResourceProvider#listChildren(org.apache
     * .sling.api.resource.Resource)
     */
    public Iterator<ResourcelistChildren(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 (.equals(path)) {
                List<Resourceresources = new ArrayList<Resource>();
                if (resourceResolver != null) {
                    resources.add(getResource(resourceResolver,
                        ));
                    resources.add(getResource(resourceResolver,
                        ));
                }
                return resources.iterator();
            }
            int searchType = -1;
            if (.equals(path)) {
                searchType = .;
            } else if (.equals(path)) {
                searchType = .;
            }
            if (searchType != -1) {
                PrincipalIterator principals = null;
                Session session = resourceResolver.adaptTo(Session.class);
                if (session != null) {
                    PrincipalManager principalManager = AccessControlUtil.getPrincipalManager(session);
                    principals = principalManager.getPrincipals(searchType);
                }
                if (principals != null) {
                    return new ChildrenIterator(parentprincipals);
                }
            }
        } 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 parentPrincipalIterator principals) {
            this. = parent;
            this. = principals;
        }
        public boolean hasNext() {
            return .hasNext();
        }
        public Resource next() {
            Principal nextPrincipal = .nextPrincipal();
            try {
                ResourceResolver resourceResolver = .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 = 
                                        + nextPrincipal.getName();
                                } else {
                                    path = 
                                        + nextPrincipal.getName();
                                }
                                return new AuthorizableResource(authorizable,
                                    resourceResolverpath);
                            }
                        }
                    }
                }
            } catch (RepositoryException re) {
                .error("Exception while looking up authorizable resource.",
                    re);
            }
            return null;
        }
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
New to GrepCode? Check out our FAQ X