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();
}
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;
}
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();
}
};
}
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);
}
}
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;
}
Aggregations