You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2015/11/29 12:59:28 UTC
[24/25] incubator-freemarker git commit: Reverted earlier change;
FreemarkerServlet ResponseCharacterEncoding init-param uses "force"
prefix again. Updated FreemarkerServlet configuration example to use
FreeMarker's output_encoding setting, also added th
Reverted earlier change; FreemarkerServlet ResponseCharacterEncoding init-param uses "force" prefix again. Updated FreemarkerServlet configuration example to use FreeMarker's output_encoding setting, also added the ftlh and fltx extensions to it. Some other minor JavaDoc adjustments.
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/116b8175
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/116b8175
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/116b8175
Branch: refs/heads/2.3
Commit: 116b817543c369dbc91aae09ea84d20040ef26f5
Parents: 5d1a411
Author: ddekany <dd...@apache.org>
Authored: Sat Nov 28 22:47:47 2015 +0100
Committer: ddekany <dd...@apache.org>
Committed: Sat Nov 28 22:47:47 2015 +0100
----------------------------------------------------------------------
.../ext/servlet/FreemarkerServlet.java | 77 ++++++--------------
src/manual/book.xml | 35 ++++++---
.../ext/servlet/FreemarkerServletTest.java | 12 +--
3 files changed, 55 insertions(+), 69 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/116b8175/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java b/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
index a7fc2e6..6ad1040 100644
--- a/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
+++ b/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
@@ -20,9 +20,8 @@
package freemarker.ext.servlet;
import java.io.IOException;
+import java.io.Writer;
import java.nio.charset.Charset;
-import java.nio.charset.IllegalCharsetNameException;
-import java.nio.charset.UnsupportedCharsetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -47,7 +46,6 @@ import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.cache.WebappTemplateLoader;
-import freemarker.core.BugException;
import freemarker.core.Configurable;
import freemarker.core.Environment;
import freemarker.core.OutputFormat;
@@ -214,10 +212,10 @@ import freemarker.template.utility.StringUtil;
* <li>{@value #INIT_PARAM_VALUE_DO_NOT_SET}: {@link FreemarkerServlet} will not set the {@link HttpServletResponse}
* "character encoding". It will still call {@link Environment#setOutputEncoding(String)}, so that the running template
* will be aware of the charset used for the output.
- * <li>A charset name, for example {@code UTF-8}: The output charset will the specified charset, regardless of
- * everything. The charset specified this way is passed to {@link HttpServletResponse#setCharacterEncoding(String)} and
- * {@link Environment#setOutputEncoding(String)}. If the charset name is not recognized by Java, the servlet
- * initialization will fail.
+ * <li>{@value #INIT_PARAM_VALUE_FORCE_PREFIX} + charset name, for example {@code force UTF-8}: The output charset will
+ * be the one specified after "force" + space, regardless of everything. The charset specified this way is passed to
+ * {@link HttpServletResponse#setCharacterEncoding(String)} and {@link Environment#setOutputEncoding(String)}. If the
+ * charset name is not recognized by Java, the servlet initialization will fail.
* </ul>
*
* <li><strong>{@value #INIT_PARAM_BUFFER_SIZE}</strong>: Sets the size of the output buffer in bytes, or if "KB" or
@@ -266,7 +264,8 @@ import freemarker.template.utility.StringUtil;
* as {@code object_wrapper}.
*
* <li><strong>Any other init-params</strong> will be interpreted as {@link Configuration}-level FreeMarker setting. See
- * the possible names and values at {@link Configuration#setSetting(String, String)}.</li>
+ * the possible names and values at {@link Configuration#setSetting(String, String)}. Note that these init-param names
+ * are starting with lower-case letter (upper-case init-params are used for FreemarkerSerlvet settings).</li>
*
* </ul>
*
@@ -420,6 +419,7 @@ public class FreemarkerServlet extends HttpServlet {
public static final String INIT_PARAM_VALUE_FROM_TEMPLATE = "fromTemplate";
public static final String INIT_PARAM_VALUE_LEGACY = "legacy";
public static final String INIT_PARAM_VALUE_DO_NOT_SET = "doNotSet";
+ public static final String INIT_PARAM_VALUE_FORCE_PREFIX = "force ";
/**
* When set, the items defined in it will be added after those coming from the
@@ -670,20 +670,9 @@ public class FreemarkerServlet extends HttpServlet {
overrideResponseContentType = initParamValueToEnum(value, OverrideResponseContentType.values());
} else if (name.equals(INIT_PARAM_RESPONSE_CHARACTER_ENCODING)) {
responseCharacterEncoding = initParamValueToEnum(value, ResponseCharacterEncoding.values());
- if (responseCharacterEncoding == ResponseCharacterEncoding.SPECIFIC_CHARSET) {
- try {
- forcedResponseCharacterEncoding = Charset.forName(value);
- } catch (IllegalArgumentException e) {
- if (!(e instanceof UnsupportedCharsetException
- || e instanceof IllegalCharsetNameException)) {
- throw e;
- }
- StringBuilder sb = new StringBuilder();
- sb.append(StringUtil.jQuote(value));
- sb.append(" isn't a valid charset name, nor it is any of the predefined values: ");
- appendEnumInitParamValues(sb, ResponseCharacterEncoding.values());
- throw new IllegalArgumentException(sb.toString());
- }
+ if (responseCharacterEncoding == ResponseCharacterEncoding.FORCE_CHARSET) {
+ String charsetName = value.substring(INIT_PARAM_VALUE_FORCE_PREFIX.length()).trim();
+ forcedResponseCharacterEncoding = Charset.forName(charsetName);
}
} else if (name.equals(INIT_PARAM_OVERRIDE_RESPONSE_LOCALE)) {
overrideResponseLocale = initParamValueToEnum(value, OverrideResponseLocale.values());
@@ -859,7 +848,7 @@ public class FreemarkerServlet extends HttpServlet {
if (responseCharacterEncoding != ResponseCharacterEncoding.LEGACY
&& responseCharacterEncoding != ResponseCharacterEncoding.DO_NOT_SET) {
// Using the Servlet 2.4 way of setting character encoding.
- if (responseCharacterEncoding != ResponseCharacterEncoding.SPECIFIC_CHARSET) {
+ if (responseCharacterEncoding != ResponseCharacterEncoding.FORCE_CHARSET) {
if (!tempSpecContentTypeContainsCharset) {
response.setCharacterEncoding(getTemplateSpecificOutputEncoding(template));
}
@@ -1005,10 +994,6 @@ public class FreemarkerServlet extends HttpServlet {
return config.getLocale();
}
- /**
- * Creates the data-model that will be passed to {@link Template#process(Object, java.io.Writer)}.
- * The default implementation in {@link FreemarkerServlet} returns a {@link AllHttpScopesHashModel}.
- */
protected TemplateModel createModel(ObjectWrapper objectWrapper,
ServletContext servletContext,
final HttpServletRequest request,
@@ -1415,7 +1400,7 @@ public class FreemarkerServlet extends HttpServlet {
}
/**
- * Called before the execution is passed to {@link Template#template.process(Object, java.io.Writer)}. This is a
+ * Called before the execution is passed to {@link Template#process(Object, java.io.Writer)}. This is a
* generic hook you might use in subclasses to perform a specific action before the template is processed.
*
* @param request
@@ -1423,7 +1408,7 @@ public class FreemarkerServlet extends HttpServlet {
* @param response
* The HTTP response. The HTTP headers are already initialized here, such as the {@code conteType} and
* the {@code responseCharacterEncoding} are already set, but you can do the final adjustments here. The
- * response {@linker Writer} isn't created yet, so changing HTTP headers and buffering parameters works.
+ * response {@link Writer} isn't created yet, so changing HTTP headers and buffering parameters works.
* @param template
* The template that will get executed
* @param model
@@ -1606,42 +1591,28 @@ public class FreemarkerServlet extends HttpServlet {
}
private <T extends InitParamValueEnum> T initParamValueToEnum(String initParamValue, T[] enumValues) {
- T wildcardEnumValue = null;
for (T enumValue : enumValues) {
String enumInitParamValue = enumValue.getInitParamValue();
- if (enumInitParamValue == null) {
- if (wildcardEnumValue != null) {
- throw new BugException();
- }
- wildcardEnumValue = enumValue;
- } else if (initParamValue.equals(enumInitParamValue)) {
+ if (initParamValue.equals(enumInitParamValue)
+ || enumInitParamValue.endsWith("}") && initParamValue.startsWith(
+ enumInitParamValue.substring(0, enumInitParamValue.indexOf("${")))) {
return enumValue;
}
}
- if (wildcardEnumValue != null) {
- return wildcardEnumValue;
- }
StringBuilder sb = new StringBuilder();
sb.append(StringUtil.jQuote(initParamValue));
sb.append(" is not a one of the enumeration values: ");
- appendEnumInitParamValues(sb, enumValues);
- throw new IllegalArgumentException(sb.toString());
- }
-
- protected <T extends InitParamValueEnum> void appendEnumInitParamValues(StringBuilder sb, T[] enumValues) {
boolean first = true;
for (T value : enumValues) {
- String initParamValue = value.getInitParamValue();
- if (initParamValue != null) { // Not a wildcard enum
- if (!first) {
- sb.append(", ");
- } else {
- first = false;
- }
- sb.append(StringUtil.jQuote(initParamValue));
+ if (!first) {
+ sb.append(", ");
+ } else {
+ first = false;
}
+ sb.append(StringUtil.jQuote(value.getInitParamValue()));
}
+ throw new IllegalArgumentException(sb.toString());
}
/**
@@ -1674,7 +1645,7 @@ public class FreemarkerServlet extends HttpServlet {
LEGACY(INIT_PARAM_VALUE_LEGACY),
FROM_TEMPLATE(INIT_PARAM_VALUE_FROM_TEMPLATE),
DO_NOT_SET(INIT_PARAM_VALUE_DO_NOT_SET),
- SPECIFIC_CHARSET(null);
+ FORCE_CHARSET(INIT_PARAM_VALUE_FORCE_PREFIX + "${charsetName}");
private final String initParamValue;
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/116b8175/src/manual/book.xml
----------------------------------------------------------------------
diff --git a/src/manual/book.xml b/src/manual/book.xml
index 05e2738..a35f8d2 100644
--- a/src/manual/book.xml
+++ b/src/manual/book.xml
@@ -10431,20 +10431,24 @@ TemplateHashModel roundingModeEnums =
<param-value>true</param-value>
</init-param>
<init-param>
- <param-name>ContentType</param-name>
- <param-value>text/html</param-value>
+ <param-name>ResponseCharacterEncoding</param-name>
+ <!-- Use the output_encoding setting of FreeMarker: -->
+ <param-value>fromTemplate</param-value>
</init-param>
<init-param>
- <param-name>ResponseCharacterEncoding</param-name>
- <param-value>UTF-8</param-value>
+ <param-name>ExceptionOnMissingTemplate</param-name>
+ <!-- true => HTTP 500 on missing template, instead of HTTP 404. -->
+ <param-value>true</param-value>
</init-param>
-
+
<!-- FreeMarker engine settings: -->
<init-param>
<param-name>incompatible_improvements</param-name>
<param-value>2.3.24</param-value>
- <!-- Recommended to set to a high value. See:
- http://freemarker.org/docs/pgui_config_incompatible_improvements.html
+ <!--
+ Recommended to set to a high value.
+ See: http://freemarker.org/docs/pgui_config_incompatible_improvements.html
+ -->
</init-param>
<init-param>
<param-name>template_exception_handler</param-name>
@@ -10458,7 +10462,13 @@ TemplateHashModel roundingModeEnums =
</init-param>
<init-param>
<param-name>default_encoding</param-name>
- <!-- The encoding of the template files. -->
+ <!-- The encoding of the template files: -->
+ <param-value>UTF-8</param-value>
+ </init-param>
+ <init-param>
+ <param-name>output_encoding</param-name>
+ <!-- The encoding of the template output; Note that you must set
+ "ResponseCharacterEncodring" to "fromTemplate" for this to work! -->
<param-value>UTF-8</param-value>
</init-param>
<init-param>
@@ -10477,6 +10487,9 @@ TemplateHashModel roundingModeEnums =
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern><emphasis>*.ftl</emphasis></url-pattern>
+ <!-- HTML and XML auto-escaped if incompatible_improvements >= 2.3.24: -->
+ <url-pattern><emphasis>*.ftlh</emphasis></url-pattern>
+ <url-pattern><emphasis>*.ftl</emphasis>x</url-pattern>
</servlet-mapping>
<replaceable>...</replaceable>
@@ -10490,6 +10503,8 @@ TemplateHashModel roundingModeEnums =
<web-resource-collection>
<web-resource-name>FreeMarker MVC Views</web-resource-name>
<url-pattern>*.ftl</url-pattern>
+ <url-pattern>*.ftlh</url-pattern>
+ <url-pattern>*.ftlx</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- Nobody is allowed to visit these directly. -->
@@ -26089,8 +26104,8 @@ TemplateModel x = env.getVariable("x"); // get variable x</programlisting>
the <literal>outputEncoding</literal> setting),
<literal>doNotSet</literal> (keeps what the caller has
already set in the <literal>ServletRespone</literal>) and
- any valid charset name (forces a specific output
- charset).</para>
+ <literal>force</literal> followed by a charset name (forces
+ a specific output charset).</para>
</listitem>
</itemizedlist>
</listitem>
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/116b8175/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java b/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
index 6e5ce09..f008280 100644
--- a/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
+++ b/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
@@ -310,23 +310,23 @@ public class FreemarkerServletTest {
assertOutputEncodingEquals(
"UTF-16LE", // <- expected response.characterEncoding
"UTF-16LE", // <- expected env.outputEncoding
- "UTF-16LE", // <- init-param
+ FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param
FOO_FTL);
assertOutputEncodingEquals(
"UTF-16LE", // <- expected response.characterEncoding
"UTF-16LE", // <- expected env.outputEncoding
- "UTF-16LE", // <- init-param
+ FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param
FOO_SRC_UTF8_FTL);
assertOutputEncodingEquals(
"UTF-16LE", // <- expected response.characterEncoding
"UTF-16LE", // <- expected env.outputEncoding
- "UTF-16LE", // <- init-param
+ FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param
FOO_OUT_UTF8_FTL);
try {
assertOutputEncodingEquals(
null, // <- expected response.characterEncoding
null, // <- expected env.outputEncoding
- "noSuchCharset", // <- init-param
+ FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "noSuchCharset", // <- init-param
FOO_FTL);
fail();
} catch (ServletException e) {
@@ -337,7 +337,7 @@ public class FreemarkerServletTest {
assertOutputEncodingEquals(
"UTF-16LE", // <- expected response.characterEncoding
"UTF-16LE", // <- expected env.outputEncoding
- "UTF-16LE", // <- init-param
+ FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param
"text/html; charset=ISO-8859-2", // ContentType init-param
FOO_FTL);
fail();
@@ -348,7 +348,7 @@ public class FreemarkerServletTest {
assertOutputEncodingEquals(
"UTF-16LE", // <- expected response.characterEncoding
"UTF-16LE", // <- expected env.outputEncoding
- "UTF-16LE", // <- init-param
+ FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param
CONTENT_TYPE_ATTR_WITH_CHARSET_FTL);
}