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;
}
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);
}
}
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);
}
}
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();
}
}
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);
}
}
Aggregations