From 32584c355112bbdb9a89b6b12984b8d989ba77cf Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Sat, 30 Dec 2023 23:55:12 +1100 Subject: [PATCH 1/8] WW-5382 Fix StrutsInternalTestCase --- .../struts2/StrutsInternalTestCase.java | 21 ++++++++++--------- .../apache/struts2/config/SettingsTest.java | 2 +- core/src/test/resources/struts.properties | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java b/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java index 30616303fa..5aad829694 100644 --- a/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java +++ b/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java @@ -18,12 +18,14 @@ */ package org.apache.struts2; +import com.opensymphony.xwork2.ActionProxyFactory; import com.opensymphony.xwork2.XWorkTestCase; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.dispatcher.PrepareOperations; import org.apache.struts2.util.StrutsTestCaseHelper; import org.apache.struts2.views.jsp.StrutsMockServletContext; + import java.util.HashMap; import java.util.Map; @@ -38,22 +40,22 @@ public abstract class StrutsInternalTestCase extends XWorkTestCase { /** * Sets up the configuration settings, XWork configuration, and * message resources - * + * * @throws java.lang.Exception */ @Override protected void setUp() throws Exception { - super.setUp(); PrepareOperations.clearDevModeOverride(); // Clear DevMode override every time (consistent ThreadLocal state for tests). initDispatcher(null); } - + protected Dispatcher initDispatcher(Map params) { servletContext = new StrutsMockServletContext(); dispatcher = StrutsTestCaseHelper.initDispatcher(servletContext, params); configurationManager = dispatcher.getConfigurationManager(); configuration = configurationManager.getConfiguration(); container = configuration.getContainer(); + actionProxyFactory = container.getInstance(ActionProxyFactory.class); container.inject(dispatcher); return dispatcher; } @@ -66,14 +68,13 @@ protected Dispatcher initDispatcher(Map params) { * @return instance of {@see Dispatcher} */ protected Dispatcher initDispatcherWithConfigs(String configs) { - Map params = new HashMap(); + Map params = new HashMap<>(); params.put("config", configs); return initDispatcher(params); } @Override protected void tearDown() throws Exception { - super.tearDown(); // maybe someone else already destroyed Dispatcher if (dispatcher != null && dispatcher.getConfigurationManager() != null) { dispatcher.cleanup(); @@ -83,14 +84,14 @@ protected void tearDown() throws Exception { } /** - * Compare if two objects are considered equal according to their fields as accessed + * Compare if two objects are considered equal according to their fields as accessed * via reflection. - * - * Utilizes {@link EqualsBuilder#reflectionEquals(java.lang.Object, java.lang.Object, boolean)} to perform + * + * Utilizes {@link EqualsBuilder#reflectionEquals(java.lang.Object, java.lang.Object, boolean)} to perform * the check, and compares transient fields as well. This may fail when run while a security manager is * active, due to a need to user reflection. - * - * + * + * * @param obj1 the first {@link Object} to compare against the other. * @param obj2 the second {@link Object} to compare against the other. * @return true if the objects are equal based on field comparisons by reflection, false otherwise. diff --git a/core/src/test/java/org/apache/struts2/config/SettingsTest.java b/core/src/test/java/org/apache/struts2/config/SettingsTest.java index c31a53adbd..90b824ae43 100644 --- a/core/src/test/java/org/apache/struts2/config/SettingsTest.java +++ b/core/src/test/java/org/apache/struts2/config/SettingsTest.java @@ -36,7 +36,7 @@ public void testSettings() { Settings settings = new DefaultSettings(); assertEquals("12345", settings.get(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)); - assertEquals("\temp", settings.get(StrutsConstants.STRUTS_MULTIPART_SAVEDIR)); + assertEquals("\\temp", settings.get(StrutsConstants.STRUTS_MULTIPART_SAVEDIR)); assertEquals("test,org/apache/struts2/othertest", settings.get( StrutsConstants.STRUTS_CUSTOM_PROPERTIES)); assertEquals("testvalue", settings.get("testkey")); diff --git a/core/src/test/resources/struts.properties b/core/src/test/resources/struts.properties index baf7481564..7d905d214a 100644 --- a/core/src/test/resources/struts.properties +++ b/core/src/test/resources/struts.properties @@ -24,7 +24,7 @@ struts.i18n.encoding=ISO-8859-1 struts.locale=de_DE -struts.multipart.saveDir=\temp +struts.multipart.saveDir=\\temp struts.multipart.maxSize=12345 ### Load custom property files (does not override struts.properties!) From 15acd72d240f356685ab2238a0ab4e62130c84ae Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Sun, 31 Dec 2023 00:29:29 +1100 Subject: [PATCH 2/8] WW-5382 Fix stale injections in Dispatcher --- .../apache/struts2/dispatcher/Dispatcher.java | 47 ++++++++----------- .../struts2/dispatcher/MockDispatcher.java | 19 +++++++- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index e378633e27..7234dfe6c3 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -119,6 +119,8 @@ public class Dispatcher { */ private static final List dispatcherListeners = new CopyOnWriteArrayList<>(); + private Container injectedContainer; + /** * Store state of StrutsConstants.STRUTS_DEVMODE setting. */ @@ -331,6 +333,12 @@ public void setHandleException(String handleException) { this.handleException = Boolean.parseBoolean(handleException); } + @Inject(StrutsConstants.STRUTS_DISPATCHER_PARAMETERSWORKAROUND) + public void setDispatchersParametersWorkaround(String dispatchersParametersWorkaround) { + this.paramsWorkaroundEnabled = Boolean.parseBoolean(dispatchersParametersWorkaround) + || (servletContext != null && StringUtils.contains(servletContext.getServerInfo(), "WebLogic")); + } + public boolean isHandleException() { return handleException; } @@ -536,17 +544,6 @@ private Container init_PreloadConfiguration() { return getContainer(); } - private void init_CheckWebLogicWorkaround(Container container) { - // test whether param-access workaround needs to be enabled - if (servletContext != null && StringUtils.contains(servletContext.getServerInfo(), "WebLogic")) { - LOG.info("WebLogic server detected. Enabling Struts parameter access work-around."); - paramsWorkaroundEnabled = true; - } else { - paramsWorkaroundEnabled = "true".equals(container.getInstance(String.class, - StrutsConstants.STRUTS_DISPATCHER_PARAMETERSWORKAROUND)); - } - } - /** * Load configurations, including both XML and zero-configuration strategies, * and update optional settings, including whether to reload configurations and resource files. @@ -567,9 +564,7 @@ public void init() { init_AliasStandardObjects(); // [7] init_DeferredXmlConfigurations(); - Container container = init_PreloadConfiguration(); - container.inject(this); - init_CheckWebLogicWorkaround(container); + getContainer(); // Inject this instance if (!dispatcherListeners.isEmpty()) { for (DispatcherListener l : dispatcherListeners) { @@ -1068,22 +1063,18 @@ public ConfigurationManager getConfigurationManager() { * @return Our dependency injection container */ public Container getContainer() { - if (ContainerHolder.get() != null) { - return ContainerHolder.get(); - } - ConfigurationManager mgr = getConfigurationManager(); - if (mgr == null) { - throw new IllegalStateException("The configuration manager shouldn't be null"); - } else { - Configuration config = mgr.getConfiguration(); - if (config == null) { - throw new IllegalStateException("Unable to load configuration"); - } else { - Container container = config.getContainer(); - ContainerHolder.store(container); - return container; + if (ContainerHolder.get() == null) { + try { + ContainerHolder.store(getConfigurationManager().getConfiguration().getContainer()); + } catch (NullPointerException e) { + throw new IllegalStateException("ConfigurationManager and/or Configuration should not be null", e); } } + if (injectedContainer != ContainerHolder.get()) { + injectedContainer = ContainerHolder.get(); + injectedContainer.inject(this); + } + return ContainerHolder.get(); } } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java index b10014995d..500e1b5418 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java @@ -19,14 +19,19 @@ package org.apache.struts2.dispatcher; import com.opensymphony.xwork2.config.ConfigurationManager; +import com.opensymphony.xwork2.inject.Container; import javax.servlet.ServletContext; -import java.util.HashMap; import java.util.Map; +/** + * We really shouldn't test with this class because it relies on changing the ConfigurationManager mid-lifecycle, + * but retaining the stale injections - this will prevent us from detecting exactly these kind of bugs in our tests... + */ public class MockDispatcher extends Dispatcher { private final ConfigurationManager copyConfigurationManager; + private boolean injectedOnce = false; public MockDispatcher(ServletContext servletContext, Map context, ConfigurationManager configurationManager) { super(servletContext, context); @@ -39,4 +44,16 @@ public void init() { ContainerHolder.clear(); this.configurationManager = copyConfigurationManager; } + + @Override + public Container getContainer() { + if (!injectedOnce) { + injectedOnce = true; + return super.getContainer(); + } + if (ContainerHolder.get() == null) { + ContainerHolder.store(getConfigurationManager().getConfiguration().getContainer()); + } + return ContainerHolder.get(); + } } From 2024d831772a8ce19cd915671a7d570762135ec9 Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Sun, 31 Dec 2023 00:30:15 +1100 Subject: [PATCH 3/8] WW-5382 Fix stale bootstrap context on ActionContext --- .../xwork2/config/impl/DefaultConfiguration.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java b/core/src/main/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java index 4a6ee1373e..3c60af76c1 100644 --- a/core/src/main/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java +++ b/core/src/main/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java @@ -325,12 +325,8 @@ public Class type() { } protected ActionContext setContext(Container cont) { - ActionContext context = ActionContext.getContext(); - if (context == null) { - ValueStack vs = cont.getInstance(ValueStackFactory.class).createValueStack(); - context = ActionContext.of(vs.getContext()).bind(); - } - return context; + ValueStack vs = cont.getInstance(ValueStackFactory.class).createValueStack(); + return ActionContext.of(vs.getContext()).bind(); } protected Container createBootstrapContainer(List providers) { From 6f1e1222bcf2d5e899f03b1a5e23aef1eb250071 Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Tue, 2 Jan 2024 01:46:28 +1100 Subject: [PATCH 4/8] WW-5382 Rework existing Dispatcher tests and base test classes --- .../xwork2/XWorkJUnit4TestCase.java | 4 - .../opensymphony/xwork2/XWorkTestCase.java | 4 - .../struts2/dispatcher/ContainerHolder.java | 12 +- .../apache/struts2/dispatcher/Dispatcher.java | 2 +- .../struts2/dispatcher/MockDispatcher.java | 59 --- .../struts2/util/StrutsTestCaseHelper.java | 24 +- .../struts2/StrutsInternalTestCase.java | 7 +- .../struts2/StrutsJUnit4InternalTestCase.java | 61 +++ .../struts2/dispatcher/DispatcherTest.java | 461 ++++++++---------- .../struts2/views/jsp/AbstractTagTest.java | 6 +- .../struts2/junit/StrutsJUnit4TestCase.java | 10 +- .../apache/struts2/junit/StrutsTestCase.java | 11 +- .../struts2/StrutsTestCasePortletTests.java | 24 +- .../apache/struts2/testng/StrutsTestCase.java | 12 +- 14 files changed, 313 insertions(+), 384 deletions(-) delete mode 100644 core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java create mode 100644 core/src/test/java/org/apache/struts2/StrutsJUnit4InternalTestCase.java diff --git a/core/src/main/java/com/opensymphony/xwork2/XWorkJUnit4TestCase.java b/core/src/main/java/com/opensymphony/xwork2/XWorkJUnit4TestCase.java index b22789cc23..fe39c0c2a8 100644 --- a/core/src/main/java/com/opensymphony/xwork2/XWorkJUnit4TestCase.java +++ b/core/src/main/java/com/opensymphony/xwork2/XWorkJUnit4TestCase.java @@ -51,10 +51,6 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { XWorkTestCaseHelper.tearDown(configurationManager); - configurationManager = null; - configuration = null; - container = null; - actionProxyFactory = null; } protected void loadConfigurationProviders(ConfigurationProvider... providers) { diff --git a/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java b/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java index 941a9e37c3..dc6bb25083 100644 --- a/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java +++ b/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java @@ -64,10 +64,6 @@ protected void setUp() throws Exception { @Override protected void tearDown() throws Exception { XWorkTestCaseHelper.tearDown(configurationManager); - configurationManager = null; - configuration = null; - container = null; - actionProxyFactory = null; } protected void loadConfigurationProviders(ConfigurationProvider... providers) { diff --git a/core/src/main/java/org/apache/struts2/dispatcher/ContainerHolder.java b/core/src/main/java/org/apache/struts2/dispatcher/ContainerHolder.java index 9565732ace..2d1c61657e 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/ContainerHolder.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/ContainerHolder.java @@ -23,25 +23,25 @@ /** * Simple class to hold Container instance per thread to minimise number of attempts * to read configuration and build each time a new configuration. - * + *

* As ContainerHolder operates just per thread (which means per request) there is no need * to check if configuration changed during the same request. If changed between requests, * first call to store Container in ContainerHolder will be with the new configuration. */ class ContainerHolder { - private static ThreadLocal instance = new ThreadLocal<>(); + private static final ThreadLocal instance = new ThreadLocal<>(); - public static void store(Container instance) { - ContainerHolder.instance.set(instance); + public static void store(Container newInstance) { + instance.set(newInstance); } public static Container get() { - return ContainerHolder.instance.get(); + return instance.get(); } public static void clear() { - ContainerHolder.instance.remove(); + instance.remove(); } } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index 7234dfe6c3..a53398b775 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -367,7 +367,7 @@ public void cleanup() { } // clean up Dispatcher itself for this thread - instance.set(null); + instance.remove(); servletContext.setAttribute(StrutsStatics.SERVLET_DISPATCHER, null); // clean up DispatcherListeners diff --git a/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java deleted file mode 100644 index 500e1b5418..0000000000 --- a/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.dispatcher; - -import com.opensymphony.xwork2.config.ConfigurationManager; -import com.opensymphony.xwork2.inject.Container; - -import javax.servlet.ServletContext; -import java.util.Map; - -/** - * We really shouldn't test with this class because it relies on changing the ConfigurationManager mid-lifecycle, - * but retaining the stale injections - this will prevent us from detecting exactly these kind of bugs in our tests... - */ -public class MockDispatcher extends Dispatcher { - - private final ConfigurationManager copyConfigurationManager; - private boolean injectedOnce = false; - - public MockDispatcher(ServletContext servletContext, Map context, ConfigurationManager configurationManager) { - super(servletContext, context); - this.copyConfigurationManager = configurationManager; - } - - @Override - public void init() { - super.init(); - ContainerHolder.clear(); - this.configurationManager = copyConfigurationManager; - } - - @Override - public Container getContainer() { - if (!injectedOnce) { - injectedOnce = true; - return super.getContainer(); - } - if (ContainerHolder.get() == null) { - ContainerHolder.store(getConfigurationManager().getConfiguration().getContainer()); - } - return ContainerHolder.get(); - } -} 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 42e107f40f..0f9a1bdf90 100644 --- a/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java +++ b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java @@ -28,19 +28,17 @@ import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.HashMap; import java.util.Map; +import static java.util.Collections.emptyMap; + /** - * Generic test setup methods to be used with any unit testing framework. + * Generic test setup methods to be used with any unit testing framework. */ public class StrutsTestCaseHelper { - - public static Dispatcher initDispatcher(ServletContext ctx, Map params) { - if (params == null) { - params = new HashMap<>(); - } - Dispatcher du = new DispatcherWrapper(ctx, params); + + public static Dispatcher initDispatcher(ServletContext ctx, Map params) { + Dispatcher du = new DispatcherWrapper(ctx, params != null ? params : emptyMap()); du.init(); Dispatcher.setInstance(du); @@ -52,7 +50,15 @@ public static Dispatcher initDispatcher(ServletContext ctx, Map p return du; } - public static void tearDown() throws Exception { + public static void tearDown(Dispatcher dispatcher) { + if (dispatcher != null && dispatcher.getConfigurationManager() != null) { + dispatcher.cleanup(); + } + tearDown(); + } + + public static void tearDown() { + (new Dispatcher(null, null)).cleanUpAfterInit(); // Clear ContainerHolder Dispatcher.setInstance(null); ActionContext.clear(); } diff --git a/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java b/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java index 5aad829694..ade928efb0 100644 --- a/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java +++ b/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java @@ -75,12 +75,7 @@ protected Dispatcher initDispatcherWithConfigs(String configs) { @Override protected void tearDown() throws Exception { - // maybe someone else already destroyed Dispatcher - if (dispatcher != null && dispatcher.getConfigurationManager() != null) { - dispatcher.cleanup(); - dispatcher = null; - } - StrutsTestCaseHelper.tearDown(); + StrutsTestCaseHelper.tearDown(dispatcher); } /** diff --git a/core/src/test/java/org/apache/struts2/StrutsJUnit4InternalTestCase.java b/core/src/test/java/org/apache/struts2/StrutsJUnit4InternalTestCase.java new file mode 100644 index 0000000000..8d72753927 --- /dev/null +++ b/core/src/test/java/org/apache/struts2/StrutsJUnit4InternalTestCase.java @@ -0,0 +1,61 @@ +/* + * 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; + +import com.opensymphony.xwork2.ActionProxyFactory; +import com.opensymphony.xwork2.XWorkJUnit4TestCase; +import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.util.StrutsTestCaseHelper; +import org.apache.struts2.views.jsp.StrutsMockServletContext; +import org.junit.After; +import org.junit.Before; + +import java.util.Map; + +public class StrutsJUnit4InternalTestCase extends XWorkJUnit4TestCase { + + protected StrutsMockServletContext servletContext; + protected Dispatcher dispatcher; + + @Override + @Before + public void setUp() throws Exception { + initDispatcher(); + } + + @Override + @After + public void tearDown() throws Exception { + StrutsTestCaseHelper.tearDown(dispatcher); + } + + protected void initDispatcher() { + initDispatcher(null); + } + + protected void initDispatcher(Map params) { + StrutsTestCaseHelper.tearDown(); + servletContext = new StrutsMockServletContext(); + dispatcher = StrutsTestCaseHelper.initDispatcher(servletContext, params); + configurationManager = dispatcher.getConfigurationManager(); + configuration = configurationManager.getConfiguration(); + container = configuration.getContainer(); + actionProxyFactory = container.getInstance(ActionProxyFactory.class); + } +} diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java index 73a526535d..267fb4e227 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java @@ -18,27 +18,29 @@ */ package org.apache.struts2.dispatcher; -import com.mockobjects.dynamic.C; -import com.mockobjects.dynamic.Mock; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.LocalizedTextProvider; import com.opensymphony.xwork2.ObjectFactory; import com.opensymphony.xwork2.StubValueStack; -import com.opensymphony.xwork2.config.Configuration; +import com.opensymphony.xwork2.config.ConfigurationException; import com.opensymphony.xwork2.config.ConfigurationManager; import com.opensymphony.xwork2.config.entities.InterceptorMapping; import com.opensymphony.xwork2.config.entities.InterceptorStackConfig; import com.opensymphony.xwork2.config.entities.PackageConfig; import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.inject.ContainerBuilder; import com.opensymphony.xwork2.interceptor.Interceptor; import com.opensymphony.xwork2.mock.MockActionInvocation; import com.opensymphony.xwork2.mock.MockActionProxy; +import com.opensymphony.xwork2.test.StubConfigurationProvider; +import com.opensymphony.xwork2.util.location.LocatableProperties; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsConstants; -import org.apache.struts2.StrutsInternalTestCase; +import org.apache.struts2.StrutsJUnit4InternalTestCase; import org.apache.struts2.dispatcher.mapper.ActionMapping; import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper; import org.apache.struts2.util.ObjectFactoryDestroyable; +import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpSession; @@ -46,18 +48,35 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.Collections; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.HashSet; import java.util.Locale; import java.util.Map; import java.util.Set; +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + /** * Test case for Dispatcher. */ -public class DispatcherTest extends StrutsInternalTestCase { +public class DispatcherTest extends StrutsJUnit4InternalTestCase { + @Test public void testDefaultResourceBundlePropertyLoaded() { LocalizedTextProvider localizedTextProvider = container.getInstance(LocalizedTextProvider.class); @@ -71,115 +90,107 @@ public void testDefaultResourceBundlePropertyLoaded() { "Error uploading: some error messages"); } + @Test public void testPrepareSetEncodingProperly() { HttpServletRequest req = new MockHttpServletRequest(); HttpServletResponse res = new MockHttpServletResponse(); - Dispatcher du = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); - }}); - du.prepare(req, res); + initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name())); + dispatcher.prepare(req, res); - assertEquals(req.getCharacterEncoding(), "utf-8"); - assertEquals(res.getCharacterEncoding(), "utf-8"); + assertEquals(req.getCharacterEncoding(), UTF_8.name()); + assertEquals(res.getCharacterEncoding(), UTF_8.name()); } + @Test public void testEncodingForXMLHttpRequest() { // given MockHttpServletRequest req = new MockHttpServletRequest(); req.addHeader("X-Requested-With", "XMLHttpRequest"); - req.setCharacterEncoding("UTF-8"); + req.setCharacterEncoding(UTF_8.name()); HttpServletResponse res = new MockHttpServletResponse(); - Dispatcher du = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "latin-2"); - }}); + initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, StandardCharsets.ISO_8859_1.name())); // when - du.prepare(req, res); + dispatcher.prepare(req, res); // then - assertEquals(req.getCharacterEncoding(), "UTF-8"); - assertEquals(res.getCharacterEncoding(), "UTF-8"); + assertEquals(req.getCharacterEncoding(), UTF_8.name()); + assertEquals(res.getCharacterEncoding(), UTF_8.name()); } + @Test public void testSetEncodingIfDiffer() { // given - Mock mock = new Mock(HttpServletRequest.class); - mock.expectAndReturn("getCharacterEncoding", "utf-8"); - mock.expectAndReturn("getHeader", "X-Requested-With", ""); - mock.expectAndReturn("getCharacterEncoding", "utf-8"); - HttpServletRequest req = (HttpServletRequest) mock.proxy(); + HttpServletRequest req = mock(HttpServletRequest.class); + when(req.getCharacterEncoding()).thenReturn(UTF_8.name()); + when(req.getHeader("X-Requested-With")).thenReturn(""); HttpServletResponse res = new MockHttpServletResponse(); - Dispatcher du = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); - }}); - + initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name())); // when - du.prepare(req, res); + dispatcher.prepare(req, res); // then - - assertEquals(req.getCharacterEncoding(), "utf-8"); - assertEquals(res.getCharacterEncoding(), "utf-8"); - mock.verify(); + assertEquals(UTF_8.name(), req.getCharacterEncoding()); + assertEquals(UTF_8.name(), res.getCharacterEncoding()); } + @Test public void testPrepareSetEncodingPropertyWithMultipartRequest() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.setContentType("multipart/form-data"); - Dispatcher du = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); - }}); - du.prepare(req, res); + initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name())); + dispatcher.prepare(req, res); - assertEquals("utf-8", req.getCharacterEncoding()); - assertEquals("utf-8", res.getCharacterEncoding()); + assertEquals(UTF_8.name(), req.getCharacterEncoding()); + assertEquals(UTF_8.name(), res.getCharacterEncoding()); } + @Test public void testPrepareMultipartRequest() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.setMethod("post"); req.setContentType("multipart/form-data; boundary=asdcvb345asd"); - Dispatcher du = initDispatcher(Collections.emptyMap()); - du.prepare(req, res); - HttpServletRequest wrapped = du.wrapRequest(req); - assertTrue(wrapped instanceof MultiPartRequestWrapper); + dispatcher.prepare(req, res); + + assertTrue(dispatcher.wrapRequest(req) instanceof MultiPartRequestWrapper); } + @Test public void testPrepareMultipartRequestAllAllowedCharacters() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.setMethod("post"); req.setContentType("multipart/form-data; boundary=01=23a.bC:D((e)d'z?p+o_r,e-"); - Dispatcher du = initDispatcher(Collections.emptyMap()); - du.prepare(req, res); - HttpServletRequest wrapped = du.wrapRequest(req); - assertTrue(wrapped instanceof MultiPartRequestWrapper); + dispatcher.prepare(req, res); + + assertTrue(dispatcher.wrapRequest(req) instanceof MultiPartRequestWrapper); } + @Test public void testPrepareMultipartRequestIllegalCharacter() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.setMethod("post"); req.setContentType("multipart/form-data; boundary=01=2;3a.bC:D((e)d'z?p+o_r,e-"); - Dispatcher du = initDispatcher(Collections.emptyMap()); - du.prepare(req, res); - HttpServletRequest wrapped = du.wrapRequest(req); - assertFalse(wrapped instanceof MultiPartRequestWrapper); + dispatcher.prepare(req, res); + + assertFalse(dispatcher.wrapRequest(req) instanceof MultiPartRequestWrapper); } + @Test public void testDispatcherListener() { final DispatcherListenerState state = new DispatcherListenerState(); @@ -198,39 +209,32 @@ public void dispatcherInitialized(Dispatcher du) { assertFalse(state.isDestroyed); assertFalse(state.isInitialized); - Dispatcher du = initDispatcher(new HashMap<>()); + dispatcher.init(); assertTrue(state.isInitialized); - du.cleanup(); + dispatcher.cleanup(); assertTrue(state.isDestroyed); } + @Test public void testConfigurationManager() { - Dispatcher du; - final InternalConfigurationManager configurationManager = new InternalConfigurationManager(Container.DEFAULT_NAME); - try { - du = new MockDispatcher(new MockServletContext(), new HashMap<>(), configurationManager); - du.init(); - Dispatcher.setInstance(du); + configurationManager = spy(new ConfigurationManager(Container.DEFAULT_NAME)); + dispatcher = spyDispatcherWithConfigurationManager(new Dispatcher(new MockServletContext(), emptyMap()), configurationManager); - assertFalse(configurationManager.destroyConfiguration); + dispatcher.init(); - du.cleanup(); + verify(configurationManager, never()).destroyConfiguration(); - assertTrue(configurationManager.destroyConfiguration); + dispatcher.cleanup(); - } finally { - Dispatcher.setInstance(null); - } + verify(configurationManager).destroyConfiguration(); } + @Test public void testInitLoadsDefaultConfig() { - Dispatcher du = new Dispatcher(new MockServletContext(), new HashMap<>()); - du.init(); - Configuration config = du.getConfigurationManager().getConfiguration(); - assertNotNull(config); + assertNotNull(configuration); Set expected = new HashSet<>(); expected.add("struts-default.xml"); expected.add("struts-beans.xml"); @@ -238,135 +242,113 @@ public void testInitLoadsDefaultConfig() { expected.add("struts-plugin.xml"); expected.add("struts.xml"); expected.add("struts-deferred.xml"); - assertEquals(expected, config.getLoadedFileNames()); - assertTrue(config.getPackageConfigs().size() > 0); - PackageConfig packageConfig = config.getPackageConfig("struts-default"); - assertTrue(packageConfig.getInterceptorConfigs().size() > 0); - assertTrue(packageConfig.getResultTypeConfigs().size() > 0); + assertEquals(expected, configuration.getLoadedFileNames()); + assertFalse(configuration.getPackageConfigs().isEmpty()); + PackageConfig packageConfig = configuration.getPackageConfig("struts-default"); + assertFalse(packageConfig.getInterceptorConfigs().isEmpty()); + assertFalse(packageConfig.getResultTypeConfigs().isEmpty()); } + @Test public void testObjectFactoryDestroy() { + dispatcher = spy(dispatcher); + Container spiedContainer = spy(container); + doReturn(spiedContainer).when(dispatcher).getContainer(); - ConfigurationManager cm = new ConfigurationManager(Container.DEFAULT_NAME); - Dispatcher du = new MockDispatcher(new MockServletContext(), new HashMap<>(), cm); - Mock mockConfiguration = new Mock(Configuration.class); - cm.setConfiguration((Configuration) mockConfiguration.proxy()); - - Mock mockContainer = new Mock(Container.class); - final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory(); - destroyedObjectFactory.setContainer((Container) mockContainer.proxy()); - mockContainer.expectAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), destroyedObjectFactory); + InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory(); + doReturn(destroyedObjectFactory).when(spiedContainer).getInstance(ObjectFactory.class); - mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy()); - mockConfiguration.expect("destroy"); - mockConfiguration.matchAndReturn("getPackageConfigs", new HashMap()); - - du.init(); assertFalse(destroyedObjectFactory.destroyed); - du.cleanup(); + dispatcher.cleanup(); assertTrue(destroyedObjectFactory.destroyed); - mockConfiguration.verify(); - mockContainer.verify(); } + @Test public void testInterceptorDestroy() { - Mock mockInterceptor = new Mock(Interceptor.class); - mockInterceptor.matchAndReturn("hashCode", 0); - mockInterceptor.expect("destroy"); - - InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy()); - + Interceptor mockedInterceptor = mock(Interceptor.class); + InterceptorMapping interceptorMapping = new InterceptorMapping("test", mockedInterceptor); InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build(); - PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build(); - Map packageConfigs = new HashMap<>(); - packageConfigs.put("test", packageConfig); + configurationManager = spy(new ConfigurationManager(Container.DEFAULT_NAME)); + dispatcher = spyDispatcherWithConfigurationManager(new Dispatcher(new MockServletContext(), emptyMap()), configurationManager); - Mock mockContainer = new Mock(Container.class); - mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory()); - - Mock mockConfiguration = new Mock(Configuration.class); - mockConfiguration.matchAndReturn("getPackageConfigs", packageConfigs); - mockConfiguration.matchAndReturn("getContainer", mockContainer.proxy()); - mockConfiguration.expect("destroy"); + dispatcher.init(); - ConfigurationManager configurationManager = new ConfigurationManager(Container.DEFAULT_NAME); - configurationManager.setConfiguration((Configuration) mockConfiguration.proxy()); + configuration = spy(configurationManager.getConfiguration()); + configurationManager.setConfiguration(configuration); + when(configuration.getPackageConfigs()).thenReturn(singletonMap("test", packageConfig)); - Dispatcher dispatcher = new MockDispatcher(new MockServletContext(), new HashMap<>(), configurationManager); - dispatcher.init(); dispatcher.cleanup(); - mockInterceptor.verify(); - mockContainer.verify(); - mockConfiguration.verify(); + verify(mockedInterceptor).destroy(); + verify(configuration).destroy(); } + @Test public void testMultipartSupportEnabledByDefault() { HttpServletRequest req = new MockHttpServletRequest(); HttpServletResponse res = new MockHttpServletResponse(); - Dispatcher du = initDispatcher(Collections.emptyMap()); - du.prepare(req, res); + dispatcher.prepare(req, res); - assertTrue(du.isMultipartSupportEnabled(req)); + assertTrue(dispatcher.isMultipartSupportEnabled(req)); } + @Test public void testIsMultipartRequest() { MockHttpServletRequest req = new MockHttpServletRequest(); HttpServletResponse res = new MockHttpServletResponse(); req.setMethod("POST"); - Dispatcher du = initDispatcher(Collections.emptyMap()); - du.prepare(req, res); + + dispatcher.prepare(req, res); req.setContentType("multipart/form-data"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data; boundary=---------------------------207103069210263"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=UTF-8"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=ISO-8859-1"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=Windows-1250"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=US-ASCII"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=UTF-16LE"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data;boundary=---------------------------207103069210263;charset=UTF-16LE"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data;boundary=---------------------------207103069210263; charset=UTF-16LE"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data;boundary=---------------------------207103069210263 ;charset=UTF-16LE"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data;boundary=---------------------------207103069210263 ; charset=UTF-16LE"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data ;boundary=---------------------------207103069210263;charset=UTF-16LE"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("multipart/form-data ; boundary=---------------------------207103069210263;charset=UTF-16LE"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); req.setContentType("Multipart/Form-Data ; boundary=---------------------------207103069210263;charset=UTF-16LE"); - assertTrue(du.isMultipartRequest(req)); + assertTrue(dispatcher.isMultipartRequest(req)); } + @Test public void testServiceActionResumePreviousProxy() throws Exception { - Dispatcher du = initDispatcher(Collections.emptyMap()); - MockActionInvocation mai = new MockActionInvocation(); ActionContext.getContext().withActionInvocation(mai); @@ -381,17 +363,15 @@ public void testServiceActionResumePreviousProxy() throws Exception { assertFalse(actionProxy.isExecutedCalled()); - du.setDevMode("false"); - du.setHandleException("false"); - du.serviceAction(req, null, new ActionMapping()); + dispatcher.setDevMode("false"); + dispatcher.setHandleException("false"); + dispatcher.serviceAction(req, null, new ActionMapping()); assertTrue("should execute previous proxy", actionProxy.isExecutedCalled()); } + @Test public void testServiceActionCreatesNewProxyIfDifferentMapping() throws Exception { - Dispatcher du = initDispatcher(Collections.emptyMap()); - container.inject(du); - MockActionInvocation mai = new MockActionInvocation(); ActionContext.getContext().withActionInvocation(mai); @@ -412,7 +392,7 @@ public void testServiceActionCreatesNewProxyIfDifferentMapping() throws Exceptio ActionMapping newActionMapping = new ActionMapping(); newActionMapping.setName("hello"); - du.serviceAction(request, response, newActionMapping); + dispatcher.serviceAction(request, response, newActionMapping); assertFalse(previousActionProxy.isExecutedCalled()); } @@ -421,196 +401,178 @@ public void testServiceActionCreatesNewProxyIfDifferentMapping() throws Exceptio * Verify proper default (true) handleExceptionState for Dispatcher and that * it properly reflects a manually configured change to false. */ + @Test public void testHandleException() { - Dispatcher du = initDispatcher(new HashMap<>()); - assertTrue("Default Dispatcher handleException state not true ?", du.isHandleException()); + assertTrue("Default Dispatcher handleException state not true ?", dispatcher.isHandleException()); - Dispatcher du2 = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_HANDLE_EXCEPTION, "false"); - }}); - assertFalse("Modified Dispatcher handleException state not false ?", du2.isHandleException()); + initDispatcher(singletonMap(StrutsConstants.STRUTS_HANDLE_EXCEPTION, "false")); + assertFalse("Modified Dispatcher handleException state not false ?", dispatcher.isHandleException()); } /** * Verify proper default (false) devMode for Dispatcher and that * it properly reflects a manually configured change to true. */ + @Test public void testDevMode() { - Dispatcher du = initDispatcher(new HashMap<>()); - assertFalse("Default Dispatcher devMode state not false ?", du.isDevMode()); + assertFalse("Default Dispatcher devMode state not false ?", dispatcher.isDevMode()); - Dispatcher du2 = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_DEVMODE, "true"); - }}); - assertTrue("Modified Dispatcher devMode state not true ?", du2.isDevMode()); + initDispatcher(singletonMap(StrutsConstants.STRUTS_DEVMODE, "true")); + assertTrue("Modified Dispatcher devMode state not true ?", dispatcher.isDevMode()); } + @Test public void testGetLocale_With_DefaultLocale_FromConfiguration() { // Given - Mock mock = new Mock(HttpServletRequest.class); + HttpServletRequest request = mock(HttpServletRequest.class); MockHttpSession mockHttpSession = new MockHttpSession(); - mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare(). - mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare(). - mock.expectAndReturn("getParameterMap", new HashMap()); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap(). - HttpServletRequest request = (HttpServletRequest) mock.proxy(); + when(request.getCharacterEncoding()).thenReturn(UTF_8.name()); + when(request.getHeader("X-Requested-With")).thenReturn(""); + when(request.getParameterMap()).thenReturn(emptyMap()); + when(request.getSession(anyBoolean())).thenReturn(mockHttpSession); HttpServletResponse response = new MockHttpServletResponse(); - Dispatcher testDispatcher = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); - // Not setting a Struts Locale here, so we should receive the default "de_DE" from the test configuration. - }}); + // Not setting a Struts Locale here, so we should receive the default "de_DE" from the test configuration. + initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name())); // When - testDispatcher.prepare(request, response); - ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response)); + dispatcher.prepare(request, response); + ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response)); // Then assertEquals(Locale.GERMANY, context.getLocale()); // Expect the Dispatcher defaultLocale value "de_DE" from the test configuration. - mock.verify(); } + @Test public void testGetLocale_With_DefaultLocale_fr_CA() { // Given - Mock mock = new Mock(HttpServletRequest.class); + HttpServletRequest request = mock(HttpServletRequest.class); MockHttpSession mockHttpSession = new MockHttpSession(); - mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare(). - mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare(). - mock.expectAndReturn("getParameterMap", new HashMap()); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap(). - HttpServletRequest request = (HttpServletRequest) mock.proxy(); + when(request.getCharacterEncoding()).thenReturn(UTF_8.name()); + when(request.getHeader("X-Requested-With")).thenReturn(""); + when(request.getParameterMap()).thenReturn(emptyMap()); + when(request.getSession(anyBoolean())).thenReturn(mockHttpSession); HttpServletResponse response = new MockHttpServletResponse(); - Dispatcher testDispatcher = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); + initDispatcher(new HashMap() {{ + put(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()); put(StrutsConstants.STRUTS_LOCALE, Locale.CANADA_FRENCH.toString()); // Set the Dispatcher defaultLocale to fr_CA. }}); // When - testDispatcher.prepare(request, response); - ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response)); + dispatcher.prepare(request, response); + ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response)); // Then assertEquals(Locale.CANADA_FRENCH, context.getLocale()); // Expect the Dispatcher defaultLocale value. - mock.verify(); } + @Test public void testGetLocale_With_BadDefaultLocale_RequestLocale_en_UK() { // Given - Mock mock = new Mock(HttpServletRequest.class); + HttpServletRequest request = mock(HttpServletRequest.class); MockHttpSession mockHttpSession = new MockHttpSession(); - mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare(). - mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare(). - mock.expectAndReturn("getLocale", Locale.UK); // From Dispatcher prepare(). - mock.expectAndReturn("getParameterMap", new HashMap()); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap(). - mock.expectAndReturn("getLocale", Locale.UK); // From createTestContextMap(). - HttpServletRequest request = (HttpServletRequest) mock.proxy(); + when(request.getCharacterEncoding()).thenReturn(UTF_8.name()); + when(request.getHeader("X-Requested-With")).thenReturn(""); + when(request.getParameterMap()).thenReturn(emptyMap()); + when(request.getSession(anyBoolean())).thenReturn(mockHttpSession); + when(request.getLocale()).thenReturn(Locale.UK); HttpServletResponse response = new MockHttpServletResponse(); - Dispatcher testDispatcher = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); + initDispatcher(new HashMap() {{ + put(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()); put(StrutsConstants.STRUTS_LOCALE, "This_is_not_a_valid_Locale_string"); // Set Dispatcher defaultLocale to an invalid value. }}); // When - testDispatcher.prepare(request, response); - ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response)); + dispatcher.prepare(request, response); + ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response)); // Then assertEquals(Locale.UK, context.getLocale()); // Expect the request set value from Mock. - mock.verify(); } + @Test public void testGetLocale_With_BadDefaultLocale_And_RuntimeException() { // Given - Mock mock = new Mock(HttpServletRequest.class); + HttpServletRequest request = mock(HttpServletRequest.class); MockHttpSession mockHttpSession = new MockHttpSession(); - mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare(). - mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare(). - mock.expectAndReturn("getLocale", Locale.UK); // From Dispatcher prepare(). - mock.expectAndReturn("getParameterMap", new HashMap()); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap(). - mock.expectAndThrow("getLocale", new IllegalStateException("Test theoretical state preventing HTTP Request Locale access")); // From createTestContextMap(). - HttpServletRequest request = (HttpServletRequest) mock.proxy(); + when(request.getCharacterEncoding()).thenReturn(UTF_8.name()); + when(request.getHeader("X-Requested-With")).thenReturn(""); + when(request.getParameterMap()).thenReturn(emptyMap()); + when(request.getSession(anyBoolean())).thenReturn(mockHttpSession); + when(request.getLocale()).thenReturn(Locale.UK); HttpServletResponse response = new MockHttpServletResponse(); - Dispatcher testDispatcher = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); + initDispatcher(new HashMap() {{ + put(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()); put(StrutsConstants.STRUTS_LOCALE, "This_is_not_a_valid_Locale_string"); // Set the Dispatcher defaultLocale to an invalid value. }}); // When - testDispatcher.prepare(request, response); - ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response)); + dispatcher.prepare(request, response); + when(request.getLocale()).thenThrow(new IllegalStateException("Test theoretical state preventing HTTP Request Locale access")); + ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response)); // Then assertEquals(Locale.getDefault(), context.getLocale()); // Expect the system default value, when BOTH Dispatcher default Locale AND request access fail. - mock.verify(); } + @Test public void testGetLocale_With_NullDefaultLocale() { // Given - Mock mock = new Mock(HttpServletRequest.class); + HttpServletRequest request = mock(HttpServletRequest.class); MockHttpSession mockHttpSession = new MockHttpSession(); - mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare(). - mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare(). - mock.expectAndReturn("getLocale", Locale.CANADA_FRENCH); // From Dispatcher prepare(). - mock.expectAndReturn("getParameterMap", new HashMap()); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap(). - mock.expectAndReturn("getLocale", Locale.CANADA_FRENCH); // From createTestContextMap(). - HttpServletRequest request = (HttpServletRequest) mock.proxy(); + when(request.getCharacterEncoding()).thenReturn(UTF_8.name()); + when(request.getHeader("X-Requested-With")).thenReturn(""); + when(request.getParameterMap()).thenReturn(emptyMap()); + when(request.getSession(anyBoolean())).thenReturn(mockHttpSession); + when(request.getLocale()).thenReturn(Locale.CANADA_FRENCH); HttpServletResponse response = new MockHttpServletResponse(); - Dispatcher testDispatcher = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); - // Attempting to set StrutsConstants.STRUTS_LOCALE to null here via parameters causes an NPE. - }}); + // Attempting to set StrutsConstants.STRUTS_LOCALE to null here via parameters causes an NPE. + initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name())); - testDispatcher.setDefaultLocale(null); // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration. + dispatcher.setDefaultLocale(null); // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration. // When - testDispatcher.prepare(request, response); - ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response)); + dispatcher.prepare(request, response); + ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response)); // Then assertEquals(Locale.CANADA_FRENCH, context.getLocale()); // Expect the request set value from Mock. - mock.verify(); } + @Test public void testGetLocale_With_NullDefaultLocale_And_RuntimeException() { // Given - Mock mock = new Mock(HttpServletRequest.class); + HttpServletRequest request = mock(HttpServletRequest.class); MockHttpSession mockHttpSession = new MockHttpSession(); - mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare(). - mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare(). - mock.expectAndReturn("getLocale", Locale.CANADA_FRENCH); // From Dispatcher prepare(). - mock.expectAndReturn("getParameterMap", new HashMap()); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare(). - mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap(). - mock.expectAndThrow("getLocale", new IllegalStateException("Test some theoretical state preventing HTTP Request Locale access")); // From createTestContextMap(). - HttpServletRequest request = (HttpServletRequest) mock.proxy(); + when(request.getCharacterEncoding()).thenReturn(UTF_8.name()); + when(request.getHeader("X-Requested-With")).thenReturn(""); + when(request.getParameterMap()).thenReturn(emptyMap()); + when(request.getSession(anyBoolean())).thenReturn(mockHttpSession); + when(request.getLocale()).thenReturn(Locale.CANADA_FRENCH); HttpServletResponse response = new MockHttpServletResponse(); - Dispatcher testDispatcher = initDispatcher(new HashMap() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); - // Attempting to set StrutsConstants.STRUTS_LOCALE to null via parameters causes an NPE. - }}); + // Attempting to set StrutsConstants.STRUTS_LOCALE to null via parameters causes an NPE. + initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name())); - testDispatcher.setDefaultLocale(null); // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration. + dispatcher.setDefaultLocale(null); // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration. // When - testDispatcher.prepare(request, response); - ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response)); + dispatcher.prepare(request, response); + when(request.getLocale()).thenThrow(new IllegalStateException("Test theoretical state preventing HTTP Request Locale access")); + ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response)); // Then assertEquals(Locale.getDefault(), context.getLocale()); // Expect the system default value when Mock request access fails. - mock.verify(); + } + + public static Dispatcher spyDispatcherWithConfigurationManager(Dispatcher dispatcher, ConfigurationManager configurationManager) { + Dispatcher spiedDispatcher = spy(dispatcher); + doReturn(configurationManager).when(spiedDispatcher).createConfigurationManager(any()); + return spiedDispatcher; } /** @@ -641,21 +603,6 @@ protected static Map createTestContextMap(Dispatcher dispatcher, response); } - static class InternalConfigurationManager extends ConfigurationManager { - public boolean destroyConfiguration = false; - - public InternalConfigurationManager(String name) { - super(name); - } - - @Override - public synchronized void destroyConfiguration() { - super.destroyConfiguration(); - destroyConfiguration = true; - } - } - - static class DispatcherListenerState { public boolean isInitialized = false; public boolean isDestroyed = false; diff --git a/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java index c1b8188fa9..59afa3a1ce 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java @@ -34,7 +34,6 @@ import org.apache.struts2.dispatcher.ApplicationMap; import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.dispatcher.HttpParameters; -import org.apache.struts2.dispatcher.MockDispatcher; import org.apache.struts2.dispatcher.RequestMap; import org.apache.struts2.dispatcher.SessionMap; @@ -42,9 +41,10 @@ import javax.servlet.jsp.JspWriter; import java.io.File; import java.io.StringWriter; -import java.util.HashMap; import java.util.Map; +import static java.util.Collections.emptyMap; + /** * Base class to extend for unit testing UI Tags. */ @@ -111,7 +111,7 @@ protected void createMocks() { pageContext.setServletContext(servletContext); mockContainer = new Mock(Container.class); - MockDispatcher du = new MockDispatcher(pageContext.getServletContext(), new HashMap<>(), configurationManager); + Dispatcher du = new Dispatcher(pageContext.getServletContext(), emptyMap()); du.init(); Dispatcher.setInstance(du); session = new SessionMap(request); diff --git a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsJUnit4TestCase.java b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsJUnit4TestCase.java index 84b76a1b61..ee486d6cc9 100644 --- a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsJUnit4TestCase.java +++ b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsJUnit4TestCase.java @@ -203,9 +203,9 @@ public void finishExecution() { * Sets up the configuration settings, XWork configuration, and * message resources */ + @Override @Before public void setUp() throws Exception { - super.setUp(); initServletMockObjects(); setupBeforeInitDispatcher(); initDispatcherParams(); @@ -239,14 +239,10 @@ protected String getConfigPath() { return null; } + @Override @After public void tearDown() throws Exception { - super.tearDown(); - if (dispatcher != null && dispatcher.getConfigurationManager() != null) { - dispatcher.cleanup(); - dispatcher = null; - } - StrutsTestCaseHelper.tearDown(); + StrutsTestCaseHelper.tearDown(dispatcher); } } diff --git a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsTestCase.java b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsTestCase.java index f64a9966f1..6280de8a81 100644 --- a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsTestCase.java +++ b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsTestCase.java @@ -168,8 +168,8 @@ protected void injectStrutsDependencies(Object object) { * Sets up the configuration settings, XWork configuration, and * message resources */ + @Override protected void setUp() throws Exception { - super.setUp(); initServletMockObjects(); setupBeforeInitDispatcher(); dispatcher = initDispatcher(dispatcherInitParams); @@ -199,14 +199,9 @@ protected Dispatcher initDispatcher(Map params) { return du; } + @Override protected void tearDown() throws Exception { - super.tearDown(); - // maybe someone else already destroyed Dispatcher - if (dispatcher != null && dispatcher.getConfigurationManager() != null) { - dispatcher.cleanup(); - dispatcher = null; - } - StrutsTestCaseHelper.tearDown(); + StrutsTestCaseHelper.tearDown(dispatcher); } } diff --git a/plugins/portlet/src/test/java/org/apache/struts2/StrutsTestCasePortletTests.java b/plugins/portlet/src/test/java/org/apache/struts2/StrutsTestCasePortletTests.java index acea0bec3e..0da8d1043c 100644 --- a/plugins/portlet/src/test/java/org/apache/struts2/StrutsTestCasePortletTests.java +++ b/plugins/portlet/src/test/java/org/apache/struts2/StrutsTestCasePortletTests.java @@ -23,12 +23,6 @@ import com.opensymphony.xwork2.ActionProxyFactory; import com.opensymphony.xwork2.XWorkTestCase; import com.opensymphony.xwork2.config.Configuration; -import java.io.UnsupportedEncodingException; -import java.util.HashMap; -import java.util.Map; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.dispatcher.HttpParameters; import org.apache.struts2.dispatcher.mapper.ActionMapper; @@ -41,6 +35,13 @@ import org.springframework.mock.web.MockPageContext; import org.springframework.mock.web.MockServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Map; + /* * Changes: This is a copy of org.apache.struts2.StrutsTestCase from the Struts 2 junit-plugin, kept in * in the same package org.apache.struts2 and renamed. Removed some unused imports, made @@ -180,8 +181,8 @@ protected void injectStrutsDependencies(Object object) { * Sets up the configuration settings, XWork configuration, and * message resources */ + @Override protected void setUp() throws Exception { - super.setUp(); initServletMockObjects(); setupBeforeInitDispatcher(); dispatcher = initDispatcher(dispatcherInitParams); @@ -211,14 +212,9 @@ protected Dispatcher initDispatcher(Map params) { return du; } + @Override protected void tearDown() throws Exception { - super.tearDown(); - // maybe someone else already destroyed Dispatcher - if (dispatcher != null && dispatcher.getConfigurationManager() != null) { - dispatcher.cleanup(); - dispatcher = null; - } - StrutsTestCaseHelper.tearDown(); + StrutsTestCaseHelper.tearDown(dispatcher); } } diff --git a/plugins/testng/src/main/java/org/apache/struts2/testng/StrutsTestCase.java b/plugins/testng/src/main/java/org/apache/struts2/testng/StrutsTestCase.java index 630eb2df69..6df81ff667 100644 --- a/plugins/testng/src/main/java/org/apache/struts2/testng/StrutsTestCase.java +++ b/plugins/testng/src/main/java/org/apache/struts2/testng/StrutsTestCase.java @@ -18,13 +18,13 @@ */ package org.apache.struts2.testng; -import java.util.Map; - import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.util.StrutsTestCaseHelper; +import org.springframework.mock.web.MockServletContext; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; -import org.springframework.mock.web.MockServletContext; + +import java.util.Map; /** * Base test class for TestNG unit tests. Provides common Struts variables @@ -33,12 +33,12 @@ public class StrutsTestCase extends TestNGXWorkTestCase { @BeforeTest + @Override protected void setUp() throws Exception { - super.setUp(); initDispatcher(null); } - protected Dispatcher initDispatcher(Map params) { + protected Dispatcher initDispatcher(Map params) { Dispatcher du = StrutsTestCaseHelper.initDispatcher(new MockServletContext(), params); configurationManager = du.getConfigurationManager(); configuration = configurationManager.getConfiguration(); @@ -56,8 +56,8 @@ protected T createAction(Class clazz) { } @AfterTest + @Override protected void tearDown() throws Exception { - super.tearDown(); StrutsTestCaseHelper.tearDown(); } } From fa5b46c7844d114a52a91b606627fc6082e37c3f Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Tue, 2 Jan 2024 02:57:19 +1100 Subject: [PATCH 5/8] WW-5382 Add test for Dispatcher reinjection --- .../struts2/dispatcher/DispatcherTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java index 267fb4e227..f633b7c012 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java @@ -569,6 +569,29 @@ public void testGetLocale_With_NullDefaultLocale_And_RuntimeException() { assertEquals(Locale.getDefault(), context.getLocale()); // Expect the system default value when Mock request access fails. } + @Test + public void dispatcherReinjectedAfterReload() { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + + dispatcher.prepare(request, response); + + assertEquals(Locale.GERMANY, dispatcher.getLocale(request)); + + configurationManager.addContainerProvider(new StubConfigurationProvider() { + @Override + public void register(ContainerBuilder builder, + LocatableProperties props) throws ConfigurationException { + props.setProperty(StrutsConstants.STRUTS_LOCALE, "fr_CA"); + } + }); + configurationManager.reload(); + dispatcher.cleanUpRequest(request); + dispatcher.prepare(request, response); + + assertEquals(Locale.CANADA_FRENCH, dispatcher.getLocale(request)); + } + public static Dispatcher spyDispatcherWithConfigurationManager(Dispatcher dispatcher, ConfigurationManager configurationManager) { Dispatcher spiedDispatcher = spy(dispatcher); doReturn(configurationManager).when(spiedDispatcher).createConfigurationManager(any()); From ae71c464a815c56c8817b609615783e37949bffb Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Tue, 2 Jan 2024 02:59:40 +1100 Subject: [PATCH 6/8] WW-5382 Delete redundant code --- .../org/apache/struts2/dispatcher/Dispatcher.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index a53398b775..6fab13b729 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -983,18 +983,7 @@ protected boolean isMultipartRequest(HttpServletRequest request) { * @return a multi part request object */ protected MultiPartRequest getMultiPartRequest() { - MultiPartRequest mpr = null; - //check for alternate implementations of MultiPartRequest - Set multiNames = getContainer().getInstanceNames(MultiPartRequest.class); - for (String multiName : multiNames) { - if (multiName.equals(multipartHandlerName)) { - mpr = getContainer().getInstance(MultiPartRequest.class, multiName); - } - } - if (mpr == null) { - mpr = getContainer().getInstance(MultiPartRequest.class); - } - return mpr; + return getContainer().getInstance(MultiPartRequest.class); } /** From 946737c81184f4b8c398a58e852faae197d6f84e Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Tue, 2 Jan 2024 03:29:54 +1100 Subject: [PATCH 7/8] WW-5382 Rework Dispatcher injections --- .../apache/struts2/dispatcher/Dispatcher.java | 91 +- .../struts2/dispatcher/ExecuteOperations.java | 4 +- .../struts2/dispatcher/InitOperations.java | 2 +- .../struts2/dispatcher/PrepareOperations.java | 8 +- .../struts2/util/StrutsTestCaseHelper.java | 2 +- .../struts2/dispatcher/DispatcherTest.java | 6 +- .../struts2/validators/DWRValidator.java | 33 +- .../portlet/dispatcher/Jsr168Dispatcher.java | 1348 ++++++++--------- .../OldDecorator2NewStrutsDecorator.java | 403 ++--- 9 files changed, 969 insertions(+), 928 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index 6fab13b729..c534069c37 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -64,6 +64,7 @@ import org.apache.struts2.config.StrutsJavaConfiguration; import org.apache.struts2.config.StrutsJavaConfigurationProvider; import org.apache.struts2.config.StrutsXmlConfigurationProvider; +import org.apache.struts2.dispatcher.mapper.ActionMapper; import org.apache.struts2.dispatcher.mapper.ActionMapping; import org.apache.struts2.dispatcher.multipart.MultiPartRequest; import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper; @@ -119,6 +120,10 @@ public class Dispatcher { */ private static final List dispatcherListeners = new CopyOnWriteArrayList<>(); + /** + * This field exists so {@link #getContainer()} can determine whether to (re-)inject this instance in the case of + * a {@link ConfigurationManager} reload. + */ private Container injectedContainer; /** @@ -146,11 +151,6 @@ public class Dispatcher { */ private String multipartSaveDir; - /** - * Stores the value of {@link StrutsConstants#STRUTS_MULTIPART_PARSER} setting - */ - private String multipartHandlerName; - /** * Stores the value of {@link StrutsConstants#STRUTS_MULTIPART_ENABLED} */ @@ -194,6 +194,11 @@ public class Dispatcher { * Store ConfigurationManager instance, set on init. */ protected ConfigurationManager configurationManager; + private ObjectFactory objectFactory; + private ActionProxyFactory actionProxyFactory; + private LocaleProviderFactory localeProviderFactory; + private StaticContentLoader staticContentLoader; + private ActionMapper actionMapper; /** * Provide the dispatcher instance for the current thread. @@ -213,6 +218,13 @@ public static void setInstance(Dispatcher instance) { Dispatcher.instance.set(instance); } + /** + * Removes the dispatcher instance for this thread. + */ + public static void clearInstance() { + Dispatcher.instance.remove(); + } + /** * Add a dispatcher lifecycle listener. * @@ -308,9 +320,12 @@ public void setMultipartSaveDir(String val) { multipartSaveDir = val; } - @Inject(StrutsConstants.STRUTS_MULTIPART_PARSER) + /** + * @deprecated since 6.4.0, no replacement. + */ + @Deprecated public void setMultipartHandler(String val) { - multipartHandlerName = val; + // no-op } @Inject(value = StrutsConstants.STRUTS_MULTIPART_ENABLED, required = false) @@ -328,6 +343,10 @@ public void setValueStackFactory(ValueStackFactory valueStackFactory) { this.valueStackFactory = valueStackFactory; } + public ValueStackFactory getValueStackFactory() { + return valueStackFactory; + } + @Inject(StrutsConstants.STRUTS_HANDLE_EXCEPTION) public void setHandleException(String handleException) { this.handleException = Boolean.parseBoolean(handleException); @@ -348,12 +367,48 @@ public void setDispatcherErrorHandler(DispatcherErrorHandler errorHandler) { this.errorHandler = errorHandler; } + @Inject + public void setObjectFactory(ObjectFactory objectFactory) { + this.objectFactory = objectFactory; + } + + @Inject + public void setActionProxyFactory(ActionProxyFactory actionProxyFactory) { + this.actionProxyFactory = actionProxyFactory; + } + + public ActionProxyFactory getActionProxyFactory() { + return actionProxyFactory; + } + + @Inject + public void setLocaleProviderFactory(LocaleProviderFactory localeProviderFactory) { + this.localeProviderFactory = localeProviderFactory; + } + + @Inject + public void setStaticContentLoader(StaticContentLoader staticContentLoader) { + this.staticContentLoader = staticContentLoader; + } + + public StaticContentLoader getStaticContentLoader() { + return staticContentLoader; + } + + @Inject + public void setActionMapper(ActionMapper actionMapper) { + this.actionMapper = actionMapper; + } + + public ActionMapper getActionMapper() { + return actionMapper; + } + /** * Releases all instances bound to this dispatcher instance. */ public void cleanup() { // clean up ObjectFactory - ObjectFactory objectFactory = getContainer().getInstance(ObjectFactory.class); if (objectFactory == null) { LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed"); } @@ -540,10 +595,6 @@ private void init_DeferredXmlConfigurations() { loadConfigPaths("struts-deferred.xml"); } - private Container init_PreloadConfiguration() { - return getContainer(); - } - /** * Load configurations, including both XML and zero-configuration strategies, * and update optional settings, including whether to reload configurations and resource files. @@ -684,7 +735,6 @@ protected ActionProxy prepareActionProxy(Map extraContext, Strin } protected ActionProxy createActionProxy(String namespace, String name, String method, Map extraContext) { - ActionProxyFactory actionProxyFactory = getContainer().getInstance(ActionProxyFactory.class); return actionProxyFactory.createActionProxy(namespace, name, method, extraContext, true, false); } @@ -860,6 +910,7 @@ protected String getSaveDir() { * @param response The response */ public void prepare(HttpServletRequest request, HttpServletResponse response) { + getContainer(); // Init ContainerHolder and reinject this instance IF ConfigurationManager was reloaded String encoding = null; if (defaultEncoding != null) { encoding = defaultEncoding; @@ -932,15 +983,12 @@ public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOExcep } if (isMultipartSupportEnabled(request) && isMultipartRequest(request)) { - MultiPartRequest multiPartRequest = getMultiPartRequest(); - LocaleProviderFactory localeProviderFactory = getContainer().getInstance(LocaleProviderFactory.class); - request = new MultiPartRequestWrapper( - multiPartRequest, - request, - getSaveDir(), - localeProviderFactory.createLocaleProvider(), - disableRequestAttributeValueStackLookup + getMultiPartRequest(), + request, + getSaveDir(), + localeProviderFactory.createLocaleProvider(), + disableRequestAttributeValueStackLookup ); } else { request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup); @@ -1065,5 +1113,4 @@ public Container getContainer() { } return ContainerHolder.get(); } - } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/ExecuteOperations.java b/core/src/main/java/org/apache/struts2/dispatcher/ExecuteOperations.java index 8671688de2..769ca0a390 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/ExecuteOperations.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/ExecuteOperations.java @@ -18,8 +18,8 @@ */ package org.apache.struts2.dispatcher; -import org.apache.struts2.dispatcher.mapper.ActionMapping; import org.apache.struts2.RequestUtils; +import org.apache.struts2.dispatcher.mapper.ActionMapping; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -54,7 +54,7 @@ public boolean executeStaticResourceRequest(HttpServletRequest request, HttpServ resourcePath = request.getPathInfo(); } - StaticContentLoader staticResourceLoader = dispatcher.getContainer().getInstance(StaticContentLoader.class); + StaticContentLoader staticResourceLoader = dispatcher.getStaticContentLoader(); if (staticResourceLoader.canHandle(resourcePath)) { staticResourceLoader.findStaticResource(resourcePath, request, response); // The framework did its job here diff --git a/core/src/main/java/org/apache/struts2/dispatcher/InitOperations.java b/core/src/main/java/org/apache/struts2/dispatcher/InitOperations.java index 819e7cdb94..367aeba55c 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/InitOperations.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/InitOperations.java @@ -57,7 +57,7 @@ public Dispatcher initDispatcher(HostConfig filterConfig) { * @return the static content loader */ public StaticContentLoader initStaticContentLoader(HostConfig filterConfig, Dispatcher dispatcher) { - StaticContentLoader loader = dispatcher.getContainer().getInstance(StaticContentLoader.class); + StaticContentLoader loader = dispatcher.getStaticContentLoader(); loader.setHostConfig(filterConfig); return loader; } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/PrepareOperations.java b/core/src/main/java/org/apache/struts2/dispatcher/PrepareOperations.java index a643de3101..6888c5b7aa 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/PrepareOperations.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/PrepareOperations.java @@ -20,13 +20,11 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.util.ValueStack; -import com.opensymphony.xwork2.util.ValueStackFactory; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.struts2.RequestUtils; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsException; -import org.apache.struts2.dispatcher.mapper.ActionMapper; import org.apache.struts2.dispatcher.mapper.ActionMapping; import javax.servlet.ServletException; @@ -78,7 +76,7 @@ public void cleanupRequest(final HttpServletRequest request) { dispatcher.cleanUpRequest(request); } finally { ActionContext.clear(); - Dispatcher.setInstance(null); + Dispatcher.clearInstance(); devModeOverride.remove(); } }); @@ -101,7 +99,7 @@ public ActionContext createActionContext(HttpServletRequest request, HttpServlet } else { ctx = ServletActionContext.getActionContext(request); //checks if we are probably in an async if (ctx == null) { - ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack(); + ValueStack stack = dispatcher.getValueStackFactory().createValueStack(); stack.getContext().putAll(dispatcher.createContextMap(request, response, null)); ctx = ActionContext.of(stack.getContext()).bind(); } @@ -188,7 +186,7 @@ public ActionMapping findActionMapping(HttpServletRequest request, HttpServletRe Object mappingAttr = request.getAttribute(STRUTS_ACTION_MAPPING_KEY); if (mappingAttr == null || forceLookup) { try { - mapping = dispatcher.getContainer().getInstance(ActionMapper.class).getMapping(request, dispatcher.getConfigurationManager()); + mapping = dispatcher.getActionMapper().getMapping(request, dispatcher.getConfigurationManager()); if (mapping != null) { request.setAttribute(STRUTS_ACTION_MAPPING_KEY, mapping); } else { 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 0f9a1bdf90..418db330ce 100644 --- a/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java +++ b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java @@ -59,7 +59,7 @@ public static void tearDown(Dispatcher dispatcher) { public static void tearDown() { (new Dispatcher(null, null)).cleanUpAfterInit(); // Clear ContainerHolder - Dispatcher.setInstance(null); + Dispatcher.clearInstance(); ActionContext.clear(); } diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java index f633b7c012..8884667204 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java @@ -251,12 +251,8 @@ public void testInitLoadsDefaultConfig() { @Test public void testObjectFactoryDestroy() { - dispatcher = spy(dispatcher); - Container spiedContainer = spy(container); - doReturn(spiedContainer).when(dispatcher).getContainer(); - InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory(); - doReturn(destroyedObjectFactory).when(spiedContainer).getInstance(ObjectFactory.class); + dispatcher.setObjectFactory(destroyedObjectFactory); assertFalse(destroyedObjectFactory.destroyed); dispatcher.cleanup(); diff --git a/plugins/dwr/src/main/java/org/apache/struts2/validators/DWRValidator.java b/plugins/dwr/src/main/java/org/apache/struts2/validators/DWRValidator.java index 0809ea0e21..f189cd1820 100644 --- a/plugins/dwr/src/main/java/org/apache/struts2/validators/DWRValidator.java +++ b/plugins/dwr/src/main/java/org/apache/struts2/validators/DWRValidator.java @@ -18,31 +18,26 @@ */ package org.apache.struts2.validators; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.dispatcher.ApplicationMap; -import org.apache.struts2.dispatcher.Dispatcher; -import org.apache.struts2.dispatcher.HttpParameters; -import org.apache.struts2.dispatcher.RequestMap; -import org.apache.struts2.dispatcher.SessionMap; - -import org.directwebremoting.WebContextFactory; - import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.ActionProxyFactory; import com.opensymphony.xwork2.DefaultActionInvocation; -import com.opensymphony.xwork2.interceptor.ValidationAware; import com.opensymphony.xwork2.ValidationAwareSupport; import com.opensymphony.xwork2.config.entities.ActionConfig; -import org.apache.logging.log4j.Logger; +import com.opensymphony.xwork2.interceptor.ValidationAware; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.struts2.dispatcher.ApplicationMap; +import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.dispatcher.HttpParameters; +import org.apache.struts2.dispatcher.RequestMap; +import org.apache.struts2.dispatcher.SessionMap; +import org.directwebremoting.WebContextFactory; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Map; /** *

@@ -87,7 +82,7 @@ public ValidationAwareSupport doPost(String namespace, String actionName, Map pa res); try { - ActionProxyFactory actionProxyFactory = du.getContainer().getInstance(ActionProxyFactory.class); + ActionProxyFactory actionProxyFactory = du.getActionProxyFactory(); ActionProxy proxy = actionProxyFactory.createActionProxy(namespace, actionName, null, ctx, true, true); proxy.execute(); Object action = proxy.getAction(); diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java index 394ef60420..29b563dac2 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java @@ -1,674 +1,674 @@ -/* - * 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.portlet.dispatcher; - -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.ActionProxy; -import com.opensymphony.xwork2.ActionProxyFactory; -import com.opensymphony.xwork2.config.ConfigurationException; -import com.opensymphony.xwork2.inject.Container; -import org.apache.commons.lang3.LocaleUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.message.ParameterizedMessage; -import org.apache.struts2.StrutsConstants; -import org.apache.struts2.StrutsException; -import org.apache.struts2.StrutsStatics; -import org.apache.struts2.dispatcher.ApplicationMap; -import org.apache.struts2.dispatcher.Dispatcher; -import org.apache.struts2.dispatcher.DispatcherConstants; -import org.apache.struts2.dispatcher.HttpParameters; -import org.apache.struts2.dispatcher.RequestMap; -import org.apache.struts2.dispatcher.SessionMap; -import org.apache.struts2.dispatcher.mapper.ActionMapper; -import org.apache.struts2.dispatcher.mapper.ActionMapping; -import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper; -import org.apache.struts2.portlet.PortletApplicationMap; -import org.apache.struts2.portlet.PortletConstants; -import org.apache.struts2.portlet.PortletPhase; -import org.apache.struts2.portlet.PortletRequestMap; -import org.apache.struts2.portlet.PortletSessionMap; -import org.apache.struts2.portlet.context.PortletActionContext; -import org.apache.struts2.portlet.servlet.PortletServletContext; -import org.apache.struts2.portlet.servlet.PortletServletRequest; -import org.apache.struts2.portlet.servlet.PortletServletResponse; -import org.apache.struts2.dispatcher.AttributeMap; - -import javax.portlet.ActionRequest; -import javax.portlet.ActionResponse; -import javax.portlet.GenericPortlet; -import javax.portlet.PortletConfig; -import javax.portlet.PortletException; -import javax.portlet.PortletMode; -import javax.portlet.PortletRequest; -import javax.portlet.PortletResponse; -import javax.portlet.RenderRequest; -import javax.portlet.RenderResponse; -import javax.portlet.WindowState; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import static org.apache.struts2.portlet.PortletConstants.ACTION_PARAM; -import static org.apache.struts2.portlet.PortletConstants.ACTION_RESET; -import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_FOR_MODE; -import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_NAME; -import static org.apache.struts2.portlet.PortletConstants.MODE_NAMESPACE_MAP; -import static org.apache.struts2.portlet.PortletConstants.MODE_PARAM; -import static org.apache.struts2.portlet.PortletConstants.PORTLET_CONFIG; -import static org.apache.struts2.portlet.PortletConstants.PORTLET_NAMESPACE; -import static org.apache.struts2.portlet.PortletConstants.REQUEST; -import static org.apache.struts2.portlet.PortletConstants.RESPONSE; - -/** - * - *

- * Struts JSR-168 portlet dispatcher. Similar to the WW2 Servlet dispatcher, - * but adjusted to a portal environment. The portlet is configured through the portlet.xml - * descriptor. Examples and descriptions follow below: - *

- * - * - * @author Nils-Helge Garli - * @author Rainer Hermanns - * - *

Init parameters

- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
NameDescriptionDefault value
portletNamespaceThe namespace for the portlet in the xwork configuration. This - * namespace is prepended to all action lookups, and makes it possible to host multiple - * portlets in the same portlet application. If this parameter is set, the complete namespace - * will be /portletNamespace/modeNamespace/actionNameThe default namespace
viewNamespaceBase namespace in the xwork configuration for the view portlet - * modeThe default namespace
editNamespaceBase namespace in the xwork configuration for the edit portlet - * modeThe default namespace
helpNamespaceBase namespace in the xwork configuration for the help portlet - * modeThe default namespace
defaultViewActionDefault action to invoke in the view portlet mode if no action is - * specifieddefault
defaultEditActionDefault action to invoke in the edit portlet mode if no action is - * specifieddefault
defaultHelpActionDefault action to invoke in the help portlet mode if no action is - * specifieddefault
- * - *

Example:

- *
- * 
- *
- * <init-param>
- *     <!-- The view mode namespace. Maps to a namespace in the xwork config file -->
- *     <name>viewNamespace</name>
- *     <value>/view</value>
- * </init-param>
- * <init-param>
- *    <!-- The default action to invoke in view mode -->
- *    <name>defaultViewAction</name>
- *    <value>index</value>
- * </init-param>
- * <init-param>
- *     <!-- The view mode namespace. Maps to a namespace in the xwork config file -->
- *     <name>editNamespace</name>
- *     <value>/edit</value>
- * </init-param>
- * <init-param>
- *     <!-- The default action to invoke in view mode -->
- *     <name>defaultEditAction</name>
- *     <value>index</value>
- * </init-param>
- * <init-param>
- *     <!-- The view mode namespace. Maps to a namespace in the xwork config file -->
- *     <name>helpNamespace</name>
- *     <value>/help</value>
- * </init-param>
- * <init-param>
- *     <!-- The default action to invoke in view mode -->
- *     <name>defaultHelpAction</name>
- *     <value>index</value>
- * </init-param>
- *
- * 
- * 
- */ -public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics { - - private static final Logger LOG = LogManager.getLogger(Jsr168Dispatcher.class); - - protected String portletNamespace = null; - - private ActionProxyFactory factory = null; - private Map modeMap = new HashMap(3); - private Map actionMap = new HashMap(3); - private Dispatcher dispatcherUtils; - private ActionMapper actionMapper; - private Container container; - private ServletContext servletContext; - - /** - * Initialize the portlet with the init parameters from portlet.xml - * - * @param cfg portlet configuration - * @throws PortletException in case of errors - */ - public void init(PortletConfig cfg) throws PortletException { - super.init(cfg); - LOG.debug("Initializing portlet {}", getPortletName()); - - Map params = new HashMap(); - for (Enumeration e = cfg.getInitParameterNames(); e.hasMoreElements(); ) { - String name = (String) e.nextElement(); - String value = cfg.getInitParameter(name); - params.put(name, value); - } - - servletContext = new PortletServletContext(cfg.getPortletContext()); - dispatcherUtils = new Dispatcher(servletContext, params); - dispatcherUtils.init(); - - // For testability - if (factory == null) { - factory = dispatcherUtils.getContainer().getInstance(ActionProxyFactory.class); - } - portletNamespace = cfg.getInitParameter("portletNamespace"); - LOG.debug("PortletNamespace: {}", portletNamespace); - - parseModeConfig(actionMap, cfg, PortletMode.VIEW, "viewNamespace", - "defaultViewAction"); - parseModeConfig(actionMap, cfg, PortletMode.EDIT, "editNamespace", - "defaultEditAction"); - parseModeConfig(actionMap, cfg, PortletMode.HELP, "helpNamespace", - "defaultHelpAction"); - parseModeConfig(actionMap, cfg, new PortletMode("config"), "configNamespace", - "defaultConfigAction"); - parseModeConfig(actionMap, cfg, new PortletMode("about"), "aboutNamespace", - "defaultAboutAction"); - parseModeConfig(actionMap, cfg, new PortletMode("print"), "printNamespace", - "defaultPrintAction"); - parseModeConfig(actionMap, cfg, new PortletMode("preview"), "previewNamespace", - "defaultPreviewAction"); - parseModeConfig(actionMap, cfg, new PortletMode("edit_defaults"), - "editDefaultsNamespace", "defaultEditDefaultsAction"); - if (StringUtils.isEmpty(portletNamespace)) { - portletNamespace = ""; - } - - container = dispatcherUtils.getContainer(); - actionMapper = container.getInstance(ActionMapper.class); - } - - /** - * Parse the mode to namespace mappings configured in portlet.xml - * - * @param actionMap The map with mode <-> default action mapping. - * @param portletConfig The PortletConfig. - * @param portletMode The PortletMode. - * @param nameSpaceParam Name of the init parameter where the namespace for the mode - * is configured. - * @param defaultActionParam Name of the init parameter where the default action to - * execute for the mode is configured. - */ - void parseModeConfig(Map actionMap, PortletConfig portletConfig, - PortletMode portletMode, String nameSpaceParam, - String defaultActionParam) { - String namespace = portletConfig.getInitParameter(nameSpaceParam); - if (StringUtils.isEmpty(namespace)) { - namespace = ""; - } - modeMap.put(portletMode, namespace); - String defaultAction = portletConfig.getInitParameter(defaultActionParam); - String method = null; - if (StringUtils.isEmpty(defaultAction)) { - defaultAction = DEFAULT_ACTION_NAME; - } - if (defaultAction.indexOf('!') >= 0) { - method = defaultAction.substring(defaultAction.indexOf('!') + 1); - defaultAction = defaultAction.substring(0, defaultAction.indexOf('!')); - } - StringBuilder fullPath = new StringBuilder(); - if (StringUtils.isNotEmpty(portletNamespace)) { - fullPath.append(portletNamespace); - } - if (StringUtils.isNotEmpty(namespace)) { - fullPath.append(namespace).append("/"); - } else { - fullPath.append("/"); - } - fullPath.append(defaultAction); - ActionMapping mapping = new ActionMapping(); - mapping.setName(getActionName(fullPath.toString())); - mapping.setNamespace(getNamespace(fullPath.toString())); - if (method != null) { - mapping.setMethod(method); - } - actionMap.put(portletMode, mapping); - } - - /** - * Service an action from the event phase. - * - * @param request action request - * @param response action response - * @throws PortletException in case of errors - * @throws IOException in case of IO errors - * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, - * javax.portlet.ActionResponse) - */ - public void processAction(ActionRequest request, ActionResponse response) - throws PortletException, IOException { - if (LOG.isDebugEnabled()) { - LOG.debug("Entering processAction in mode ", request.getPortletMode().toString()); - } - resetActionContext(); - try { - serviceAction(request, response, getRequestMap(request), getParameterMap(request), - getSessionMap(request), getApplicationMap(), - portletNamespace, PortletPhase.ACTION_PHASE); - if (LOG.isDebugEnabled()) LOG.debug("Leaving processAction"); - } finally { - ActionContext.clear(); - } - } - - /** - * Service an action from the render phase. - * - * @param request render request - * @param response render response - * @throws PortletException in case of errors - * @throws IOException in case of IO errors - * @see javax.portlet.Portlet#render(javax.portlet.RenderRequest, - * javax.portlet.RenderResponse) - */ - public void render(RenderRequest request, RenderResponse response) - throws PortletException, IOException { - - if (LOG.isDebugEnabled()) { - LOG.debug("Entering render in mode ", request.getPortletMode().toString()); - } - resetActionContext(); - response.setTitle(getTitle(request)); - if (!request.getWindowState().equals(WindowState.MINIMIZED)) { - try { - // Check to see if an event set the render to be included directly - serviceAction(request, response, getRequestMap(request), getParameterMap(request), - getSessionMap(request), getApplicationMap(), - portletNamespace, PortletPhase.RENDER_PHASE); - if (LOG.isDebugEnabled()) LOG.debug("Leaving render"); - } finally { - resetActionContext(); - } - } - } - - /** - * Reset the action context. - */ - void resetActionContext() { - ActionContext.clear(); - } - - /** - * Merges all application and portlet attributes into a single - * HashMap to represent the entire Action context. - * - * @param requestMap a Map of all request attributes. - * @param parameterMap a Map of all request parameters. - * @param sessionMap a Map of all session attributes. - * @param applicationMap a Map of all servlet context attributes. - * @param request the PortletRequest object. - * @param response the PortletResponse object. - * @param servletRequest the HttpServletRequest object. - * @param servletResponse the HttpServletResponse object. - * @param servletContext the ServletContext object. - * @param portletConfig the PortletConfig object. - * @param phase The portlet phase (render or action, see - * {@link PortletConstants}) - * @return a HashMap representing the Action context. - * @throws IOException in case of IO errors - */ - public Map createContextMap(Map requestMap, Map parameterMap, - Map sessionMap, Map applicationMap, - PortletRequest request, PortletResponse response, HttpServletRequest servletRequest, - HttpServletResponse servletResponse, ServletContext servletContext, - PortletConfig portletConfig, PortletPhase phase) throws IOException { - - // TODO Must put http request/response objects into map for use with - container.inject(servletRequest); - - // ServletActionContext - Map extraContext = ActionContext.of(new HashMap<>()) - .withServletRequest(servletRequest) - .withServletResponse(servletResponse) - .withServletContext(servletContext) - .withParameters(HttpParameters.create(parameterMap).build()) - .withSession(sessionMap) - .withApplication(applicationMap) - .withLocale(getLocale(request)) - .with(StrutsStatics.STRUTS_PORTLET_CONTEXT, getPortletContext()) - .with(REQUEST, request) - .with(RESPONSE, response) - .with(PORTLET_CONFIG, portletConfig) - .with(PORTLET_NAMESPACE, portletNamespace) - .with(DEFAULT_ACTION_FOR_MODE, actionMap.get(request.getPortletMode())) - // helpers to get access to request/session/application scope - .with(DispatcherConstants.REQUEST, requestMap) - .with(DispatcherConstants.SESSION, sessionMap) - .with(DispatcherConstants.APPLICATION, applicationMap) - .with(DispatcherConstants.PARAMETERS, parameterMap) - .with(MODE_NAMESPACE_MAP, modeMap) - .with(PortletConstants.DEFAULT_ACTION_MAP, actionMap) - .with(PortletConstants.PHASE, phase) - .getContextMap(); - - AttributeMap attrMap = new AttributeMap(extraContext); - extraContext.put("attr", attrMap); - - return extraContext; - } - - protected Locale getLocale(PortletRequest request) { - String defaultLocale = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE); - Locale locale; - if (defaultLocale != null) { - try { - locale = LocaleUtils.toLocale(defaultLocale); - } catch (IllegalArgumentException e) { - LOG.warn(new ParameterizedMessage("Cannot convert 'struts.locale' = [{}] to proper locale, defaulting to request locale [{}]", - defaultLocale, request.getLocale()), e); - locale = request.getLocale(); - } - } else { - locale = request.getLocale(); - } - return locale; - } - - /** - * Loads the action and executes it. This method first creates the action - * context from the given parameters then loads an ActionProxy - * from the given action name and namespace. After that, the action is - * executed and output channels throught the response object. - * - * @param request the HttpServletRequest object. - * @param response the HttpServletResponse object. - * @param requestMap a Map of request attributes. - * @param parameterMap a Map of request parameters. - * @param sessionMap a Map of all session attributes. - * @param applicationMap a Map of all application attributes. - * @param portletNamespace the namespace or context of the action. - * @param phase The portlet phase (render or action, see {@link PortletConstants}) - * @throws PortletException in case of errors - */ - public void serviceAction(PortletRequest request, PortletResponse response, Map requestMap, Map parameterMap, - Map sessionMap, Map applicationMap, String portletNamespace, - PortletPhase phase) throws PortletException { - if (LOG.isDebugEnabled()) LOG.debug("serviceAction"); - Dispatcher.setInstance(dispatcherUtils); - String actionName = null; - String namespace; - try { - HttpServletRequest servletRequest = new PortletServletRequest(request, getPortletContext()); - HttpServletResponse servletResponse = createPortletServletResponse(response); - if (phase.isAction()) { - servletRequest = dispatcherUtils.wrapRequest(servletRequest); - if (servletRequest instanceof MultiPartRequestWrapper) { - // Multipart request. Request parameters are encoded in the multipart data, - // so we need to manually add them to the parameter map. - parameterMap.putAll(servletRequest.getParameterMap()); - } - } - container.inject(servletRequest); - ActionMapping mapping = getActionMapping(request, servletRequest); - actionName = mapping.getName(); - if ("renderDirect".equals(actionName)) { - namespace = request.getParameter(PortletConstants.RENDER_DIRECT_NAMESPACE); - } else { - namespace = mapping.getNamespace(); - } - Map extraContext = createContextMap(requestMap, parameterMap, - sessionMap, applicationMap, request, response, servletRequest, servletResponse, - servletContext, getPortletConfig(), phase); - extraContext.put(PortletConstants.ACTION_MAPPING, mapping); - if (LOG.isDebugEnabled()) { - LOG.debug("Creating action proxy for name = " + actionName + ", namespace = " + namespace); - } - ActionProxy proxy = factory.createActionProxy(namespace, actionName, mapping.getMethod(), extraContext); - request.setAttribute("struts.valueStack", proxy.getInvocation().getStack()); - proxy.execute(); - } catch (ConfigurationException e) { - if (LOG.isErrorEnabled()) { - LOG.error("Could not find action", e); - } - throw new PortletException("Could not find action " + actionName, e); - } catch (Exception e) { - if (LOG.isErrorEnabled()) { - LOG.error("Could not execute action", e); - } - throw new PortletException("Error executing action " + actionName, e); - } finally { - Dispatcher.setInstance(null); - } - } - - /** - * Returns a Map of all application attributes. Copies all attributes from - * the {@link PortletActionContext}into an {@link ApplicationMap}. - * - * @return a Map of all application attributes. - */ - protected Map getApplicationMap() { - return new PortletApplicationMap(getPortletContext()); - } - - /** - * Gets the namespace of the action from the request. The namespace is the - * same as the portlet mode. E.g, view mode is mapped to namespace - * view, and edit mode is mapped to the namespace - * edit - * - * @param portletRequest the PortletRequest object. - * @param servletRequest the ServletRequest to use - * @return the namespace of the action. - */ - protected ActionMapping getActionMapping(final PortletRequest portletRequest, final HttpServletRequest servletRequest) { - ActionMapping mapping; - String actionPath = getDefaultActionPath(portletRequest); - if (resetAction(portletRequest)) { - mapping = actionMap.get(portletRequest.getPortletMode()); - } else { - actionPath = servletRequest.getParameter(ACTION_PARAM); - if (StringUtils.isEmpty(actionPath)) { - mapping = actionMap.get(portletRequest.getPortletMode()); - } else { - - // Use the usual action mapper, but it is expecting an action extension - // on the uri, so we add the default one, which should be ok as the - // portlet is a portlet first, a servlet second - mapping = actionMapper.getMapping(servletRequest, dispatcherUtils.getConfigurationManager()); - } - } - - if (mapping == null) { - throw new StrutsException("Unable to locate action mapping for request, probably due to an invalid action path: " + actionPath); - } - return mapping; - } - - protected String getDefaultActionPath(PortletRequest portletRequest) { - return null; - } - - /** - * Get the namespace part of the action path. - * - * @param actionPath Full path to action - * @return The namespace part. - */ - String getNamespace(String actionPath) { - int idx = actionPath.lastIndexOf('/'); - String namespace = ""; - if (idx >= 0) { - namespace = actionPath.substring(0, idx); - } - return namespace; - } - - /** - * Get the action name part of the action path. - * - * @param actionPath Full path to action - * @return The action name. - */ - String getActionName(String actionPath) { - int idx = actionPath.lastIndexOf('/'); - String action = actionPath; - if (idx >= 0) { - action = actionPath.substring(idx + 1); - } - return action; - } - - /** - * Returns a Map of all request parameters. This implementation just calls - * {@link PortletRequest#getParameterMap()}. - * - * @param request the PortletRequest object. - * @return a Map of all request parameters. - * @throws IOException if an exception occurs while retrieving the parameter - * map. - */ - protected Map getParameterMap(PortletRequest request) throws IOException { - return new HashMap(request.getParameterMap()); - } - - /** - * Returns a Map of all request attributes. The default implementation is to - * wrap the request in a {@link RequestMap}. Override this method to - * customize how request attributes are mapped. - * - * @param request the PortletRequest object. - * @return a Map of all request attributes. - */ - protected Map getRequestMap(PortletRequest request) { - return new PortletRequestMap(request); - } - - /** - * Returns a Map of all session attributes. The default implementation is to - * wrap the reqeust in a {@link SessionMap}. Override this method to - * customize how session attributes are mapped. - * - * @param request the PortletRequest object. - * @return a Map of all session attributes. - */ - protected Map getSessionMap(PortletRequest request) { - return new PortletSessionMap(request); - } - - /** - * Convenience method to ease testing. - * - * @param factory action proxy factory - */ - protected void setActionProxyFactory(ActionProxyFactory factory) { - this.factory = factory; - } - - /** - * Check to see if the action parameter is valid for the current portlet mode. If the portlet - * mode has been changed with the portal widgets, the action name is invalid, since the - * action name belongs to the previous executing portlet mode. If this method evaluates to - * true the default<Mode>Action is used instead. - * - * @param request The portlet request. - * @return true if the action should be reset. - */ - private boolean resetAction(PortletRequest request) { - boolean reset = false; - Map paramMap = request.getParameterMap(); - String[] modeParam = (String[]) paramMap.get(MODE_PARAM); - if (modeParam != null && modeParam.length == 1) { - String originatingMode = modeParam[0]; - String currentMode = request.getPortletMode().toString(); - if (!currentMode.equals(originatingMode)) { - reset = true; - } - } - if (reset) { - request.setAttribute(ACTION_RESET, Boolean.TRUE); - } else { - request.setAttribute(ACTION_RESET, Boolean.FALSE); - } - return reset; - } - - public void destroy() { - if (dispatcherUtils != null) { - dispatcherUtils.cleanup(); - } else { - if (LOG.isWarnEnabled()) { - LOG.warn("Something is seriously wrong, DispatcherUtil is not initialized (null) "); - } - } - } - - /** - * @param actionMapper the actionMapper to set - */ - public void setActionMapper(ActionMapper actionMapper) { - this.actionMapper = actionMapper; - } - - /** - * Method to create a PortletServletResponse matching the used Portlet API, to be overridden for JSR286 Dispatcher. - * - * @param response The Response used for building the wrapper. - * @return The wrapper response for Servlet bound usage. - */ - protected PortletServletResponse createPortletServletResponse(PortletResponse response) { - return new PortletServletResponse(response); - } - -} +/* + * 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.portlet.dispatcher; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ActionProxyFactory; +import com.opensymphony.xwork2.config.ConfigurationException; +import com.opensymphony.xwork2.inject.Container; +import org.apache.commons.lang3.LocaleUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.message.ParameterizedMessage; +import org.apache.struts2.StrutsConstants; +import org.apache.struts2.StrutsException; +import org.apache.struts2.StrutsStatics; +import org.apache.struts2.dispatcher.ApplicationMap; +import org.apache.struts2.dispatcher.AttributeMap; +import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.dispatcher.DispatcherConstants; +import org.apache.struts2.dispatcher.HttpParameters; +import org.apache.struts2.dispatcher.RequestMap; +import org.apache.struts2.dispatcher.SessionMap; +import org.apache.struts2.dispatcher.mapper.ActionMapper; +import org.apache.struts2.dispatcher.mapper.ActionMapping; +import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper; +import org.apache.struts2.portlet.PortletApplicationMap; +import org.apache.struts2.portlet.PortletConstants; +import org.apache.struts2.portlet.PortletPhase; +import org.apache.struts2.portlet.PortletRequestMap; +import org.apache.struts2.portlet.PortletSessionMap; +import org.apache.struts2.portlet.context.PortletActionContext; +import org.apache.struts2.portlet.servlet.PortletServletContext; +import org.apache.struts2.portlet.servlet.PortletServletRequest; +import org.apache.struts2.portlet.servlet.PortletServletResponse; + +import javax.portlet.ActionRequest; +import javax.portlet.ActionResponse; +import javax.portlet.GenericPortlet; +import javax.portlet.PortletConfig; +import javax.portlet.PortletException; +import javax.portlet.PortletMode; +import javax.portlet.PortletRequest; +import javax.portlet.PortletResponse; +import javax.portlet.RenderRequest; +import javax.portlet.RenderResponse; +import javax.portlet.WindowState; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import static org.apache.struts2.portlet.PortletConstants.ACTION_PARAM; +import static org.apache.struts2.portlet.PortletConstants.ACTION_RESET; +import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_FOR_MODE; +import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_NAME; +import static org.apache.struts2.portlet.PortletConstants.MODE_NAMESPACE_MAP; +import static org.apache.struts2.portlet.PortletConstants.MODE_PARAM; +import static org.apache.struts2.portlet.PortletConstants.PORTLET_CONFIG; +import static org.apache.struts2.portlet.PortletConstants.PORTLET_NAMESPACE; +import static org.apache.struts2.portlet.PortletConstants.REQUEST; +import static org.apache.struts2.portlet.PortletConstants.RESPONSE; + +/** + * + *

+ * Struts JSR-168 portlet dispatcher. Similar to the WW2 Servlet dispatcher, + * but adjusted to a portal environment. The portlet is configured through the portlet.xml + * descriptor. Examples and descriptions follow below: + *

+ * + * + * @author Nils-Helge Garli + * @author Rainer Hermanns + * + *

Init parameters

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
NameDescriptionDefault value
portletNamespaceThe namespace for the portlet in the xwork configuration. This + * namespace is prepended to all action lookups, and makes it possible to host multiple + * portlets in the same portlet application. If this parameter is set, the complete namespace + * will be /portletNamespace/modeNamespace/actionNameThe default namespace
viewNamespaceBase namespace in the xwork configuration for the view portlet + * modeThe default namespace
editNamespaceBase namespace in the xwork configuration for the edit portlet + * modeThe default namespace
helpNamespaceBase namespace in the xwork configuration for the help portlet + * modeThe default namespace
defaultViewActionDefault action to invoke in the view portlet mode if no action is + * specifieddefault
defaultEditActionDefault action to invoke in the edit portlet mode if no action is + * specifieddefault
defaultHelpActionDefault action to invoke in the help portlet mode if no action is + * specifieddefault
+ * + *

Example:

+ *
+ * 
+ *
+ * <init-param>
+ *     <!-- The view mode namespace. Maps to a namespace in the xwork config file -->
+ *     <name>viewNamespace</name>
+ *     <value>/view</value>
+ * </init-param>
+ * <init-param>
+ *    <!-- The default action to invoke in view mode -->
+ *    <name>defaultViewAction</name>
+ *    <value>index</value>
+ * </init-param>
+ * <init-param>
+ *     <!-- The view mode namespace. Maps to a namespace in the xwork config file -->
+ *     <name>editNamespace</name>
+ *     <value>/edit</value>
+ * </init-param>
+ * <init-param>
+ *     <!-- The default action to invoke in view mode -->
+ *     <name>defaultEditAction</name>
+ *     <value>index</value>
+ * </init-param>
+ * <init-param>
+ *     <!-- The view mode namespace. Maps to a namespace in the xwork config file -->
+ *     <name>helpNamespace</name>
+ *     <value>/help</value>
+ * </init-param>
+ * <init-param>
+ *     <!-- The default action to invoke in view mode -->
+ *     <name>defaultHelpAction</name>
+ *     <value>index</value>
+ * </init-param>
+ *
+ * 
+ * 
+ */ +public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics { + + private static final Logger LOG = LogManager.getLogger(Jsr168Dispatcher.class); + + protected String portletNamespace = null; + + private ActionProxyFactory factory = null; + private Map modeMap = new HashMap(3); + private Map actionMap = new HashMap(3); + private Dispatcher dispatcherUtils; + private ActionMapper actionMapper; + private Container container; + private ServletContext servletContext; + + /** + * Initialize the portlet with the init parameters from portlet.xml + * + * @param cfg portlet configuration + * @throws PortletException in case of errors + */ + public void init(PortletConfig cfg) throws PortletException { + super.init(cfg); + LOG.debug("Initializing portlet {}", getPortletName()); + + Map params = new HashMap(); + for (Enumeration e = cfg.getInitParameterNames(); e.hasMoreElements(); ) { + String name = (String) e.nextElement(); + String value = cfg.getInitParameter(name); + params.put(name, value); + } + + servletContext = new PortletServletContext(cfg.getPortletContext()); + dispatcherUtils = new Dispatcher(servletContext, params); + dispatcherUtils.init(); + + // For testability + if (factory == null) { + factory = dispatcherUtils.getActionProxyFactory(); + } + portletNamespace = cfg.getInitParameter("portletNamespace"); + LOG.debug("PortletNamespace: {}", portletNamespace); + + parseModeConfig(actionMap, cfg, PortletMode.VIEW, "viewNamespace", + "defaultViewAction"); + parseModeConfig(actionMap, cfg, PortletMode.EDIT, "editNamespace", + "defaultEditAction"); + parseModeConfig(actionMap, cfg, PortletMode.HELP, "helpNamespace", + "defaultHelpAction"); + parseModeConfig(actionMap, cfg, new PortletMode("config"), "configNamespace", + "defaultConfigAction"); + parseModeConfig(actionMap, cfg, new PortletMode("about"), "aboutNamespace", + "defaultAboutAction"); + parseModeConfig(actionMap, cfg, new PortletMode("print"), "printNamespace", + "defaultPrintAction"); + parseModeConfig(actionMap, cfg, new PortletMode("preview"), "previewNamespace", + "defaultPreviewAction"); + parseModeConfig(actionMap, cfg, new PortletMode("edit_defaults"), + "editDefaultsNamespace", "defaultEditDefaultsAction"); + if (StringUtils.isEmpty(portletNamespace)) { + portletNamespace = ""; + } + + container = dispatcherUtils.getContainer(); + actionMapper = container.getInstance(ActionMapper.class); + } + + /** + * Parse the mode to namespace mappings configured in portlet.xml + * + * @param actionMap The map with mode <-> default action mapping. + * @param portletConfig The PortletConfig. + * @param portletMode The PortletMode. + * @param nameSpaceParam Name of the init parameter where the namespace for the mode + * is configured. + * @param defaultActionParam Name of the init parameter where the default action to + * execute for the mode is configured. + */ + void parseModeConfig(Map actionMap, PortletConfig portletConfig, + PortletMode portletMode, String nameSpaceParam, + String defaultActionParam) { + String namespace = portletConfig.getInitParameter(nameSpaceParam); + if (StringUtils.isEmpty(namespace)) { + namespace = ""; + } + modeMap.put(portletMode, namespace); + String defaultAction = portletConfig.getInitParameter(defaultActionParam); + String method = null; + if (StringUtils.isEmpty(defaultAction)) { + defaultAction = DEFAULT_ACTION_NAME; + } + if (defaultAction.indexOf('!') >= 0) { + method = defaultAction.substring(defaultAction.indexOf('!') + 1); + defaultAction = defaultAction.substring(0, defaultAction.indexOf('!')); + } + StringBuilder fullPath = new StringBuilder(); + if (StringUtils.isNotEmpty(portletNamespace)) { + fullPath.append(portletNamespace); + } + if (StringUtils.isNotEmpty(namespace)) { + fullPath.append(namespace).append("/"); + } else { + fullPath.append("/"); + } + fullPath.append(defaultAction); + ActionMapping mapping = new ActionMapping(); + mapping.setName(getActionName(fullPath.toString())); + mapping.setNamespace(getNamespace(fullPath.toString())); + if (method != null) { + mapping.setMethod(method); + } + actionMap.put(portletMode, mapping); + } + + /** + * Service an action from the event phase. + * + * @param request action request + * @param response action response + * @throws PortletException in case of errors + * @throws IOException in case of IO errors + * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, + * javax.portlet.ActionResponse) + */ + public void processAction(ActionRequest request, ActionResponse response) + throws PortletException, IOException { + if (LOG.isDebugEnabled()) { + LOG.debug("Entering processAction in mode ", request.getPortletMode().toString()); + } + resetActionContext(); + try { + serviceAction(request, response, getRequestMap(request), getParameterMap(request), + getSessionMap(request), getApplicationMap(), + portletNamespace, PortletPhase.ACTION_PHASE); + if (LOG.isDebugEnabled()) LOG.debug("Leaving processAction"); + } finally { + ActionContext.clear(); + } + } + + /** + * Service an action from the render phase. + * + * @param request render request + * @param response render response + * @throws PortletException in case of errors + * @throws IOException in case of IO errors + * @see javax.portlet.Portlet#render(javax.portlet.RenderRequest, + * javax.portlet.RenderResponse) + */ + public void render(RenderRequest request, RenderResponse response) + throws PortletException, IOException { + + if (LOG.isDebugEnabled()) { + LOG.debug("Entering render in mode ", request.getPortletMode().toString()); + } + resetActionContext(); + response.setTitle(getTitle(request)); + if (!request.getWindowState().equals(WindowState.MINIMIZED)) { + try { + // Check to see if an event set the render to be included directly + serviceAction(request, response, getRequestMap(request), getParameterMap(request), + getSessionMap(request), getApplicationMap(), + portletNamespace, PortletPhase.RENDER_PHASE); + if (LOG.isDebugEnabled()) LOG.debug("Leaving render"); + } finally { + resetActionContext(); + } + } + } + + /** + * Reset the action context. + */ + void resetActionContext() { + ActionContext.clear(); + } + + /** + * Merges all application and portlet attributes into a single + * HashMap to represent the entire Action context. + * + * @param requestMap a Map of all request attributes. + * @param parameterMap a Map of all request parameters. + * @param sessionMap a Map of all session attributes. + * @param applicationMap a Map of all servlet context attributes. + * @param request the PortletRequest object. + * @param response the PortletResponse object. + * @param servletRequest the HttpServletRequest object. + * @param servletResponse the HttpServletResponse object. + * @param servletContext the ServletContext object. + * @param portletConfig the PortletConfig object. + * @param phase The portlet phase (render or action, see + * {@link PortletConstants}) + * @return a HashMap representing the Action context. + * @throws IOException in case of IO errors + */ + public Map createContextMap(Map requestMap, Map parameterMap, + Map sessionMap, Map applicationMap, + PortletRequest request, PortletResponse response, HttpServletRequest servletRequest, + HttpServletResponse servletResponse, ServletContext servletContext, + PortletConfig portletConfig, PortletPhase phase) throws IOException { + + // TODO Must put http request/response objects into map for use with + container.inject(servletRequest); + + // ServletActionContext + Map extraContext = ActionContext.of(new HashMap<>()) + .withServletRequest(servletRequest) + .withServletResponse(servletResponse) + .withServletContext(servletContext) + .withParameters(HttpParameters.create(parameterMap).build()) + .withSession(sessionMap) + .withApplication(applicationMap) + .withLocale(getLocale(request)) + .with(StrutsStatics.STRUTS_PORTLET_CONTEXT, getPortletContext()) + .with(REQUEST, request) + .with(RESPONSE, response) + .with(PORTLET_CONFIG, portletConfig) + .with(PORTLET_NAMESPACE, portletNamespace) + .with(DEFAULT_ACTION_FOR_MODE, actionMap.get(request.getPortletMode())) + // helpers to get access to request/session/application scope + .with(DispatcherConstants.REQUEST, requestMap) + .with(DispatcherConstants.SESSION, sessionMap) + .with(DispatcherConstants.APPLICATION, applicationMap) + .with(DispatcherConstants.PARAMETERS, parameterMap) + .with(MODE_NAMESPACE_MAP, modeMap) + .with(PortletConstants.DEFAULT_ACTION_MAP, actionMap) + .with(PortletConstants.PHASE, phase) + .getContextMap(); + + AttributeMap attrMap = new AttributeMap(extraContext); + extraContext.put("attr", attrMap); + + return extraContext; + } + + protected Locale getLocale(PortletRequest request) { + String defaultLocale = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE); + Locale locale; + if (defaultLocale != null) { + try { + locale = LocaleUtils.toLocale(defaultLocale); + } catch (IllegalArgumentException e) { + LOG.warn(new ParameterizedMessage("Cannot convert 'struts.locale' = [{}] to proper locale, defaulting to request locale [{}]", + defaultLocale, request.getLocale()), e); + locale = request.getLocale(); + } + } else { + locale = request.getLocale(); + } + return locale; + } + + /** + * Loads the action and executes it. This method first creates the action + * context from the given parameters then loads an ActionProxy + * from the given action name and namespace. After that, the action is + * executed and output channels throught the response object. + * + * @param request the HttpServletRequest object. + * @param response the HttpServletResponse object. + * @param requestMap a Map of request attributes. + * @param parameterMap a Map of request parameters. + * @param sessionMap a Map of all session attributes. + * @param applicationMap a Map of all application attributes. + * @param portletNamespace the namespace or context of the action. + * @param phase The portlet phase (render or action, see {@link PortletConstants}) + * @throws PortletException in case of errors + */ + public void serviceAction(PortletRequest request, PortletResponse response, Map requestMap, Map parameterMap, + Map sessionMap, Map applicationMap, String portletNamespace, + PortletPhase phase) throws PortletException { + if (LOG.isDebugEnabled()) LOG.debug("serviceAction"); + Dispatcher.setInstance(dispatcherUtils); + String actionName = null; + String namespace; + try { + HttpServletRequest servletRequest = new PortletServletRequest(request, getPortletContext()); + HttpServletResponse servletResponse = createPortletServletResponse(response); + if (phase.isAction()) { + servletRequest = dispatcherUtils.wrapRequest(servletRequest); + if (servletRequest instanceof MultiPartRequestWrapper) { + // Multipart request. Request parameters are encoded in the multipart data, + // so we need to manually add them to the parameter map. + parameterMap.putAll(servletRequest.getParameterMap()); + } + } + container.inject(servletRequest); + ActionMapping mapping = getActionMapping(request, servletRequest); + actionName = mapping.getName(); + if ("renderDirect".equals(actionName)) { + namespace = request.getParameter(PortletConstants.RENDER_DIRECT_NAMESPACE); + } else { + namespace = mapping.getNamespace(); + } + Map extraContext = createContextMap(requestMap, parameterMap, + sessionMap, applicationMap, request, response, servletRequest, servletResponse, + servletContext, getPortletConfig(), phase); + extraContext.put(PortletConstants.ACTION_MAPPING, mapping); + if (LOG.isDebugEnabled()) { + LOG.debug("Creating action proxy for name = " + actionName + ", namespace = " + namespace); + } + ActionProxy proxy = factory.createActionProxy(namespace, actionName, mapping.getMethod(), extraContext); + request.setAttribute("struts.valueStack", proxy.getInvocation().getStack()); + proxy.execute(); + } catch (ConfigurationException e) { + if (LOG.isErrorEnabled()) { + LOG.error("Could not find action", e); + } + throw new PortletException("Could not find action " + actionName, e); + } catch (Exception e) { + if (LOG.isErrorEnabled()) { + LOG.error("Could not execute action", e); + } + throw new PortletException("Error executing action " + actionName, e); + } finally { + Dispatcher.clearInstance(); + } + } + + /** + * Returns a Map of all application attributes. Copies all attributes from + * the {@link PortletActionContext}into an {@link ApplicationMap}. + * + * @return a Map of all application attributes. + */ + protected Map getApplicationMap() { + return new PortletApplicationMap(getPortletContext()); + } + + /** + * Gets the namespace of the action from the request. The namespace is the + * same as the portlet mode. E.g, view mode is mapped to namespace + * view, and edit mode is mapped to the namespace + * edit + * + * @param portletRequest the PortletRequest object. + * @param servletRequest the ServletRequest to use + * @return the namespace of the action. + */ + protected ActionMapping getActionMapping(final PortletRequest portletRequest, final HttpServletRequest servletRequest) { + ActionMapping mapping; + String actionPath = getDefaultActionPath(portletRequest); + if (resetAction(portletRequest)) { + mapping = actionMap.get(portletRequest.getPortletMode()); + } else { + actionPath = servletRequest.getParameter(ACTION_PARAM); + if (StringUtils.isEmpty(actionPath)) { + mapping = actionMap.get(portletRequest.getPortletMode()); + } else { + + // Use the usual action mapper, but it is expecting an action extension + // on the uri, so we add the default one, which should be ok as the + // portlet is a portlet first, a servlet second + mapping = actionMapper.getMapping(servletRequest, dispatcherUtils.getConfigurationManager()); + } + } + + if (mapping == null) { + throw new StrutsException("Unable to locate action mapping for request, probably due to an invalid action path: " + actionPath); + } + return mapping; + } + + protected String getDefaultActionPath(PortletRequest portletRequest) { + return null; + } + + /** + * Get the namespace part of the action path. + * + * @param actionPath Full path to action + * @return The namespace part. + */ + String getNamespace(String actionPath) { + int idx = actionPath.lastIndexOf('/'); + String namespace = ""; + if (idx >= 0) { + namespace = actionPath.substring(0, idx); + } + return namespace; + } + + /** + * Get the action name part of the action path. + * + * @param actionPath Full path to action + * @return The action name. + */ + String getActionName(String actionPath) { + int idx = actionPath.lastIndexOf('/'); + String action = actionPath; + if (idx >= 0) { + action = actionPath.substring(idx + 1); + } + return action; + } + + /** + * Returns a Map of all request parameters. This implementation just calls + * {@link PortletRequest#getParameterMap()}. + * + * @param request the PortletRequest object. + * @return a Map of all request parameters. + * @throws IOException if an exception occurs while retrieving the parameter + * map. + */ + protected Map getParameterMap(PortletRequest request) throws IOException { + return new HashMap(request.getParameterMap()); + } + + /** + * Returns a Map of all request attributes. The default implementation is to + * wrap the request in a {@link RequestMap}. Override this method to + * customize how request attributes are mapped. + * + * @param request the PortletRequest object. + * @return a Map of all request attributes. + */ + protected Map getRequestMap(PortletRequest request) { + return new PortletRequestMap(request); + } + + /** + * Returns a Map of all session attributes. The default implementation is to + * wrap the reqeust in a {@link SessionMap}. Override this method to + * customize how session attributes are mapped. + * + * @param request the PortletRequest object. + * @return a Map of all session attributes. + */ + protected Map getSessionMap(PortletRequest request) { + return new PortletSessionMap(request); + } + + /** + * Convenience method to ease testing. + * + * @param factory action proxy factory + */ + protected void setActionProxyFactory(ActionProxyFactory factory) { + this.factory = factory; + } + + /** + * Check to see if the action parameter is valid for the current portlet mode. If the portlet + * mode has been changed with the portal widgets, the action name is invalid, since the + * action name belongs to the previous executing portlet mode. If this method evaluates to + * true the default<Mode>Action is used instead. + * + * @param request The portlet request. + * @return true if the action should be reset. + */ + private boolean resetAction(PortletRequest request) { + boolean reset = false; + Map paramMap = request.getParameterMap(); + String[] modeParam = (String[]) paramMap.get(MODE_PARAM); + if (modeParam != null && modeParam.length == 1) { + String originatingMode = modeParam[0]; + String currentMode = request.getPortletMode().toString(); + if (!currentMode.equals(originatingMode)) { + reset = true; + } + } + if (reset) { + request.setAttribute(ACTION_RESET, Boolean.TRUE); + } else { + request.setAttribute(ACTION_RESET, Boolean.FALSE); + } + return reset; + } + + public void destroy() { + if (dispatcherUtils != null) { + dispatcherUtils.cleanup(); + } else { + if (LOG.isWarnEnabled()) { + LOG.warn("Something is seriously wrong, DispatcherUtil is not initialized (null) "); + } + } + } + + /** + * @param actionMapper the actionMapper to set + */ + public void setActionMapper(ActionMapper actionMapper) { + this.actionMapper = actionMapper; + } + + /** + * Method to create a PortletServletResponse matching the used Portlet API, to be overridden for JSR286 Dispatcher. + * + * @param response The Response used for building the wrapper. + * @return The wrapper response for Servlet bound usage. + */ + protected PortletServletResponse createPortletServletResponse(PortletResponse response) { + return new PortletServletResponse(response); + } + +} diff --git a/plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/OldDecorator2NewStrutsDecorator.java b/plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/OldDecorator2NewStrutsDecorator.java index f5a0cd1a8d..989141044d 100644 --- a/plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/OldDecorator2NewStrutsDecorator.java +++ b/plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/OldDecorator2NewStrutsDecorator.java @@ -1,199 +1,204 @@ -/* - * 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.sitemesh; - -import com.opensymphony.module.sitemesh.RequestConstants; -import com.opensymphony.sitemesh.Content; -import com.opensymphony.sitemesh.webapp.SiteMeshWebAppContext; -import com.opensymphony.sitemesh.webapp.decorator.BaseWebAppDecorator; -import com.opensymphony.xwork2.*; -import com.opensymphony.xwork2.interceptor.PreResultListener; -import com.opensymphony.xwork2.util.ValueStack; -import com.opensymphony.xwork2.util.ValueStackFactory; -import freemarker.template.Configuration; -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.dispatcher.Dispatcher; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Locale; - -/** - * Adapts a SiteMesh 2 {@link com.opensymphony.module.sitemesh.Decorator} to a - * SiteMesh 3 {@link com.opensymphony.sitemesh.Decorator}. - * - * @since SiteMesh 3 - */ -public abstract class OldDecorator2NewStrutsDecorator extends BaseWebAppDecorator implements RequestConstants { - - protected com.opensymphony.module.sitemesh.Decorator oldDecorator; - private static String customEncoding; - - public OldDecorator2NewStrutsDecorator(com.opensymphony.module.sitemesh.Decorator oldDecorator) { - this.oldDecorator = oldDecorator; - } - - public OldDecorator2NewStrutsDecorator() { - oldDecorator = null; - } - - - /** - * Applies the decorator, using the relevent contexts - * - * @param content The content - * @param request The servlet request - * @param response The servlet response - * @param servletContext The servlet context - * @param ctx The action context for this request, populated with the server state - */ - protected abstract void render(Content content, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, ActionContext ctx) throws ServletException, IOException; - - /** - * Applies the decorator, creating the relevent contexts and delegating to - * the extended applyDecorator(). - * - * @param content The content - * @param request The servlet request - * @param response The servlet response - * @param servletContext The servlet context - * @param webAppContext The web app context - */ - - protected void render(Content content, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, SiteMeshWebAppContext webAppContext) throws IOException, ServletException { - - // see if the URI path (webapp) is set - if (oldDecorator.getURIPath() != null) { - // in a security conscious environment, the servlet container - // may return null for a given URL - if (servletContext.getContext(oldDecorator.getURIPath()) != null) { - servletContext = servletContext.getContext(oldDecorator.getURIPath()); - } - } - - ActionContext ctx = ServletActionContext.getActionContext(request); - if (ctx == null) { - // ok, one isn't associated with the request, so let's create one using the current Dispatcher - ValueStack vs = Dispatcher.getInstance().getContainer().getInstance(ValueStackFactory.class).createValueStack(); - vs.getContext().putAll(Dispatcher.getInstance().createContextMap(request, response, null)); - ctx = ActionContext.of(vs.getContext()); - if (ctx.getActionInvocation() == null) { - // put in a dummy ActionSupport so basic functionality still works - ActionSupport action = new ActionSupport(); - vs.push(action); - ctx.withActionInvocation(new DummyActionInvocation(action)); - } - } - - // delegate to the actual page decorator - render(content, request, response, servletContext, ctx); - - } - - /** - * Returns the locale used for the {@link freemarker.template.Configuration#getTemplate(String, java.util.Locale)} call. The base implementation - * simply returns the locale setting of the action (assuming the action implements {@link LocaleProvider}) or, if - * the action does not the configuration's locale is returned. Override this method to provide different behaviour, - */ - protected Locale getLocale(ActionInvocation invocation, Configuration configuration) { - if (invocation.getAction() instanceof LocaleProvider) { - return ((LocaleProvider) invocation.getAction()).getLocale(); - } else { - return configuration.getLocale(); - } - } - - - /** - * Gets the L18N encoding of the system. The default is UTF-8. - */ - protected String getEncoding() { - String encoding = customEncoding; - if (encoding == null) { - encoding = System.getProperty("file.encoding"); - } - if (encoding == null) { - encoding = "UTF-8"; - } - return encoding; - } - - - static class DummyActionInvocation implements ActionInvocation { - - ActionSupport action; - - public DummyActionInvocation(ActionSupport action) { - this.action = action; - } - - public Object getAction() { - return action; - } - - public boolean isExecuted() { - return false; - } - - public ActionContext getInvocationContext() { - return null; - } - - public ActionProxy getProxy() { - return null; - } - - public Result getResult() throws Exception { - return null; - } - - public String getResultCode() { - return null; - } - - public void setResultCode(String resultCode) { - } - - public ValueStack getStack() { - return null; - } - - public void addPreResultListener(PreResultListener listener) { - } - - public String invoke() throws Exception { - return null; - } - - public String invokeActionOnly() throws Exception { - return null; - } - - public void setActionEventListener(ActionEventListener listener) { - } - - public void init(ActionProxy proxy) { - } - - } - -} +/* + * 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.sitemesh; + +import com.opensymphony.module.sitemesh.RequestConstants; +import com.opensymphony.sitemesh.Content; +import com.opensymphony.sitemesh.webapp.SiteMeshWebAppContext; +import com.opensymphony.sitemesh.webapp.decorator.BaseWebAppDecorator; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionEventListener; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.LocaleProvider; +import com.opensymphony.xwork2.Result; +import com.opensymphony.xwork2.interceptor.PreResultListener; +import com.opensymphony.xwork2.util.ValueStack; +import freemarker.template.Configuration; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.dispatcher.Dispatcher; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Locale; + +/** + * Adapts a SiteMesh 2 {@link com.opensymphony.module.sitemesh.Decorator} to a + * SiteMesh 3 {@link com.opensymphony.sitemesh.Decorator}. + * + * @since SiteMesh 3 + */ +public abstract class OldDecorator2NewStrutsDecorator extends BaseWebAppDecorator implements RequestConstants { + + protected com.opensymphony.module.sitemesh.Decorator oldDecorator; + private static String customEncoding; + + public OldDecorator2NewStrutsDecorator(com.opensymphony.module.sitemesh.Decorator oldDecorator) { + this.oldDecorator = oldDecorator; + } + + public OldDecorator2NewStrutsDecorator() { + oldDecorator = null; + } + + + /** + * Applies the decorator, using the relevent contexts + * + * @param content The content + * @param request The servlet request + * @param response The servlet response + * @param servletContext The servlet context + * @param ctx The action context for this request, populated with the server state + */ + protected abstract void render(Content content, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, ActionContext ctx) throws ServletException, IOException; + + /** + * Applies the decorator, creating the relevent contexts and delegating to + * the extended applyDecorator(). + * + * @param content The content + * @param request The servlet request + * @param response The servlet response + * @param servletContext The servlet context + * @param webAppContext The web app context + */ + + protected void render(Content content, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, SiteMeshWebAppContext webAppContext) throws IOException, ServletException { + + // see if the URI path (webapp) is set + if (oldDecorator.getURIPath() != null) { + // in a security conscious environment, the servlet container + // may return null for a given URL + if (servletContext.getContext(oldDecorator.getURIPath()) != null) { + servletContext = servletContext.getContext(oldDecorator.getURIPath()); + } + } + + ActionContext ctx = ServletActionContext.getActionContext(request); + if (ctx == null) { + // ok, one isn't associated with the request, so let's create one using the current Dispatcher + ValueStack vs = Dispatcher.getInstance().getValueStackFactory().createValueStack(); + vs.getContext().putAll(Dispatcher.getInstance().createContextMap(request, response, null)); + ctx = ActionContext.of(vs.getContext()); + if (ctx.getActionInvocation() == null) { + // put in a dummy ActionSupport so basic functionality still works + ActionSupport action = new ActionSupport(); + vs.push(action); + ctx.withActionInvocation(new DummyActionInvocation(action)); + } + } + + // delegate to the actual page decorator + render(content, request, response, servletContext, ctx); + + } + + /** + * Returns the locale used for the {@link freemarker.template.Configuration#getTemplate(String, java.util.Locale)} call. The base implementation + * simply returns the locale setting of the action (assuming the action implements {@link LocaleProvider}) or, if + * the action does not the configuration's locale is returned. Override this method to provide different behaviour, + */ + protected Locale getLocale(ActionInvocation invocation, Configuration configuration) { + if (invocation.getAction() instanceof LocaleProvider) { + return ((LocaleProvider) invocation.getAction()).getLocale(); + } else { + return configuration.getLocale(); + } + } + + + /** + * Gets the L18N encoding of the system. The default is UTF-8. + */ + protected String getEncoding() { + String encoding = customEncoding; + if (encoding == null) { + encoding = System.getProperty("file.encoding"); + } + if (encoding == null) { + encoding = "UTF-8"; + } + return encoding; + } + + + static class DummyActionInvocation implements ActionInvocation { + + ActionSupport action; + + public DummyActionInvocation(ActionSupport action) { + this.action = action; + } + + public Object getAction() { + return action; + } + + public boolean isExecuted() { + return false; + } + + public ActionContext getInvocationContext() { + return null; + } + + public ActionProxy getProxy() { + return null; + } + + public Result getResult() throws Exception { + return null; + } + + public String getResultCode() { + return null; + } + + public void setResultCode(String resultCode) { + } + + public ValueStack getStack() { + return null; + } + + public void addPreResultListener(PreResultListener listener) { + } + + public String invoke() throws Exception { + return null; + } + + public String invokeActionOnly() throws Exception { + return null; + } + + public void setActionEventListener(ActionEventListener listener) { + } + + public void init(ActionProxy proxy) { + } + + } + +} From 3d25caa0a8ecd2175b59d6b85f2ba3918e67000d Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Tue, 2 Jan 2024 03:48:49 +1100 Subject: [PATCH 8/8] WW-5382 Update Dispatcher#getContainer JavaDoc --- .../main/java/org/apache/struts2/dispatcher/Dispatcher.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index c534069c37..3947d89d25 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -1095,7 +1095,11 @@ public ConfigurationManager getConfigurationManager() { } /** - * Expose the dependency injection container. + * Exposes a thread-cached reference of the dependency injection container. If the container is found to have + * changed since the last time it was cached, this Dispatcher instance is re-injected to ensure no stale + * configuration/dependencies persist. + *

+ * A non-cached reference can be obtained by calling {@link #getConfigurationManager()}. * * @return Our dependency injection container */