Search in sources :

Example 1 with SightlyException

use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.

the class URIManipulationFilterExtension method addQueryParameters.

private void addQueryParameters(RuntimeObjectModel runtimeObjectModel, Map<String, Collection<String>> parameters, Map<String, Object> queryParameters) {
    for (Map.Entry<String, Object> entry : queryParameters.entrySet()) {
        Object entryValue = entry.getValue();
        try {
            if (runtimeObjectModel.isCollection(entryValue)) {
                Collection<Object> collection = runtimeObjectModel.toCollection(entryValue);
                Collection<String> values = new ArrayList<>(collection.size());
                for (Object o : collection) {
                    values.add(URLEncoder.encode(runtimeObjectModel.toString(o), "UTF-8"));
                }
                parameters.put(entry.getKey(), values);
            } else {
                Collection<String> values = new ArrayList<>(1);
                values.add(URLEncoder.encode(runtimeObjectModel.toString(entryValue), "UTF-8"));
                parameters.put(entry.getKey(), values);
            }
        } catch (UnsupportedEncodingException e) {
            throw new SightlyException(e);
        }
    }
}
Also used : SightlyException(org.apache.sling.scripting.sightly.SightlyException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with SightlyException

use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.

the class XSSRuntimeExtension method call.

@Override
public Object call(final RenderContext renderContext, Object... arguments) {
    if (arguments.length < 2) {
        throw new SightlyException(String.format("Extension %s requires at least %d arguments", RuntimeFunction.XSS, 2));
    }
    Object original = arguments[0];
    Object option = arguments[1];
    Object hint = null;
    if (arguments.length >= 3) {
        hint = arguments[2];
    }
    MarkupContext markupContext = null;
    if (option != null && option instanceof String) {
        String name = (String) option;
        markupContext = MarkupContext.lookup(name);
    }
    if (markupContext == MarkupContext.UNSAFE) {
        return original;
    }
    if (markupContext == null) {
        LOG.warn("Expression context {} is invalid, expression will be replaced by the empty string", option);
        return "";
    }
    String text = renderContext.getObjectModel().toString(original);
    return applyXSSFilter(text, hint, markupContext);
}
Also used : SightlyException(org.apache.sling.scripting.sightly.SightlyException) MarkupContext(org.apache.sling.scripting.sightly.compiler.expression.MarkupContext)

Example 3 with SightlyException

use of org.apache.sling.scripting.sightly.SightlyException 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 4 with SightlyException

use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.

the class SightlyScriptEngine method eval.

@Override
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
    checkArguments(reader, scriptContext);
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    final SlingHttpServletRequest request = slingBindings.getRequest();
    if (request == null) {
        throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
    }
    final Object oldValue = request.getAttribute(SlingBindings.class.getName());
    try {
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        SightlyCompiledScript compiledScript = internalCompile(reader, scriptContext);
        return compiledScript.eval(scriptContext);
    } catch (Exception e) {
        throw new ScriptException(e);
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldValue);
    }
}
Also used : ScriptException(javax.script.ScriptException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SightlyException(org.apache.sling.scripting.sightly.SightlyException) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 5 with SightlyException

use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.

the class UseRuntimeExtension method call.

@Override
public Object call(final RenderContext renderContext, Object... arguments) {
    ExtensionUtils.checkArgumentCount(RuntimeFunction.USE, arguments, 2);
    RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
    String identifier = runtimeObjectModel.toString(arguments[0]);
    if (StringUtils.isEmpty(identifier)) {
        throw new SightlyException("data-sly-use needs to be passed an identifier");
    }
    Map<String, Object> useArgumentsMap = runtimeObjectModel.toMap(arguments[1]);
    Bindings useArguments = new SimpleBindings(Collections.unmodifiableMap(useArgumentsMap));
    ArrayList<UseProvider> providers = new ArrayList<>(providersMap.values());
    ListIterator<UseProvider> iterator = providers.listIterator(providers.size());
    while (iterator.hasPrevious()) {
        UseProvider provider = iterator.previous();
        ProviderOutcome outcome = provider.provide(identifier, renderContext, useArguments);
        Throwable failureCause;
        if (outcome.isSuccess()) {
            return outcome.getResult();
        } else if ((failureCause = outcome.getCause()) != null) {
            throw new SightlyException("Identifier " + identifier + " cannot be correctly instantiated by the Use API", failureCause);
        }
    }
    throw new SightlyException("No use provider could resolve identifier " + identifier);
}
Also used : RuntimeObjectModel(org.apache.sling.scripting.sightly.render.RuntimeObjectModel) ArrayList(java.util.ArrayList) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ProviderOutcome(org.apache.sling.scripting.sightly.use.ProviderOutcome) UseProvider(org.apache.sling.scripting.sightly.use.UseProvider) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings)

Aggregations

SightlyException (org.apache.sling.scripting.sightly.SightlyException)17 Bindings (javax.script.Bindings)6 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)6 Resource (org.apache.sling.api.resource.Resource)5 SimpleBindings (javax.script.SimpleBindings)3 SlingBindings (org.apache.sling.api.scripting.SlingBindings)3 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)3 Function (org.mozilla.javascript.Function)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ScriptException (javax.script.ScriptException)2 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)2 RenderUnit (org.apache.sling.scripting.sightly.java.compiler.RenderUnit)2 AsyncContainer (org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer)2 TimingFunction (org.apache.sling.scripting.sightly.js.impl.async.TimingFunction)2 HybridObject (org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject)2 Context (org.mozilla.javascript.Context)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1