Author: musachy
Date: Mon Nov 2 22:23:28 2009
New Revision: 832148
URL:
http://svn.apache.org/viewvc?rev=832148&view=revLog:
WW-3305 FreemarkerResult tamper Content-Type
patch provided by zhouyanming
Added:
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/freemarker/something.ftl
Modified:
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java?rev=832148&r1=832147&r2=832148&view=diff==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java Mon Nov 2 22:23:28 2009
@@ -311,22 +311,25 @@
protected boolean preTemplateProcess(Template template, TemplateModel model) throws IOException {
Object attrContentType = template.getCustomAttribute("content_type");
- if (attrContentType != null) {
- ServletActionContext.getResponse().setContentType(attrContentType.toString());
- } else {
- String contentType = getContentType();
+ HttpServletResponse response = ServletActionContext.getResponse();
+ if (response.getContentType() == null) {
+ if (attrContentType != null) {
+ response.setContentType(attrContentType.toString());
+ } else {
+ String contentType = getContentType();
+
+ if (contentType == null) {
+ contentType = "text/html";
+ }
+
+ String encoding = template.getEncoding();
+
+ if (encoding != null) {
+ contentType = contentType + "; charset=" + encoding;
+ }
- if (contentType == null) {
- contentType = "text/html";
+ response.setContentType(contentType);
}
-
- String encoding = template.getEncoding();
-
- if (encoding != null) {
- contentType = contentType + "; charset=" + encoding;
- }
-
- ServletActionContext.getResponse().setContentType(contentType);
}
return true;
Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java?rev=832148&r1=832147&r2=832148&view=diff==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java (original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java Mon Nov 2 22:23:28 2009
@@ -145,6 +145,45 @@
}
}
+ public void testContentTypeIsNotOverwritten() throws Exception {
+ servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
+ "nested.ftl").toURI()).toURL().getFile());
+
+ FreemarkerResult result = new FreemarkerResult();
+ result.setLocation("nested.ftl");
+ result.setFreemarkerManager(mgr);
+
+ response.setContentType("contenttype");
+ result.execute(invocation);
+ assertEquals("contenttype", response.getContentType());
+ }
+
+ public void testDefaultContentType() throws Exception {
+ servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
+ "nested.ftl").toURI()).toURL().getFile());
+
+ FreemarkerResult result = new FreemarkerResult();
+ result.setLocation("nested.ftl");
+ result.setFreemarkerManager(mgr);
+
+ assertNull(response.getContentType());
+ result.execute(invocation);
+ assertEquals("text/html; charset=UTF-8", response.getContentType());
+ }
+
+ public void testContentTypeFromTemplate() throws Exception {
+ servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
+ "something.ftl").toURI()).toURL().getFile());
+
+ FreemarkerResult result = new FreemarkerResult();
+ result.setLocation("something.ftl");
+ result.setFreemarkerManager(mgr);
+
+ assertNull(response.getContentType());
+ result.execute(invocation);
+ assertEquals("text/xml", response.getContentType());
+ }
+
protected void setUp() throws Exception {
super.setUp();
mgr = new FreemarkerManager();
Added: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/freemarker/something.ftl
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/freemarker/something.ftl?rev=832148&view=auto==============================================================================
--- struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/freemarker/something.ftl (added)
+++ struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/freemarker/something.ftl Mon Nov 2 22:23:28 2009
@@ -0,0 +1,24 @@
+<#ftl attributes={"content_type": "text/xml"} />
+<#--
+/*
+ * $Id: someFreeMarkerFile.ftl 590812 2007-10-31 20:32:54Z apetrelli $
+ *
+ * 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.
+ */
+-->
+test