From commits-return-19646-archive-asf-public=cust-asf.ponee.io@struts.apache.org Thu Mar 25 13:00:02 2021 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mxout1-ec2-va.apache.org (mxout1-ec2-va.apache.org [3.227.148.255]) by mx-eu-01.ponee.io (Postfix) with ESMTPS id 5F0E7180679 for ; Thu, 25 Mar 2021 14:00:02 +0100 (CET) Received: from mail.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by mxout1-ec2-va.apache.org (ASF Mail Server at mxout1-ec2-va.apache.org) with SMTP id 8AF6D44D59 for ; Thu, 25 Mar 2021 13:00:01 +0000 (UTC) Received: (qmail 69464 invoked by uid 500); 25 Mar 2021 12:57:02 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 69357 invoked by uid 99); 25 Mar 2021 12:57:02 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Mar 2021 12:57:02 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 476898E947; Thu, 25 Mar 2021 12:57:02 +0000 (UTC) Date: Thu, 25 Mar 2021 12:57:02 +0000 To: "commits@struts.apache.org" Subject: [struts] branch struts-2-5-x updated: [WW-5117] Reorders stack (#475) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <161667702217.21030.9966766840572591410@gitbox.apache.org> From: yasserzamani@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: struts X-Git-Refname: refs/heads/struts-2-5-x X-Git-Reftype: branch X-Git-Oldrev: 7e912a2985a450dc6de5c22fcee87f5ab9896b40 X-Git-Newrev: a65b8d5d51d8bed0bdb10dcca57058712a5c0521 X-Git-Rev: a65b8d5d51d8bed0bdb10dcca57058712a5c0521 X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated This is an automated email from the ASF dual-hosted git repository. yasserzamani pushed a commit to branch struts-2-5-x in repository https://gitbox.apache.org/repos/asf/struts.git The following commit(s) were added to refs/heads/struts-2-5-x by this push: new a65b8d5 [WW-5117] Reorders stack (#475) a65b8d5 is described below commit a65b8d5d51d8bed0bdb10dcca57058712a5c0521 Author: Lukasz Lenart AuthorDate: Thu Mar 25 13:56:13 2021 +0100 [WW-5117] Reorders stack (#475) * Moves action on top just before the tag * Moves pushes out of try-finally block --- .../template/FreemarkerTemplateEngine.java | 17 +++++-- .../template/simple/dynamic-attributes.ftl | 2 +- .../test/java/org/apache/struts2/TestAction.java | 12 ++++- .../apache/struts2/views/jsp/ui/HiddenTest.java | 55 +++++++++++++++++++--- .../org/apache/struts2/views/jsp/ui/Hidden-3.txt | 5 ++ 5 files changed, 79 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/components/template/FreemarkerTemplateEngine.java b/core/src/main/java/org/apache/struts2/components/template/FreemarkerTemplateEngine.java index a272697..2d7ebae 100644 --- a/core/src/main/java/org/apache/struts2/components/template/FreemarkerTemplateEngine.java +++ b/core/src/main/java/org/apache/struts2/components/template/FreemarkerTemplateEngine.java @@ -64,7 +64,7 @@ public class FreemarkerTemplateEngine extends BaseTemplateEngine { public void setFreemarkerManager(FreemarkerManager mgr) { this.freemarkerManager = mgr; } - + public void renderTemplate(TemplateRenderingContext templateContext) throws Exception { // get the various items required from the stack ValueStack stack = templateContext.getStack(); @@ -121,6 +121,10 @@ public class FreemarkerTemplateEngine extends BaseTemplateEngine { ActionInvocation ai = ActionContext.getContext().getActionInvocation(); Object action = (ai == null) ? null : ai.getAction(); + if (action == null) { + LOG.warn("Rendering tag {} out of Action scope, accessing directly JSPs is not recommended! " + + "Please read https://struts.apache.org/security/#never-expose-jsp-files-directly", templateName); + } SimpleHash model = freemarkerManager.buildTemplateModel(stack, action, servletContext, req, res, config.getObjectWrapper()); model.put("tag", templateContext.getTag()); @@ -144,15 +148,20 @@ public class FreemarkerTemplateEngine extends BaseTemplateEngine { } }; + LOG.debug("Puts action on the top of ValueStack, just before the tag"); + action = stack.pop(); + stack.push(templateContext.getTag()); + stack.push(action); try { - stack.push(templateContext.getTag()); template.process(model, writer); } finally { - stack.pop(); + stack.pop(); // removes action + stack.pop(); // removes tag + stack.push(action); // puts back action } } protected String getSuffix() { return "ftl"; } -} \ No newline at end of file +} diff --git a/core/src/main/resources/template/simple/dynamic-attributes.ftl b/core/src/main/resources/template/simple/dynamic-attributes.ftl index a6e3943..95de4b7 100644 --- a/core/src/main/resources/template/simple/dynamic-attributes.ftl +++ b/core/src/main/resources/template/simple/dynamic-attributes.ftl @@ -29,4 +29,4 @@ ${aKey}="${value?html}"<#rt/> <#rt/> -<#rt/> \ No newline at end of file +<#rt/> diff --git a/core/src/test/java/org/apache/struts2/TestAction.java b/core/src/test/java/org/apache/struts2/TestAction.java index 97fa175..35b48b3 100644 --- a/core/src/test/java/org/apache/struts2/TestAction.java +++ b/core/src/test/java/org/apache/struts2/TestAction.java @@ -49,6 +49,7 @@ public class TestAction extends ActionSupport { private List list3; private SomeEnum status = SomeEnum.COMPLETED; private Float floatNumber; + private Long id; private final Map texts = new HashMap(); @@ -213,7 +214,7 @@ public class TestAction extends ActionSupport { public void setStatus(SomeEnum status) { this.status = status; } - + public List getStatusList() { return Arrays.asList(SomeEnum.values()); } @@ -225,4 +226,13 @@ public class TestAction extends ActionSupport { public void setFloatNumber(Float floatNumber) { this.floatNumber = floatNumber; } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/HiddenTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/HiddenTest.java index 37b5303..41f8950 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/HiddenTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/HiddenTest.java @@ -18,15 +18,14 @@ */ package org.apache.struts2.views.jsp.ui; -import java.util.HashMap; -import java.util.Map; - +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.mock.MockActionInvocation; import org.apache.struts2.TestAction; import org.apache.struts2.views.jsp.AbstractUITagTest; +import java.util.HashMap; +import java.util.Map; -/** - */ public class HiddenTest extends AbstractUITagTest { public void testSimple() throws Exception { @@ -62,13 +61,57 @@ public class HiddenTest extends AbstractUITagTest { verify(TextFieldTag.class.getResource("Hidden-2.txt")); } + public void testDynamicAttributesWithActionInvocation() throws Exception { + TestAction testAction = (TestAction) action; + testAction.setId(27357L); + + MockActionInvocation ai = new MockActionInvocation(); + ai.setAction(action); + ActionContext.getContext().setActionInvocation(ai); + + HiddenTag tag = new HiddenTag(); + tag.setPageContext(pageContext); + tag.setId("einszwei"); + tag.setName("first"); + tag.setValue("%{id}"); + tag.setDynamicAttribute("", "data-wuffmiauww", "%{id}"); + + tag.doStartTag(); + tag.doEndTag(); + + assertSame(stack.pop(), testAction); + assertNotSame(stack.pop(), tag); + + verify(TextFieldTag.class.getResource("Hidden-3.txt")); + } + + public void testDynamicAttributesWithStack() throws Exception { + TestAction testAction = (TestAction) action; + testAction.setId(27357L); + + HiddenTag tag = new HiddenTag(); + tag.setPageContext(pageContext); + tag.setId("einszwei"); + tag.setName("first"); + tag.setValue("%{id}"); + tag.setDynamicAttribute("", "data-wuffmiauww", "%{id}"); + + tag.doStartTag(); + tag.doEndTag(); + + assertSame(stack.pop(), testAction); + assertNotSame(stack.pop(), tag); + + verify(TextFieldTag.class.getResource("Hidden-3.txt")); + } + /** * Initialize a map of {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder} for generic tag * property testing. Will be used when calling {@link #verifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag, * String, String[])} as properties to verify.
This implementation extends testdata from AbstractUITag. * * @return A Map of PropertyHolders values bound to {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder#getName()} - * as key. + * as key. */ protected Map initializedGenericTagTestProperties() { Map result = new HashMap(); diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Hidden-3.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Hidden-3.txt new file mode 100644 index 0000000..c02c136 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Hidden-3.txt @@ -0,0 +1,5 @@ + + + + +