From 71d4a7be3bd616139088018aea5ce6ca5aed1140 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Sat, 1 Feb 2020 12:47:52 +0100 Subject: [PATCH] WW-5047 Upgrades to VelocityEngine 2.1 and VelocityTools 3.0 --- apps/showcase/pom.xml | 11 -- bundles/demo/pom.xml | 10 +- core/pom.xml | 13 --- .../views/TagLibraryDirectiveProvider.java | 2 +- .../struts2/views/util/ContextUtil.java | 8 +- .../util/InvocationSessionStoreTest.java | 2 +- plugins/embeddedjsp/pom.xml | 4 + .../resources/org/apache/struts2/complex0.jsp | 3 +- plugins/portlet/pom.xml | 13 --- .../sitemesh/VelocityDecoratorServlet.java | 2 +- plugins/velocity/pom.xml | 11 +- .../views/velocity/StrutsVelocityContext.java | 14 ++- .../views/velocity/VelocityManager.java | 109 ++++++++---------- .../views/velocity}/VelocityStrutsUtil.java | 3 +- .../views/velocity/VelocityTagLibrary.java | 6 +- pom.xml | 30 ++--- 16 files changed, 93 insertions(+), 148 deletions(-) rename {core/src/main/java/org/apache/struts2/util => plugins/velocity/src/main/java/org/apache/struts2/views/velocity}/VelocityStrutsUtil.java (95%) diff --git a/apps/showcase/pom.xml b/apps/showcase/pom.xml index 99bceb716..dc0ca61f2 100644 --- a/apps/showcase/pom.xml +++ b/apps/showcase/pom.xml @@ -122,17 +122,6 @@ ${log4j2.version} - - - org.apache.velocity - velocity - - - - org.apache.velocity - velocity-tools - - opensymphony sitemesh diff --git a/bundles/demo/pom.xml b/bundles/demo/pom.xml index 4ea541009..b9bec99dc 100644 --- a/bundles/demo/pom.xml +++ b/bundles/demo/pom.xml @@ -34,14 +34,8 @@ - org.apache.velocity - velocity - - - - org.apache.velocity - velocity-tools - true + org.apache.struts + struts2-velocity-plugin diff --git a/core/pom.xml b/core/pom.xml index f74a3fef7..f6038f657 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -203,19 +203,6 @@ ognl - - - org.apache.velocity - velocity - true - - - - org.apache.velocity - velocity-tools - true - - org.apache.logging.log4j diff --git a/core/src/main/java/org/apache/struts2/views/TagLibraryDirectiveProvider.java b/core/src/main/java/org/apache/struts2/views/TagLibraryDirectiveProvider.java index 76368467d..ccc8e4b38 100644 --- a/core/src/main/java/org/apache/struts2/views/TagLibraryDirectiveProvider.java +++ b/core/src/main/java/org/apache/struts2/views/TagLibraryDirectiveProvider.java @@ -36,6 +36,6 @@ public interface TagLibraryDirectiveProvider { * * @return A list of Velocity directive classes */ - List getDirectiveClasses(); + List> getDirectiveClasses(); } diff --git a/core/src/main/java/org/apache/struts2/views/util/ContextUtil.java b/core/src/main/java/org/apache/struts2/views/util/ContextUtil.java index e530fa861..8d493f617 100644 --- a/core/src/main/java/org/apache/struts2/views/util/ContextUtil.java +++ b/core/src/main/java/org/apache/struts2/views/util/ContextUtil.java @@ -41,9 +41,7 @@ import com.opensymphony.xwork2.util.ValueStack; */ public class ContextUtil { public static final String REQUEST = "request"; - public static final String REQUEST2 = "request"; public static final String RESPONSE = "response"; - public static final String RESPONSE2 = "response"; public static final String SESSION = "session"; public static final String BASE = "base"; public static final String STACK = "stack"; @@ -51,12 +49,10 @@ public class ContextUtil { public static final String STRUTS = "struts"; public static final String ACTION = "action"; - public static Map getStandardContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { - HashMap map = new HashMap(); + public static Map getStandardContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { + HashMap map = new HashMap<>(); map.put(REQUEST, req); - map.put(REQUEST2, req); map.put(RESPONSE, res); - map.put(RESPONSE2, res); map.put(SESSION, req.getSession(false)); map.put(BASE, req.getContextPath()); map.put(STACK, stack); diff --git a/core/src/test/java/org/apache/struts2/util/InvocationSessionStoreTest.java b/core/src/test/java/org/apache/struts2/util/InvocationSessionStoreTest.java index 8cfc3f4d0..5d0843b82 100644 --- a/core/src/test/java/org/apache/struts2/util/InvocationSessionStoreTest.java +++ b/core/src/test/java/org/apache/struts2/util/InvocationSessionStoreTest.java @@ -19,6 +19,7 @@ package org.apache.struts2.util; import com.mockobjects.dynamic.Mock; +import com.mockobjects.servlet.MockPageContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionProxy; @@ -31,7 +32,6 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Map; -import org.apache.struts.mock.MockPageContext; import org.apache.struts2.ServletActionContext; diff --git a/plugins/embeddedjsp/pom.xml b/plugins/embeddedjsp/pom.xml index 9e69fcad7..ce459dc9a 100644 --- a/plugins/embeddedjsp/pom.xml +++ b/plugins/embeddedjsp/pom.xml @@ -36,6 +36,10 @@ org.apache.commons commons-lang3 + + org.apache.struts + struts2-velocity-plugin + org.springframework spring-test diff --git a/plugins/embeddedjsp/src/test/resources/org/apache/struts2/complex0.jsp b/plugins/embeddedjsp/src/test/resources/org/apache/struts2/complex0.jsp index 263a1421e..184ce1781 100644 --- a/plugins/embeddedjsp/src/test/resources/org/apache/struts2/complex0.jsp +++ b/plugins/embeddedjsp/src/test/resources/org/apache/struts2/complex0.jsp @@ -27,12 +27,11 @@ <%@ page import="com.opensymphony.xwork2.util.TextParseUtil" %> <%@ page import="com.opensymphony.xwork2.util.WildcardUtil" %> <%@ page import="org.apache.struts2.StrutsConstants" %> -<%@ page import="org.apache.struts2.StrutsConstants" %> <%@ page import="org.apache.struts2.util.ComponentUtils" %> <%@ page import="org.apache.struts2.util.ContainUtil" %> <%@ page import="org.apache.struts2.util.StrutsUtil" %> <%@ page import="org.apache.struts2.util.URLDecoderUtil" %> -<%@ page import="org.apache.struts2.util.VelocityStrutsUtil" %> +<%@ page import="org.apache.struts2.views.velocity.VelocityStrutsUtil" %> <%@ taglib prefix="r" uri="http://jakarta.apache.org/taglibs/request-1.0" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> diff --git a/plugins/portlet/pom.xml b/plugins/portlet/pom.xml index 81f08b65c..c5198b8bb 100644 --- a/plugins/portlet/pom.xml +++ b/plugins/portlet/pom.xml @@ -70,19 +70,6 @@ commons-lang3 - - - org.apache.velocity - velocity - true - - - - org.apache.velocity - velocity-tools - true - - javax.portlet diff --git a/plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/VelocityDecoratorServlet.java b/plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/VelocityDecoratorServlet.java index 7d3b7c90b..7fd22656e 100644 --- a/plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/VelocityDecoratorServlet.java +++ b/plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/VelocityDecoratorServlet.java @@ -89,7 +89,7 @@ public class VelocityDecoratorServlet extends VelocityViewServlet { // we can get these now that velocity is initialized defaultContentType = getVelocityProperty(VelocityView.CONTENT_TYPE_KEY, VelocityView.DEFAULT_CONTENT_TYPE); - String encoding = getVelocityProperty(RuntimeConstants.OUTPUT_ENCODING, VelocityView.DEFAULT_OUTPUT_ENCODING); + String encoding = getVelocityProperty(RuntimeConstants.ENCODING_DEFAULT, VelocityView.DEFAULT_OUTPUT_ENCODING); // For non Latin-1 encodings, ensure that the charset is // included in the Content-Type header. diff --git a/plugins/velocity/pom.xml b/plugins/velocity/pom.xml index 649e1d4c1..8da8e57a0 100644 --- a/plugins/velocity/pom.xml +++ b/plugins/velocity/pom.xml @@ -36,12 +36,17 @@ org.apache.velocity - velocity + velocity-engine-core - org.apache.velocity - velocity-tools + org.apache.velocity.tools + velocity-tools-view + + + + org.apache.velocity.tools + velocity-tools-view-jsp diff --git a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java index f8f6075f5..62ac6c79d 100644 --- a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java @@ -22,21 +22,23 @@ import org.apache.velocity.VelocityContext; import com.opensymphony.xwork2.util.ValueStack; +import java.util.List; + public class StrutsVelocityContext extends VelocityContext { - ValueStack stack; - VelocityContext[] chainedContexts; + private ValueStack stack; + private List chainedContexts; public StrutsVelocityContext(ValueStack stack) { this(null, stack); } - public StrutsVelocityContext(VelocityContext[] chainedContexts, ValueStack stack) { + public StrutsVelocityContext(List chainedContexts, ValueStack stack) { this.chainedContexts = chainedContexts; this.stack = stack; } - public boolean internalContainsKey(Object key) { + public boolean internalContainsKey(String key) { boolean contains = super.internalContainsKey(key); // first let's check to see if we contain the requested key @@ -46,13 +48,13 @@ public class StrutsVelocityContext extends VelocityContext { // if not, let's search for the key in the ognl value stack if (stack != null) { - Object o = stack.findValue(key.toString()); + Object o = stack.findValue(key); if (o != null) { return true; } - o = stack.getContext().get(key.toString()); + o = stack.getContext().get(key); if (o != null) { return true; } diff --git a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java index 6cbbaed82..942609d2f 100644 --- a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java @@ -28,7 +28,6 @@ import org.apache.logging.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsConstants; import org.apache.struts2.StrutsException; -import org.apache.struts2.util.VelocityStrutsUtil; import org.apache.struts2.views.TagLibraryDirectiveProvider; import org.apache.struts2.views.jsp.ui.OgnlTool; import org.apache.struts2.views.util.ContextUtil; @@ -36,9 +35,9 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.context.Context; -import org.apache.velocity.tools.view.ToolboxManager; -import org.apache.velocity.tools.view.context.ChainedContext; -import org.apache.velocity.tools.view.servlet.ServletToolboxManager; +import org.apache.velocity.tools.ToolContext; +import org.apache.velocity.tools.ToolManager; +import org.apache.velocity.tools.ToolboxFactory; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @@ -47,7 +46,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.StringTokenizer; /** * Manages the environment for Velocity result types @@ -67,7 +72,7 @@ public class VelocityManager { /** * A reference to the toolbox manager. */ - protected ToolboxManager toolboxManager = null; + protected ToolManager toolboxManager = null; private String toolBoxLocation; /** @@ -98,7 +103,7 @@ public class VelocityManager { /** * @return a reference to the VelocityEngine used by all struts velocity thingies with the exception of - * directly accessed *.vm pages + * directly accessed *.vm pages */ public VelocityEngine getVelocityEngine() { return velocityEngine; @@ -120,22 +125,19 @@ public class VelocityManager { * * * @param stack the current {@link ValueStack} - * @param req the current HttpServletRequest - * @param res the current HttpServletResponse + * @param req the current HttpServletRequest + * @param res the current HttpServletResponse * @return a new StrutsVelocityContext */ public Context createContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { - Context result = null; - VelocityContext[] chainedContexts = prepareChainedContexts(req, res, stack.getContext()); + List chainedContexts = prepareChainedContexts(req, res, stack.getContext()); StrutsVelocityContext context = new StrutsVelocityContext(chainedContexts, stack); - Map standardMap = ContextUtil.getStandardContext(stack, req, res); - for (Object o : standardMap.entrySet()) { - Map.Entry entry = (Map.Entry) o; - context.put((String) entry.getKey(), entry.getValue()); + Map standardMap = ContextUtil.getStandardContext(stack, req, res); + for (Map.Entry entry : standardMap.entrySet()) { + context.put(entry.getKey(), entry.getValue()); } context.put(STRUTS, new VelocityStrutsUtil(velocityEngine, context, stack, req, res)); - ServletContext ctx = null; try { ctx = ServletActionContext.getServletContext(); @@ -144,9 +146,10 @@ public class VelocityManager { LOG.debug("internal toolbox context ignored"); } + Context result; if (toolboxManager != null && ctx != null) { - ChainedContext chained = new ChainedContext(context, velocityEngine, req, res, ctx); - chained.setToolbox(toolboxManager.getToolbox(chained)); + ToolContext chained = new ToolContext(velocityEngine); + chained.addToolbox(toolboxManager.getToolboxFactory().createToolbox(ToolboxFactory.DEFAULT_SCOPE)); result = chained; } else { result = context; @@ -161,32 +164,24 @@ public class VelocityManager { * perform any initialization of the contexts. All that must be done in the * context itself. * - * @param servletRequest the servlet request object + * @param servletRequest the servlet request object * @param servletResponse the servlet response object - * @param extraContext map with extra context + * @param extraContext map with extra context * @return an VelocityContext[] of contexts to chain */ - protected VelocityContext[] prepareChainedContexts(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Map extraContext) { - if (this.chainedContextNames == null) { - return null; - } - List contextList = new ArrayList(); - for (int i = 0; i < chainedContextNames.length; i++) { - String className = chainedContextNames[i]; - try { - VelocityContext velocityContext = (VelocityContext) objectFactory.buildBean(className, null); - contextList.add(velocityContext); - } catch (Exception e) { - LOG.warn("Warning. {} caught while attempting to instantiate a chained VelocityContext, {} -- skipping", e.getClass().getName(), className); + protected List prepareChainedContexts(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Map extraContext) { + List contextList = new ArrayList<>(); + if (this.chainedContextNames != null) { + for (String className : chainedContextNames) { + try { + VelocityContext velocityContext = (VelocityContext) objectFactory.buildBean(className, extraContext); + contextList.add(velocityContext); + } catch (Exception e) { + LOG.warn("Warning. {} caught while attempting to instantiate a chained VelocityContext, {} -- skipping", e.getClass().getName(), className); + } } } - if (contextList.size() > 0) { - VelocityContext[] extraContexts = new VelocityContext[contextList.size()]; - contextList.toArray(extraContexts); - return extraContexts; - } else { - return null; - } + return contextList; } /** @@ -341,7 +336,7 @@ public class VelocityManager { this.toolBoxLocation = toolboxLocation; } - public ToolboxManager getToolboxManager() { + public ToolManager getToolboxManager() { return toolboxManager; } @@ -373,15 +368,14 @@ public class VelocityManager { /** * Initializes the ServletToolboxManager for this servlet's * toolbox (if any). - * - * @param context the servlet context */ - protected void initToolbox(ServletContext context) { - /* if we have a toolbox, get a manager for it */ + protected void initToolbox(ServletContext servletContext) { if (StringUtils.isNotBlank(toolBoxLocation)) { - toolboxManager = ServletToolboxManager.getInstance(context, toolBoxLocation); + LOG.info("Configuring Velocity ToolManager with {}", toolBoxLocation); + toolboxManager = new ToolManager(); + toolboxManager.configure(toolBoxLocation); } else { - Velocity.info("VelocityViewServlet: No toolbox entry in configuration."); + LOG.info("Skipping TooManager initialisation, {} was not defined", StrutsConstants.STRUTS_VELOCITY_TOOLBOXLOCATION); } } @@ -411,7 +405,6 @@ public class VelocityManager { * * * @param context the current ServletContext. may not be null - * * @return the new velocity engine */ protected VelocityEngine newVelocityEngine(ServletContext context) { @@ -428,7 +421,7 @@ public class VelocityManager { // Set the velocity attribute for the servlet context // if this is not set the webapp loader WILL NOT WORK velocityEngine.setApplicationAttribute(ServletContext.class.getName(), - context); + context); try { velocityEngine.init(p); @@ -447,21 +440,18 @@ public class VelocityManager { *
  • we need to define the various Struts custom user directives such as #param, #tag, and #bodytag
  • * * - * @param context the servlet context + * @param context the servlet context * @param properties velocity properties */ private void applyDefaultConfiguration(ServletContext context, Properties properties) { // ensure that caching isn't overly aggressive - /** - * Load a default resource loader definition if there isn't one present. - * Ben Hall (22/08/2003) - */ + LOG.debug("Load a default resource loader definition if there isn't one present."); if (properties.getProperty(Velocity.RESOURCE_LOADER) == null) { properties.setProperty(Velocity.RESOURCE_LOADER, "strutsfile, strutsclass"); } - /** + /* * If there's a "real" path add it for the strutsfile resource loader. * If there's no real path and they haven't configured a loader then we change * resource loader property to just use the strutsclass loader @@ -487,11 +477,10 @@ public class VelocityManager { properties.setProperty(Velocity.RESOURCE_LOADER, prop); } - /** + /* * Refactored the Velocity templates for the Struts taglib into the classpath from the web path. This will * enable Struts projects to have access to the templates by simply including the Struts jar file. * Unfortunately, there does not appear to be a macro for the class loader keywords - * Matt Ho - Mon Mar 17 00:21:46 PST 2003 */ properties.setProperty("strutsclass.resource.loader.description", "Velocity Classpath Resource Loader"); properties.setProperty("strutsclass.resource.loader.class", "org.apache.struts2.views.velocity.StrutsResourceLoader"); @@ -502,8 +491,8 @@ public class VelocityManager { StringBuilder sb = new StringBuilder(); for (TagLibraryDirectiveProvider tagLibrary : tagLibraries) { - List directives = tagLibrary.getDirectiveClasses(); - for (Class directive : directives) { + List> directives = tagLibrary.getDirectiveClasses(); + for (Class directive : directives) { addDirective(sb, directive); } } @@ -520,11 +509,11 @@ public class VelocityManager { properties.setProperty("userdirective", userdirective); } - private void addDirective(StringBuilder sb, Class clazz) { + private void addDirective(StringBuilder sb, Class clazz) { sb.append(clazz.getName()).append(","); } - private static final String replace(String string, String oldString, String newString) { + private static String replace(String string, String oldString, String newString) { if (string == null) { return null; } diff --git a/core/src/main/java/org/apache/struts2/util/VelocityStrutsUtil.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityStrutsUtil.java similarity index 95% rename from core/src/main/java/org/apache/struts2/util/VelocityStrutsUtil.java rename to plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityStrutsUtil.java index 9d53964df..853669226 100644 --- a/core/src/main/java/org/apache/struts2/util/VelocityStrutsUtil.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityStrutsUtil.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.struts2.util; +package org.apache.struts2.views.velocity; import java.io.CharArrayWriter; import java.io.IOException; @@ -24,6 +24,7 @@ import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.struts2.util.StrutsUtil; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.context.Context; import org.apache.velocity.exception.MethodInvocationException; diff --git a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityTagLibrary.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityTagLibrary.java index 6135164b2..f4d37d02d 100644 --- a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityTagLibrary.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityTagLibrary.java @@ -61,8 +61,8 @@ import java.util.List; public class VelocityTagLibrary implements TagLibraryDirectiveProvider { @Override - public List getDirectiveClasses() { - Class[] directives = new Class[] { + public List> getDirectiveClasses() { + Class[] directives = new Class[] { ActionDirective.class, BeanDirective.class, CheckBoxDirective.class, @@ -106,7 +106,7 @@ public class VelocityTagLibrary implements TagLibraryDirectiveProvider { * @deprecated please use {#getDirectiveClasses} */ @Deprecated() - public List getVelocityDirectiveClasses() { + public List> getVelocityDirectiveClasses() { return getDirectiveClasses(); } diff --git a/pom.xml b/pom.xml index 8b42a1749..3500e00a6 100644 --- a/pom.xml +++ b/pom.xml @@ -734,28 +734,20 @@ org.apache.velocity - velocity - 1.7 + velocity-engine-core + 2.1 - org.apache.velocity - velocity-tools - 2.0 - - - javax.servlet - servlet-api - - - struts - struts - - - velocity - velocity - - + org.apache.velocity.tools + velocity-tools-view + 3.0 + + + + org.apache.velocity.tools + velocity-tools-view-jsp + 3.0