mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-3305 FreemarkerResult tamper Content-Type
patch provided by zhouyanming git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@832148 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -311,22 +311,25 @@ public class FreemarkerResult extends StrutsResultSupport {
|
||||
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";
|
||||
if (contentType == null) {
|
||||
contentType = "text/html";
|
||||
}
|
||||
|
||||
String encoding = template.getEncoding();
|
||||
|
||||
if (encoding != null) {
|
||||
contentType = contentType + "; charset=" + encoding;
|
||||
}
|
||||
|
||||
response.setContentType(contentType);
|
||||
}
|
||||
|
||||
String encoding = template.getEncoding();
|
||||
|
||||
if (encoding != null) {
|
||||
contentType = contentType + "; charset=" + encoding;
|
||||
}
|
||||
|
||||
ServletActionContext.getResponse().setContentType(contentType);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -145,6 +145,45 @@ public class FreeMarkerResultTest extends StrutsTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user