diff --git a/core/src/main/java/com/opensymphony/xwork2/ActionContext.java b/core/src/main/java/com/opensymphony/xwork2/ActionContext.java index fbc15b9acd..3c9d0e53f0 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ActionContext.java +++ b/core/src/main/java/com/opensymphony/xwork2/ActionContext.java @@ -63,7 +63,7 @@ public class ActionContext implements Serializable { /** * Constant for the name of the action being executed. * - * @deprecated use helper methods instead + * @deprecated scope will be narrowed to "private", use helper methods instead */ @Deprecated public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name"; @@ -95,19 +95,23 @@ public class ActionContext implements Serializable { /** * Constant for the action's {@link com.opensymphony.xwork2.ActionInvocation invocation} context. + * @deprecated scope will be narrowed to "private", use helper methods instead */ + @Deprecated public static final String ACTION_INVOCATION = "com.opensymphony.xwork2.ActionContext.actionInvocation"; /** * Constant for the map of type conversion errors. - * @deprecated use helper method instead + * @deprecated scope will be narrowed to "private", use helper methods instead */ @Deprecated public static final String CONVERSION_ERRORS = "com.opensymphony.xwork2.ActionContext.conversionErrors"; /** * Constant for the container + * @deprecated scope will be narrowed to "private", use helper methods instead */ + @Deprecated public static final String CONTAINER = "com.opensymphony.xwork2.ActionContext.container"; private final Map context; @@ -518,4 +522,15 @@ public ActionContext usePageContextOrClear(ActionContext actionContext) { return this; } + public ActionContext withExtraContext(Map extraContext) { + if (extraContext != null) { + getContext().context.putAll(extraContext); + } + return this; + } + + public ActionContext withLocale(Locale locale) { + put(LOCALE, locale); + return this; + } } diff --git a/core/src/main/java/com/opensymphony/xwork2/DefaultActionInvocation.java b/core/src/main/java/com/opensymphony/xwork2/DefaultActionInvocation.java index 215d56855b..acf7a6515b 100644 --- a/core/src/main/java/com/opensymphony/xwork2/DefaultActionInvocation.java +++ b/core/src/main/java/com/opensymphony/xwork2/DefaultActionInvocation.java @@ -324,9 +324,10 @@ protected void createAction(Map contextMap) { } protected Map createContextMap() { - Map contextMap; + ActionContext oldContext = ActionContext.getContext(); + ActionContext actionContext; - if ((extraContext != null) && (extraContext.containsKey(ActionContext.VALUE_STACK))) { + if (extraContext != null && extraContext.containsKey(ActionContext.VALUE_STACK)) { // In case the ValueStack was passed in stack = (ValueStack) extraContext.get(ActionContext.VALUE_STACK); @@ -334,26 +335,30 @@ protected Map createContextMap() { throw new IllegalStateException("There was a null Stack set into the extra params."); } - contextMap = stack.getContext(); + actionContext = stack.getActionContext(); } else { // create the value stack // this also adds the ValueStack to its context stack = valueStackFactory.createValueStack(); // create the action context - contextMap = stack.getContext(); + actionContext = stack.getActionContext(); } - // put extraContext in - if (extraContext != null) { - contextMap.putAll(extraContext); + try { + return actionContext + .bind() + .withExtraContext(extraContext) + .withActionInvocation(this) + .withContainer(container) + .getContextMap(); + } finally { + ActionContext.clear(); + if (oldContext != null) { + LOG.debug("Re-binding the old context"); + oldContext.bind(); + } } - - //put this DefaultActionInvocation into the context map - contextMap.put(ActionContext.ACTION_INVOCATION, this); - contextMap.put(ActionContext.CONTAINER, container); - - return contextMap; } /** diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java index bf4e5716cf..d19fd6bfaa 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStackFactory.java @@ -34,17 +34,17 @@ import org.apache.commons.lang3.BooleanUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.apache.struts2.StrutsConstants; import java.util.Map; import java.util.Set; -import org.apache.struts2.StrutsConstants; /** * Creates an Ognl value stack */ public class OgnlValueStackFactory implements ValueStackFactory { - - private static final Logger LOG = LogManager.getLogger(OgnlValueStackFactory.class); + + private static final Logger LOG = LogManager.getLogger(OgnlValueStackFactory.class); protected XWorkConverter xworkConverter; protected CompoundRootAccessor compoundRootAccessor; @@ -55,7 +55,7 @@ public class OgnlValueStackFactory implements ValueStackFactory { protected void setXWorkConverter(XWorkConverter converter) { this.xworkConverter = converter; } - + @Inject("system") protected void setTextProvider(TextProvider textProvider) { this.textProvider = textProvider; @@ -63,20 +63,44 @@ protected void setTextProvider(TextProvider textProvider) { public ValueStack createValueStack() { ValueStack stack = new OgnlValueStack(xworkConverter, compoundRootAccessor, textProvider, - containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess()); + containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess()); container.inject(stack); - stack.getContext().put(ActionContext.CONTAINER, container); - return stack; + ActionContext oldContext = ActionContext.getContext(); + try { + return stack.getActionContext() + .bind() + .withContainer(container) + .withValueStack(stack) + .getValueStack(); + } finally { + ActionContext.clear(); + if (oldContext != null) { + LOG.debug("Re-binding the old context"); + oldContext.bind(); + } + } } public ValueStack createValueStack(ValueStack stack) { ValueStack result = new OgnlValueStack(stack, xworkConverter, compoundRootAccessor, - containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess()); + containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess()); container.inject(result); - stack.getContext().put(ActionContext.CONTAINER, container); - return result; + ActionContext oldContext = ActionContext.getContext(); + try { + return result.getActionContext() + .bind() + .withContainer(container) + .withValueStack(result) + .getValueStack(); + } finally { + ActionContext.clear(); + if (oldContext != null) { + LOG.debug("Re-binding the old context"); + oldContext.bind(); + } + } } - + @Inject protected void setContainer(Container container) throws ClassNotFoundException { Set names = container.getInstanceNames(PropertyAccessor.class); @@ -116,7 +140,7 @@ protected void setContainer(Container container) throws ClassNotFoundException { /** * Retrieve allowsStaticMethodAccess state from the container (allows for lazy fetching) - * + * * @return */ protected boolean containerAllowsStaticMethodAccess() { @@ -125,7 +149,7 @@ protected boolean containerAllowsStaticMethodAccess() { /** * Retrieve allowStaticFieldAccess state from the container (allows for lazy fetching) - * + * * @return */ protected boolean containerAllowsStaticFieldAccess() { diff --git a/core/src/main/java/com/opensymphony/xwork2/util/TextParseUtil.java b/core/src/main/java/com/opensymphony/xwork2/util/TextParseUtil.java index f22a255b6a..c054856699 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/TextParseUtil.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/TextParseUtil.java @@ -164,7 +164,7 @@ public Object evaluate(String parsedValue) { } }; - TextParser parser = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(TextParser.class); + TextParser parser = stack.getActionContext().getContainer().getInstance(TextParser.class); return parser.evaluate(openChars, expression, ognlEval, maxLoopCount); } @@ -205,8 +205,8 @@ public Object evaluate(String parsedValue) { } }; - Map context = stack.getContext(); - TextParser parser = ((Container)context.get(ActionContext.CONTAINER)).getInstance(TextParser.class); + ActionContext actionContext = stack.getActionContext(); + TextParser parser = actionContext.getContainer().getInstance(TextParser.class); Object result = parser.evaluate(openChars, expression, ognlEval, maxLoopCount); @@ -216,10 +216,10 @@ public Object evaluate(String parsedValue) { Collection casted = (Collection)result; resultCol = new ArrayList<>(); - XWorkConverter conv = ((Container)context.get(ActionContext.CONTAINER)).getInstance(XWorkConverter.class); + XWorkConverter conv = actionContext.getContainer().getInstance(XWorkConverter.class); for (Object element : casted) { - String stringElement = (String)conv.convertValue(context, element, String.class); + String stringElement = (String) conv.convertValue(actionContext.getContextMap(), element, String.class); if (shallBeIncluded(stringElement, excludeEmptyElements)) { if (evaluator != null) { stringElement = evaluator.evaluate(stringElement).toString(); diff --git a/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java b/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java index 6bef3af3d2..bde73ca920 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java @@ -39,14 +39,8 @@ public static ConfigurationManager setUp() throws Exception { // Reset the value stack ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); - stack.getContext().put(ActionContext.CONTAINER, container); - ActionContext.of(stack.getContext()).bind(); - - // clear out localization - //container.getInstance(LocalizedTextUtil.class).reset(); - - - //ObjectFactory.setObjectFactory(container.getInstance(ObjectFactory.class)); + stack.getActionContext().withContainer(container).withValueStack(stack).bind(); + return configurationManager; } @@ -79,9 +73,8 @@ public void register(ContainerBuilder builder, LocatableProperties props) throws // Reset the value stack ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); - stack.getContext().put(ActionContext.CONTAINER, container); - ActionContext.of(stack.getContext()).bind(); - + stack.getActionContext().withContainer(container).withValueStack(stack).bind(); + return configurationManager; } diff --git a/core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java b/core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java index 550fa355b1..b8b7a3c51e 100644 --- a/core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java +++ b/core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java @@ -137,8 +137,7 @@ public void renderFormUrl(Form formComponent) { } else { // no action supplied? ok, then default to the current request // (action or general URL) - ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get( - ActionContext.ACTION_INVOCATION); + ActionInvocation ai = formComponent.getStack().getActionContext().getActionInvocation(); if (ai != null) { action = ai.getProxy().getActionName(); namespace = ai.getProxy().getNamespace(); diff --git a/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java index e87ec6e581..42e107f40f 100644 --- a/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java +++ b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java @@ -47,9 +47,8 @@ public static Dispatcher initDispatcher(ServletContext ctx, Map p // Reset the value stack Container container = du.getContainer(); ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); - stack.getContext().put(ActionContext.CONTAINER, container); - ActionContext.of(stack.getContext()).bind(); - + stack.getActionContext().withContainer(container).withValueStack(stack).bind(); + return du; } diff --git a/core/src/main/java/org/apache/struts2/util/StrutsUtil.java b/core/src/main/java/org/apache/struts2/util/StrutsUtil.java index 647a743520..2d95314bfa 100644 --- a/core/src/main/java/org/apache/struts2/util/StrutsUtil.java +++ b/core/src/main/java/org/apache/struts2/util/StrutsUtil.java @@ -61,9 +61,9 @@ public StrutsUtil(ValueStack stack, HttpServletRequest request, HttpServletRespo this.stack = stack; this.request = request; this.response = response; - this.ognl = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(OgnlTool.class); - this.urlHelper = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(UrlHelper.class); - this.objectFactory = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(ObjectFactory.class); + this.ognl = stack.getActionContext().getContainer().getInstance(OgnlTool.class); + this.urlHelper = stack.getActionContext().getContainer().getInstance(UrlHelper.class); + this.objectFactory = stack.getActionContext().getContainer().getInstance(ObjectFactory.class); } public Object bean(Object aName) throws Exception { diff --git a/core/src/main/java/org/apache/struts2/views/freemarker/tags/TagModel.java b/core/src/main/java/org/apache/struts2/views/freemarker/tags/TagModel.java index 5d307a2bf3..112457485c 100644 --- a/core/src/main/java/org/apache/struts2/views/freemarker/tags/TagModel.java +++ b/core/src/main/java/org/apache/struts2/views/freemarker/tags/TagModel.java @@ -55,7 +55,7 @@ public TagModel(ValueStack stack, HttpServletRequest req, HttpServletResponse re public Writer getWriter(Writer writer, Map params) throws TemplateModelException, IOException { Component bean = getBean(); - Container container = (Container) stack.getContext().get(ActionContext.CONTAINER); + Container container = stack.getActionContext().getContainer(); container.inject(bean); Map unwrappedParameters = unwrapParameters(params); diff --git a/core/src/main/java/org/apache/struts2/views/jsp/ComponentTagSupport.java b/core/src/main/java/org/apache/struts2/views/jsp/ComponentTagSupport.java index f39d5817df..9dd463cb6d 100644 --- a/core/src/main/java/org/apache/struts2/views/jsp/ComponentTagSupport.java +++ b/core/src/main/java/org/apache/struts2/views/jsp/ComponentTagSupport.java @@ -44,7 +44,7 @@ public int doEndTag() throws JspException { public int doStartTag() throws JspException { ValueStack stack = getStack(); component = getBean(stack, (HttpServletRequest) pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse()); - Container container = (Container) stack.getContext().get(ActionContext.CONTAINER); + Container container = stack.getActionContext().getContainer(); container.inject(component); populateParams(); diff --git a/core/src/main/java/org/apache/struts2/views/util/ContextUtil.java b/core/src/main/java/org/apache/struts2/views/util/ContextUtil.java index 8d493f6176..d213f5a791 100644 --- a/core/src/main/java/org/apache/struts2/views/util/ContextUtil.java +++ b/core/src/main/java/org/apache/struts2/views/util/ContextUtil.java @@ -56,10 +56,10 @@ public static Map getStandardContext(ValueStack stack, HttpServl map.put(SESSION, req.getSession(false)); map.put(BASE, req.getContextPath()); map.put(STACK, stack); - map.put(OGNL, ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(OgnlTool.class)); + map.put(OGNL, stack.getActionContext().getContainer().getInstance(OgnlTool.class)); map.put(STRUTS, new StrutsUtil(stack, req, res)); - ActionInvocation invocation = (ActionInvocation) stack.getContext().get(ActionContext.ACTION_INVOCATION); + ActionInvocation invocation = stack.getActionContext().getActionInvocation(); if (invocation != null) { map.put(ACTION, invocation.getAction()); } @@ -71,22 +71,23 @@ public static Map getStandardContext(ValueStack stack, HttpServl * @param context stack's context * @return boolean */ - public static boolean isUseAltSyntax(Map context) { + public static boolean isUseAltSyntax(Map context) { // We didn't make altSyntax static cause, if so, struts.configuration.xml.reload will not work // plus the Configuration implementation should cache the properties, which the framework's // configuration implementation does - return "true".equals(((Container)context.get(ActionContext.CONTAINER)).getInstance(String.class, StrutsConstants.STRUTS_TAG_ALTSYNTAX)) ||( + String tagAltSytnax = ActionContext.of(context).getContainer().getInstance(String.class, StrutsConstants.STRUTS_TAG_ALTSYNTAX); + return "true".equals(tagAltSytnax) ||( (context.containsKey("useAltSyntax") && context.get("useAltSyntax") != null && "true".equals(context.get("useAltSyntax").toString()))); } - + /** * Returns a String for overriding the default templateSuffix if templateSuffix is on the stack * @param context stack's context * @return String */ - public static String getTemplateSuffix(Map context) { + public static String getTemplateSuffix(Map context) { return context.containsKey("templateSuffix") ? (String) context.get("templateSuffix") : null; } } diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidatorSupportTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidatorSupportTest.java index b9b805ca69..c1b7f1836d 100644 --- a/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidatorSupportTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidatorSupportTest.java @@ -28,10 +28,9 @@ public class ValidatorSupportTest extends XWorkTestCase { public void testConditionalParseExpression() { OgnlValueStack stack = (OgnlValueStack) container.getInstance(ValueStackFactory.class).createValueStack(); - stack.getContext().put(ActionContext.CONTAINER, container); stack.getContext().put("something", "somevalue"); - ActionContext.of(stack.getContext()).bind(); + ActionContext.of(stack.getContext()).withContainer(container).bind(); ValidatorSupport validator = new ValidatorSupport() { public void validate(Object object) throws ValidationException { diff --git a/core/src/test/java/org/apache/struts2/result/ServletRedirectResultTest.java b/core/src/test/java/org/apache/struts2/result/ServletRedirectResultTest.java index a1c046ac36..c6be2ab0df 100644 --- a/core/src/test/java/org/apache/struts2/result/ServletRedirectResultTest.java +++ b/core/src/test/java/org/apache/struts2/result/ServletRedirectResultTest.java @@ -293,11 +293,10 @@ public void testIncludeCollectionParameterInResult() throws Exception { ActionInvocation mockInvocation = control.createMock(ActionInvocation.class); ValueStack mockValueStack = control.createMock(ValueStack.class); - Map mockContext = new HashMap<>(); - mockContext.put(ActionContext.CONTAINER, container); + ActionContext actionContext = ActionContext.of(new HashMap<>()).withContainer(container); expect(mockInvocation.getStack()).andReturn(mockValueStack); - expect(mockValueStack.getContext()).andReturn(mockContext); + expect(mockValueStack.getActionContext()).andReturn(actionContext); expect(mockInvocation.getStack()).andReturn(mockValueStack); @@ -308,7 +307,7 @@ public void testIncludeCollectionParameterInResult() throws Exception { expect(mockActionProxy.getConfig()).andReturn(actionConfig); expect(mockInvocation.getInvocationContext()).andReturn(context); - expect(mockValueStack.getContext()).andReturn(mockContext); + expect(mockValueStack.getActionContext()).andReturn(actionContext); control.replay(); result.setActionMapper(container.getInstance(ActionMapper.class)); diff --git a/core/src/test/java/org/apache/struts2/views/jsp/TextTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/TextTagTest.java index e0a1201c99..684d262ef1 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/TextTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/TextTagTest.java @@ -18,15 +18,11 @@ */ package org.apache.struts2.views.jsp; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Locale; - -import javax.servlet.jsp.JspException; -import javax.servlet.jsp.tagext.BodyTag; - +import com.mockobjects.servlet.MockJspWriter; +import com.opensymphony.xwork2.Action; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.util.ValueStackFactory; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsException; import org.apache.struts2.TestAction; @@ -34,16 +30,19 @@ import org.apache.struts2.views.jsp.ui.StrutsBodyContent; import org.apache.struts2.views.jsp.ui.TestAction1; -import com.mockobjects.servlet.MockJspWriter; -import com.opensymphony.xwork2.Action; -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.util.ValueStack; -import com.opensymphony.xwork2.util.ValueStackFactory; +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.tagext.BodyTag; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +import static org.junit.Assert.assertNotEquals; /** * TextTagTest - * */ public class TextTagTest extends AbstractTagTest { @@ -173,8 +172,7 @@ public void testTextTagUsesValueStackInRequestNotActionContext() throws JspExcep final StringBuffer buffer = writer.getBuffer(); buffer.delete(0, buffer.length()); ValueStack newStack = container.getInstance(ValueStackFactory.class).createValueStack(); - newStack.getContext().put(ActionContext.LOCALE, foreignLocale); - newStack.getContext().put(ActionContext.CONTAINER, container); + newStack.getActionContext().withLocale(foreignLocale).withContainer(container); newStack.push(container.inject(TestAction1.class)); request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, newStack); assertNotSame(ActionContext.getContext().getValueStack().peek(), newStack.peek()); @@ -203,10 +201,9 @@ public void testTextTagUsesLocaleFromValueStack() throws JspException { final StringBuffer buffer = writer.getBuffer(); buffer.delete(0, buffer.length()); String value_int = getLocalizedMessage(foreignLocale); - assertFalse(value_default.equals(value_int)); + assertNotEquals(value_default, value_int); ValueStack newStack = container.getInstance(ValueStackFactory.class).createValueStack(stack); - newStack.getContext().put(ActionContext.LOCALE, foreignLocale); - newStack.getContext().put(ActionContext.CONTAINER, container); + newStack.getActionContext().withLocale(foreignLocale).withContainer(container); assertNotSame(newStack.getContext().get(ActionContext.LOCALE), ActionContext.getContext().getLocale()); request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, newStack); assertEquals(ActionContext.getContext().getValueStack().peek(), newStack.peek()); diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java index 0a4e50d971..bbee23b501 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java @@ -110,6 +110,6 @@ public void register(ContainerBuilder builder, LocatableProperties props) throws configurationManager.reload(); container = configurationManager.getConfiguration().getContainer(); - stack.getContext().put(ActionContext.CONTAINER, container); + stack.getActionContext().withContainer(container); } } \ No newline at end of file diff --git a/core/src/test/java/org/apache/struts2/views/util/ContextUtilTest.java b/core/src/test/java/org/apache/struts2/views/util/ContextUtilTest.java index 3da24477bd..9a79cb8bb5 100755 --- a/core/src/test/java/org/apache/struts2/views/util/ContextUtilTest.java +++ b/core/src/test/java/org/apache/struts2/views/util/ContextUtilTest.java @@ -36,7 +36,7 @@ public class ContextUtilTest extends StrutsInternalTestCase { private void setAltSyntax(ValueStack stack, String val) { Mock container = new Mock(Container.class); container.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_TAG_ALTSYNTAX)), val); - stack.getContext().put(ActionContext.CONTAINER, container.proxy()); + stack.getActionContext().withContainer((Container) container.proxy()); } public void testAltSyntaxMethod1() throws Exception { diff --git a/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java b/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java index 1532e9bbfd..9266e49230 100644 --- a/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java +++ b/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java @@ -427,7 +427,7 @@ public void testDecodeSpacesInQueryString() throws Exception { public void setUp() throws Exception { super.setUp(); stubContainer = new StubContainer(container); - ActionContext.getContext().put(ActionContext.CONTAINER, stubContainer); + ActionContext.getContext().withContainer(stubContainer); urlHelper = new DefaultUrlHelper(); } diff --git a/plugins/embeddedjsp/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java b/plugins/embeddedjsp/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java index ceaa8df96e..a5b3bb254f 100644 --- a/plugins/embeddedjsp/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java +++ b/plugins/embeddedjsp/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java @@ -330,9 +330,8 @@ protected void setUp() throws Exception { EasyMock.replay(request); //mock value stack - Map stackContext = new HashMap<>(); ValueStack valueStack = EasyMock.createNiceMock(ValueStack.class); - EasyMock.expect(valueStack.getContext()).andReturn(stackContext).anyTimes(); + EasyMock.expect(valueStack.getActionContext()).andReturn(ActionContext.getContext()).anyTimes(); EasyMock.replay(valueStack); //mock converter @@ -355,7 +354,6 @@ protected void setUp() throws Exception { EasyMock.expect(container.getInstance(FileManagerFactory.class)).andReturn(fileManagerFactory).anyTimes(); EasyMock.replay(container); - stackContext.put(ActionContext.CONTAINER, container); ActionContext.of(new HashMap<>()) .withParameters(HttpParameters.create(params).build()) diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java index aa14c6714b..00f17fce0a 100644 --- a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java @@ -21,6 +21,7 @@ package org.apache.struts2.views.java.simple; +import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.conversion.impl.XWorkConverter; import com.opensymphony.xwork2.inject.Container; @@ -101,11 +102,13 @@ protected void setUp() throws Exception { context = new TemplateRenderingContext(template, writer, stack, map, null); stackContext.put(Component.COMPONENT_STACK, new Stack()); + ActionContext actionContext = ActionContext.of(stackContext).bind(); request = createNiceMock(HttpServletRequest.class); expect(request.getContextPath()).andReturn("/some/path").anyTimes(); response = createNiceMock(HttpServletResponse.class); + expect(stack.getActionContext()).andReturn(actionContext).anyTimes(); expect(stack.getContext()).andReturn(stackContext).anyTimes(); Container container = createNiceMock(Container.class); @@ -114,15 +117,12 @@ protected void setUp() throws Exception { expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes(); TextParser parser = new OgnlTextParser(); expect(container.getInstance(TextParser.class)).andReturn(parser).anyTimes(); - stackContext.put(ActionContext.CONTAINER, container); - replay(request); replay(stack); replay(container); - ActionContext actionContext = ActionContext.of(stackContext).bind(); - actionContext.withServletRequest(request); + actionContext.withContainer(container).withServletRequest(request); } protected static String s(String input) { diff --git a/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java b/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java index 9277d67d04..4b51f7ec02 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java @@ -165,7 +165,7 @@ public void renderFormUrl(Form formComponent) { if (formComponent.action != null) { action = formComponent.findString(formComponent.action); } else { - ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION); + ActionInvocation ai = formComponent.getStack().getActionContext().getActionInvocation(); action = ai.getProxy().getActionName(); } diff --git a/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java b/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java index fbc87db024..afc2c5c2b5 100644 --- a/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java +++ b/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java @@ -43,7 +43,7 @@ public void setUp() throws Exception { super.setUp(); ActionProxy actionProxy = getActionProxy("/portlettest/test"); // creates new empty ActionContext - ActionContext.getContext().put(ActionContext.ACTION_INVOCATION, actionProxy.getInvocation()); + ActionContext.getContext().withActionInvocation(actionProxy.getInvocation()); PortletContext portletCtx = new MockPortletContext(); ActionContext.getContext().put(StrutsStatics.STRUTS_PORTLET_CONTEXT, portletCtx); diff --git a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java index 99f0059403..e955b32aa3 100644 --- a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java @@ -62,7 +62,7 @@ public boolean render(InternalContextAdapter ctx, Writer writer, Node node) thro HttpServletRequest req = (HttpServletRequest) stack.getContext().get(ServletActionContext.HTTP_REQUEST); HttpServletResponse res = (HttpServletResponse) stack.getContext().get(ServletActionContext.HTTP_RESPONSE); Component bean = getBean(stack, req, res); - Container container = (Container) stack.getContext().get(ActionContext.CONTAINER); + Container container = stack.getActionContext().getContainer(); container.inject(bean); // get the parameters Map params = createPropertyMap(ctx, node);