-
Notifications
You must be signed in to change notification settings - Fork 811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WW-5379 Implement alternative mechanism for Velocity directives to obtain ValueStack #822
Changes from all commits
3dbc5c2
3eddd56
450ee91
ee600f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,14 +19,15 @@ | |
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; | ||
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 Object internalGet(String key) { | |
} | ||
|
||
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 @@ protected Object chainedContextGet(String key) { | |
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; | ||
} | ||
Comment on lines
+109
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to have this public? Also this requires to cast to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it has to be but I wonder if Directives can simply obtain the ValueStack from the ActionContext? It's not clear to me if the ValueStack on the ActionContext changes between the time of Velocity context creation and directive rendering. I recall this was how the ValueStack was obtained in WebWork 2.1 but I presume it was changed for a reason? But also, there's no change in terms of security as the stack was already exposed on the And yep I can definitely use a marker interface to allow more flexibility in the Velocity context implementation used by applications. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn't change as far I understand, yet I'm not sure if this is true :) Let's leave it as is, at some point I will solve this puzzle ;-) |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 int getType() { | |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't occur but left it for backwards compatibility in case there is a scenario I didn't foresee |
||
// 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 boolean render(InternalContextAdapter ctx, Writer writer, Node node) thro | |
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: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was technically a bug as we were failing to look at chained contexts within our chained contexts