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