Skip to content

Commit

Permalink
Merge pull request #691 from apache/WW-5261-tag-utils
Browse files Browse the repository at this point in the history
[WW-5261] Avoids creating ValueStack if no ActionContext is available
  • Loading branch information
lukaszlenart committed Jun 19, 2023
2 parents 5a67d58 + 93304a3 commit 13013e7
Show file tree
Hide file tree
Showing 11 changed files with 795 additions and 804 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@
import org.apache.struts2.StrutsException;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.RequestMap;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.RequestMap;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.apache.struts2.views.jsp.TagUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -53,7 +51,7 @@
* namespace. The body content of the tag is used to render the results from the Action. Any result processor defined
* for this action in struts.xml will be ignored, <i>unless</i> the executeResult parameter is specified.</p>
* <!-- END SNIPPET: javadoc -->
*
* <p>
* <!-- START SNIPPET: params -->
* <ul>
* <li>id (String) - the id (if specified) to put the action under stack's context.
Expand Down Expand Up @@ -111,9 +109,8 @@
* &lt;s:property value=&quot;#attr.stringByAction&quot; /&gt;
* <!-- END SNIPPET: example -->
* </pre>
*
*/
@StrutsTag(name="action", tldTagClass="org.apache.struts2.views.jsp.ActionTag", description="Execute an action from within a view")
@StrutsTag(name = "action", tldTagClass = "org.apache.struts2.views.jsp.ActionTag", description = "Execute an action from within a view")
public class ActionComponent extends ContextBean {
private static final Logger LOG = LogManager.getLogger(ActionComponent.class);

Expand Down Expand Up @@ -143,7 +140,7 @@ public ActionComponent(ValueStack stack, HttpServletRequest req, HttpServletResp
public void setActionProxyFactory(ActionProxyFactory actionProxyFactory) {
this.actionProxyFactory = actionProxyFactory;
}

@Inject
public void setValueStackFactory(ValueStackFactory valueStackFactory) {
this.valueStackFactory = valueStackFactory;
Expand All @@ -163,7 +160,7 @@ public boolean end(Writer writer, String body) {
try {
writer.flush();
} catch (IOException e) {
LOG.warn("error while trying to flush writer ", e);
LOG.warn("error while trying to flush writer ", e);
}
}
executeAction();
Expand All @@ -188,11 +185,11 @@ protected Map<String, Object> createExtraContext() {
Dispatcher du = Dispatcher.getInstance();
Map<String, Object> extraContext = du.createContextMap(
new RequestMap(req),
newParams,
session,
application,
req,
res);
newParams,
session,
application,
req,
res);

ValueStack newStack = valueStackFactory.createValueStack(stack);

Expand All @@ -206,7 +203,7 @@ protected Map<String, Object> createExtraContext() {
/**
* Creates parameters map using parameters from the value stack and component parameters. Any non-String array
* values will be converted into a single-value String array.
*
*
* @return A map of String[] parameters
*/
protected HttpParameters createParametersForContext() {
Expand All @@ -233,8 +230,6 @@ public ActionProxy getProxy() {
* attempt to derive a namespace using buildNamespace(). The ActionProxy
* and the namespace will be saved into the instance variables proxy and
* namespace respectively.
*
* @see org.apache.struts2.views.jsp.TagUtils#buildNamespace
*/
protected void executeAction() {
String actualName = findString(name, "name", "Action name is required. Example: updatePerson");
Expand All @@ -254,7 +249,7 @@ protected void executeAction() {
String namespace;

if (this.namespace == null) {
namespace = TagUtils.buildNamespace(actionMapper, getStack(), req);
namespace = getNamespace(getStack());
} else {
namespace = findString(this.namespace);
}
Expand Down Expand Up @@ -291,32 +286,32 @@ protected void executeAction() {
}
}

@StrutsTagAttribute(required=true,description="Name of the action to be executed (without the extension suffix eg. .action)")
@StrutsTagAttribute(required = true, description = "Name of the action to be executed (without the extension suffix eg. .action)")
public void setName(String name) {
this.name = name;
}

@StrutsTagAttribute(description="Namespace for action to call", defaultValue="namespace from where tag is used")
@StrutsTagAttribute(description = "Namespace for action to call", defaultValue = "namespace from where tag is used")
public void setNamespace(String namespace) {
this.namespace = namespace;
}

@StrutsTagAttribute(description="Whether the result of this action (probably a view) should be executed/rendered", type="Boolean", defaultValue="false")
@StrutsTagAttribute(description = "Whether the result of this action (probably a view) should be executed/rendered", type = "Boolean", defaultValue = "false")
public void setExecuteResult(boolean executeResult) {
this.executeResult = executeResult;
}

@StrutsTagAttribute(description="Whether the request parameters are to be included when the action is invoked", type="Boolean", defaultValue="false")
@StrutsTagAttribute(description = "Whether the request parameters are to be included when the action is invoked", type = "Boolean", defaultValue = "false")
public void setIgnoreContextParams(boolean ignoreContextParams) {
this.ignoreContextParams = ignoreContextParams;
}

@StrutsTagAttribute(description="Whether the writer should be flush upon end of action component tag, default to true", type="Boolean", defaultValue="true")
@StrutsTagAttribute(description = "Whether the writer should be flush upon end of action component tag, default to true", type = "Boolean", defaultValue = "true")
public void setFlush(boolean flush) {
this.flush = flush;
}

@StrutsTagAttribute(description="Whether an exception should be rethrown, if the target action throws an exception", type="Boolean", defaultValue="false")
@StrutsTagAttribute(description = "Whether an exception should be rethrown, if the target action throws an exception", type = "Boolean", defaultValue = "false")
public void setRethrowException(boolean rethrowException) {
this.rethrowException = rethrowException;
}
Expand Down
21 changes: 14 additions & 7 deletions core/src/main/java/org/apache/struts2/components/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.struts2.components;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.security.NotExcludedAcceptedPatternsChecker;
import com.opensymphony.xwork2.util.TextParseUtil;
Expand All @@ -34,7 +36,6 @@
import org.apache.struts2.util.ComponentUtils;
import org.apache.struts2.util.FastByteArrayOutputStream;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.apache.struts2.views.jsp.TagUtils;
import org.apache.struts2.views.util.UrlHelper;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -372,8 +373,8 @@ protected Object findValue(String expr, String field, String errorMsg) {
* evaluating to String.class, else the whole <code>expression</code> is evaluated
* against the stack.
*
* @param expression OGNL expression.
* @param toType the type expected to find.
* @param expression OGNL expression.
* @param toType the type expected to find.
* @return the Object found, or <tt>null</tt> if not found.
*/
protected Object findValue(String expression, Class<?> toType) {
Expand Down Expand Up @@ -429,7 +430,7 @@ protected String determineNamespace(String namespace, ValueStack stack, HttpServ
String result;

if (namespace == null) {
result = TagUtils.buildNamespace(actionMapper, stack, req);
result = getNamespace(stack);
} else {
result = findString(namespace);
}
Expand All @@ -441,6 +442,12 @@ protected String determineNamespace(String namespace, ValueStack stack, HttpServ
return result;
}

protected String getNamespace(ValueStack stack) {
ActionContext context = ActionContext.of(stack.getContext());
ActionInvocation invocation = context.getActionInvocation();
return invocation.getProxy().getNamespace();
}

/**
* Pushes this component's parameter Map as well as the component itself on to the stack
* and then copies the supplied parameters over. Because the component's parameter Map is
Expand Down Expand Up @@ -581,12 +588,12 @@ protected Collection<String> getStandardAttributes() {
* <em>Note:</em> All Tag classes that extend {@link org.apache.struts2.views.jsp.StrutsBodyTagSupport} must implement a setter for
* this attribute (same name), and it must be defined at the Tag class level.
* Defining a setter in the superclass alone is insufficient (results in "Cannot find a setter method for the attribute").
*
* <p>
* See {@link org.apache.struts2.views.jsp.StrutsBodyTagSupport#clearTagStateForTagPoolingServers() for additional details.
*
* @param performClearTagStateForTagPoolingServers true if tag state should be cleared, false otherwise.
*/
@StrutsTagAttribute(description="Whether to clear all tag state during doEndTag() processing (if applicable)", type="Boolean", defaultValue="false")
@StrutsTagAttribute(description = "Whether to clear all tag state during doEndTag() processing (if applicable)", type = "Boolean", defaultValue = "false")
public void setPerformClearTagStateForTagPoolingServers(boolean performClearTagStateForTagPoolingServers) {
this.performClearTagStateForTagPoolingServers = performClearTagStateForTagPoolingServers;
}
Expand All @@ -609,7 +616,7 @@ protected boolean isAcceptableExpression(String expression) {
}

LOG.warn("Expression [{}] isn't allowed by pattern [{}]! See Accepted / Excluded patterns at\n" +
"https://struts.apache.org/security/", expression, isAllowed.getAllowedPattern());
"https://struts.apache.org/security/", expression, isAllowed.getAllowedPattern());

return false;
}
Expand Down
Loading

0 comments on commit 13013e7

Please sign in to comment.