Search in sources :

Example 16 with SlingBindings

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

the class JspScriptEngineFactory method callErrorPageJsp.

/**
     * Call the error page
     * @param bindings The bindings
     * @param scriptHelper Script helper service
     * @param context The script context
     * @param scriptName The name of the script
     */
private void callErrorPageJsp(final Bindings bindings, final SlingScriptHelper scriptHelper, final ScriptContext context, final String scriptName) {
    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();
    }
    final SlingIOProvider io = this.ioProvider;
    final JspFactoryHandler jspfh = this.jspFactoryHandler;
    // abort if JSP Support is shut down concurrently (SLING-2704)
    if (io == null || jspfh == null) {
        logger.warn("callJsp: JSP Script Engine seems to be shut down concurrently; not calling {}", scriptHelper.getScript().getScriptResource().getPath());
        return;
    }
    final ResourceResolver oldResolver = io.setRequestResourceResolver(resolver);
    jspfh.incUsage();
    try {
        final JspServletWrapper errorJsp = getJspWrapper(scriptName, slingBindings);
        errorJsp.service(slingBindings);
        // The error page could be inside an include.
        final SlingHttpServletRequest request = slingBindings.getRequest();
        final Throwable t = (Throwable) request.getAttribute("javax.servlet.jsp.jspException");
        final Object newException = request.getAttribute("javax.servlet.error.exception");
        // t==null means the attribute was not set.
        if ((newException != null) && (newException == t)) {
            request.removeAttribute("javax.servlet.error.exception");
        }
        // now clear the error code - to prevent double handling.
        request.removeAttribute("javax.servlet.error.status_code");
        request.removeAttribute("javax.servlet.error.request_uri");
        request.removeAttribute("javax.servlet.error.status_code");
        request.removeAttribute("javax.servlet.jsp.jspException");
    } finally {
        jspfh.decUsage();
        io.resetRequestResourceResolver(oldResolver);
    }
}
Also used : JspServletWrapper(org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) JspFactoryHandler(org.apache.sling.scripting.jsp.jasper.compiler.JspRuntimeContext.JspFactoryHandler) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 17 with SlingBindings

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

the class JspScriptEngineFactory method callJsp.

/**
     * Call a JSP script
     * @param bindings The bindings
     * @param scriptHelper Script helper service
     * @param context The script context
     * @throws SlingServletException
     * @throws SlingIOException
     */
private void callJsp(final Bindings bindings, final SlingScriptHelper scriptHelper, final ScriptContext context) {
    ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, SlingScriptConstants.SLING_SCOPE);
    if (resolver == null) {
        resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
    }
    final SlingIOProvider io = this.ioProvider;
    final JspFactoryHandler jspfh = this.jspFactoryHandler;
    // abort if JSP Support is shut down concurrently (SLING-2704)
    if (io == null || jspfh == null) {
        logger.warn("callJsp: JSP Script Engine seems to be shut down concurrently; not calling {}", scriptHelper.getScript().getScriptResource().getPath());
        return;
    }
    final ResourceResolver oldResolver = io.setRequestResourceResolver(resolver);
    jspfh.incUsage();
    try {
        final SlingBindings slingBindings = new SlingBindings();
        slingBindings.putAll(bindings);
        final JspServletWrapper jsp = getJspWrapper(scriptHelper, slingBindings);
        // create a SlingBindings object
        jsp.service(slingBindings);
    } finally {
        jspfh.decUsage();
        io.resetRequestResourceResolver(oldResolver);
    }
}
Also used : JspServletWrapper(org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) JspFactoryHandler(org.apache.sling.scripting.jsp.jasper.compiler.JspRuntimeContext.JspFactoryHandler) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Example 18 with SlingBindings

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

the class ScriptUseProvider method evalScript.

