Search in sources :

Example 31 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.

the class MultipleInjectorTest method setup.

@Before
public void setup() {
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    bindings = new SlingBindings();
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    // binding injector should be asked first as it has a lower service ranking!
    factory.bindInjector(bindingsInjector, new ServicePropertiesMap(1, 1));
    factory.bindInjector(attributesInjector, new ServicePropertiesMap(2, 2));
    factory.bindStaticInjectAnnotationProcessorFactory(bindingsInjector, new ServicePropertiesMap(1, 1));
    when(request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
    factory.adapterImplementations.addClassesAsAdapterAndImplementation(ForTwoInjectorsWithSource.class, ForTwoInjectors.class, ForTwoInjectorsWithInvalidSource.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Before(org.junit.Before)

Example 32 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.

the class SlingObjectInjectorRequestTest method setUp.

@Before
public void setUp() {
    SlingBindings bindings = new SlingBindings();
    bindings.put(SlingBindings.SLING, this.scriptHelper);
    when(this.request.getResourceResolver()).thenReturn(this.resourceResolver);
    when(this.request.getResource()).thenReturn(this.resource);
    when(this.request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
    when(this.scriptHelper.getResponse()).thenReturn(this.response);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Before(org.junit.Before)

Example 33 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings 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);
    }
}
Also used : ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException) SlingIOException(org.apache.sling.api.SlingIOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 34 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)

Example 35 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings 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();
    }
}
Also used : ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException) SlingIOException(org.apache.sling.api.SlingIOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ServletException(javax.servlet.ServletException) ScriptException(javax.script.ScriptException) SlingServletException(org.apache.sling.api.SlingServletException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException)

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