Author: radu Date: Fri Oct 2 16:02:09 2015 New Revision: 1706441 URL: http://svn.apache.org/viewvc?rev=1706441&view=rev Log: SLING-4442 - Invalid parametrisation of Sightly statements should lead to exceptions instead of errors
* made data-sly-call, data-sly-use and data-sly-include fail with SightlyException for invalid expressions Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/IncludeRuntimeExtension.java sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/RenderUnit.java sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/IncludeRuntimeExtension.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/IncludeRuntimeExtension.java?rev=1706441&r1=1706440&r2=1706441&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/IncludeRuntimeExtension.java (original) +++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/IncludeRuntimeExtension.java Fri Oct 2 16:02:09 2015 @@ -18,14 +18,11 @@ ******************************************************************************/ package org.apache.sling.scripting.sightly.impl.engine.extension; -import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Map; - import javax.script.Bindings; import javax.servlet.Servlet; -import javax.servlet.ServletException; import org.apache.commons.lang.StringUtils; import org.apache.felix.scr.annotations.Component; @@ -68,9 +65,6 @@ public class IncludeRuntimeExtension imp String originalPath = RenderUtils.toString(arguments[0]); Map options = (Map) arguments[1]; String path = buildPath(originalPath, options); - if (path == null) { - throw new SightlyException("Path for data-sly-include is empty"); - } StringWriter output = new StringWriter(); final Bindings bindings = renderContext.getBindings(); includeScript(bindings, path, new PrintWriter(output)); @@ -98,8 +92,9 @@ public class IncludeRuntimeExtension imp private void includeScript(final Bindings bindings, String script, PrintWriter out) { if (StringUtils.isEmpty(script)) { - LOG.error("Script path cannot be empty"); + throw new SightlyException("Path for data-sly-include is empty"); } else { + LOG.debug("Attempting to include script {}.", script); SlingScriptHelper slingScriptHelper = (SlingScriptHelper) bindings.get(SlingBindings.SLING); ServletResolver servletResolver = slingScriptHelper.getService(ServletResolver.class); if (servletResolver != null) { @@ -110,16 +105,14 @@ public class IncludeRuntimeExtension imp SlingHttpServletResponse response = (SlingHttpServletResponse) bindings.get(SlingBindings.RESPONSE); PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, response); servlet.service(request, resWrapper); - } catch (ServletException e) { - throw new SightlyException("Failed to include script " + script, e); - } catch (IOException e) { + } catch (Exception e) { throw new SightlyException("Failed to include script " + script, e); } } else { - LOG.error("Failed to locate script {}", script); + throw new SightlyException("Failed to locate script " + script); } } else { - LOG.error("Sling ServletResolver service is unavailable, failed to include {}", script); + throw new SightlyException("Sling ServletResolver service is unavailable, failed to include " + script); } } } Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java?rev=1706441&r1=1706440&r2=1706441&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java (original) +++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java Fri Oct 2 16:02:09 2015 @@ -23,7 +23,6 @@ import java.util.Collections; import java.util.ListIterator; import java.util.Map; import java.util.concurrent.ConcurrentSkipListMap; - import javax.script.Bindings; import javax.script.SimpleBindings; @@ -74,24 +73,23 @@ public class UseRuntimeExtension impleme } String identifier = RenderUtils.toString(arguments[0]); if (StringUtils.isEmpty(identifier)) { - return null; + throw new SightlyException("data-sly-use needs to be passed an identifier"); } Map<String, Object> useArgumentsMap = RenderUtils.toMap(arguments[1]); Bindings useArguments = new SimpleBindings(Collections.unmodifiableMap(useArgumentsMap)); ArrayList<UseProvider> providers = new ArrayList<UseProvider>(providersMap.values()); ListIterator<UseProvider> iterator = providers.listIterator(providers.size()); - Throwable failureCause = null; 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); } } - LOG.error("No use provider could resolve identifier " + identifier); - return null; + throw new SightlyException("No use provider could resolve identifier " + identifier); } // OSGi ################################################################################################################################ Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/RenderUnit.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/RenderUnit.java?rev=1706441&r1=1706440&r2=1706441&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/RenderUnit.java (original) +++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/RenderUnit.java Fri Oct 2 16:02:09 2015 @@ -30,6 +30,7 @@ import javax.script.SimpleBindings; import org.apache.sling.api.scripting.SlingBindings; import org.apache.sling.api.scripting.SlingScriptHelper; import org.apache.sling.scripting.sightly.Record; +import org.apache.sling.scripting.sightly.SightlyException; import org.apache.sling.scripting.sightly.impl.utils.RenderUtils; import org.apache.sling.scripting.sightly.render.RenderContext; @@ -71,7 +72,16 @@ public abstract class RenderUnit impleme @SuppressWarnings({"unused", "unchecked"}) protected void callUnit(RenderContext renderContext, Object templateObj, Object argsObj) { if (!(templateObj instanceof RenderUnit)) { - return; + if (templateObj == null) { + throw new SightlyException("data-sly-call: expression evaluates to null."); + } + if (RenderUtils.isPrimitive(templateObj)) { + throw new SightlyException("data-sly-call: primitive \"" + templateObj.toString() + "\" does not represent a Sightly " + + "template."); + } else if (templateObj instanceof String) { + throw new SightlyException("data-sly-call: String '" + templateObj.toString() + "' does not represent a Sightly template."); + } + throw new SightlyException("data-sly-call: " + templateObj.getClass().getName() + " does not represent a Sightly template."); } RenderUnit unit = (RenderUnit) templateObj; SlingScriptHelper ssh = (SlingScriptHelper) renderContext.getBindings().get(SlingBindings.SLING); Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java?rev=1706441&r1=1706440&r2=1706441&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java (original) +++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java Fri Oct 2 16:02:09 2015 @@ -364,7 +364,7 @@ public class RenderUtils { private static final Set<Class<?>> primitiveClasses = primitiveClasses(); - private static boolean isPrimitive(Object obj) { + public static boolean isPrimitive(Object obj) { return primitiveClasses.contains(obj.getClass()); }