use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.
the class CallTag method doEndTag.
@Override
public int doEndTag() throws JspException {
final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
final SlingScriptHelper scriptHelper = bindings.getSling();
final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
final RequestProgressTracker tracker = TagUtil.getRequest(pageContext).getRequestProgressTracker();
String servletName = null;
final Servlet servlet;
if (!ignoreComponentHierarchy) {
final Resource resource = bindings.getResource();
servlet = servletResolver.resolveServlet(resource, this.script);
if (servlet != null) {
servletName = RequestUtil.getServletName(servlet);
tracker.log("Including script {0} for path={1}, type={2}: {3}", script, resource.getPath(), resource.getResourceType(), servletName);
}
} else {
final ResourceResolver resolver = bindings.getRequest().getResourceResolver();
final String scriptPath;
if (!script.startsWith("/")) {
// resolve relative script
String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
// check if parent resides on search path
for (String sp : resolver.getSearchPath()) {
if (parentPath.startsWith(sp)) {
parentPath = parentPath.substring(sp.length());
break;
}
}
scriptPath = parentPath + "/" + script;
} else {
scriptPath = this.script;
}
servlet = servletResolver.resolveServlet(resolver, scriptPath);
if (servlet != null) {
servletName = RequestUtil.getServletName(servlet);
tracker.log("Including script {0} (ignoring component hierarchy): {1}", script, servletName);
}
}
if (servlet == null) {
throw new JspException("Could not find script " + script);
}
try {
if (flush && !(pageContext.getOut() instanceof BodyContent)) {
// might throw an IOException of course
pageContext.getOut().flush();
}
// wrap the response to get the correct output order
SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
tracker.startTimer(servletName);
servlet.service(pageContext.getRequest(), response);
tracker.logTimer(servletName);
return EVAL_PAGE;
} catch (Exception e) {
throw new JspException("Error while executing script " + script, e);
}
}
use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.
the class GetPropertyTag method getClassLoader.
/**
* Method for retrieving the classloader from the OSGi console.
*
* @return the classloader
*/
protected ClassLoader getClassLoader() {
final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
final SlingScriptHelper scriptHelper = bindings.getSling();
final DynamicClassLoaderManager dynamicClassLoaderManager = scriptHelper.getService(DynamicClassLoaderManager.class);
final ClassLoader classLoader = dynamicClassLoaderManager.getDynamicClassLoader();
return classLoader;
}
use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.
the class AbstractSlingContextImplTest method testSlingBindings.
@Test
public void testSlingBindings() {
SlingBindings bindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
assertNotNull(bindings);
assertSame(context.request(), bindings.get(SlingBindings.REQUEST));
assertSame(context.response(), bindings.get(SlingBindings.RESPONSE));
assertSame(context.slingScriptHelper(), bindings.get(SlingBindings.SLING));
}
use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.
the class StandaloneScriptExecutionServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
final SlingScript script = request.getResource().adaptTo(SlingScript.class);
if (script == null) {
throw new ServletException("Resource does not adapt to a SlingScript:" + request.getResource().getPath());
}
// Execute the script without providing a request or response, in the simplest possible way
final SlingBindings bindings = new SlingBindings();
final StringWriter sw = new StringWriter();
bindings.put("StandaloneScriptExecutionServletOutput", sw);
script.eval(bindings);
response.setContentType("text/plain");
response.getWriter().write(sw.toString());
}
use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.
the class ScriptConsolePlugin method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final String contentType = getContentType(req);
resp.setContentType(contentType);
if (contentType.startsWith("text/")) {
resp.setCharacterEncoding("UTF-8");
}
final String script = getCodeValue(req);
final SlingBindings bindings = new SlingBindings();
final PrintWriter pw = resp.getWriter();
//Populate bindings
bindings.put(SlingBindings.REQUEST, req);
bindings.put(SlingBindings.READER, new StringReader(script));
bindings.put(SlingBindings.RESPONSE, resp);
bindings.put(SlingBindings.OUT, pw);
//Also expose the bundleContext to simplify scripts interaction with the
//enclosing OSGi container
bindings.put("bundleContext", bundleContext);
final String lang = WebConsoleUtil.getParameter(req, "lang");
final Resource resource = new RuntimeScriptResource(lang, script);
final boolean webClient = "webconsole".equals(WebConsoleUtil.getParameter(req, "client"));
SlingScript slingScript = resource.adaptTo(SlingScript.class);
try {
log.debug("Executing script {}", script);
slingScript.eval(bindings);
} catch (Throwable t) {
if (!webClient) {
resp.setStatus(500);
}
pw.println(exceptionToString(t));
log.warn("Error in executing script", t);
}
}
Aggregations