freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [05/25] incubator-freemarker git commit: Continued FREEMARKER-1 "Option to not to overwrite response ContentType in FreemarkerServlet": <#ftl attributes={ 'content_type': 'text/plain' }> wins even if content type overriding is disabled.
Date Sun, 29 Nov 2015 12:13:11 GMT
Continued FREEMARKER-1 "Option to not to overwrite response ContentType in FreemarkerServlet":
<#ftl attributes={ 'content_type': 'text/plain' }> wins even if content type overriding
is disabled.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/9e7f359a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/9e7f359a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/9e7f359a

Branch: refs/heads/master
Commit: 9e7f359a164112c813377a654fcd361405c5edd4
Parents: 64ec04b
Author: ddekany <ddekany@apache.org>
Authored: Sat Oct 17 19:41:13 2015 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Sat Oct 17 19:41:13 2015 +0200

----------------------------------------------------------------------
 .../ext/servlet/FreemarkerServlet.java          | 10 +++----
 .../ext/servlet/FreemarkerServletTest.java      | 29 ++++++++++++++++++++
 .../freemarker/ext/servlet/contentTypeAttr.ftl  | 20 ++++++++++++++
 3 files changed, 54 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9e7f359a/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 929bde4..841c8b1 100644
--- a/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
+++ b/src/main/java/freemarker/ext/servlet/FreemarkerServlet.java
@@ -725,11 +725,11 @@ public class FreemarkerServlet extends HttpServlet {
                     "Unexpected error when loading template " + StringUtil.jQuoteNoXSS(templatePath)
+ ".", e);
         }
 
-        if (overrideResponseContentType || response.getContentType() == null) {
-            Object attrContentType = template.getCustomAttribute("content_type");
-            if (attrContentType != null) {
-                response.setContentType(attrContentType.toString());
-            } else {
+        Object attrContentType = template.getCustomAttribute("content_type");
+        if (attrContentType != null) {
+            response.setContentType(attrContentType.toString());
+        } else {
+            if (overrideResponseContentType || response.getContentType() == null) {
                 if (noCharsetInContentType) {
                     response.setContentType(contentType + "; charset=" + template.getEncoding());
                 } else {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9e7f359a/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 20ac78d..9239ee0 100644
--- a/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
+++ b/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
@@ -169,6 +169,35 @@ public class FreemarkerServletTest {
         assertEquals(HttpServletResponse.SC_OK, response.getStatus());
         assertTrue(response.getContentType().contains("text/css"));
     }
+
+    @Test
+    public void testContentTypeInitParams_ftlAttrAlwaysWins_DefaultOverriding() throws ServletException,
IOException {
+        MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/contentTypeAttr.ftl");
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        response.setContentType("application/json");
+        assertEquals("application/json", response.getContentType());
+
+        createFreemarkerServlet().doGet(request, response);
+        LOG.debug("response content: " + response.getContentAsString());
+
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+        assertEquals("text/plain", response.getContentType());
+    }
+    
+    @Test
+    public void testContentTypeInitParams_ftlAttrAlwaysWins_NoOverriding() throws ServletException,
IOException {
+        MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/contentTypeAttr.ftl");
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        response.setContentType("application/json");
+        assertEquals("application/json", response.getContentType());
+
+        createFreemarkerServlet(FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE,
"false")
+                .doGet(request, response);
+        LOG.debug("response content: " + response.getContentAsString());
+
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+        assertEquals("text/plain", response.getContentType());
+    }
     
     private FreemarkerServlet createFreemarkerServlet(String... initParams) throws ServletException
{
         MockServletConfig servletConfig = new MockServletConfig(servletContext);

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9e7f359a/src/test/resources/freemarker/ext/servlet/contentTypeAttr.ftl
----------------------------------------------------------------------
diff --git a/src/test/resources/freemarker/ext/servlet/contentTypeAttr.ftl b/src/test/resources/freemarker/ext/servlet/contentTypeAttr.ftl
new file mode 100644
index 0000000..241f0b7
--- /dev/null
+++ b/src/test/resources/freemarker/ext/servlet/contentTypeAttr.ftl
@@ -0,0 +1,20 @@
+<#ftl attributes={ 'content_type': 'text/plain' }>
+<#--
+  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


Mime
View raw message