Search in sources :

Example 1 with SlingBindings

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

the class ContainerImplTest 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, formStructureHelperFactory);
    FormsHelperStubber.createStub();
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Page(com.day.cq.wcm.api.Page) Before(org.junit.Before)

Example 2 with SlingBindings

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

the class WCMUsePojoBaseTest method getResourceBackedBindings.

/**
     * <p>
     * Creates a {@link Bindings} map initialised with the following default bindings available to Sightly use objects based on {@link
     * WCMUsePojo}:
     * </p>
     * <ul>
     * <li>{@link SlingBindings#RESOURCE}</li>
     * <li>{@link SlingBindings#REQUEST}</li>
     * <li>{@link SlingBindings#RESPONSE}</li>
     * <li>{@link WCMBindings#PROPERTIES}</li>
     * <li>{@link WCMBindings#WCM_MODE}</li>
     * <li>{@link WCMBindings#PAGE_MANAGER}</li>
     * <li>{@link WCMBindings#RESOURCE_PAGE}</li>
     * <li>{@link WCMBindings#CURRENT_PAGE}</li>
     * <li>{@link WCMBindings#PAGE_PROPERTIES}</li>
     * </ul>
     *
     * @param resourcePath the path to a resource already loaded in the testing context
     * @return the bindings map
     */
protected Bindings getResourceBackedBindings(String resourcePath) {
    Bindings bindings = getDefaultSlingBindings();
    Resource resource = context.resourceResolver().getResource(resourcePath);
    if (resource != null) {
        ValueMap properties = resource.adaptTo(ValueMap.class);
        bindings.put(SlingBindings.RESOURCE, resource);
        bindings.put(WCMBindings.PROPERTIES, properties);
        bindings.put(WCMBindings.WCM_MODE, new SightlyWCMMode(context.request()));
        PageManager pageManager = context.pageManager();
        bindings.put(WCMBindings.PAGE_MANAGER, pageManager);
        context.request().setResource(resource);
        Page resourcePage = pageManager.getContainingPage(resource);
        if (resourcePage != null) {
            bindings.put(WCMBindings.RESOURCE_PAGE, resourcePage);
            bindings.put(WCMBindings.CURRENT_PAGE, resourcePage);
            bindings.put(WCMBindings.PAGE_PROPERTIES, properties);
        }
    } else {
        throw new IllegalArgumentException("Cannot find a resource at " + resourcePath);
    }
    return bindings;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) SightlyWCMMode(com.adobe.cq.sightly.SightlyWCMMode) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) WCMBindings(com.adobe.cq.sightly.WCMBindings)

Example 3 with SlingBindings

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

the class AdaptiveImageServletTest method prepareRequestResponsePair.

private Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> prepareRequestResponsePair(String resourcePath, String selectorString, String extension) {
    final MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(aemContext.resourceResolver(), aemContext.bundleContext());
    final MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    Resource resource = resourceResolver.getResource(resourcePath);
    request.setResource(resource);
    MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
    requestPathInfo.setSelectorString(selectorString);
    requestPathInfo.setExtension(extension);
    SlingBindings bindings = new SlingBindings();
    bindings.put(SlingBindings.REQUEST, request);
    bindings.put(SlingBindings.RESPONSE, response);
    bindings.put(SlingBindings.SLING, aemContext.slingScriptHelper());
    bindings.put(SlingBindings.RESOLVER, resourceResolver);
    request.setAttribute(SlingBindings.class.getName(), bindings);
    return new Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse>() {

        @Override
        public MockSlingHttpServletRequest getLeft() {
            return request;
        }

        @Override
        public MockSlingHttpServletResponse getRight() {
            return response;
        }

        @Override
        public MockSlingHttpServletResponse setValue(MockSlingHttpServletResponse value) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : MockRequestPathInfo(org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo) SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Pair(org.apache.commons.lang3.tuple.Pair)

Example 4 with SlingBindings

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

the class DefaultSlingScript method service.

/**
     * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
public void service(ServletRequest req, ServletResponse res) {
    final SlingHttpServletRequest request = (SlingHttpServletRequest) req;
    try {
        // prepare the properties for the script
        final SlingBindings props = new SlingBindings();
        props.setRequest((SlingHttpServletRequest) req);
        props.setResponse((SlingHttpServletResponse) res);
        // try to set content type (unless included)
        if (request.getAttribute(SlingConstants.ATTR_INCLUDE_SERVLET_PATH) == null) {
            final String contentType = request.getResponseContentType();
            if (contentType != null) {
                res.setContentType(contentType);
                // see SLING-679
                if (contentType.startsWith("text/")) {
                    res.setCharacterEncoding("UTF-8");
                }
            } else {
                LOGGER.debug("service: No response content type defined for request {}.", request.getRequestURI());
            }
        } else {
            LOGGER.debug("service: Included request, not setting content type and encoding");
        }
        // evaluate the script now using the ScriptEngine
        eval(props);
    } catch (ScriptEvaluationException see) {
        // log in the request progress tracker
        logScriptError(request, see);
        throw see;
    } catch (SlingException e) {
        // log in the request progress tracker
        logScriptError(request, e);
        throw e;
    } catch (Exception e) {
        // log in the request progress tracker
        logScriptError(request, e);
        throw new SlingException("Cannot get DefaultSlingScript: " + e.getMessage(), e);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingException(org.apache.sling.api.SlingException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) SlingException(org.apache.sling.api.SlingException) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) IOException(java.io.IOException)

Example 5 with SlingBindings

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

the class SlingBindingsVariablesListJsonServlet method getBindingsByEngine.

/**
     * Gets the {@link Bindings} object for the given {@link ScriptEngineFactory}.
     * It only considers the default context "request".
     *
     * @see <a href="https://issues.apache.org/jira/browse/SLING-3038">binding contexts(SLING-3083)</a>
     *
     * @param scriptEngineFactory the factory of the script engine, for which to retrieve the bindings
     * @param request the current request (necessary to create the bindings)
     * @param response the current response (necessary to create the bindings)
     * @return the bindings (list of key/value pairs) as defined by {@link Bindings} for the given script engine.
     * @throws IOException
     */
private Bindings getBindingsByEngine(ScriptEngineFactory scriptEngineFactory, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    // use default context only
    String context = SlingScriptAdapterFactory.BINDINGS_CONTEXT;
    final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProviderTracker.getBindingsValuesProviders(scriptEngineFactory, context);
    Resource invalidScriptResource = new NonExistingResource(request.getResourceResolver(), "some/invalid/scriptpath");
    DefaultSlingScript defaultSlingScript = new DefaultSlingScript(bundleContext, invalidScriptResource, scriptEngineFactory.getScriptEngine(), bindingsValuesProviders, null, null);
    // prepare the bindings (similar as in DefaultSlingScript#service)
    final SlingBindings initalBindings = new SlingBindings();
    initalBindings.setRequest(request);
    initalBindings.setResponse(response);
    final Bindings bindings = defaultSlingScript.verifySlingBindings(initalBindings);
    // only thing being added in {DefaultSlingScript#call(...)} is resource resolver
    bindings.put(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, request.getResourceResolver());
    return bindings;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Aggregations

SlingBindings (org.apache.sling.api.scripting.SlingBindings)57 Resource (org.apache.sling.api.resource.Resource)26 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)19 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)12 Page (com.day.cq.wcm.api.Page)11 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)9 Before (org.junit.Before)8 Bindings (javax.script.Bindings)7 IOException (java.io.IOException)6 SimpleBindings (javax.script.SimpleBindings)6 Map (java.util.Map)5 MockRequestPathInfo (org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo)5 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)5 PrintWriter (java.io.PrintWriter)4 HashMap (java.util.HashMap)4 ScriptException (javax.script.ScriptException)4 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)4 Servlet (javax.servlet.Servlet)3 ServletException (javax.servlet.ServletException)3