You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2016/06/02 17:22:12 UTC
svn commit: r1746606 - in /velocity/tools/trunk:
velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/
velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/
velocity-tools-view/src/main/java/org/apache/velocity/tools/...
Author: cbrisson
Date: Thu Jun 2 17:22:12 2016
New Revision: 1746606
URL: http://svn.apache.org/viewvc?rev=1746606&view=rev
Log:
revert changes inadvertendly commited in java sources
Added:
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/OldXmlFactoryConfigurationRuleSet.java
- copied unchanged from r1746582, velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/OldXmlFactoryConfigurationRuleSet.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/DataInfo.java
- copied unchanged from r1746582, velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/DataInfo.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ToolboxManager.java
- copied unchanged from r1746582, velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ToolboxManager.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ToolboxRuleSet.java
- copied unchanged from r1746582, velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ToolboxRuleSet.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolInfo.java
- copied unchanged from r1746582, velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolInfo.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/XMLToolboxManager.java
- copied unchanged from r1746582, velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/XMLToolboxManager.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/context/
- copied from r1746582, velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/context/
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/servlet/
- copied from r1746582, velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/servlet/
Modified:
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java
Modified: velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java?rev=1746606&r1=1746605&r2=1746606&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java (original)
+++ velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java Thu Jun 2 17:22:12 2016
@@ -19,15 +19,14 @@ package org.apache.velocity.tools.generi
* under the License.
*/
-import java.net.URLDecoder;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
import org.apache.velocity.shaded.commons.lang3.StringEscapeUtils;
import org.apache.velocity.tools.config.DefaultKey;
/**
- * Tool for working with escaping and unescaping in Velocity templates.
- * It provides methods to escape or unescape outputs for Velocity, Java, JavaScript, HTML, HTTP, XML and SQL.
+ * Tool for working with escaping in Velocity templates.
+ * It provides methods to escape outputs for Velocity, Java, JavaScript, HTML, HTTP, XML and SQL.
* Also provides methods to render VTL characters that otherwise needs escaping.
*
* <p><pre>
@@ -37,27 +36,21 @@ import org.apache.velocity.tools.config.
*
* $java -> He didn't say, "Stop!"
* $esc.java($java) -> He didn't say, \"Stop!\"
- * $esc.unjava($esc($java)) -> He didn't say, "Stop!"
*
* $javascript -> He didn't say, "Stop!"
* $esc.javascript($javascript) -> He didn\'t say, \"Stop!\"
- * $esc.unjavascript($esc.javascript($javascript)) -> He didn't say, "Stop!"
*
* $html -> "bread" & "butter"
* $esc.html($html) -> &quot;bread&quot; &amp; &quot;butter&quot;
- * $esc.unhtml($esc.html($html)) -> "bread" & "butter"
*
* $xml -> "bread" & "butter"
* $esc.xml($xml) -> &quot;bread&quot; &amp; &quot;butter&quot;
- * $esc.unxml($esc.xml($xml)) -> "bread" & "butter"
*
* $sql -> McHale's Navy
* $esc.sql($sql) -> McHale''s Navy
- * $esc.sql($esc.unsql($sqlà)) -> McHale''s Navy
*
* $url -> hello here & there
- * $esc.url($url) -> hello+here+%26+there
- * $esc.unurl($esc.url($url)) -> hello+here+%26+there
+ * $esc.url -> hello+here+%26+there
*
* $esc.dollar -> $
* $esc.d -> $
@@ -197,27 +190,6 @@ public class EscapeTool extends SafeConf
}
return StringEscapeUtils.escapeJava(String.valueOf(string));
}
-
- /**
- * Unescapes the characters in a <code>String</code> using Java String rules.
- * <br />
- * Delegates the process to {@link StringEscapeUtils#unescapeJava(String)}.
- *
- * @param string the string to unescape values, may be null
- * @return String with unescaped values, <code>null</code> if null string input
- *
- * @see StringEscapeUtils#unescapeJava(String)
- * @since VelocityTools 3.0
- */
- public String unjava(Object string)
- {
- if (string == null)
- {
- return null;
- }
- return StringEscapeUtils.unescapeJava(String.valueOf(string));
- }
-
/**
* Escapes the characters in a <code>String</code> using java.util.Properties rules for escaping property keys.
@@ -300,12 +272,12 @@ public class EscapeTool extends SafeConf
/**
* Escapes the characters in a <code>String</code> using JavaScript String rules.
* <br />
- * Delegates the process to {@link StringEscapeUtils#escapeEcmaScript(String)}.
+ * Delegates the process to {@link StringEscapeUtils#escapeJavaScript(String)}.
*
* @param string the string to escape values, may be null
* @return String with escaped values, <code>null</code> if null string input
*
- * @see StringEscapeUtils#escapeEcmaScript(String)
+ * @see StringEscapeUtils#escapeJavaScript(String)
*/
public String javascript(Object string)
{
@@ -317,29 +289,9 @@ public class EscapeTool extends SafeConf
}
/**
- * Unescapes the characters in a <code>String</code> using JavaScript String rules.
- * <br />
- * Delegates the process to {@link StringEscapeUtils#unescapeEcmaScript(String)}.
- *
- * @param string the string to unescape, may be null
- * @return unescaped String, <code>null</code> if null string input
- *
- * @see StringEscapeUtils#unescapeEcmaScript(String)
- * @since VelocityTools 3.0
- */
- public String unjavascript(Object string)
- {
- if (string == null)
- {
- return null;
- }
- return StringEscapeUtils.unescapeEcmaScript(String.valueOf(string));
- }
-
- /**
* Escapes the characters in a <code>String</code> using HTML entities.
* <br />
- * Delegates the process to {@link StringEscapeUtils#escapeHtml4(String)}.
+ * Delegates the process to {@link StringEscapeUtils#escapeHtml(String)}.
*
* @param string the string to escape, may be null
* @return a new escaped <code>String</code>, <code>null</code> if null string input
@@ -356,26 +308,6 @@ public class EscapeTool extends SafeConf
}
/**
- * Unescapes the characters in a <code>String</code> encoded with HTML entities.
- * <br />
- * Delegates the process to {@link StringEscapeUtils#unescapeHtml4(String)}.
- *
- * @param string the string to unescape, may be null
- * @return a new unescaped <code>String</code>, <code>null</code> if null string input
- *
- * @see StringEscapeUtils#unescapeHtml4(String)
- * @since VelocityTools 3.0
- */
- public String unhtml(Object string)
- {
- if (string == null)
- {
- return null;
- }
- return StringEscapeUtils.unescapeHtml4(String.valueOf(string));
- }
-
- /**
* Escape the characters in a <code>String</code> to be suitable to use as an HTTP parameter value.
* <br/>
* Uses UTF-8 as default character encoding.
@@ -397,27 +329,6 @@ public class EscapeTool extends SafeConf
}
/**
- * Unscape the characters in a <code>String</code> encoded as an HTTP parameter value.
- * <br/>
- * Uses UTF-8 as default character encoding.
- * @param string the string to unescape, may be null
- * @return a new unescaped <code>String</code>, <code>null</code> if null string input
- *
- * @see java.net.URLDecoder#decode(String,String).
- * @since VelocityTools 3.0
- */
- public String unurl(Object string) {
- if (string == null) {
- return null;
- }
- try {
- return URLDecoder.decode(String.valueOf(string),"UTF-8");
- } catch(UnsupportedEncodingException uee) {
- return null;
- }
- }
-
- /**
* Escapes the characters in a <code>String</code> using XML entities.
* <br />
* Delegates the process to {@link StringEscapeUtils#escapeXml(String)}.
@@ -437,56 +348,19 @@ public class EscapeTool extends SafeConf
}
/**
- * Unescapes the characters in a <code>String</code> encoded with XML entities.
- * <br />
- * Delegates the process to {@link StringEscapeUtils#escapeXml(String)}.
- *
- * @param string the string to unescape, may be null
- * @return a new unescaped <code>String</code>, <code>null</code> if null string input
- *
- * @see StringEscapeUtils#unescapeXml(String)
- * @since VelocityTools 3.0
- */
- public String unxml(Object string)
- {
- if (string == null)
- {
- return null;
- }
- return StringEscapeUtils.unescapeXml(String.valueOf(string));
- }
-
- /**
* Escapes the characters in a <code>String</code> to be suitable to pass to an SQL query.
* <br />
+ * Delegates the process to {@link StringEscapeUtils#escapeSql(String)}.
*
* @param string the string to escape, may be null
* @return a new String, escaped for SQL, <code>null</code> if null string input
*
+ * @see StringEscapeUtils#escapeSql(String)
*/
public String sql(Object string)
{
if (string == null)
{
- return null;
- }
- return String.valueOf(string).replace("'", "''");
- }
-
- /**
- * Unescapes the characters in a <code>String</code> already encoded for use in a SQL query.
- * <br />
- * Delegates the process to {@link StringEscapeUtils#escapeSql(String)}.
- *
- * @param string the string to escape, may be null
- * @return a new unescaped String, <code>null</code> if null string input
- * @since VelocityTools 3.0
- *
- */
- public String unsql(Object string)
- {
- if (string == null)
- {
return null;
}
return String.valueOf(string).replace("'", "''");
Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ServletUtils.java?rev=1746606&r1=1746605&r2=1746606&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ServletUtils.java (original)
+++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ServletUtils.java Thu Jun 2 17:22:12 2016
@@ -25,8 +25,6 @@ import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
-import java.util.HashMap;
-import java.util.Map;
import javax.servlet.FilterConfig;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
@@ -349,6 +347,13 @@ public class ServletUtils
public static FactoryConfiguration getConfiguration(String path,
ServletContext application)
{
+ return getConfiguration(path, application, path.endsWith("toolbox.xml"));
+ }
+
+ public static FactoryConfiguration getConfiguration(String path,
+ ServletContext application,
+ boolean deprecationSupportMode)
+ {
// first make sure we can even get such a file
InputStream inputStream = getInputStream(path, application);
if (inputStream == null)
@@ -358,10 +363,10 @@ public class ServletUtils
// then make sure it's a file type we recognize
FileFactoryConfiguration config = null;
- String source = "ServletUtils.getConfiguration("+path+")";
+ String source = "ServletUtils.getConfiguration("+path+",ServletContext[,depMode="+deprecationSupportMode+"])";
if (path.endsWith(".xml"))
{
- config = new XmlFactoryConfiguration(source);
+ config = new XmlFactoryConfiguration(deprecationSupportMode, source);
}
else if (path.endsWith(".properties"))
{
@@ -433,40 +438,4 @@ public class ServletUtils
{
}
- private static Map<String,String> mimeTypesMap = null;
-
- static
- {
- // limit ourselves to text mime types, with "vtl" and "vhtml" extensions
- mimeTypesMap = new HashMap<String,String>();
- mimeTypesMap.put("cal", "text/calendar");
- mimeTypesMap.put("css", "text/css");
- mimeTypesMap.put("csv", "text/csv");
- mimeTypesMap.put("html", "text/html");
- mimeTypesMap.put("html4", "text/html");
- mimeTypesMap.put("html5", "text/html");
- mimeTypesMap.put("json", "application/json");
- mimeTypesMap.put("js", "text/javascript");
- mimeTypesMap.put("jsp", "text/html");
- mimeTypesMap.put("md", "text/markdown");
- mimeTypesMap.put("php", "text/html");
- mimeTypesMap.put("ps", "application/postscript");
- mimeTypesMap.put("rss", "application/rss+xml");
- mimeTypesMap.put("rtf", "text/rtf");
- mimeTypesMap.put("sgml", "text/sgml");
- mimeTypesMap.put("svg", "image/svg+xml");
- mimeTypesMap.put("tsv", "text/tab-separated-values");
- mimeTypesMap.put("txt", "text/plain");
- mimeTypesMap.put("vhtml", "text/html");
- mimeTypesMap.put("vtl", "text/html");
- mimeTypesMap.put("xhtml", "text/html");
- mimeTypesMap.put("xml", "text/xml");
- mimeTypesMap.put("xslt", "application/xstl+xml");
- }
-
- public static String getMimeTypeFromExtension(String extension)
- {
- return mimeTypesMap.get(extension);
- }
-
}
Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java?rev=1746606&r1=1746605&r2=1746606&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java (original)
+++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java Thu Jun 2 17:22:12 2016
@@ -86,7 +86,6 @@ import org.apache.velocity.util.SimplePo
* @author <a href="mailto:kjohnson@transparent.com">Kent Johnson</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @author Nathan Bubna
- * @author <a href="mailto:cbrisson@apache.org">Claude Brisson</a>
*
* @version $Id: VelocityView.java 511959 2007-02-26 19:24:39Z nbubna $
*/
@@ -106,7 +105,7 @@ public class VelocityView extends ViewTo
public static final String DEFAULT_CONTENT_TYPE = "text/html";
/** Default encoding for the output stream */
- public static final String DEFAULT_OUTPUT_ENCODING = "UTF-8";
+ public static final String DEFAULT_OUTPUT_ENCODING = "ISO-8859-1";
/**
* Key used to access the toolbox configuration file path from the
@@ -115,6 +114,9 @@ public class VelocityView extends ViewTo
* placed in the ServletContext attributes.
*/
public static final String TOOLS_KEY = ServletUtils.CONFIGURATION_KEY;
+ @Deprecated
+ public static final String DEPRECATED_TOOLS_KEY =
+ "org.apache.velocity.toolbox";
/**
* Default toolbox configuration file path. If no alternate value for
@@ -122,6 +124,9 @@ public class VelocityView extends ViewTo
*/
public static final String USER_TOOLS_PATH =
"/WEB-INF/tools.xml";
+ @Deprecated
+ public static final String DEPRECATED_USER_TOOLS_PATH =
+ "/WEB-INF/toolbox.xml";
/**
* Default Runtime properties.
@@ -145,9 +150,14 @@ public class VelocityView extends ViewTo
/**
* Controls loading of available default tool configurations
- * provided by VelocityTools. The default tools will be loaded
- * automatically unless you explicitly set this property to {@code false}
- * in your init params.
+ * provided by VelocityTools. The default behavior is conditional;
+ * if {@link #DEPRECATION_SUPPORT_MODE_KEY} has not been set to
+ * {@code false} and there is an old {@code toolbox.xml} configuration
+ * present, then the defaults will not be loaded unless you explicitly
+ * set this property to {@code true} in your init params. If there
+ * is no {@code toolbox.xml} and/or the deprecation support is turned off,
+ * then the default tools will be loaded automatically unless you
+ * explicitly set this property to {@code false} in your init params.
*/
public static final String LOAD_DEFAULTS_KEY =
"org.apache.velocity.tools.loadDefaults";
@@ -168,8 +178,18 @@ public class VelocityView extends ViewTo
public static final String USER_OVERWRITE_KEY =
"org.apache.velocity.tools.userCanOverwriteTools";
+ /**
+ * Controls support for deprecated tools and configuration.
+ * The default is {@code true}; set to {@code false} to turn off
+ * support for deprecated tools and configuration.
+ */
+ public static final String DEPRECATION_SUPPORT_MODE_KEY =
+ "org.apache.velocity.tools.deprecationSupportMode";
+
+
private static SimplePool writerPool = new SimplePool(40);
private String defaultContentType = DEFAULT_CONTENT_TYPE;
+ private boolean deprecationSupportMode = true;
public VelocityView(ServletConfig config)
{
@@ -194,6 +214,16 @@ public class VelocityView extends ViewTo
init(config);
}
+ @Deprecated
+ protected final void setDeprecationSupportMode(boolean support)
+ {
+ if (deprecationSupportMode != support)
+ {
+ this.deprecationSupportMode = support;
+ debug("deprecationSupportMode is now %s", (support ? "on" : "off"));
+ }
+ }
+
/**
* Overrides super class to ensure engine is not set to null.
*/
@@ -265,6 +295,12 @@ public class VelocityView extends ViewTo
this.velocity = new VelocityEngine();
}
+ // default is true for these, so just watch for false
+ String depMode = config.findInitParameter(DEPRECATION_SUPPORT_MODE_KEY);
+ if (depMode != null && depMode.equalsIgnoreCase("false"))
+ {
+ setDeprecationSupportMode(false);
+ }
String allowOverwrite = config.findInitParameter(USER_OVERWRITE_KEY);
if (allowOverwrite != null && allowOverwrite.equalsIgnoreCase("false"))
{
@@ -374,10 +410,16 @@ public class VelocityView extends ViewTo
/**
* Here's the configuration lookup/loading order:
* <ol>
- * <li>{@link ConfigurationUtils#getDefaultTools()}</li>
+ * <li>If deprecationSupportMode is true:
+ * <ol>
+ * <li>Config file optionally specified by {@code org.apache.velocity.toolbox} init-param (servlet or servletContext)</li>
+ * <li>If none, config file optionally at {@code /WEB-INF/toolbox.xml} (deprecated conventional location)</li>
+ * </ol>
+ * </li>
+ * <li>If no old toolbox or loadDefaults is true, {@link ConfigurationUtils#getDefaultTools()}</li>
* <li>{@link ConfigurationUtils#getAutoLoaded}(false)</li>
* <li>Config file optionally specified by servletContext {@code org.apache.velocity.tools} init-param</li>
- * <li>Config file optionally at {@code /WEB-INF/tools.xml} (conventional location)</li>
+ * <li>Config file optionally at {@code /WEB-INF/tools.xml} (new conventional location)</li>
* <li>Config file optionally specified by servlet {@code org.apache.velocity.tools} init-param</li>
* </ol>
* Remember that as these configurations are added on top of each other,
@@ -391,10 +433,21 @@ public class VelocityView extends ViewTo
{
FactoryConfiguration factoryConfig = new FactoryConfiguration("VelocityView.configure(config,factory)");
+ boolean hasOldToolbox = false;
+ if (this.deprecationSupportMode)
+ {
+ FactoryConfiguration oldToolbox = getDeprecatedConfig(config);
+ if (oldToolbox != null)
+ {
+ hasOldToolbox = true;
+ factoryConfig.addConfiguration(oldToolbox);
+ }
+ }
+
// only load the default tools if they have explicitly said to
// or if they are not using an old toolbox and have said nothing
String loadDefaults = config.findInitParameter(LOAD_DEFAULTS_KEY);
- if ((loadDefaults == null) ||
+ if ((!hasOldToolbox && loadDefaults == null) ||
"true".equalsIgnoreCase(loadDefaults))
{
// add all available default tools
@@ -404,7 +457,10 @@ public class VelocityView extends ViewTo
else
{
// let the user know that the defaults were suppressed
- debug("Default tools configuration has been suppressed.");
+ debug("Default tools configuration has been suppressed%s",
+ (hasOldToolbox ?
+ " to avoid conflicts with older application's context and toolbox definition." :
+ "."));
}
// this gets the auto loaded config from the classpath
@@ -449,6 +505,45 @@ public class VelocityView extends ViewTo
configure(factoryConfig);
}
+ /**
+ * First tries to find a path to a toolbox under the deprecated
+ * {@code org.apache.velocity.toolbox} key.
+ * If found, it tries to load the configuration there and will blow up
+ * if there is no config file there.
+ * If not found, it looks for a config file at /WEB-INF/toolbox.xml
+ * (the deprecated default location) and tries to load it if found.
+ */
+ @Deprecated
+ protected FactoryConfiguration getDeprecatedConfig(JeeConfig config)
+ {
+ FactoryConfiguration toolbox = null;
+
+ // look for specified path under the deprecated toolbox key
+ String oldPath = config.findInitParameter(DEPRECATED_TOOLS_KEY);
+ if (oldPath != null)
+ {
+ // ok, they said the toolbox.xml should be there
+ // so this should blow up if it is not
+ toolbox = getConfiguration(oldPath, true);
+ }
+ else
+ {
+ // check for deprecated user configuration at the old conventional
+ // location. be silent if missing, log deprecation warning otherwise
+ oldPath = DEPRECATED_USER_TOOLS_PATH;
+ toolbox = getConfiguration(oldPath);
+ }
+
+ if (toolbox != null)
+ {
+ debug("Loaded deprecated configuration from: %s", oldPath);
+ getLog().warn("Please upgrade to new \"/WEB-INF/tools.xml\" format and conventional location."+
+ " Support for \"/WEB-INF/toolbox.xml\" format and conventional file name will "+
+ "be removed in a future version.");
+ }
+ return toolbox;
+ }
+
private boolean setConfig(FactoryConfiguration factory, String path, boolean require)
{
if (path == null)
@@ -561,7 +656,8 @@ public class VelocityView extends ViewTo
try
{
config = ServletUtils.getConfiguration(path,
- this.servletContext);
+ this.servletContext,
+ this.deprecationSupportMode);
if (config == null)
{
String msg = "Did not find resource at: "+path;
@@ -603,36 +699,32 @@ public class VelocityView extends ViewTo
String encoding = getProperty(RuntimeConstants.OUTPUT_ENCODING,
DEFAULT_OUTPUT_ENCODING);
- // Ensure that the charset is included in the Content-Type header.
- int index = defaultContentType.lastIndexOf("charset=");
- if (index < 0)
- {
- // the charset specifier is not yet present in header.
- // append character encoding to default content-type
- defaultContentType += "; charset=" + encoding;
- }
- else
+ // For non Latin-1 encodings, ensure that the charset is
+ // included in the Content-Type header.
+ if (!DEFAULT_OUTPUT_ENCODING.equalsIgnoreCase(encoding))
{
- // The user may have configuration issues.
- getLog().info("Charset was already specified in the Content-Type property.");
- // but listen to the last to speak
- defaultContentType = defaultContentType.substring(0, index + 8) + encoding;
+ int index = defaultContentType.lastIndexOf("charset");
+ if (index < 0)
+ {
+ // the charset specifier is not yet present in header.
+ // append character encoding to default content-type
+ this.defaultContentType += "; charset=" + encoding;
+ }
+ else
+ {
+ // The user may have configuration issues.
+ getLog().info("Charset was already " +
+ "specified in the Content-Type property. " +
+ "Output encoding property will be ignored.");
+ }
}
- debug("Default ContentType was changed to %s", defaultContentType);
+ debug("Default Content-Type is: %s", defaultContentType);
}
- /**
- * Returns the configured default encoding (parsed from content type,
- * or UTF-8 by default)
- */
- public String getEncoding()
- {
- int i = this.defaultContentType.lastIndexOf("charset=");
- return i != -1 ?
- this.defaultContentType.substring(i + 8).replace('"',' ').trim() :
- DEFAULT_OUTPUT_ENCODING;
- }
+
+
+
/******************* REQUEST PROCESSING ****************************/
@@ -689,7 +781,14 @@ public class VelocityView extends ViewTo
HttpServletResponse response)
{
ViewToolContext ctx;
- ctx = new ViewToolContext(velocity, request, response, servletContext);
+ if (this.deprecationSupportMode)
+ {
+ ctx = new ChainedContext(velocity, request, response, servletContext);
+ }
+ else
+ {
+ ctx = new ViewToolContext(velocity, request, response, servletContext);
+ }
prepareContext(ctx, request);
return ctx;
}
@@ -708,6 +807,29 @@ public class VelocityView extends ViewTo
}
/**
+ * <p>Gets the requested template.</p>
+ *
+ * @param request client request
+ * @param response client response.
+ * @return Velocity Template object or null
+ * @deprecated Use {@link #getTemplate(HttpServletRequest)}.
+ */
+ public Template getTemplate(HttpServletRequest request,
+ HttpServletResponse response)
+ {
+ String path = ServletUtils.getPath(request);
+ if (response == null)
+ {
+ return getTemplate(path);
+ }
+ else
+ {
+ return getTemplate(path, response.getCharacterEncoding());
+ }
+ }
+
+
+ /**
* Retrieves the requested template.
*
* @param name The file name of the template to retrieve relative to the
Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java?rev=1746606&r1=1746605&r2=1746606&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java (original)
+++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java Thu Jun 2 17:22:12 2016
@@ -275,8 +275,7 @@ public class VelocityViewServlet extends
* Sets the content type of the response. This is available to be overriden
* by a derived class.
*
- * <p>The default implementation is to detect content type based on the
- * requested path extension, if any, or otherwise to rely on:
+ * <p>The default implementation is :
* <code>
* response.setContentType(getVelocityView().getDefaultContentType());
* </code>
@@ -290,24 +289,7 @@ public class VelocityViewServlet extends
protected void setContentType(HttpServletRequest request,
HttpServletResponse response)
{
- String contentType = null;
- String uri = request.getRequestURI();
- int dot = uri.lastIndexOf('.');
- if (dot != -1)
- {
- String extension = uri.substring(dot + 1);
- contentType = ServletUtils.getMimeTypeFromExtension(extension);
- }
- if (contentType == null)
- {
- contentType = getVelocityView().getDefaultContentType();
- }
- else
- {
- // append charset
- contentType += ";charset=" + getVelocityView().getEncoding();
- }
- response.setContentType(contentType);
+ response.setContentType(getVelocityView().getDefaultContentType());
}
protected Template getTemplate(HttpServletRequest request,
Modified: velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java?rev=1746606&r1=1746605&r2=1746606&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java (original)
+++ velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java Thu Jun 2 17:22:12 2016
@@ -61,6 +61,7 @@ public class VelocityViewTest
Context context = createMock(Context.class);
expect(config.getServletContext()).andReturn(servletContext);
+ expect(config.findInitParameter(VelocityView.DEPRECATION_SUPPORT_MODE_KEY)).andReturn("false");
expect(config.findInitParameter(VelocityView.USER_OVERWRITE_KEY)).andReturn(null);
expect(config.findInitParameter(VelocityView.LOAD_DEFAULTS_KEY)).andReturn("false");
expect(servletContext.getInitParameter(VelocityView.PROPERTIES_KEY)).andReturn(null);