Search in sources :

Example 36 with SlingBindings

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);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) JspException(javax.servlet.jsp.JspException) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) ServletResolver(org.apache.sling.api.servlets.ServletResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Servlet(javax.servlet.Servlet) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Example 37 with SlingBindings

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;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) DynamicClassLoaderManager(org.apache.sling.commons.classloader.DynamicClassLoaderManager)

Example 38 with SlingBindings

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));
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Test(org.junit.Test)

Example 39 with SlingBindings

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());
}
Also used : ServletException(javax.servlet.ServletException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) StringWriter(java.io.StringWriter) SlingScript(org.apache.sling.api.scripting.SlingScript)

Example 40 with SlingBindings

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);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScript(org.apache.sling.api.scripting.SlingScript) StringReader(java.io.StringReader) Resource(org.apache.sling.api.resource.Resource) PrintWriter(java.io.PrintWriter)

Aggregations

SlingBindings (org.apache.sling.api.scripting.SlingBindings)41 Resource (org.apache.sling.api.resource.Resource)14 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 Page (com.day.cq.wcm.api.Page)8 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)8 Before (org.junit.Before)8 Bindings (javax.script.Bindings)7 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)7 SimpleBindings (javax.script.SimpleBindings)6 IOException (java.io.IOException)4 PrintWriter (java.io.PrintWriter)4 ScriptException (javax.script.ScriptException)4 ServletException (javax.servlet.ServletException)3 SlingException (org.apache.sling.api.SlingException)3 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)3 SlingScript (org.apache.sling.api.scripting.SlingScript)3 Test (org.junit.Test)3 SightlyWCMMode (com.adobe.cq.sightly.SightlyWCMMode)2 Style (com.day.cq.wcm.api.designer.Style)2