Search in sources :

Example 11 with SlingScriptHelper

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());
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Test(org.junit.Test)

Example 12 with SlingScriptHelper

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;
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Locale(java.util.Locale) IContext(org.thymeleaf.context.IContext) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ServletContext(javax.servlet.ServletContext) Writer(java.io.Writer)

Example 13 with SlingScriptHelper

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;
}
Also used : ScriptException(javax.script.ScriptException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) Template(freemarker.template.Template)

Example 14 with SlingScriptHelper

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;
}
Also used : ScriptException(javax.script.ScriptException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) Bindings(javax.script.Bindings)

Example 15 with SlingScriptHelper

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"));
}
Also used : SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Aggregations

SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)26 SlingBindings (org.apache.sling.api.scripting.SlingBindings)15 Resource (org.apache.sling.api.resource.Resource)12 Bindings (javax.script.Bindings)10 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)9 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)8 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)5 ScriptException (javax.script.ScriptException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 Servlet (javax.servlet.Servlet)3 ServletResolver (org.apache.sling.api.servlets.ServletResolver)3 SightlyException (org.apache.sling.scripting.sightly.SightlyException)3 Test (org.junit.Test)3 BufferedReader (java.io.BufferedReader)2 Reader (java.io.Reader)2 Locale (java.util.Locale)2 JspException (javax.servlet.jsp.JspException)2 BodyContent (javax.servlet.jsp.tagext.BodyContent)2