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:14 UTC
[10/25] incubator-freemarker git commit: FREEMARKER-2 adding
'OverrideResponseLocale' init param option in FreemarkerServlet
FREEMARKER-2 adding 'OverrideResponseLocale' init param option in
FreemarkerServlet
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/1e4c64f1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/1e4c64f1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/1e4c64f1
Branch: refs/heads/2.3
Commit: 1e4c64f1c0230ec9f6ddc70fed6e142d3b8199ca
Parents: eef30af
Author: Woonsan Ko <wo...@apache.org>
Authored: Sun Oct 25 23:41:04 2015 +0900
Committer: Woonsan Ko <wo...@apache.org>
Committed: Sun Oct 25 23:41:04 2015 +0900
----------------------------------------------------------------------
.../ext/servlet/FreemarkerServlet.java | 69 ++++++++++----
.../ext/servlet/FreemarkerServletTest.java | 95 ++++++++++++++++++--
2 files changed, 144 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1e4c64f1/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 c344bb4..560c459 100644
--- a/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
+++ b/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
@@ -140,8 +140,9 @@ import freemarker.template.utility.StringUtil;
* when nothing else specifies the MIME type. The things that may specify the MIME type (and hence this init-param is
* ignored), starting with the highest precedence, are:
* <ol>
- * <li>If the {@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE} init-param is {@value #INIT_PARAM_VALUE_NEVER} (it
- * itn't be default), then the value of {@link HttpServletResponse#getContentType()} is if that's non-{@code null}.
+ * <li>If the {@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE} init-param is {@value #INIT_PARAM_VALUE_NEVER} (the
+ * default is {@value #INIT_PARAM_VALUE_ALWAYS}), then the value of {@link HttpServletResponse#getContentType()} is used
+ * if that's non-{@code null}.
* <li>The template's custom attribute name <tt>content_type</tt> in the <tt>attributes</tt> parameter of the
* <tt><#ftl></tt> directive. This is a legacy feature, deprecated by the {@link OutputFormat} mechanism.
* <li>The {@linkplain Template#getOutputFormat() output format of the template}, if that has non-{@code null} MIME-type
@@ -159,15 +160,23 @@ import freemarker.template.utility.StringUtil;
* then the charset (encoding) of the actual template file will appended after it, which, as per the Servlet
* specification, also sets the actual encoding used to write the response body.</li>
*
- * <li><strong>{@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE}</strong> (since 2.3.24): Specifies when should we
- * override the {@code contentType} that's already set (i.e., non-{@code null}) in the {@link HttpServletResponse}. The
- * default is {@value #INIT_PARAM_VALUE_ALWAYS}, which means that we always set the content type. Another possible value
- * is {@value #INIT_PARAM_VALUE_NEVER}, which means that we don't set the content type in the response, unless
- * {@link HttpServletResponse#getContentType()} is {@code null}. The third possible value is
- * {@value #INIT_PARAM_VALUE_WHEN_TEMPLATE_HAS_MIME_TYPE}, which means that we only set the content type if either the
- * template has an associated {@link OutputFormat} with non-{@code null} {@link OutputFormat#getMimeType()}, or it has a
- * custom attribute with name <tt>content_type</tt>, or {@link HttpServletResponse#getContentType()} is {@code null}.
- * Setting this init-param allows you to specify the content type before forwarding to {@link FreemarkerServlet}.</li>
+ * <li><strong>{@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE}</strong> (since 2.3.24): Specifies when we should
+ * override the {@code contentType} that might be already set (i.e., non-{@code null}) in the
+ * {@link HttpServletResponse}. The default is {@value #INIT_PARAM_VALUE_ALWAYS}, which means that we always set the
+ * content type. Another possible value is {@value #INIT_PARAM_VALUE_NEVER}, which means that we don't set the content
+ * type in the response, unless {@link HttpServletResponse#getContentType()} is {@code null}. The third possible value
+ * is {@value #INIT_PARAM_VALUE_WHEN_TEMPLATE_HAS_MIME_TYPE}, which means that we only set the content type if either
+ * the template has an associated {@link OutputFormat} with non-{@code null} {@link OutputFormat#getMimeType()}, or it
+ * has a custom attribute with name <tt>content_type</tt>, or {@link HttpServletResponse#getContentType()} is
+ * {@code null}. Setting this init-param allows you to specify the content type before forwarding to
+ * {@link FreemarkerServlet}.</li>
+ *
+ * <li><strong>{@value #INIT_PARAM_OVERRIDE_RESPONSE_LOCALE}</strong> (since 2.3.24): Specifies when we should
+ * override the template {@code locale} that might be already set (i.e., non-{@code null}) in the {@link HttpServletRequest}.
+ * The default is {@value #INIT_PARAM_VALUE_ALWAYS}, which means that we always deduce the template {@code locale}
+ * by invoking {@link #deduceLocale(String, HttpServletRequest, HttpServletResponse)}. Another possible value is
+ * {@value #INIT_PARAM_VALUE_NEVER}, which means that we don't deduce the template {@code locale}, unless
+ * {@link HttpServletRequest#getLocale()} is {@code null}.
*
* <li><strong>{@value #INIT_PARAM_BUFFER_SIZE}</strong>: Sets the size of the output buffer in bytes, or if "KB" or
* "MB" is written after the number (like {@code <param-value>256 KB</param-value>}) then in kilobytes or megabytes.
@@ -310,7 +319,14 @@ public class FreemarkerServlet extends HttpServlet {
* @since 2.3.24
*/
public static final String INIT_PARAM_RESPONSE_CHARACTER_ENCODING = "ResponseCharacterEncoding";
-
+
+ /**
+ * Init-param name - see the {@link FreemarkerServlet} class documentation about the init-params.
+ *
+ * @since 2.3.24
+ */
+ public static final String INIT_PARAM_OVERRIDE_RESPONSE_LOCALE = "OverrideResponseLocale";
+
/**
* Init-param name - see the {@link FreemarkerServlet} class documentation about the init-params.
*
@@ -462,6 +478,7 @@ public class FreemarkerServlet extends HttpServlet {
private OverrideResponseContentType overrideResponseContentType = OverrideResponseContentType.ALWAYS;
private ResponseCharacterEncoding responseCharacterEncoding = ResponseCharacterEncoding.LEGACY;
private boolean contentTypeContainsCharset;
+ private OverrideResponseLocale overrideResponseLocale = OverrideResponseLocale.ALWAYS;
private List/*<MetaInfTldSource>*/ metaInfTldSources;
private List/*<String>*/ classpathTlds;
@@ -610,6 +627,8 @@ public class FreemarkerServlet extends HttpServlet {
overrideResponseContentType = initParamValueToEnum(value, OverrideResponseContentType.values());
} else if (name.equals(INIT_PARAM_RESPONSE_CHARACTER_ENCODING)) {
responseCharacterEncoding = initParamValueToEnum(value, ResponseCharacterEncoding.values());
+ } else if (name.equals(INIT_PARAM_OVERRIDE_RESPONSE_LOCALE)) {
+ overrideResponseLocale = initParamValueToEnum(value, OverrideResponseLocale.values());
} else if (name.equals(INIT_PARAM_EXCEPTION_ON_MISSING_TEMPLATE)) {
exceptionOnMissingTemplate = StringUtil.getYesNo(value);
} else if (name.equals(INIT_PARAM_META_INF_TLD_LOCATIONS)) {;
@@ -741,8 +760,12 @@ public class FreemarkerServlet extends HttpServlet {
LOG.debug("Requested template " + StringUtil.jQuoteNoXSS(templatePath) + ".");
}
- final Locale locale = deduceLocale(templatePath, request, response);
-
+ Locale locale = request.getLocale();
+
+ if (locale == null || overrideResponseLocale != OverrideResponseLocale.NEVER) {
+ locale = deduceLocale(templatePath, request, response);
+ }
+
final Template template;
try {
template = config.getTemplate(templatePath, locale);
@@ -1510,5 +1533,21 @@ public class FreemarkerServlet extends HttpServlet {
}
}
-
+
+ private enum OverrideResponseLocale implements InitParamValueEnum {
+ ALWAYS(INIT_PARAM_VALUE_ALWAYS),
+ NEVER(INIT_PARAM_VALUE_NEVER);
+
+ private final String initParamValue;
+
+ OverrideResponseLocale(String initParamValue) {
+ this.initParamValue = initParamValue;
+ }
+
+ @Override
+ public String getInitParamValue() {
+ return initParamValue;
+ }
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1e4c64f1/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 5f7e699..f18bbcb 100644
--- a/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
+++ b/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
@@ -22,9 +22,11 @@ import static freemarker.ext.servlet.FreemarkerServlet.*;
import static org.junit.Assert.*;
import java.io.IOException;
+import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
@@ -35,6 +37,8 @@ import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext;
import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.TemplateModel;
public class FreemarkerServletTest {
@@ -128,13 +132,45 @@ public class FreemarkerServletTest {
null, INIT_PARAM_VALUE_NEVER, // <- init-params
"outputFormatHeader.ftl", "application/json"); // <- request
}
-
+
+ @Test
+ public void testResponseLocaleInitParams() throws Exception {
+ // By default, the Configurable.locale is set to Locale.getDefault().
+ final Locale defaultLocale = Locale.getDefault();
+
+ assertTemplateLocaleEquals(
+ defaultLocale, // <- expected template locale
+ null, // <- request locale
+ null, // <- init-param
+ "foo.ftl");
+ assertTemplateLocaleEquals(
+ defaultLocale, // <- expected template locale
+ Locale.FRENCH, // <- request locale
+ null, // <- init-param
+ "foo.ftl");
+ assertTemplateLocaleEquals(
+ defaultLocale, // <- expected template locale
+ Locale.FRENCH, // <- request locale
+ INIT_PARAM_VALUE_ALWAYS, // <- init-param
+ "foo.ftl");
+ assertTemplateLocaleEquals(
+ defaultLocale, // <- expected template locale
+ null, // <- request locale
+ INIT_PARAM_VALUE_NEVER, // <- init-param
+ "foo.ftl");
+ assertTemplateLocaleEquals(
+ Locale.FRENCH, // <- expected template locale
+ Locale.FRENCH, // <- request locale
+ INIT_PARAM_VALUE_NEVER, // <- init-param
+ "foo.ftl");
+ }
+
private void assertResponseContentTypeEquals(
String exptectContentType,
String ctInitParam, String overrideCTInitParam,
String templateName, String responseCT)
throws ServletException, IOException {
- MockHttpServletRequest request = createMockHttpServletRequest(servletContext, templateName);
+ MockHttpServletRequest request = createMockHttpServletRequest(servletContext, templateName, null);
MockHttpServletResponse response = new MockHttpServletResponse();
if (responseCT != null) {
@@ -167,15 +203,64 @@ public class FreemarkerServletTest {
}
}
+ private void assertTemplateLocaleEquals(
+ Locale exptectLocale,
+ Locale requestLocale,
+ String overrideResponseLocaleInitParam,
+ String templateName)
+ throws ServletException, IOException {
+ MockHttpServletRequest request = createMockHttpServletRequest(servletContext, templateName, requestLocale);
+ MockHttpServletResponse response = new MockHttpServletResponse();
+
+ MockServletConfig servletConfig = new MockServletConfig(servletContext);
+ servletConfig.addInitParameter(INIT_PARAM_TEMPLATE_PATH, TEST_TEMPLATE_PATH);
+
+ if (overrideResponseLocaleInitParam != null) {
+ servletConfig.addInitParameter(INIT_PARAM_OVERRIDE_RESPONSE_LOCALE, overrideResponseLocaleInitParam);
+ }
+
+ final Template [] processedTemplateHolder = new Template[1];
+
+ FreemarkerServlet freemarkerServlet = new FreemarkerServlet() {
+
+ @Override
+ protected void postTemplateProcess(
+ HttpServletRequest request,
+ HttpServletResponse response,
+ Template template,
+ TemplateModel data)
+ throws ServletException, IOException {
+ processedTemplateHolder[0] = template;
+ }
+ };
+
+ try {
+ freemarkerServlet.init(servletConfig);
+ freemarkerServlet.doGet(request, response);
+
+ assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+ assertEquals(exptectLocale, processedTemplateHolder[0].getLocale());
+ } finally {
+ freemarkerServlet.destroy();
+ }
+ }
+
private MockHttpServletRequest createMockHttpServletRequest(final ServletContext servletContext,
- final String pathInfo) {
- MockHttpServletRequest servletRequest = new MockHttpServletRequest(servletContext);
+ final String pathInfo, final Locale requestLocale) {
+ MockHttpServletRequest servletRequest = new MockHttpServletRequest(servletContext) {
+ @Override
+ public Locale getLocale() {
+ return requestLocale;
+ }
+ };
+
servletRequest.setServerName("localhost");
servletRequest.setServerPort(8080);
servletRequest.setContextPath("");
servletRequest.setRequestURI(pathInfo);
servletRequest.setPathInfo(pathInfo);
+
return servletRequest;
}
-
+
}
\ No newline at end of file