Search in sources :

Example 6 with SlingScriptHelper

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

the class RhinoJavaScriptEngine method getScriptName.

private String getScriptName(ScriptContext scriptContext) {
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    String scriptName = (String) bindings.get(ScriptEngine.FILENAME);
    if (scriptName != null && !"".equals(scriptName)) {
        return scriptName;
    }
    SlingScriptHelper sling = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
    if (sling != null) {
        return sling.getScript().getScriptResource().getPath();
    }
    return NO_SCRIPT_NAME;
}
Also used : SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Example 7 with SlingScriptHelper

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

the class RenderUnitProvider method provide.

@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
    if (identifier.endsWith("." + SightlyScriptEngineFactory.EXTENSION)) {
        Bindings globalBindings = renderContext.getBindings();
        SlingScriptHelper sling = BindingsUtils.getHelper(globalBindings);
        SlingHttpServletRequest request = BindingsUtils.getRequest(globalBindings);
        final Resource renderUnitResource = ScriptUtils.resolveScript(scriptingResourceResolverProvider.getRequestScopedResourceResolver(), renderContext, identifier);
        if (renderUnitResource == null) {
            Resource caller = ResourceResolution.getResourceForRequest(request.getResourceResolver(), request);
            if (caller != null) {
                String resourceSuperType = caller.getResourceSuperType();
                StringBuilder errorMessage = new StringBuilder("Cannot find resource ");
                errorMessage.append(identifier).append(" for base path ").append(caller.getPath());
                if (StringUtils.isNotEmpty(resourceSuperType)) {
                    errorMessage.append(" with resource super type ").append(resourceSuperType);
                }
                errorMessage.append(".");
                return ProviderOutcome.failure(new SightlyException(errorMessage.toString()));
            } else {
                return ProviderOutcome.failure(new SightlyException("Cannot resolve template " + identifier + " for script " + sling.getScript().getScriptResource().getPath()));
            }
        }
        RenderUnit renderUnit;
        try {
            CachedScript cachedScript = scriptCache.getScript(renderUnitResource.getPath());
            final SightlyCompiledScript compiledScript;
            if (cachedScript != null) {
                compiledScript = (SightlyCompiledScript) cachedScript.getCompiledScript();
            } else {
                SightlyScriptEngine sightlyScriptEngine = (SightlyScriptEngine) scriptEngineManager.getEngineByName(SightlyScriptEngineFactory.SHORT_NAME);
                String encoding = renderUnitResource.getResourceMetadata().getCharacterEncoding();
                if (StringUtils.isEmpty(encoding)) {
                    encoding = "UTF-8";
                }
                InputStream inputStream = renderUnitResource.adaptTo(InputStream.class);
                if (inputStream == null) {
                    return ProviderOutcome.failure();
                }
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding);
                ScriptNameAwareReader reader = new ScriptNameAwareReader(inputStreamReader, renderUnitResource.getPath());
                compiledScript = (SightlyCompiledScript) sightlyScriptEngine.compile(reader);
                scriptCache.putScript(new CachedScript() {

                    @Override
                    public String getScriptPath() {
                        return renderUnitResource.getPath();
                    }

                    @Override
                    public CompiledScript getCompiledScript() {
                        return compiledScript;
                    }
                });
            }
            renderUnit = compiledScript.getRenderUnit();
            return ProviderOutcome.success(renderUnit);
        } catch (Exception e) {
            return ProviderOutcome.failure(e);
        }
    }
    return ProviderOutcome.failure();
}
Also used : SightlyCompiledScript(org.apache.sling.scripting.sightly.impl.engine.SightlyCompiledScript) CompiledScript(javax.script.CompiledScript) RenderUnit(org.apache.sling.scripting.sightly.java.compiler.RenderUnit) InputStreamReader(java.io.InputStreamReader) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) InputStream(java.io.InputStream) CachedScript(org.apache.sling.scripting.api.CachedScript) Resource(org.apache.sling.api.resource.Resource) SightlyScriptEngine(org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine) Bindings(javax.script.Bindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SightlyException(org.apache.sling.scripting.sightly.SightlyException) ScriptNameAwareReader(org.apache.sling.scripting.core.ScriptNameAwareReader) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SightlyCompiledScript(org.apache.sling.scripting.sightly.impl.engine.SightlyCompiledScript)

Example 8 with SlingScriptHelper

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

the class SightlyScriptEngine method getScriptName.

private String getScriptName(ScriptContext scriptContext) {
    if (scriptContext != null) {
        Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
        String scriptName = (String) bindings.get(ScriptEngine.FILENAME);
        if (scriptName != null && !"".equals(scriptName)) {
            return scriptName;
        }
        SlingScriptHelper sling = BindingsUtils.getHelper(bindings);
        if (sling != null) {
            return sling.getScript().getScriptResource().getPath();
        }
    }
    return NO_SCRIPT;
}
Also used : SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Example 9 with SlingScriptHelper

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

the class IncludeRuntimeExtension method includeScript.

private void includeScript(final Bindings bindings, String script, PrintWriter out) {
    if (StringUtils.isEmpty(script)) {
        throw new SightlyException("Path for data-sly-include is empty");
    } else {
        LOG.debug("Attempting to include script {}.", script);
        SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
        ServletResolver servletResolver = slingScriptHelper.getService(ServletResolver.class);
        if (servletResolver != null) {
            SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
            Servlet servlet = servletResolver.resolveServlet(request.getResource(), script);
            if (servlet != null) {
                try {
                    SlingHttpServletResponse response = BindingsUtils.getResponse(bindings);
                    PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, response);
                    servlet.service(request, resWrapper);
                } catch (Exception e) {
                    throw new SightlyException("Failed to include script " + script, e);
                }
            } else {
                throw new SightlyException("Failed to locate script " + script);
            }
        } else {
            throw new SightlyException("Sling ServletResolver service is unavailable, failed to include " + script);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletResolver(org.apache.sling.api.servlets.ServletResolver) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Servlet(javax.servlet.Servlet) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 10 with SlingScriptHelper

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

the class InjectorSpecificAnnotationTest method testScriptVariableField.

@Test
public void testScriptVariableField() throws InvalidSyntaxException {
    SlingBindings bindings = new SlingBindings();
    SlingScriptHelper helper = mock(SlingScriptHelper.class);
    bindings.setSling(helper);
    when(request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
    InjectorSpecificAnnotationModel model = factory.getAdapter(request, InjectorSpecificAnnotationModel.class);
    assertNotNull("Could not instanciate model", model);
    assertEquals(helper, model.getHelper());
}
Also used : InjectorSpecificAnnotationModel(org.apache.sling.models.testmodels.classes.InjectorSpecificAnnotationModel) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) 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