Search in sources :

Example 11 with SlingException

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

the class JcrResourceProvider method registerListeners.

/**
     * Register all observation listeners.
     */
private void registerListeners() {
    if (this.repository != null) {
        logger.debug("Registering resource listeners...");
        try {
            this.listenerConfig = new JcrListenerBaseConfig(this.getProviderContext().getObservationReporter(), this.repository);
            for (final ObserverConfiguration config : this.getProviderContext().getObservationReporter().getObserverConfigurations()) {
                logger.debug("Registering listener for {}", config.getPaths());
                final Closeable listener = new JcrResourceListener(this.listenerConfig, config);
                this.listeners.put(config, listener);
            }
        } catch (final RepositoryException e) {
            throw new SlingException("Can't create the JCR event listener.", e);
        }
        logger.debug("Registered resource listeners");
    }
}
Also used : JcrListenerBaseConfig(org.apache.sling.jcr.resource.internal.JcrListenerBaseConfig) Closeable(java.io.Closeable) ObserverConfiguration(org.apache.sling.spi.resource.provider.ObserverConfiguration) SlingException(org.apache.sling.api.SlingException) RepositoryException(javax.jcr.RepositoryException) JcrResourceListener(org.apache.sling.jcr.resource.internal.JcrResourceListener)

Example 12 with SlingException

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

the class JspServletWrapper method service.

/**
     * @param bindings
     * @throws SlingIOException
     * @throws SlingServletException
     * @throws IllegalArgumentException if the Jasper Precompile controller
     *             request parameter has an illegal value.
     */
public void service(final SlingBindings bindings) {
    final SlingHttpServletRequest request = bindings.getRequest();
    final Object oldValue = request.getAttribute(SlingBindings.class.getName());
    try {
        request.setAttribute(SlingBindings.class.getName(), bindings);
        service(request, bindings.getResponse());
    } catch (SlingException se) {
        // rethrow as is
        throw se;
    } catch (IOException ioe) {
        throw new SlingIOException(ioe);
    } catch (ServletException se) {
        throw new SlingServletException(se);
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldValue);
    }
}
Also used : ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException) SlingIOException(org.apache.sling.api.SlingIOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 13 with SlingException

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

the class JspServletWrapper method handleJspExceptionInternal.

/**
     * Returns only a ServletException or a SlingException
     */
private Exception handleJspExceptionInternal(final Exception ex) throws ServletException {
    Throwable realException = ex;
    String exMessage = "";
    if (ex instanceof ServletException) {
        realException = ((ServletException) ex).getRootCause();
        // root cause might be null (eg. for a JasperException ex)
        if (realException == null) {
            realException = ex;
        } else {
            exMessage = ex.toString();
        }
    }
    // avoid nested ScriptEvaluationExceptions (eg. in nested jsp includes)
    while (realException instanceof ScriptEvaluationException) {
        realException = realException.getCause();
    }
    try {
        // First identify the stack frame in the trace that represents the JSP
        StackTraceElement[] frames = realException.getStackTrace();
        StackTraceElement jspFrame = null;
        for (int i = 0; i < frames.length; ++i) {
            if (frames[i].getClassName().equals(this.theServlet.getClass().getName())) {
                jspFrame = frames[i];
                break;
            }
        }
        if (jspFrame == null) {
            // to the generated servlet class, we can't really add anything
            if (ex instanceof ServletException) {
                return ex;
            }
            return new SlingException(ex) {
            };
        }
        int javaLineNumber = jspFrame.getLineNumber();
        JavacErrorDetail detail = ErrorDispatcher.createJavacError(jspFrame.getMethodName(), this.ctxt.getCompiler().getPageNodes(), null, javaLineNumber, ctxt);
        // If the line number is less than one we couldn't find out
        // where in the JSP things went wrong
        int jspLineNumber = detail.getJspBeginLineNumber();
        if (jspLineNumber < 1) {
            if (realException instanceof ServletException) {
                return (ServletException) realException;
            }
            return new SlingException(exMessage, realException);
        }
        if (options.getDisplaySourceFragment() && detail.getJspExtract() != null) {
            return new SlingException(Localizer.getMessage("jsp.exception", detail.getJspFileName(), "" + jspLineNumber) + "\n\n" + detail.getJspExtract() + "\n", realException);
        }
        return new SlingException(Localizer.getMessage("jsp.exception", detail.getJspFileName(), "" + jspLineNumber), realException);
    } catch (final Exception je) {
        // If anything goes wrong, just revert to the original behaviour
        if (realException instanceof ServletException) {
            return (ServletException) realException;
        }
        return new SlingException(exMessage, realException);
    }
}
Also used : ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) JavacErrorDetail(org.apache.sling.scripting.jsp.jasper.compiler.JavacErrorDetail) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingException(org.apache.sling.api.SlingException) ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException) UnavailableException(javax.servlet.UnavailableException) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingPageException(org.apache.sling.scripting.jsp.SlingPageException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException)

Example 14 with SlingException

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

the class ResourceResolverImpl method resolveInternal.

/**
     * Internally resolves the absolute path. The will almost always contain
     * request selectors and an extension. Therefore this method uses the
     * {@link ResourcePathIterator} to cut off parts of the path to find the
     * actual resource.
     * <p>
     * This method operates in two steps:
     * <ol>
     * <li>Check the path directly
     * <li>Drill down the resource tree from the root down to the resource trying to get the child
     * as per the respective path segment or finding a child whose <code>sling:alias</code> property
     * is set to the respective name.
     * </ol>
     * <p>
     * If neither mechanism (direct access and drill down) resolves to a resource this method
     * returns <code>null</code>.
     *
     * @param absPath
     *            The absolute path of the resource to return.
     * @return The resource found or <code>null</code> if the resource could not
     *         be found. The
     *         {@link org.apache.sling.api.resource.ResourceMetadata#getResolutionPathInfo()
     *         resolution path info} field of the resource returned is set to
     *         the part of the <code>absPath</code> which has been cut off by
     *         the {@link ResourcePathIterator} to resolve the resource.
     */
