Search in sources :

Example 1 with ResourceNotFoundException

use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.

the class SlingRequestProcessorImpl method doProcessRequest.

/**
     * This method is directly called by the Sling main servlet.
     */
public void doProcessRequest(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final ResourceResolver resourceResolver) throws IOException {
    // setting the Sling request and response
    final RequestData requestData = new RequestData(this, servletRequest, servletResponse);
    final SlingHttpServletRequest request = requestData.getSlingRequest();
    final SlingHttpServletResponse response = requestData.getSlingResponse();
    // record the request for the web console display
    RequestHistoryConsolePlugin.recordRequest(request);
    try {
        final ServletResolver sr = this.servletResolver;
        // check that we have all required services
        if (resourceResolver == null) {
            throw new UnavailableException("ResourceResolver");
        } else if (sr == null) {
            throw new UnavailableException("ServletResolver");
        }
        // initialize the request data - resolve resource and servlet
        Resource resource = requestData.initResource(resourceResolver);
        requestData.initServlet(resource, sr);
        FilterHandle[] filters = filterManager.getFilters(FilterChainType.REQUEST);
        if (filters != null) {
            FilterChain processor = new RequestSlingFilterChain(this, filters);
            request.getRequestProgressTracker().log("Applying " + FilterChainType.REQUEST + "filters");
            processor.doFilter(request, response);
        } else {
            // no filters, directly call resource level filters and servlet
            processComponent(request, response, FilterChainType.COMPONENT);
        }
    } catch (final SlingHttpServletResponseImpl.WriterAlreadyClosedException wace) {
        log.error("Writer has already been closed.", wace);
    } catch (ResourceNotFoundException rnfe) {
        // send this exception as a 404 status
        log.info("service: Resource {} not found", rnfe.getResource());
        handleError(HttpServletResponse.SC_NOT_FOUND, rnfe.getMessage(), request, response);
    } catch (final SlingException se) {
        // we assume, that this is the name of the causing servlet
        if (requestData.getActiveServletName() != null) {
            request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
        }
        // send this exception as is (albeit unwrapping and wrapped
        // exception.
        Throwable t = se;
        while (t instanceof SlingException && t.getCause() != null) {
            t = t.getCause();
        }
        log.error("service: Uncaught SlingException", t);
        handleError(t, request, response);
    } catch (AccessControlException ace) {
        // SLING-319 if anything goes wrong, send 403/FORBIDDEN
        log.info("service: Authenticated user {} does not have enough rights to executed requested action", request.getRemoteUser());
        handleError(HttpServletResponse.SC_FORBIDDEN, null, request, response);
    } catch (UnavailableException ue) {
        // exception is thrown before the SlingHttpServletRequest/Response
        // is properly set up due to missing dependencies. In this case
        // we must not use the Sling error handling infrastructure but
        // just return a 503 status response handled by the servlet
        // container environment
        final int status = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
        final String errorMessage = ue.getMessage() + " service missing, cannot service requests";
        log.error("{} , sending status {}", errorMessage, status);
        servletResponse.sendError(status, errorMessage);
    } catch (IOException ioe) {
        // forward IOException up the call chain to properly handle it
        throw ioe;
    } catch (Throwable t) {
        // we assume, that this is the name of the causing servlet
        if (requestData.getActiveServletName() != null) {
            request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
        }
        log.error("service: Uncaught Throwable", t);
        handleError(t, request, response);
    } finally {
        if (mbean != null) {
            mbean.addRequestData(requestData);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) FilterHandle(org.apache.sling.engine.impl.filter.FilterHandle) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) SlingComponentFilterChain(org.apache.sling.engine.impl.filter.SlingComponentFilterChain) FilterChain(javax.servlet.FilterChain) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain) UnavailableException(javax.servlet.UnavailableException) Resource(org.apache.sling.api.resource.Resource) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) ServletResolver(org.apache.sling.api.servlets.ServletResolver) RequestData(org.apache.sling.engine.impl.request.RequestData) SlingException(org.apache.sling.api.SlingException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Example 2 with ResourceNotFoundException

use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.

the class DeleteAcesServlet method deleteAces.

/* (non-Javadoc)
	 * @see org.apache.sling.jcr.jackrabbit.accessmanager.DeleteAces#deleteAces(javax.jcr.Session, java.lang.String, java.lang.String[])
	 */
public void deleteAces(Session jcrSession, String resourcePath, String[] principalNamesToDelete) throws RepositoryException {
    if (principalNamesToDelete == null) {
        throw new RepositoryException("principalIds were not sumitted.");
    } else {
        if (jcrSession == null) {
            throw new RepositoryException("JCR Session not found");
        }
        if (resourcePath == null) {
            throw new ResourceNotFoundException("Resource path was not supplied.");
        }
        Item item = jcrSession.getItem(resourcePath);
        if (item != null) {
            resourcePath = item.getPath();
        } else {
            throw new ResourceNotFoundException("Resource is not a JCR Node");
        }
        //load the principalIds array into a set for quick lookup below
        Set<String> pidSet = new HashSet<String>();
        pidSet.addAll(Arrays.asList(principalNamesToDelete));
        try {
            AccessControlManager accessControlManager = AccessControlUtil.getAccessControlManager(jcrSession);
            AccessControlList updatedAcl = getAccessControlList(accessControlManager, resourcePath, false);
            //keep track of the existing Aces for the target principal
            AccessControlEntry[] accessControlEntries = updatedAcl.getAccessControlEntries();
            List<AccessControlEntry> oldAces = new ArrayList<AccessControlEntry>();
            for (AccessControlEntry ace : accessControlEntries) {
                if (pidSet.contains(ace.getPrincipal().getName())) {
                    oldAces.add(ace);
                }
            }
            //remove the old aces
            if (!oldAces.isEmpty()) {
                for (AccessControlEntry ace : oldAces) {
                    updatedAcl.removeAccessControlEntry(ace);
                }
            }
            //apply the changed policy
            accessControlManager.setPolicy(resourcePath, updatedAcl);
        } catch (RepositoryException re) {
            throw new RepositoryException("Failed to delete access control.", re);
        }
    }
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) AccessControlList(javax.jcr.security.AccessControlList) Item(javax.jcr.Item) ArrayList(java.util.ArrayList) AccessControlEntry(javax.jcr.security.AccessControlEntry) RepositoryException(javax.jcr.RepositoryException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) HashSet(java.util.HashSet)

Example 3 with ResourceNotFoundException

use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.

the class ModifyAceServlet method modifyAce.

/* (non-Javadoc)
	 * @see org.apache.sling.jcr.jackrabbit.accessmanager.ModifyAce#modifyAce(javax.jcr.Session, java.lang.String, java.lang.String, java.util.Map, java.lang.String)
	 */
public void modifyAce(Session jcrSession, String resourcePath, String principalId, Map<String, String> privileges, String order) throws RepositoryException {
    if (jcrSession == null) {
        throw new RepositoryException("JCR Session not found");
    }
    if (principalId == null) {
        throw new RepositoryException("principalId was not submitted.");
    }
    PrincipalManager principalManager = AccessControlUtil.getPrincipalManager(jcrSession);
    Principal principal = principalManager.getPrincipal(principalId);
    if (resourcePath == null) {
        throw new ResourceNotFoundException("Resource path was not supplied.");
    }
    Item item = jcrSession.getItem(resourcePath);
    if (item != null) {
        resourcePath = item.getPath();
    } else {
        throw new ResourceNotFoundException("Resource is not a JCR Node");
    }
    // Collect the modified privileges from the request.
    Set<String> grantedPrivilegeNames = new HashSet<String>();
    Set<String> deniedPrivilegeNames = new HashSet<String>();
    Set<String> removedPrivilegeNames = new HashSet<String>();
    Set<Entry<String, String>> entrySet = privileges.entrySet();
    for (Entry<String, String> entry : entrySet) {
        String privilegeName = entry.getKey();
        if (privilegeName.startsWith("privilege@")) {
            privilegeName = privilegeName.substring(10);
        }
        String parameterValue = entry.getValue();
        if (parameterValue != null && parameterValue.length() > 0) {
            if ("granted".equals(parameterValue)) {
                grantedPrivilegeNames.add(privilegeName);
            } else if ("denied".equals(parameterValue)) {
                deniedPrivilegeNames.add(privilegeName);
            } else if ("none".equals(parameterValue)) {
                removedPrivilegeNames.add(privilegeName);
            }
        }
    }
    // Make the actual changes.
    try {
        AccessControlUtil.replaceAccessControlEntry(jcrSession, resourcePath, principal, grantedPrivilegeNames.toArray(new String[grantedPrivilegeNames.size()]), deniedPrivilegeNames.toArray(new String[deniedPrivilegeNames.size()]), removedPrivilegeNames.toArray(new String[removedPrivilegeNames.size()]), order);
        if (jcrSession.hasPendingChanges()) {
            jcrSession.save();
        }
    } catch (RepositoryException re) {
        throw new RepositoryException("Failed to create ace.", re);
    }
}
Also used : PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) Item(javax.jcr.Item) Entry(java.util.Map.Entry) RepositoryException(javax.jcr.RepositoryException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) Principal(java.security.Principal) HashSet(java.util.HashSet)

