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/10/17 19:07:37 UTC
[1/3] incubator-freemarker git commit: FREEMARKER-1 adding
OverrideResponseContentType init param in FreemarketServlet
Repository: incubator-freemarker
Updated Branches:
refs/heads/2.3-gae 0256df536 -> fe08488da
FREEMARKER-1 adding OverrideResponseContentType init param in FreemarketServlet
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/e89dbfce
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/e89dbfce
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/e89dbfce
Branch: refs/heads/2.3-gae
Commit: e89dbfce5f2d2274653062dfd6911a62c354c39b
Parents: 0eccad5
Author: Woonsan Ko <wo...@apache.org>
Authored: Mon Oct 12 22:31:16 2015 +0900
Committer: Woonsan Ko <wo...@apache.org>
Committed: Mon Oct 12 22:31:16 2015 +0900
----------------------------------------------------------------------
.classpath | 2 +
ivy.xml | 10 +-
.../ext/servlet/FreemarkerServlet.java | 37 ++++--
.../ext/servlet/FreemarkerServletTest.java | 124 +++++++++++++++++++
.../resources/freemarker/ext/servlet/foo.ftl | 19 +++
5 files changed, 181 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/e89dbfce/.classpath
----------------------------------------------------------------------
diff --git a/.classpath b/.classpath
index ad83438..0716ff4 100644
--- a/.classpath
+++ b/.classpath
@@ -54,5 +54,7 @@
<classpathentry kind="lib" path="ide-dependencies/jcl-over-slf4j-1.6.1.jar"/>
<classpathentry kind="lib" path="ide-dependencies/junit-4.12.jar"/>
<classpathentry kind="lib" path="ide-dependencies/hamcrest-library-1.3.jar"/>
+ <classpathentry kind="lib" path="ide-dependencies/spring-core-2.5.6.SEC03.jar"/>
+ <classpathentry kind="lib" path="ide-dependencies/spring-test-2.5.6.SEC03.jar"/>
<classpathentry kind="output" path=".bin/"/>
</classpath>
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/e89dbfce/ivy.xml
----------------------------------------------------------------------
diff --git a/ivy.xml b/ivy.xml
index f81c3c3..bf21fb6 100644
--- a/ivy.xml
+++ b/ivy.xml
@@ -4,6 +4,7 @@
<!DOCTYPE ivy-module [
<!ENTITY jetty.version "7.6.16.v20140903">
<!ENTITY slf4j.version "1.6.1">
+ <!ENTITY spring.version "2.5.6.SEC03">
]>
<ivy-module version="2.0">
<info organisation="org.freemarker" module="freemarker">
@@ -141,7 +142,14 @@
<exclude org="org.slf4j" name="jcl104-over-slf4j" />
<exclude org="log4j" name="log4j" />
</dependency>
-
+
+ <dependency org="org.springframework" name="spring-core" rev="&spring.version;" conf="test->default">
+ <exclude org="commons-logging" name="commons-logging" />
+ </dependency>
+ <dependency org="org.springframework" name="spring-test" rev="&spring.version;" conf="test->default">
+ <exclude org="commons-logging" name="commons-logging" />
+ </dependency>
+
<!-- docs -->
<dependency org="org.freemarker" name="docgen" rev="2.0-branch-head" conf="manual->default" changing="true" />
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/e89dbfce/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 83eb14c..776a884 100644
--- a/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
+++ b/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
@@ -140,7 +140,12 @@ import freemarker.template.utility.StringUtil;
* actual template file will be used (in the response HTTP header and for encoding the output stream). Note that this
* setting can be overridden on a per-template basis by specifying a custom attribute named <tt>content_type</tt> in the
* <tt>attributes</tt> parameter of the <tt><#ftl></tt> directive.</li>
- *
+ *
+ * <li><strong>{@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE}</strong> (since 2.4.0): If set to true, overrides the ContentType
+ * of the response by using either <strong>{@value #INIT_PARAM_CONTENT_TYPE}</strong> parameter setting or other
+ * information (see the description of <strong>{@value #INIT_PARAM_CONTENT_TYPE}</strong> for detail). The default
+ * value is <tt>false</tt>.</li>
+ *
* <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.
* This corresponds to {@link HttpServletResponse#setBufferSize(int)}. If the {@link HttpServletResponse} state doesn't
@@ -273,6 +278,13 @@ public class FreemarkerServlet extends HttpServlet {
/**
* 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_CONTENT_TYPE = "OverrideResponseContentType";
+
+ /**
+ * Init-param name - see the {@link FreemarkerServlet} class documentation about the init-params.
*
* @since 2.3.22
*/
@@ -314,7 +326,7 @@ public class FreemarkerServlet extends HttpServlet {
private static final String DEPR_INITPARAM_TEMPLATE_EXCEPTION_HANDLER_IGNORE = "ignore";
private static final String DEPR_INITPARAM_DEBUG = "debug";
- private static final String DEFAULT_CONTENT_TYPE = "text/html";
+ static final String DEFAULT_CONTENT_TYPE = "text/html";
/**
* When set, the items defined in it will be added after those coming from the
@@ -412,6 +424,7 @@ public class FreemarkerServlet extends HttpServlet {
@SuppressFBWarnings(value="SE_BAD_FIELD", justification="Not investing into making this Servlet serializable")
private ObjectWrapper wrapper;
private String contentType;
+ private boolean overrideResponseContentType = true;
private boolean noCharsetInContentType;
private List/*<MetaInfTldSource>*/ metaInfTldSources;
private List/*<String>*/ classpathTlds;
@@ -557,6 +570,8 @@ public class FreemarkerServlet extends HttpServlet {
debug = StringUtil.getYesNo(value);
} else if (name.equals(INIT_PARAM_CONTENT_TYPE)) {
contentType = value;
+ } else if (name.equals(INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE)) {
+ overrideResponseContentType = StringUtil.getYesNo(value);
} else if (name.equals(INIT_PARAM_EXCEPTION_ON_MISSING_TEMPLATE)) {
exceptionOnMissingTemplate = StringUtil.getYesNo(value);
} else if (name.equals(INIT_PARAM_META_INF_TLD_LOCATIONS)) {;
@@ -708,15 +723,17 @@ public class FreemarkerServlet extends HttpServlet {
throw newServletExceptionWithFreeMarkerLogging(
"Unexpected error when loading template " + StringUtil.jQuoteNoXSS(templatePath) + ".", e);
}
-
- Object attrContentType = template.getCustomAttribute("content_type");
- if (attrContentType != null) {
- response.setContentType(attrContentType.toString());
- } else {
- if (noCharsetInContentType) {
- response.setContentType(contentType + "; charset=" + template.getEncoding());
+
+ if (overrideResponseContentType) {
+ Object attrContentType = template.getCustomAttribute("content_type");
+ if (attrContentType != null) {
+ response.setContentType(attrContentType.toString());
} else {
- response.setContentType(contentType);
+ if (noCharsetInContentType) {
+ response.setContentType(contentType + "; charset=" + template.getEncoding());
+ } else {
+ response.setContentType(contentType);
+ }
}
}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/e89dbfce/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
new file mode 100644
index 0000000..9690638
--- /dev/null
+++ b/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package freemarker.ext.servlet;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockServletConfig;
+import org.springframework.mock.web.MockServletContext;
+
+import freemarker.log.Logger;
+
+public class FreemarkerServletTest {
+
+ private static final Logger LOG = Logger.getLogger("freemarker.servlet");
+
+ private static final String TEST_TEMPLATE_PATH = "classpath:freemarker/ext/servlet";
+
+ private FreemarkerServlet freemarkerServlet;
+
+ private MockServletContext servletContext;
+ private MockServletConfig servletConfig;
+
+ @Before
+ public void setUp() throws ServletException, IOException {
+ servletContext = new MockServletContext();
+ servletContext.setContextPath("/");
+
+ servletConfig = new MockServletConfig(servletContext);
+ servletConfig.addInitParameter(FreemarkerServlet.INIT_PARAM_TEMPLATE_PATH, TEST_TEMPLATE_PATH);
+
+ freemarkerServlet = new FreemarkerServlet();
+ freemarkerServlet.init(servletConfig);
+ }
+
+ @After
+ public void tearDown() {
+ freemarkerServlet.destroy();
+ }
+
+ @Test
+ public void testContentTypeInitParams_withNoResponseContentType_ByDefault() throws ServletException, IOException {
+ MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ assertNull(response.getContentType());
+
+ freemarkerServlet.doGet(request, response);
+ LOG.debug("response content: " + response.getContentAsString());
+
+ assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+ assertTrue(response.getContentType().contains(FreemarkerServlet.DEFAULT_CONTENT_TYPE));
+ }
+
+ @Test
+ public void testContentTypeInitParams_withResponseContentType_ByDefault() throws ServletException, IOException {
+ MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ response.setContentType("application/json");
+ assertEquals("application/json", response.getContentType());
+
+ freemarkerServlet.doGet(request, response);
+ LOG.debug("response content: " + response.getContentAsString());
+
+ assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+ assertTrue(response.getContentType().contains(FreemarkerServlet.DEFAULT_CONTENT_TYPE));
+ }
+
+ @Test
+ public void testContentTypeInitParams_withResponseContentType_NoOverriding() throws ServletException, IOException {
+ servletConfig.addInitParameter(FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE, "false");
+ freemarkerServlet = new FreemarkerServlet();
+ freemarkerServlet.init(servletConfig);
+
+ MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ response.setContentType("application/json");
+ assertEquals("application/json", response.getContentType());
+
+ freemarkerServlet.doGet(request, response);
+ LOG.debug("response content: " + response.getContentAsString());
+
+ assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+ assertEquals("application/json", response.getContentType());
+
+ freemarkerServlet.destroy();
+ }
+
+ private MockHttpServletRequest createMockHttpServletRequest(final ServletContext servletContext,
+ final String pathInfo) {
+ MockHttpServletRequest servletRequest = new MockHttpServletRequest(servletContext);
+ servletRequest.setServerName("localhost");
+ servletRequest.setServerPort(8080);
+ servletRequest.setContextPath("");
+ servletRequest.setRequestURI(pathInfo);
+ servletRequest.setPathInfo(pathInfo);
+ return servletRequest;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/e89dbfce/src/test/resources/freemarker/ext/servlet/foo.ftl
----------------------------------------------------------------------
diff --git a/src/test/resources/freemarker/ext/servlet/foo.ftl b/src/test/resources/freemarker/ext/servlet/foo.ftl
new file mode 100644
index 0000000..a3aac20
--- /dev/null
+++ b/src/test/resources/freemarker/ext/servlet/foo.ftl
@@ -0,0 +1,19 @@
+<#--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+foo
\ No newline at end of file
[2/3] incubator-freemarker git commit: Merge remote-tracking branch
'FREEMARKER-1-option-overrideResponseContentType' into 2.3-gae
Posted by dd...@apache.org.
Merge remote-tracking branch 'FREEMARKER-1-option-overrideResponseContentType' into 2.3-gae
FREEMARKER-1: Adding OverrideResponseContentType init param in FreemarkerServlet
GitHub pull request #5
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/3ab293ad
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/3ab293ad
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/3ab293ad
Branch: refs/heads/2.3-gae
Commit: 3ab293ad12e1aaf2e2bededcd3540884d35f9fae
Parents: 0256df5 e89dbfc
Author: ddekany <dd...@apache.org>
Authored: Sat Oct 17 17:44:28 2015 +0200
Committer: ddekany <dd...@apache.org>
Committed: Sat Oct 17 17:48:10 2015 +0200
----------------------------------------------------------------------
.classpath | 2 +
ivy.xml | 10 +-
.../ext/servlet/FreemarkerServlet.java | 37 ++++--
.../ext/servlet/FreemarkerServletTest.java | 124 +++++++++++++++++++
.../resources/freemarker/ext/servlet/foo.ftl | 19 +++
5 files changed, 181 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3ab293ad/.classpath
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3ab293ad/ivy.xml
----------------------------------------------------------------------
[3/3] incubator-freemarker git commit: Continued FREEMARKER-1 "Option
to not to overwrite response ContentType in FreemarkerServlet": Always set
response Content-type header when it's null.
Posted by dd...@apache.org.
Continued FREEMARKER-1 "Option to not to overwrite response ContentType in FreemarkerServlet": Always set response Content-type header when it's null.
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/fe08488d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/fe08488d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/fe08488d
Branch: refs/heads/2.3-gae
Commit: fe08488da50e12dfcb8a5c600417a391df218d26
Parents: 3ab293a
Author: ddekany <dd...@apache.org>
Authored: Sat Oct 17 19:07:08 2015 +0200
Committer: ddekany <dd...@apache.org>
Committed: Sat Oct 17 19:07:08 2015 +0200
----------------------------------------------------------------------
.../ext/servlet/FreemarkerServlet.java | 59 ++++++++++----------
.../ext/servlet/FreemarkerServletTest.java | 19 +++++++
2 files changed, 49 insertions(+), 29 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fe08488d/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 776a884..929bde4 100644
--- a/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
+++ b/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
@@ -88,9 +88,9 @@ import freemarker.template.utility.StringUtil;
* <code><#assign tiles=JspTaglibs["/WEB-INF/struts-tiles.tld"]></code>.
*
* <li>A custom directive named {@code include_page} allows you to include the output of another servlet resource from
- * your servlet container, just as if you used {@code ServletRequest.getRequestDispatcher(path).include()}:
- * {@code <@include_page path="/myWebapp/somePage.jsp"/>}. You can also pass parameters to the newly included page by
- * passing a hash named {@code params}:
+ * your servlet container, just as if you used {@code ServletRequest.getRequestDispatcher(path).include()}: {@code
+ * <@include_page path="/myWebapp/somePage.jsp"/>}. You can also pass parameters to the newly included page by passing a
+ * hash named {@code params}:
* <code><@include_page path="/myWebapp/somePage.jsp" params= lang: "en", q="5"}/></code>. By default, the request
* parameters of the original request (the one being processed by FreemarkerServlet) are also inherited by the include.
* You can explicitly control this inheritance using the {@code inherit_params} parameter:
@@ -116,15 +116,15 @@ import freemarker.template.utility.StringUtil;
* you want to load templates from the specified package accessible through the Thread Context Class Loader of the
* thread that initializes this servlet.<br>
* If {@code incompatible_improvements} is set to 2.3.22 (or higher), you can specify multiple comma separated locations
- * inside square brackets, like: {@code [ WEB-INF/templates, classpath:com/example/myapp/templates ]}.
- * This internally creates a {@link MultiTemplateLoader}. Note again that if {@code incompatible_improvements} isn't
- * set to at least 2.3.22, the initial {@code [} has no special meaning, and so this feature is unavailable.<br>
+ * inside square brackets, like: {@code [ WEB-INF/templates, classpath:com/example/myapp/templates ]}. This internally
+ * creates a {@link MultiTemplateLoader}. Note again that if {@code incompatible_improvements} isn't set to at least
+ * 2.3.22, the initial {@code [} has no special meaning, and so this feature is unavailable.<br>
* Any of the above can have a {@code ?setting(name=value, ...)} postfix to set the JavaBeans properties of the
* {@link TemplateLoader} created. For example,
- * {@code /templates?settings(attemptFileAccess=false, URLConnectionUsesCaches=true)}
- * calls {@link WebappTemplateLoader#setAttemptFileAccess(boolean)}
- * and {@link WebappTemplateLoader#setURLConnectionUsesCaches(Boolean)} to tune the {@link WebappTemplateLoader}.
- * For backward compatibility (not recommended!), you can use the {@code class://} prefix, like in
+ * {@code /templates?settings(attemptFileAccess=false, URLConnectionUsesCaches=true)} calls
+ * {@link WebappTemplateLoader#setAttemptFileAccess(boolean)} and
+ * {@link WebappTemplateLoader#setURLConnectionUsesCaches(Boolean)} to tune the {@link WebappTemplateLoader}. For
+ * backward compatibility (not recommended!), you can use the {@code class://} prefix, like in
* <tt>class://com/example/templates</tt> format, which is similar to {@code classpath:}, except that it uses the
* defining class loader of this servlet's class. This can cause template not found errors, if that class (in
* {@code freemarer.jar} usually) is not local to the web application, while the templates are.<br>
@@ -134,17 +134,20 @@ import freemarker.template.utility.StringUtil;
* <li><strong>{@value #INIT_PARAM_NO_CACHE}</strong>: If set to true, generates headers in the response that advise the
* HTTP client not to cache the returned page. The default is <tt>false</tt>.</li>
*
- * <li><strong>{@value #INIT_PARAM_CONTENT_TYPE}</strong>: If specified, response uses the specified Content-type HTTP
- * header. The value may include the charset (e.g. <tt>"text/html; charset=ISO-8859-1"</tt>). If not specified,
- * <tt>"text/html"</tt> is used. If the charset is not specified in this init-param, then the charset (encoding) of the
- * actual template file will be used (in the response HTTP header and for encoding the output stream). Note that this
- * setting can be overridden on a per-template basis by specifying a custom attribute named <tt>content_type</tt> in the
- * <tt>attributes</tt> parameter of the <tt><#ftl></tt> directive.</li>
+ * <li><strong>{@value #INIT_PARAM_CONTENT_TYPE}</strong>: The Content-type HTTP header value used in the HTTP responses
+ * (unless <strong>{@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE}</strong> is set to {@code false} and the response
+ * Content-type is already set by the time {@link FreemarkerServlet} is invoked). Defaults to <tt>"text/html"</tt>. The
+ * value may include the charset (e.g. <tt>"text/html; charset=ISO-8859-1"</tt>). If the charset is not specified in
+ * this init-param, then the charset (encoding) of the actual template file will be used (both in the response HTTP
+ * header and for encoding the output stream). Note that this setting can be overridden on a per-template basis by
+ * specifying a custom attribute named <tt>content_type</tt> in the <tt>attributes</tt> parameter of the
+ * <tt><#ftl></tt> directive.</li>
*
- * <li><strong>{@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE}</strong> (since 2.4.0): If set to true, overrides the ContentType
- * of the response by using either <strong>{@value #INIT_PARAM_CONTENT_TYPE}</strong> parameter setting or other
- * information (see the description of <strong>{@value #INIT_PARAM_CONTENT_TYPE}</strong> for detail). The default
- * value is <tt>false</tt>.</li>
+ * <li><strong>{@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE}</strong> (since 2.3.24): If set to {@code true}
+ * (which is the default), the Content-type HTTP header of the response is always set to the value of the
+ * <strong>{@value #INIT_PARAM_CONTENT_TYPE}</strong> setting. If set to {@code false}, {@link FreemarkerServlet} will
+ * only set the Content-type HTTP response header if it isn't already set when {@link FreemarkerServlet} is invoked.
+ * Thus, {@code false} allows you to specify the Content-type before forwarding to {@link FreemarkerServlet}.</li>
*
* <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.
@@ -162,9 +165,9 @@ import freemarker.template.utility.StringUtil;
*
* <li><strong>{@value #INIT_PARAM_META_INF_TLD_LOCATIONS}</strong> (since 2.3.22): Comma separated list of items, each
* is either {@value #META_INF_TLD_LOCATION_WEB_INF_PER_LIB_JARS}, or {@value #META_INF_TLD_LOCATION_CLASSPATH}
- * optionally followed by colon and a regular expression, or {@value #META_INF_TLD_LOCATION_CLEAR}. For example
- * {@code <param-value>classpath:.*myoverride.*\.jar$, webInfPerLibJars, classpath:.*taglib.*\.jar$</param-value>}, or
- * {@code <param-value>classpath</param-value>}. (Whitespace around the commas and list items will be ignored.) See
+ * optionally followed by colon and a regular expression, or {@value #META_INF_TLD_LOCATION_CLEAR}. For example {@code
+ * <param-value>classpath:.*myoverride.*\.jar$, webInfPerLibJars, classpath:.*taglib.*\.jar$</param-value>}, or {@code
+ * <param-value>classpath</param-value>}. (Whitespace around the commas and list items will be ignored.) See
* {@link TaglibFactory#setMetaInfTldSources(List)} for more information. Defaults to a list that contains
* {@value #META_INF_TLD_LOCATION_WEB_INF_PER_LIB_JARS} only (can be overridden with
* {@link #createDefaultMetaInfTldSources()}). Note that this can be also specified with the
@@ -230,12 +233,10 @@ import freemarker.template.utility.StringUtil;
* the servlet container (with the proper cause exception). After all, if the visited URL had an associated "action" but
* the template behind it is missing, that's an internal server error, not a wrong URL.</li>
*
- * <li>
- * If the template contains parsing errors, it will log it with error level, then the servlet throws
+ * <li>If the template contains parsing errors, it will log it with error level, then the servlet throws
* {@link ServletException} to the servlet container (with the proper cause exception).</li>
*
- * <li>
- * If the template throws exception during its execution, and the value of the {@code template_exception_handler}
+ * <li>If the template throws exception during its execution, and the value of the {@code template_exception_handler}
* init-param is {@code rethrow} (recommended), it will log it with error level and then the servlet throws
* {@link ServletException} to the servlet container (with the proper cause exception). But beware, the default value of
* the {@code template_exception_handler} init-param is {@code html_debug}, which is for development only! Set it to
@@ -596,7 +597,7 @@ public class FreemarkerServlet extends HttpServlet {
noCharsetInContentType = true;
int i = contentType.toLowerCase().indexOf("charset=");
if (i != -1) {
- char c = ' ';
+ char c = 0;
i--;
while (i >= 0) {
c = contentType.charAt(i);
@@ -724,7 +725,7 @@ public class FreemarkerServlet extends HttpServlet {
"Unexpected error when loading template " + StringUtil.jQuoteNoXSS(templatePath) + ".", e);
}
- if (overrideResponseContentType) {
+ if (overrideResponseContentType || response.getContentType() == null) {
Object attrContentType = template.getCustomAttribute("content_type");
if (attrContentType != null) {
response.setContentType(attrContentType.toString());
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fe08488d/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 9690638..bf2ef8a 100644
--- a/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
+++ b/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
@@ -111,6 +111,25 @@ public class FreemarkerServletTest {
freemarkerServlet.destroy();
}
+ @Test
+ public void testContentTypeInitParams_withNoResponseContentType_NoOverriding() throws ServletException, IOException {
+ servletConfig.addInitParameter(FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE, "false");
+ freemarkerServlet = new FreemarkerServlet();
+ freemarkerServlet.init(servletConfig);
+
+ MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ assertNull(response.getContentType());
+
+ freemarkerServlet.doGet(request, response);
+ LOG.debug("response content: " + response.getContentAsString());
+
+ assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+ assertTrue(response.getContentType().contains(FreemarkerServlet.DEFAULT_CONTENT_TYPE));
+
+ freemarkerServlet.destroy();
+ }
+
private MockHttpServletRequest createMockHttpServletRequest(final ServletContext servletContext,
final String pathInfo) {
MockHttpServletRequest servletRequest = new MockHttpServletRequest(servletContext);