private Resource resolveInternal(final String absPath, final Map<String, String> parameters) {
    Resource resource = null;
    String curPath = absPath;
    try {
        final ResourcePathIterator it = new ResourcePathIterator(absPath);
        while (it.hasNext() && resource == null) {
            curPath = it.next();
            resource = getAbsoluteResourceInternal(null, curPath, parameters, true);
        }
    } catch (final Exception ex) {
        throw new SlingException("Problem trying " + curPath + " for request path " + absPath, ex);
    }
    // uriPath = curPath + sling.resolutionPathInfo
    if (resource != null) {
        final String rpi = absPath.substring(curPath.length());
        resource.getResourceMetadata().setResolutionPath(absPath.substring(0, curPath.length()));
        resource.getResourceMetadata().setResolutionPathInfo(rpi);
        resource.getResourceMetadata().setParameterMap(parameters);
        logger.debug("resolveInternal: Found resource {} with path info {} for {}", new Object[] { resource, rpi, absPath });
    } else {
        String tokenizedPath = absPath;
        // no direct resource found, so we have to drill down into the
        // resource tree to find a match
        resource = getAbsoluteResourceInternal(null, "/", parameters, true);
        //SLING-5638
        if (resource == null) {
            resource = getAbsoluteResourceInternal(absPath, parameters, true);
            if (resource != null) {
                tokenizedPath = tokenizedPath.substring(resource.getPath().length());
            }
        }
        final StringBuilder resolutionPath = new StringBuilder();
        final StringTokenizer tokener = new StringTokenizer(tokenizedPath, "/");
        while (resource != null && tokener.hasMoreTokens()) {
            final String childNameRaw = tokener.nextToken();
            Resource nextResource = getChildInternal(resource, childNameRaw);
            if (nextResource != null) {
                resource = nextResource;
                resolutionPath.append("/").append(childNameRaw);
            } else {
                String childName = null;
                final ResourcePathIterator rpi = new ResourcePathIterator(childNameRaw);
                while (rpi.hasNext() && nextResource == null) {
                    childName = rpi.next();
                    nextResource = getChildInternal(resource, childName);
                }
                // switch the currentResource to the nextResource (may be
                // null)
                resource = nextResource;
                resolutionPath.append("/").append(childName);
                // with the extension cut off
                if (nextResource != null) {
                    break;
                }
            }
        }
        // uriPath = curPath + sling.resolutionPathInfo
        if (resource != null) {
            final String path = resolutionPath.toString();
            final String pathInfo = absPath.substring(path.length());
            resource.getResourceMetadata().setResolutionPath(path);
            resource.getResourceMetadata().setResolutionPathInfo(pathInfo);
            resource.getResourceMetadata().setParameterMap(parameters);
            logger.debug("resolveInternal: Found resource {} with path info {} for {}", new Object[] { resource, pathInfo, absPath });
        }
    }
    return resource;
}
Also used : StringTokenizer(java.util.StringTokenizer) StarResource(org.apache.sling.resourceresolver.impl.helper.StarResource) RedirectResource(org.apache.sling.resourceresolver.impl.helper.RedirectResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) SlingException(org.apache.sling.api.SlingException) ResourcePathIterator(org.apache.sling.resourceresolver.impl.helper.ResourcePathIterator) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString) PersistenceException(org.apache.sling.api.resource.PersistenceException) SlingException(org.apache.sling.api.SlingException) URIException(org.apache.sling.resourceresolver.impl.helper.URIException) LoginException(org.apache.sling.api.resource.LoginException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Example 15 with SlingException

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

the class ModifyOperation method getResourcePath.

@Override
protected String getResourcePath(SlingHttpServletRequest request) {
    // calculate the paths
    StringBuilder rootPathBuf = new StringBuilder();
    String suffix;
    Resource currentResource = request.getResource();
    if (ResourceUtil.isSyntheticResource(currentResource)) {
        // no resource, treat the missing resource path as suffix
        suffix = currentResource.getPath();
    } else {
        // resource for part of the path, use request suffix
        suffix = request.getRequestPathInfo().getSuffix();
        if (suffix != null) {
            // cut off any selectors/extension from the suffix
            int dotPos = suffix.indexOf('.');
            if (dotPos > 0) {
                suffix = suffix.substring(0, dotPos);
            }
        }
        // and preset the path buffer with the resource path
        rootPathBuf.append(currentResource.getPath());
    }
    // check for extensions or create suffix in the suffix
    boolean doGenerateName = false;
    if (suffix != null) {
        // check whether it is a create request (trailing /)
        if (suffix.endsWith(SlingPostConstants.DEFAULT_CREATE_SUFFIX)) {
            suffix = suffix.substring(0, suffix.length() - SlingPostConstants.DEFAULT_CREATE_SUFFIX.length());
            doGenerateName = true;
        // or with the star suffix /*
        } else if (suffix.endsWith(SlingPostConstants.STAR_CREATE_SUFFIX)) {
            suffix = suffix.substring(0, suffix.length() - SlingPostConstants.STAR_CREATE_SUFFIX.length());
            doGenerateName = true;
        }
        // append the remains of the suffix to the path buffer
        rootPathBuf.append(suffix);
    }
    String path = rootPathBuf.toString();
    if (doGenerateName) {
        try {
            path = generateName(request, path);
        } catch (PersistenceException re) {
            throw new SlingException("Failed to generate name", re);
        }
    }
    return path;
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) SlingException(org.apache.sling.api.SlingException)

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