Example 4 with ResourceNotFoundException

use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.

the class DeleteAuthorizableServlet method deleteGroup.

/* (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<Modification> changes) throws 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");
    }
    String groupPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID();
    group.remove();
    changes.add(Modification.onDeleted(groupPath));
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) DeleteGroup(org.apache.sling.jackrabbit.usermanager.DeleteGroup) UserManager(org.apache.jackrabbit.api.security.user.UserManager) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Example 5 with ResourceNotFoundException

use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.

the class UpdateGroupServlet method updateGroup.

/* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.UpdateGroup#updateGroup(javax.jcr.Session, java.lang.String, java.util.Map, java.util.List)
     */
public Group updateGroup(Session jcrSession, String name, Map<String, ?> properties, List<Modification> changes) throws RepositoryException {
    Group group = null;
    UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
    Authorizable authorizable = userManager.getAuthorizable(name);
    if (authorizable instanceof Group) {
        group = (Group) authorizable;
    } else {
        throw new ResourceNotFoundException("Group to update could not be determined");
    }
    String groupPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID();
    Collection<RequestProperty> reqProperties = collectContent(properties);
    try {
        // cleanup any old content (@Delete parameters)
        processDeletes(group, reqProperties, changes);
        // write content from form
        writeContent(jcrSession, group, reqProperties, changes);
        // update the group memberships
        ResourceResolver resourceResolver = null;
        try {
            //create a resource resolver to resolve the relative paths used for group membership values
            final Map<String, Object> authInfo = new HashMap<String, Object>();
            authInfo.put(org.apache.sling.jcr.resource.api.JcrResourceConstants.AUTHENTICATION_INFO_SESSION, jcrSession);
            resourceResolver = resourceResolverFactory.getResourceResolver(authInfo);
            Resource baseResource = resourceResolver.getResource(groupPath);
            updateGroupMembership(baseResource, properties, group, changes);
        } catch (LoginException e) {
            throw new RepositoryException(e);
        } finally {
            if (resourceResolver != null) {
                resourceResolver.close();
            }
        }
    } catch (RepositoryException re) {
        throw new RepositoryException("Failed to update group.", re);
    }
    return group;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) UpdateGroup(org.apache.sling.jackrabbit.usermanager.UpdateGroup) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) RepositoryException(javax.jcr.RepositoryException) RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) LoginException(org.apache.sling.api.resource.LoginException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)22 RepositoryException (javax.jcr.RepositoryException)10 Resource (org.apache.sling.api.resource.Resource)10 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)6 UserManager (org.apache.jackrabbit.api.security.user.UserManager)5 ArrayList (java.util.ArrayList)4 Session (javax.jcr.Session)4 Group (org.apache.jackrabbit.api.security.user.Group)4 User (org.apache.jackrabbit.api.security.user.User)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Item (javax.jcr.Item)3 ServletException (javax.servlet.ServletException)3 InputStream (java.io.InputStream)2 PrintWriter (java.io.PrintWriter)2 Principal (java.security.Principal)2 LinkedHashMap (java.util.LinkedHashMap)2 Entry (java.util.Map.Entry)2