private ProviderOutcome evalScript(Resource scriptResource, Bindings bindings) {
    SlingScript slingScript = scriptResource.adaptTo(SlingScript.class);
    if (slingScript == null) {
        return ProviderOutcome.failure();
    }
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    Object scriptEval = slingScript.eval(slingBindings);
    return ProviderOutcome.notNullOrFailure(scriptEval);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScript(org.apache.sling.api.scripting.SlingScript)

Example 19 with SlingBindings

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

the class SightlyScriptEngine method eval.

@Override
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
    checkArguments(reader, scriptContext);
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    final SlingHttpServletRequest request = slingBindings.getRequest();
    if (request == null) {
        throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
    }
    final Object oldValue = request.getAttribute(SlingBindings.class.getName());
    try {
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        SightlyCompiledScript compiledScript = internalCompile(reader, scriptContext);
        return compiledScript.eval(scriptContext);
    } catch (Exception e) {
        throw new ScriptException(e);
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldValue);
    }
}
Also used : ScriptException(javax.script.ScriptException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SightlyException(org.apache.sling.scripting.sightly.SightlyException) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 20 with SlingBindings

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

the class SightlyCompiledScriptTest method testEvalSlingBindings.

/**
     * Tests that SlingBindings are correctly handled by compiled scripts, by setting them from the script context to the request
     * attributes.
     * @throws ScriptException
     */
@Test
public void testEvalSlingBindings() throws ScriptException {
    ScriptEngine scriptEngine = mock(ScriptEngine.class);
    final RenderUnit renderUnit = mock(RenderUnit.class);
    Whitebox.setInternalState(renderUnit, "subTemplates", new HashMap<String, Object>());
    final BundleContext bundleContext = MockOsgi.newBundleContext();
    bundleContext.registerService(ExtensionRegistryService.class.getName(), mock(ExtensionRegistryService.class), new Hashtable<String, Object>());
    ResourceResolver resourceResolver = MockSling.newResourceResolver(bundleContext);
    final MockSlingHttpServletRequest request = spy(new MockSlingHttpServletRequest(resourceResolver, bundleContext));
    SightlyCompiledScript compiledScript = spy(new SightlyCompiledScript(scriptEngine, renderUnit));
    ScriptContext scriptContext = mock(ScriptContext.class);
    StringWriter writer = new StringWriter();
    when(scriptContext.getWriter()).thenReturn(writer);
    Bindings scriptContextBindings = new SimpleBindings() {

        {
            put("test", "testValue");
            put(SlingBindings.REQUEST, request);
            put(SlingBindings.SLING, MockSling.newSlingScriptHelper(bundleContext));
        }
    };
    SlingBindings oldBindings = new SlingBindings();
    oldBindings.put("old", "oldValue");
    request.setAttribute(SlingBindings.class.getName(), oldBindings);
    when(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)).thenReturn(scriptContextBindings);
    compiledScript.eval(scriptContext);
    ArgumentCaptor<SlingBindings> slingBindingsArgumentCaptor = ArgumentCaptor.forClass(SlingBindings.class);
    ArgumentCaptor<String> attributeNameArgumentCaptor = ArgumentCaptor.forClass(String.class);
    // request.setAttribute should have been invoked 3 times: once here, twice in the compiled script
    verify(request, times(3)).setAttribute(attributeNameArgumentCaptor.capture(), slingBindingsArgumentCaptor.capture());
    List<SlingBindings> slingBindingsValues = slingBindingsArgumentCaptor.getAllValues();
    int invocation = 1;
    for (SlingBindings bindings : slingBindingsValues) {
        switch(invocation) {
            case 1:
                assertEquals(oldBindings, bindings);
                break;
            case 2:
                assertEquals(3, bindings.size());
                for (Map.Entry<String, Object> entry : scriptContextBindings.entrySet()) {
                    assertEquals(entry.getValue(), bindings.get(entry.getKey()));
                }
                break;
            case 3:
                assertEquals(oldBindings, bindings);
        }
        invocation++;
    }
    for (String key : attributeNameArgumentCaptor.getAllValues()) {
        assertEquals(SlingBindings.class.getName(), key);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) RenderUnit(org.apache.sling.scripting.sightly.java.compiler.RenderUnit) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SimpleBindings(javax.script.SimpleBindings) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) HashMap(java.util.HashMap) Map(java.util.Map) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

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