Search in sources :

Example 21 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings 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;
}
Also used : RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings) RenderContextImpl(org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 22 with SlingBindings

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

the class SlingContextImpl method request.

/**
     * @return Sling request
     */
public final MockSlingHttpServletRequest request() {
    if (this.request == null) {
        this.request = new MockSlingHttpServletRequest(this.resourceResolver(), this.bundleContext());
        // initialize sling bindings
        SlingBindings bindings = new SlingBindings();
        bindings.put(SlingBindings.REQUEST, this.request);
        bindings.put(SlingBindings.RESPONSE, response());
        bindings.put(SlingBindings.SLING, slingScriptHelper());
        this.request.setAttribute(SlingBindings.class.getName(), bindings);
    }
    return this.request;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)

Example 23 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class HiddenImplTest method prepareHiddenFieldForTest.

private Field prepareHiddenFieldForTest(String resourcePath) {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(CONTEXT.resourceResolver(), CONTEXT.bundleContext());
    Resource resource = CONTEXT.resourceResolver().getResource(resourcePath);
    request.setResource(resource);
    Page currentPage = CONTEXT.pageManager().getPage(CONTAINING_PAGE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.put(WCMBindings.CURRENT_PAGE, currentPage);
    slingBindings.put(SlingBindings.RESOURCE, resource);
    slingBindings.put(WCMBindings.PROPERTIES, resource.getValueMap());
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    return request.adaptTo(Field.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page)

Example 24 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class BreadcrumbImplTest method setUp.

@Before
public void setUp() throws Exception {
    Page page = context.currentPage(CURRENT_PAGE);
    slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.put(WCMBindings.CURRENT_PAGE, page);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Page(com.day.cq.wcm.api.Page) Before(org.junit.Before)

Example 25 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class TextImplTest method setUp.

@Before
public void setUp() {
    Page page = context.currentPage(CONTAINING_PAGE);
    slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.put(WCMBindings.CURRENT_PAGE, page);
    context.registerService(FormStructureHelperFactory.class, new FormStructureHelperFactory() {

        @Override
        public FormStructureHelper getFormStructureHelper(Resource formElement) {
            return null;
        }
    });
    FormsHelperStubber.createStub();
}
Also used : FormStructureHelper(com.day.cq.wcm.foundation.forms.FormStructureHelper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) FormStructureHelperFactory(com.day.cq.wcm.foundation.forms.FormStructureHelperFactory) Before(org.junit.Before)

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