WW-5047 Upgrades to VelocityEngine 2.1 and VelocityTools 3.0

This commit is contained in:
Lukasz Lenart
2020-02-01 12:47:52 +01:00
parent 642b945995
commit 71d4a7be3b
16 changed files with 93 additions and 148 deletions
-11
View File
@@ -122,17 +122,6 @@
<version>${log4j2.version}</version>
</dependency>
<!-- Velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
</dependency>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
+2 -8
View File
@@ -34,14 +34,8 @@
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<optional>true</optional>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-velocity-plugin</artifactId>
</dependency>
<dependency>
-13
View File
@@ -203,19 +203,6 @@
<artifactId>ognl</artifactId>
</dependency>
<!-- Velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<optional>true</optional>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
@@ -36,6 +36,6 @@ public interface TagLibraryDirectiveProvider {
*
* @return A list of Velocity directive classes
*/
List<Class> getDirectiveClasses();
List<Class<?>> getDirectiveClasses();
}
@@ -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<String, Object> getStandardContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
HashMap<String, Object> 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);
@@ -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;
+4
View File
@@ -36,6 +36,10 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-velocity-plugin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
@@ -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" %>
<html>
-13
View File
@@ -70,19 +70,6 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- Velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<optional>true</optional>
</dependency>
<!-- Portlet -->
<dependency>
<groupId>javax.portlet</groupId>
@@ -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.
+8 -3
View File
@@ -36,12 +36,17 @@
<!-- Velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<artifactId>velocity-engine-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<groupId>org.apache.velocity.tools</groupId>
<artifactId>velocity-tools-view</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity.tools</groupId>
<artifactId>velocity-tools-view-jsp</artifactId>
</dependency>
<dependency>
@@ -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<VelocityContext> chainedContexts;
public StrutsVelocityContext(ValueStack stack) {
this(null, stack);
}
public StrutsVelocityContext(VelocityContext[] chainedContexts, ValueStack stack) {
public StrutsVelocityContext(List<VelocityContext> 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;
}
@@ -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 <b>all</b> 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 {
* </ul>
*
* @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<VelocityContext> 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<String, Object> standardMap = ContextUtil.getStandardContext(stack, req, res);
for (Map.Entry<String, Object> 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<VelocityContext> prepareChainedContexts(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Map<String, Object> extraContext) {
List<VelocityContext> 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 {
* </ul>
*
* @param context the current ServletContext. may <b>not</b> 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 {
* <li>we need to define the various Struts custom user directives such as #param, #tag, and #bodytag</li>
* </ul>
*
* @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<Class> directives = tagLibrary.getDirectiveClasses();
for (Class directive : directives) {
List<Class<?>> 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;
}
@@ -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;
@@ -61,8 +61,8 @@ import java.util.List;
public class VelocityTagLibrary implements TagLibraryDirectiveProvider {
@Override
public List<Class> getDirectiveClasses() {
Class[] directives = new Class[] {
public List<Class<?>> 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<Class> getVelocityDirectiveClasses() {
public List<Class<?>> getVelocityDirectiveClasses() {
return getDirectiveClasses();
}
+11 -19
View File
@@ -734,28 +734,20 @@
<!-- Velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>struts</groupId>
<artifactId>struts</artifactId>
</exclusion>
<exclusion>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
</exclusion>
</exclusions>
<groupId>org.apache.velocity.tools</groupId>
<artifactId>velocity-tools-view</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity.tools</groupId>
<artifactId>velocity-tools-view-jsp</artifactId>
<version>3.0</version>
</dependency>
<dependency>