Search in sources :

Example 16 with ResourceNotFoundException

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

the class HtmlRendererServlet method doGet.

@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException {
    final Resource r = req.getResource();
    if (ResourceUtil.isNonExistingResource(r)) {
        throw new ResourceNotFoundException("No data to render.");
    }
    resp.setContentType(req.getResponseContentType());
    resp.setCharacterEncoding("UTF-8");
    final PrintWriter pw = resp.getWriter();
    final boolean isIncluded = req.getAttribute(SlingConstants.ATTR_REQUEST_SERVLET) != null;
    @SuppressWarnings({ "rawtypes" }) final Map map = r.adaptTo(Map.class);
    if (map != null) {
        printProlog(pw, isIncluded);
        printResourceInfo(pw, r);
        render(pw, r, map);
        printEpilog(pw, isIncluded);
    } else if (r.adaptTo(String.class) != null) {
        printProlog(pw, isIncluded);
        printResourceInfo(pw, r);
        render(pw, r, r.adaptTo(String.class));
        printEpilog(pw, isIncluded);
    } else if (r.adaptTo(String[].class) != null) {
        printProlog(pw, isIncluded);
        printResourceInfo(pw, r);
        render(pw, r, r.adaptTo(String[].class));
        printEpilog(pw, isIncluded);
    } else {
        if (!isIncluded) {
            // NO Content
            resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
        }
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 17 with ResourceNotFoundException

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

the class PlainTextRendererServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp) throws ServletException, IOException {
    final Resource r = req.getResource();
    if (ResourceUtil.isNonExistingResource(r)) {
        throw new ResourceNotFoundException("No data to render.");
    }
    /*
         * TODO if(srd != null) { renderSyntheticResource(req, resp, srd);
         * return; }
         */
    resp.setContentType(req.getResponseContentType());
    resp.setCharacterEncoding("UTF-8");
    final PrintWriter pw = resp.getWriter();
    @SuppressWarnings("unchecked") final Map map = r.adaptTo(Map.class);
    if (map != null) {
        dump(pw, r, map);
    } else if (r.adaptTo(String.class) != null) {
        printPropertyValue(pw, ResourceUtil.getName(r), r.adaptTo(String.class), false);
    } else if (r.adaptTo(String[].class) != null) {
        printPropertyValue(pw, ResourceUtil.getName(r), r.adaptTo(String[].class), false);
    } else {
        resp.sendError(HttpServletResponse.SC_NO_CONTENT);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 18 with ResourceNotFoundException

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

the class StreamRendererServlet method processRequest.

private void processRequest(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    // whether this servlet is called as of a request include
    final boolean included = request.getAttribute(SlingConstants.ATTR_REQUEST_SERVLET) != null;
    // ensure no extension or "res"
    String ext = request.getRequestPathInfo().getExtension();
    if (ext != null && !ext.equals(EXT_RES)) {
        request.getRequestProgressTracker().log("StreamRendererServlet does not support for extension " + ext);
        if (included || response.isCommitted()) {
            log.error("StreamRendererServlet does not support extension {}", ext);
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
        return;
    }
    Resource resource = request.getResource();
    if (ResourceUtil.isNonExistingResource(resource)) {
        throw new ResourceNotFoundException("No data to render.");
    }
    if ("/".equals(request.getRequestPathInfo().getSuffix())) {
        renderDirectory(request, response, included);
        return;
    }
    // check the last modification time and If-Modified-Since header
    if (!included) {
        ResourceMetadata meta = resource.getResourceMetadata();
        long modifTime = meta.getModificationTime();
        if (unmodified(request, modifTime)) {
            response.setStatus(SC_NOT_MODIFIED);
            return;
        }
    }
    // fall back to plain text rendering if the resource has no stream
    if (resource.getResourceType().equals(JcrConstants.NT_LINKEDFILE)) {
        final ValueMap vm = resource.adaptTo(ValueMap.class);
        final String actualResourcePath = vm.get(JcrConstants.JCR_CONTENT, String.class);
        resource = request.getResourceResolver().getResource(actualResourcePath);
    }
    InputStream stream = resource.adaptTo(InputStream.class);
    if (stream != null) {
        if (isHeadRequest(request)) {
            setContentLength(response, resource.getResourceMetadata().getContentLength());
            setHeaders(resource, response);
            return;
        }
        streamResource(resource, stream, included, request, response);
    } else {
        // the resource is the root, do not redirect, immediately index
        if (isRootResourceRequest(resource)) {
            renderDirectory(request, response, included);
        } else if (included || response.isCommitted()) {
            // request is included or committed, not redirecting
            request.getRequestProgressTracker().log("StreamRendererServlet: Not redirecting with trailing slash, response is committed or request included");
            log.warn("StreamRendererServlet: Not redirecting with trailing slash, response is committed or request included");
        } else {
            // redirect to this with trailing slash to render the index
            String url = request.getResourceResolver().map(request, resource.getPath()) + "/";
            response.sendRedirect(url);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 19 with ResourceNotFoundException

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

the class AbstractPostServlet method doPost.

/*
     * (non-Javadoc)
     * @see
     * org.apache.sling.api.servlets.SlingAllMethodsServlet#doPost(org.apache
     * .sling.api.SlingHttpServletRequest,
     * org.apache.sling.api.SlingHttpServletResponse)
     */
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse httpResponse) throws ServletException, IOException {
    // prepare the response
    AbstractPostResponse response = createHtmlResponse(request);
    response.setReferer(request.getHeader("referer"));
    // calculate the paths
    String path = getItemPath(request);
    response.setPath(path);
    // location
    response.setLocation(externalizePath(request, path));
    // parent location
    path = ResourceUtil.getParent(path);
    if (path != null) {
        response.setParentLocation(externalizePath(request, path));
    }
    Session session = request.getResourceResolver().adaptTo(Session.class);
    final List<Modification> changes = new ArrayList<Modification>();
    try {
        handleOperation(request, response, changes);
        // set changes on html response
        for (Modification change : changes) {
            switch(change.getType()) {
                case MODIFY:
                    response.onModified(change.getSource());
                    break;
                case DELETE:
                    response.onDeleted(change.getSource());
                    break;
                case MOVE:
                    response.onMoved(change.getSource(), change.getDestination());
                    break;
                case COPY:
                    response.onCopied(change.getSource(), change.getDestination());
                    break;
                case CREATE:
                    response.onCreated(change.getSource());
                    break;
                case ORDER:
                    response.onChange("ordered", change.getSource(), change.getDestination());
                    break;
            }
        }
        if (session.hasPendingChanges()) {
            session.save();
        }
    } catch (ResourceNotFoundException rnfe) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND, rnfe.getMessage());
    } catch (Throwable throwable) {
        log.debug("Exception while handling POST " + request.getResource().getPath() + " with " + getClass().getName(), throwable);
        response.setError(throwable);
    } finally {
        try {
            if (session.hasPendingChanges()) {
                session.refresh(false);
            }
        } catch (RepositoryException e) {
            log.warn("RepositoryException in finally block: {}", e.getMessage(), e);
        }
    }
    // check for redirect URL if processing succeeded
    if (response.isSuccessful()) {
        String redirect = getRedirectUrl(request, response);
        if (redirect != null) {
            httpResponse.sendRedirect(redirect);
            return;
        }
    }
    // create a html response and send if unsuccessful or no redirect
    response.send(httpResponse, isSetStatus(request));
}
Also used : Modification(org.apache.sling.servlets.post.Modification) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) AbstractPostResponse(org.apache.sling.servlets.post.AbstractPostResponse) Session(javax.jcr.Session)

Example 20 with ResourceNotFoundException

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

the class ChangeUserPasswordServlet method changePassword.

/* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.ChangeUserPassword#changePassword(javax.jcr.Session, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.List)
     */
public User changePassword(Session jcrSession, String name, String oldPassword, String newPassword, String newPasswordConfirm, List<Modification> changes) throws RepositoryException {
    if ("anonymous".equals(name)) {
        throw new RepositoryException("Can not change the password of the anonymous user.");
    }
    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 update could not be determined");
    }
    //SLING-2069: if the current user is an administrator, then a missing oldPwd is ok,
    // otherwise the oldPwd must be supplied.
    boolean administrator = false;
    // check that the submitted parameter values have valid values.
    if (oldPassword == null || oldPassword.length() == 0) {
        try {
            UserManager um = AccessControlUtil.getUserManager(jcrSession);
            User currentUser = (User) um.getAuthorizable(jcrSession.getUserID());
            administrator = currentUser.isAdmin();
            if (!administrator) {
                //check if the user is a member of the 'User administrator' group
                Authorizable userAdmin = um.getAuthorizable(this.userAdminGroupName);
                if (userAdmin instanceof Group) {
                    boolean isMember = ((Group) userAdmin).isMember(currentUser);
                    if (isMember) {
                        administrator = true;
                    }
                }
            }
        } catch (Exception ex) {
            log.warn("Failed to determine if the user is an admin, assuming not. Cause: " + ex.getMessage());
            administrator = false;
        }
        if (!administrator) {
            throw new RepositoryException("Old Password was not submitted");
        }
    }
    if (newPassword == null || newPassword.length() == 0) {
        throw new RepositoryException("New Password was not submitted");
    }
    if (!newPassword.equals(newPasswordConfirm)) {
        throw new RepositoryException("New Password does not match the confirmation password");
    }
    try {
        if (oldPassword != null && oldPassword.length() > 0) {
            // verify old password
            user.changePassword(newPassword, oldPassword);
        } else {
            user.changePassword(newPassword);
        }
        final String passwordPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PREFIX + user.getID() + "/rep:password";
        changes.add(Modification.onModified(passwordPath));
    } catch (RepositoryException re) {
        throw new RepositoryException("Failed to change user password.", re);
    }
    return user;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) User(org.apache.jackrabbit.api.security.user.User) UserManager(org.apache.jackrabbit.api.security.user.UserManager) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) RepositoryException(javax.jcr.RepositoryException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) RepositoryException(javax.jcr.RepositoryException) 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