Search in sources :

Example 1 with SlingException

use of org.apache.sling.api.SlingException in project sling by apache.

the class ResourceResolverImpl method isResourceType.

/**
     * @see org.apache.sling.api.resource.ResourceResolver#isResourceType(org.apache.sling.api.resource.Resource, java.lang.String)
     */
@Override
public boolean isResourceType(final Resource resource, final String resourceType) {
    boolean result = false;
    if (resource != null && resourceType != null) {
        //  to go up the resource super type hierarchy.
        if (ResourceTypeUtil.areResourceTypesEqual(resourceType, resource.getResourceType(), getSearchPath())) {
            result = true;
        } else {
            Set<String> superTypesChecked = new HashSet<>();
            String superType = this.getParentResourceType(resource);
            while (!result && superType != null) {
                if (ResourceTypeUtil.areResourceTypesEqual(resourceType, superType, getSearchPath())) {
                    result = true;
                } else {
                    superTypesChecked.add(superType);
                    superType = this.getParentResourceType(superType);
                    if (superType != null && superTypesChecked.contains(superType)) {
                        throw new SlingException("Cyclic dependency for resourceSuperType hierarchy detected on resource " + resource.getPath(), null);
                    }
                }
            }
        }
    }
    return result;
}
Also used : SlingException(org.apache.sling.api.SlingException) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString) HashSet(java.util.HashSet)

Example 2 with SlingException

use of org.apache.sling.api.SlingException in project sling by apache.

the class DefaultSlingScript method service.

/**
     * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
public void service(ServletRequest req, ServletResponse res) {
    final SlingHttpServletRequest request = (SlingHttpServletRequest) req;
    try {
        // prepare the properties for the script
        final SlingBindings props = new SlingBindings();
        props.setRequest((SlingHttpServletRequest) req);
        props.setResponse((SlingHttpServletResponse) res);
        // try to set content type (unless included)
        if (request.getAttribute(SlingConstants.ATTR_INCLUDE_SERVLET_PATH) == null) {
            final String contentType = request.getResponseContentType();
            if (contentType != null) {
                res.setContentType(contentType);
                // see SLING-679
                if (contentType.startsWith("text/")) {
                    res.setCharacterEncoding("UTF-8");
                }
            } else {
                LOGGER.debug("service: No response content type defined for request {}.", request.getRequestURI());
            }
        } else {
            LOGGER.debug("service: Included request, not setting content type and encoding");
        }
        // evaluate the script now using the ScriptEngine
        eval(props);
    } catch (ScriptEvaluationException see) {
        // log in the request progress tracker
        logScriptError(request, see);
        throw see;
    } catch (SlingException e) {
        // log in the request progress tracker
        logScriptError(request, e);
        throw e;
    } catch (Exception e) {
        // log in the request progress tracker
        logScriptError(request, e);
        throw new SlingException("Cannot get DefaultSlingScript: " + e.getMessage(), e);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingException(org.apache.sling.api.SlingException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) SlingException(org.apache.sling.api.SlingException) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) IOException(java.io.IOException)

Example 3 with SlingException

use of org.apache.sling.api.SlingException in project sling by apache.

the class SlingIOProvider method getResourcePaths.

Set<String> getResourcePaths(final String path) {
    final Set<String> paths = new HashSet<String>();
    ResourceResolver resolver = requestResourceResolver.get();
    if (resolver != null) {
        try {
            final String cleanedPath = cleanPath(path, false);
            final boolean startsWithWebInfTags = cleanedPath.startsWith(WEB_INF_TAGS);
            Resource resource = resolver.getResource(startsWithWebInfTags ? cleanedPath.substring(WEB_INF_TAGS.length()) : cleanedPath);
            if (resource != null) {
                Iterator<Resource> entries = resolver.listChildren(resource);
                while (entries.hasNext()) {
                    final String entryPath = entries.next().getPath();
                    if (startsWithWebInfTags) {
                        paths.add(WEB_INF_TAGS + entryPath);
                    } else {
                        paths.add(entryPath);
                    }
                }
            }
        } catch (final SlingException se) {
            log.warn("getResourcePaths: Cannot list children of " + path, se);
        }
    }
    return paths.isEmpty() ? null : paths;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SlingException(org.apache.sling.api.SlingException) HashSet(java.util.HashSet)

Example 4 with SlingException

use of org.apache.sling.api.SlingException 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 5 with SlingException

use of org.apache.sling.api.SlingException in project sling by apache.

the class AuthorizableResourceProvider method listChildren.

@Override
public Iterator<Resource> listChildren(ResolveContext<Object> ctx, Resource parent) {
    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(ctx, SYSTEM_USER_MANAGER_USER_PATH, null, null));
                resources.add(getResource(ctx, SYSTEM_USER_MANAGER_GROUP_PATH, null, null));
            }
            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;
            Session session = resourceResolver.adaptTo(Session.class);
            if (session != null) {
                PrincipalManager principalManager = AccessControlUtil.getPrincipalManager(session);
                principals = principalManager.getPrincipals(searchType);
            }
            if (principals != null) {
                return new ChildrenIterator(parent, principals);
            }
        }
    } catch (RepositoryException re) {
        throw new SlingException("Error listing children of resource: " + parent.getPath(), re);
    }
    return null;
}
Also used : PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) PrincipalIterator(org.apache.jackrabbit.api.security.principal.PrincipalIterator) SlingException(org.apache.sling.api.SlingException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Aggregations

SlingException (org.apache.sling.api.SlingException)20 Resource (org.apache.sling.api.resource.Resource)10 IOException (java.io.IOException)8 RepositoryException (javax.jcr.RepositoryException)6 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 ServletException (javax.servlet.ServletException)3 SlingIOException (org.apache.sling.api.SlingIOException)3 SlingServletException (org.apache.sling.api.SlingServletException)3 SlingBindings (org.apache.sling.api.scripting.SlingBindings)3 Closeable (java.io.Closeable)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Node (javax.jcr.Node)2 ScriptException (javax.script.ScriptException)2 UnavailableException (javax.servlet.UnavailableException)2 StringUtils.defaultString (org.apache.commons.lang3.StringUtils.defaultString)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 ResourceMetadata (org.apache.sling.api.resource.ResourceMetadata)2 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)2