Search in sources :

Example 16 with SlingException

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

the class SlingIOProvider method lastModified.

/**
     * Returns the value of the last modified meta data field of the resource
     * found at file name or zero if the meta data field is not set. If the
     * resource does not exist or an error occurrs finding the resource, -1 is
     * returned.
     */
public long lastModified(final String path) {
    if (path.startsWith(":")) {
        return this.classLoaderWriter.getLastModified(path.substring(1));
    }
    ResourceResolver resolver = requestResourceResolver.get();
    if (resolver != null) {
        try {
            final Resource resource = resolver.getResource(cleanPath(path, true));
            if (resource != null) {
                ResourceMetadata meta = resource.getResourceMetadata();
                long modTime = meta.getModificationTime();
                return (modTime > 0) ? modTime : 0;
            }
        } catch (final SlingException se) {
            log.error("Cannot get last modification time for " + path, se);
        }
    }
    // fallback to "non-existant" in case of problems
    return -1;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SlingException(org.apache.sling.api.SlingException) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 17 with SlingException

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

the class ProcessorManagerImpl method getProcessor.

/**
     * @see org.apache.sling.rewriter.ProcessorManager#getProcessor(org.apache.sling.rewriter.ProcessorConfiguration, org.apache.sling.rewriter.ProcessingContext)
     */
@Override
public Processor getProcessor(ProcessorConfiguration configuration, ProcessingContext context) {
    if (configuration == null) {
        throw new IllegalArgumentException("Processor configuration is missing.");
    }
    if (context == null) {
        throw new IllegalArgumentException("Processor context is missing.");
    }
    boolean isPipeline = false;
    if (configuration instanceof ProcessorConfigurationImpl) {
        isPipeline = ((ProcessorConfigurationImpl) configuration).isPipeline();
    } else {
        isPipeline = configuration instanceof PipelineConfiguration;
    }
    try {
        if (isPipeline) {
            final PipelineImpl pipeline = new PipelineImpl(this.factoryCache);
            pipeline.init(context, configuration);
            return pipeline;
        }
        final Processor processor = new ProcessorWrapper(configuration, this.factoryCache);
        processor.init(context, configuration);
        return processor;
    } catch (final IOException ioe) {
        throw new SlingException("Unable to setup processor: " + ioe.getMessage(), ioe);
    }
}
Also used : Processor(org.apache.sling.rewriter.Processor) SlingException(org.apache.sling.api.SlingException) PipelineConfiguration(org.apache.sling.rewriter.PipelineConfiguration) IOException(java.io.IOException)

Example 18 with SlingException

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

the class SlingPostOperationExample method run.

public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) {
    final Resource r = request.getResource();
    final Node n = r.adaptTo(Node.class);
    try {
        response.setPath(r.getPath());
        response.setTitle("Content modified by " + getClass().getSimpleName());
        n.setProperty(getClass().getName(), "Operation was applied to " + n.getPath());
        n.getSession().save();
    } catch (RepositoryException re) {
        throw new SlingException(getClass().getSimpleName() + " failed", re);
    }
}
Also used : Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) SlingException(org.apache.sling.api.SlingException) RepositoryException(javax.jcr.RepositoryException)

Example 19 with SlingException

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

the class JavaScriptEngineFactory method callServlet.

/**
     * Call the servlet.
     * @param bindings The bindings for the script invocation
     * @param scriptHelper The script helper.
     * @param context The script context.
     * @throws SlingServletException
     * @throws SlingIOException
     */
private void callServlet(final Bindings bindings, final SlingScriptHelper scriptHelper, final ScriptContext context) {
    // create a SlingBindings object
    final SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, SlingScriptConstants.SLING_SCOPE);
    if (resolver == null) {
        resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
    }
    ioProvider.setRequestResourceResolver(resolver);
    final SlingHttpServletRequest request = slingBindings.getRequest();
    final Object oldValue = request.getAttribute(SlingBindings.class.getName());
    try {
        final ServletWrapper servlet = getWrapperAdapter(scriptHelper);
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        servlet.service(request, slingBindings.getResponse());
    } catch (SlingException se) {
        // rethrow as is
        throw se;
    } catch (IOException ioe) {
        throw new SlingIOException(ioe);
    } catch (ServletException se) {
        throw new SlingServletException(se);
    } catch (Exception ex) {
        throw new SlingException(null, ex);
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldValue);
        ioProvider.resetRequestResourceResolver();
    }
}
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) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) 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) ServletException(javax.servlet.ServletException) ScriptException(javax.script.ScriptException) SlingServletException(org.apache.sling.api.SlingServletException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException)

Example 20 with SlingException

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

the class SlingIOProvider method getInputStream.

/**
     * Returns an InputStream for the file name which is looked up with the
     * ResourceProvider and retrieved from the Resource if the StreamProvider
     * interface is implemented.
     */
public InputStream getInputStream(String fileName) throws FileNotFoundException, IOException {
    try {
        final Resource resource = getResourceInternal(fileName);
        if (resource == null) {
            throw new FileNotFoundException("Cannot find " + fileName);
        }
        final InputStream stream = resource.adaptTo(InputStream.class);
        if (stream == null) {
            throw new FileNotFoundException("Cannot find " + fileName);
        }
        return stream;
    } catch (SlingException se) {
        throw (IOException) new IOException("Failed to get InputStream for " + fileName).initCause(se);
    }
}
Also used : InputStream(java.io.InputStream) Resource(org.apache.sling.api.resource.Resource) FileNotFoundException(java.io.FileNotFoundException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException)

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