Return-Path: X-Original-To: apmail-freemarker-notifications-archive@minotaur.apache.org Delivered-To: apmail-freemarker-notifications-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 931241817E for ; Sat, 17 Oct 2015 17:07:42 +0000 (UTC) Received: (qmail 52139 invoked by uid 500); 17 Oct 2015 17:07:42 -0000 Delivered-To: apmail-freemarker-notifications-archive@freemarker.apache.org Received: (qmail 52122 invoked by uid 500); 17 Oct 2015 17:07:42 -0000 Mailing-List: contact notifications-help@freemarker.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@freemarker.incubator.apache.org Delivered-To: mailing list notifications@freemarker.incubator.apache.org Received: (qmail 52113 invoked by uid 99); 17 Oct 2015 17:07:42 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 17 Oct 2015 17:07:42 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 116F01A26FC for ; Sat, 17 Oct 2015 17:07:42 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.77 X-Spam-Level: * X-Spam-Status: No, score=1.77 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, T_RP_MATCHES_RCVD=-0.01] autolearn=disabled Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id 9VfXszYyRm4m for ; Sat, 17 Oct 2015 17:07:39 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with SMTP id 3CAA72054C for ; Sat, 17 Oct 2015 17:07:38 +0000 (UTC) Received: (qmail 52058 invoked by uid 99); 17 Oct 2015 17:07:37 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 17 Oct 2015 17:07:37 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 06FC8E042D; Sat, 17 Oct 2015 17:07:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ddekany@apache.org To: notifications@freemarker.incubator.apache.org Date: Sat, 17 Oct 2015 17:07:37 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [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 Authored: Mon Oct 12 22:31:16 2015 +0900 Committer: Woonsan Ko 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 @@ + + 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 @@ + ]> @@ -141,7 +142,14 @@ - + + + + + + + + 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 content_type in the * attributes parameter of the <#ftl> directive. - * + * + *
  • {@value #INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE} (since 2.4.0): If set to true, overrides the ContentType + * of the response by using either {@value #INIT_PARAM_CONTENT_TYPE} parameter setting or other + * information (see the description of {@value #INIT_PARAM_CONTENT_TYPE} for detail). The default + * value is false.
  • + * *
  • {@value #INIT_PARAM_BUFFER_SIZE}: Sets the size of the output buffer in bytes, or if "KB" or * "MB" is written after the number (like {@code 256 KB}) 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/**/ metaInfTldSources; private List/**/ 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