use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.
the class RenderContextImpl method call.
@Override
public Object call(String functionName, Object... arguments) {
Map<String, RuntimeExtension> extensions = extensionRegistryService.extensions();
RuntimeExtension extension = extensions.get(functionName);
if (extension == null) {
throw new SightlyException("Runtime extension is not available: " + functionName);
}
return extension.call(this, arguments);
}
use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.
the class SightlyCompiledScript method eval.
@Override
public Object eval(ScriptContext context) throws ScriptException {
Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
SlingBindings slingBindings = new SlingBindings();
slingBindings.putAll(bindings);
SlingHttpServletRequest request = slingBindings.getRequest();
if (request == null) {
throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
}
Object oldBindings = request.getAttribute(SlingBindings.class.getName());
try {
request.setAttribute(SlingBindings.class.getName(), slingBindings);
RenderContext renderContext = new RenderContextImpl(context);
PrintWriter out = new PrintWriter(context.getWriter());
renderUnit.render(out, renderContext, new SimpleBindings());
} finally {
request.setAttribute(SlingBindings.class.getName(), oldBindings);
}
return null;
}
use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.
the class IncludeRuntimeExtension method includeScript.
private void includeScript(final Bindings bindings, String script, PrintWriter out) {
if (StringUtils.isEmpty(script)) {
throw new SightlyException("Path for data-sly-include is empty");
} else {
LOG.debug("Attempting to include script {}.", script);
SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
ServletResolver servletResolver = slingScriptHelper.getService(ServletResolver.class);
if (servletResolver != null) {
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
Servlet servlet = servletResolver.resolveServlet(request.getResource(), script);
if (servlet != null) {
try {
SlingHttpServletResponse response = BindingsUtils.getResponse(bindings);
PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, response);
servlet.service(request, resWrapper);
} catch (Exception e) {
throw new SightlyException("Failed to include script " + script, e);
}
} else {
throw new SightlyException("Failed to locate script " + script);
}
} else {
throw new SightlyException("Sling ServletResolver service is unavailable, failed to include " + script);
}
}
}
use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.
the class DependencyResolver method resolve.
/**
* Resolve a dependency
* @param dependency the dependency identifier
* @param callback the callback that will receive the resolved dependency
*/
public void resolve(String dependency, UnaryCallback callback) {
if (!Utils.isJsScript(dependency)) {
throw new SightlyException("Only JS scripts are allowed as dependencies. Invalid dependency: " + dependency);
}
Resource scriptResource = Utils.getScriptResource(caller, dependency, globalBindings);
jsEnvironment.runResource(scriptResource, globalBindings, Utils.EMPTY_BINDINGS, callback);
}
use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.
the class AsyncExtractor method decodeJSPromise.
private void decodeJSPromise(final Scriptable promise, final UnaryCallback callback) {
try {
Context context = Context.enter();
final AsyncContainer errorContainer = new AsyncContainer();
final Function errorHandler = createErrorHandler(errorContainer);
final Function successHandler = convertCallback(callback);
EventLoopInterop.schedule(context, new Runnable() {
@Override
public void run() {
ScriptableObject.callMethod(promise, THEN_METHOD, new Object[] { successHandler, errorHandler });
}
});
if (errorContainer.isCompleted()) {
throw new SightlyException("Promise has completed with failure: " + Context.toString(errorContainer.getResult()));
}
} finally {
Context.exit();
}
}
Aggregations