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.post;
 
 import java.util.List;
 
 

Rest Service Description

Deletes an Authorizable, currently a user or a group. Maps on to nodes of resourceType sling/users or sling/users like /rep:system/rep:userManager/rep:users or /rep:system/rep:userManager/rep:groups mapped to a resource url /system/userManager/user or /system/userManager/group. This servlet responds at /system/userManager/user.delete.html or /system/userManager/group.delete.html. The servlet also responds to single delete requests eg /system/userManager/group/newGroup.delete.html

Methods

  • POST

Post Parameters

:applyTo
An array of relative resource references to Authorizables to be deleted, if this parameter is present, the url is ignored and all the Authorizables in the list are removed.

Response

200
Success, no body.
404
The resource was not found
500
Failure

Example

curl -Fgo=1 http://localhost:8080/system/userManager/user/ieb.delete.html
 
 @Component (immediate=true, metatype=true,
 		label="%deleteAuthorizable.post.operation.name",
 		description="%deleteAuthorizable.post.operation.description")
 @Service (value={
 	Servlet.class,
 	DeleteUser.class,
 	DeleteGroup.class,
 })		
 	@Property (name="sling.servlet.resourceTypes",
 			value={
 					"sling/user",
 					"sling/group",
 					"sling/userManager"
 				}),
	@Property (name="sling.servlet.methods",
			value="POST"),
	@Property (name="sling.servlet.selectors",
			value="delete")
})
        implements DeleteUserDeleteGroupDeleteAuthorizables {
    private static final long serialVersionUID = 5874621724096106496L;
    /*
     * (non-Javadoc)
     * @see
     * org.apache.sling.jackrabbit.usermanager.post.AbstractAuthorizablePostServlet
     * #handleOperation(org.apache.sling.api.SlingHttpServletRequest,
     * org.apache.sling.api.servlets.HtmlResponse, java.util.List)
     */
    @Override
    protected void handleOperation(SlingHttpServletRequest request,
    		AbstractPostResponse responseList<Modificationchanges)
            throws RepositoryException {
        Session session = request.getResourceResolver().adaptTo(Session.class);
        Resource resource = request.getResource();
        String[] applyTo = request.getParameterValues(.);
        if (applyTo != null) {
            deleteAuthorizables(session,
                    resource,
                    applyTo
                    changes);
        } else {
            Authorizable item = resource.adaptTo(Authorizable.class);
            if (item == null) {
                String msg = "Missing source " + resource.getPath()
                    + " for delete";
                response.setStatus(.msg);
                throw new ResourceNotFoundException(msg);
            } else {
                if (item instanceof User) {
                    deleteUser(sessionitem.getID(), changes);
                } else if (item instanceof Group) {
                    deleteGroup(sessionitem.getID(), changes);
                }
            }
        }
    }
    
    /* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.DeleteUser#deleteUser(javax.jcr.Session, java.lang.String, java.util.List)
     */
    public void deleteUser(Session jcrSessionString name,
            List<Modificationchangesthrows RepositoryException {
        User user;
        UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
        Authorizable authorizable = userManager.getAuthorizable(name);
        if (authorizable instanceof User) {
            user = (User)authorizable;
        } else {
            throw new ResourceNotFoundException(
                "User to delete could not be determined");
        }
        
                            + user.getID();
        user.remove();
        changes.add(Modification.onDeleted(userPath));
    }
    
    /* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.DeleteGroup#deleteGroup(javax.jcr.Session, java.lang.String, java.util.List)
     */
    public void deleteGroup(Session jcrSession
                            String name,
                            List<Modificationchangesthrows RepositoryException {
        Group group;
        UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
        Authorizable authorizable = userManager.getAuthorizable(name);
        if (authorizable instanceof Group) {
            group = (Group)authorizable;
        } else {
            throw new ResourceNotFoundException(
                "Group to delete could not be determined");
        }
        
                                + group.getID();
        group.remove();
        changes.add(Modification.onDeleted(groupPath));
    }
    /* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.DeleteAuthorizables#deleteAuthorizables(javax.jcr.Session, org.apache.sling.api.resource.Resource, java.lang.String[], java.util.List)
     */
    public void deleteAuthorizables(Session jcrSession
                                    Resource baseResource,
                                    String[] paths
                                    List<Modificationchanges)
            throws RepositoryException {
        ApplyToIterator iterator = new ApplyToIterator(baseResourcepaths);
        while (iterator.hasNext()) {
            Resource resource = iterator.next();
            Authorizable item = resource.adaptTo(Authorizable.class);
            if (item != null) {
                item.remove();
                changes.add(Modification.onDeleted(resource.getPath()));
            }
        }
    }
    private static class ApplyToIterator implements Iterator<Resource> {
        private final ResourceResolver resolver;
        private final Resource baseResource;
        private final String [] paths;
        private int pathIndex;
        private Resource nextResource;
        ApplyToIterator(Resource baseResourceString [] paths) {
            this. = baseResource.getResourceResolver();
            this. = baseResource;
            this. = paths;
            this. = 0;
             = seek();
        }
        public boolean hasNext() {
            return  != null;
        }
        public Resource next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            Resource result = ;
             = seek();
            return result;
        }
        public void remove() {
            throw new UnsupportedOperationException();
        }
        private Resource seek() {
            while ( < .) {
                String path = [];
                ++;
                Resource res = .getResource(path);
                if (res != null) {
                    return res;
                }
            }
            // no more elements in the array
            return null;
        }
    }
New to GrepCode? Check out our FAQ X