This is an automated email from the ASF dual-hosted git repository. kusal pushed a commit to branch issue/WW-5363-velocity-order in repository https://gitbox.apache.org/repos/asf/struts.git
commit e9c996a17ebf05ec2bde8f2516c8eaa608268c85 Author: Kusal Kithul-Godage <g...@kusal.io> AuthorDate: Mon Nov 13 23:58:41 2023 +1100 WW-5363 Velocity: read chained contexts before ValueStack --- .../views/velocity/StrutsVelocityContext.java | 49 +++++++++++++++------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java index cca2b5dc3..96c213252 100644 --- a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java @@ -24,6 +24,7 @@ import org.apache.velocity.VelocityContext; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Function; public class StrutsVelocityContext extends VelocityContext { @@ -54,7 +55,7 @@ public class StrutsVelocityContext extends VelocityContext { /** * @deprecated please use {@link #StrutsVelocityContext(List, ValueStack)} */ - @Deprecated() + @Deprecated public StrutsVelocityContext(VelocityContext[] chainedContexts, ValueStack stack) { this(new ArrayList<>(Arrays.asList(chainedContexts)), stack); } @@ -64,28 +65,44 @@ public class StrutsVelocityContext extends VelocityContext { } public Object internalGet(String key) { - Object val = super.internalGet(key); - if (val != null) { - return val; - } - if (stack != null) { - val = stack.findValue(key); + return chainedGets(key, super::internalGet, this::chainedContextGet, this::stackGet, this::stackContextGet); + } + + @SafeVarargs + protected static Object chainedGets(String key, Function<String, Object> ...contextGets) { + for (Function<String, Object> contextGet : contextGets) { + Object val = contextGet.apply(key); if (val != null) { return val; } - val = stack.getContext().get(key); + } + return null; + } + + protected Object stackGet(String key) { + if (stack == null) { + return null; + } + return stack.findValue(key); + } + + protected Object stackContextGet(String key) { + if (stack == null) { + return null; + } + return stack.getContext().get(key); + } + + protected Object chainedContextGet(String key) { + if (chainedContexts == null) { + return null; + } + for (VelocityContext chainedContext : chainedContexts) { + Object val = chainedContext.internalGet(key); if (val != null) { return val; } } - if (chainedContexts != null) { - for (VelocityContext chainedContext : chainedContexts) { - val = chainedContext.internalGet(key); - if (val != null) { - return val; - } - } - } return null; } }