From e9c996a17ebf05ec2bde8f2516c8eaa608268c85 Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Mon, 13 Nov 2023 23:58:41 +1100 Subject: [PATCH] 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 cca2b5dc31..96c213252b 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 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 StrutsVelocityContext(ValueStack stack) { /** * @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 boolean internalContainsKey(String key) { } 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 ...contextGets) { + for (Function 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; } }