mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Merge pull request #822 from apache/WW-5379-velocity-stack-alt
WW-5379 Implement alternative mechanism for Velocity directives to obtain ValueStack
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.util;
|
||||
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
|
||||
/**
|
||||
* @since 6.4.0
|
||||
*/
|
||||
public interface ValueStackProvider {
|
||||
|
||||
ValueStack getValueStack();
|
||||
|
||||
}
|
||||
+10
-4
@@ -19,6 +19,7 @@
|
||||
package org.apache.struts2.views.velocity;
|
||||
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import org.apache.struts2.util.ValueStackProvider;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -26,7 +27,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class StrutsVelocityContext extends VelocityContext {
|
||||
public class StrutsVelocityContext extends VelocityContext implements ValueStackProvider {
|
||||
|
||||
private final ValueStack stack;
|
||||
private final List<VelocityContext> chainedContexts;
|
||||
@@ -77,10 +78,10 @@ public class StrutsVelocityContext extends VelocityContext {
|
||||
}
|
||||
|
||||
protected List<Function<String, Object>> contextGetterList() {
|
||||
return Arrays.asList(this::superGet, this::chainedContextGet, this::stackGet);
|
||||
return Arrays.asList(this::superInternalGet, this::chainedContextGet, this::stackGet);
|
||||
}
|
||||
|
||||
protected Object superGet(String key) {
|
||||
protected Object superInternalGet(String key) {
|
||||
return super.internalGet(key);
|
||||
}
|
||||
|
||||
@@ -96,11 +97,16 @@ public class StrutsVelocityContext extends VelocityContext {
|
||||
return null;
|
||||
}
|
||||
for (VelocityContext chainedContext : chainedContexts) {
|
||||
Object val = chainedContext.internalGet(key);
|
||||
Object val = chainedContext.get(key);
|
||||
if (val != null) {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueStack getValueStack() {
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
|
||||
+39
-13
@@ -18,26 +18,28 @@
|
||||
*/
|
||||
package org.apache.struts2.views.velocity.components;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.components.Component;
|
||||
import org.apache.struts2.util.ValueStackProvider;
|
||||
import org.apache.struts2.views.util.ContextUtil;
|
||||
import org.apache.velocity.context.AbstractContext;
|
||||
import org.apache.velocity.context.Context;
|
||||
import org.apache.velocity.context.InternalContextAdapter;
|
||||
import org.apache.velocity.context.InternalWrapperContext;
|
||||
import org.apache.velocity.exception.MethodInvocationException;
|
||||
import org.apache.velocity.exception.ParseErrorException;
|
||||
import org.apache.velocity.exception.ResourceNotFoundException;
|
||||
import org.apache.velocity.runtime.directive.Directive;
|
||||
import org.apache.velocity.runtime.parser.node.Node;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class AbstractDirective extends Directive {
|
||||
public String getName() {
|
||||
@@ -57,8 +59,11 @@ public abstract class AbstractDirective extends Directive {
|
||||
protected abstract Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res);
|
||||
|
||||
public boolean render(InternalContextAdapter ctx, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
|
||||
// get the bean
|
||||
ValueStack stack = (ValueStack) ctx.get("stack");
|
||||
ValueStack stack = extractValueStack(ctx);
|
||||
if (stack == null) {
|
||||
// Fallback to assuming the ValueStack was put into the Velocity context (as is by default)
|
||||
stack = (ValueStack) ctx.get(ContextUtil.STACK);
|
||||
}
|
||||
HttpServletRequest req = (HttpServletRequest) stack.getContext().get(ServletActionContext.HTTP_REQUEST);
|
||||
HttpServletResponse res = (HttpServletResponse) stack.getContext().get(ServletActionContext.HTTP_RESPONSE);
|
||||
Component bean = getBean(stack, req, res);
|
||||
@@ -79,6 +84,27 @@ public abstract class AbstractDirective extends Directive {
|
||||
return true;
|
||||
}
|
||||
|
||||
private ValueStack extractValueStack(Context context) {
|
||||
do {
|
||||
if (context instanceof ValueStackProvider) {
|
||||
return ((ValueStackProvider) context).getValueStack();
|
||||
}
|
||||
context = extractContext(context);
|
||||
} while (context != null);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Context extractContext(Context context) {
|
||||
if (context instanceof InternalWrapperContext) {
|
||||
return ((InternalWrapperContext) context).getInternalUserContext();
|
||||
}
|
||||
if (context instanceof AbstractContext) {
|
||||
return ((AbstractContext) context).getChainedContext();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Create a Map of properties that the user has passed in. For example:
|
||||
|
||||
+2
-2
@@ -54,7 +54,7 @@ public class StrutsVelocityContextTest {
|
||||
|
||||
@Test
|
||||
public void getChainedValue() {
|
||||
when(chainedContext.internalGet("foo")).thenReturn("bar");
|
||||
when(chainedContext.get("foo")).thenReturn("bar");
|
||||
assertEquals("bar", strutsVelocityContext.internalGet("foo"));
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public class StrutsVelocityContextTest {
|
||||
when(stack.findValue("foo")).thenReturn("qux");
|
||||
assertEquals("qux", strutsVelocityContext.internalGet("foo"));
|
||||
|
||||
when(chainedContext.internalGet("foo")).thenReturn("baz");
|
||||
when(chainedContext.get("foo")).thenReturn("baz");
|
||||
assertEquals("baz", strutsVelocityContext.internalGet("foo"));
|
||||
|
||||
strutsVelocityContext.put("foo", "bar");
|
||||
|
||||
Reference in New Issue
Block a user