use of org.apache.sling.api.scripting.SlingScriptHelper in project sling by apache.
the class InjectorSpecificAnnotationTest method testScriptVariableConstructor.
@Test
public void testScriptVariableConstructor() throws InvalidSyntaxException {
SlingBindings bindings = new SlingBindings();
SlingScriptHelper helper = mock(SlingScriptHelper.class);
bindings.setSling(helper);
when(request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
org.apache.sling.models.testmodels.classes.constructorinjection.InjectorSpecificAnnotationModel model = factory.getAdapter(request, org.apache.sling.models.testmodels.classes.constructorinjection.InjectorSpecificAnnotationModel.class);
assertNotNull("Could not instanciate model", model);
assertEquals(helper, model.getHelper());
}
use of org.apache.sling.api.scripting.SlingScriptHelper in project sling by apache.
the class ThymeleafScriptEngine method eval.
@Override
public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
final SlingHttpServletRequest request = helper.getRequest();
final SlingHttpServletResponse response = helper.getResponse();
// only used by Thymeleaf's ServletContextResourceResolver (TODO check if still true for 3.0)
final ServletContext servletContext = null;
final Locale locale = helper.getResponse().getLocale();
final String scriptName = helper.getScript().getScriptResource().getPath();
final Writer writer = scriptContext.getWriter();
try {
final ResourceResolver resourceResolver = thymeleafScriptEngineFactory.getRequestScopedResourceResolver();
final IContext context = new SlingWebContext(request, response, servletContext, resourceResolver, locale, bindings);
thymeleafScriptEngineFactory.getTemplateEngine().process(scriptName, context, writer);
} catch (Exception e) {
logger.error("Failure rendering Thymeleaf template '{}': {}", scriptName, e.getMessage());
throw new ScriptException(e);
}
return null;
}
use of org.apache.sling.api.scripting.SlingScriptHelper in project sling by apache.
the class FreemarkerScriptEngine method eval.
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
// ensure GET request
if (!"GET".equals(helper.getRequest().getMethod())) {
throw new ScriptException("FreeMarker templates only support GET requests");
}
freemarkerScriptEngineFactory.getTemplateModels().forEach(bindings::put);
final String scriptName = helper.getScript().getScriptResource().getPath();
try {
final Template template = new Template(scriptName, reader, configuration);
template.process(bindings, scriptContext.getWriter());
} catch (Throwable t) {
final String message = String.format("Failure processing FreeMarker template %s.", scriptName);
logger.error(message, t);
throw new ScriptException(message);
}
return null;
}
use of org.apache.sling.api.scripting.SlingScriptHelper in project sling by apache.
the class EsxScriptEngine method eval.
@Override
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
log.debug("starting to eval ESX Script");
Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
SlingScriptHelper scriptHelper = (SlingScriptHelper) bindings.get("sling");
Resource scriptResource = scriptHelper.getScript().getScriptResource();
Resource resource = scriptHelper.getRequest().getResource();
ModuleScript moduleScript = new ModuleScript(ModuleScript.JS_FILE, scriptResource);
//public Module (EsxScriptEngineFactory factory, Resource resource, ModuleScript moduleScript, String id, Module parent, SlingScriptHelper scriptHelper) throws ScriptException {
Module module = new Module((EsxScriptEngineFactory) getFactory(), resource, moduleScript, scriptResource.getPath(), null, scriptHelper, Module.LOADER_JS);
try {
Object moduleResults = module.require(scriptResource.getPath());
String result = ((EsxScriptEngineFactory) getFactory()).getNashornEngine().eval("if(exports.render && typeof exports.render === 'function') { exports.render('test'); }" + " else if(typeof exports === 'class') { new exports().render('function') } else {" + "'You need to define either a render function or export an object with a render method'; }", module).toString();
context.getWriter().write(result);
} catch (IOException ex) {
throw new ScriptException(ex);
}
return null;
}
use of org.apache.sling.api.scripting.SlingScriptHelper in project acs-aem-commons by Adobe-Consulting-Services.
the class SyntheticFormResourceTest method syntheticResourceTest.
@Test
public void syntheticResourceTest() throws DeserializeException {
SlingHttpServletRequest mockRequest = mock(SlingHttpServletRequest.class);
SlingScriptHelper mockScriptHelper = mock(SlingScriptHelper.class);
when(mockScriptHelper.getRequest()).thenReturn(mockRequest);
Map<String, FieldComponent> form = AnnotatedFieldDeserializer.getFormFields(getClass(), mockScriptHelper);
assertNotNull(form.get("textComponentTest"));
Resource fieldResource = form.get("textComponentTest").buildComponentResource();
assertEquals("granite/ui/components/coral/foundation/form/textfield", fieldResource.getResourceType());
assertEquals("granite/ui/components/coral/foundation/form/field", fieldResource.getResourceSuperType());
assertEquals("textComponentTest", fieldResource.getResourceMetadata().get("name"));
assertEquals("Text component", fieldResource.getResourceMetadata().get("fieldLabel"));
assertEquals(true, fieldResource.getResourceMetadata().get("required"));
}
Aggregations