diff --git a/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java b/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java index 3453d8977..5fe0b3a6f 100644 --- a/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java +++ b/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java @@ -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; diff --git a/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java b/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java index 1d9c6595f..ef5178751 100644 --- a/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java +++ b/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java @@ -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(); diff --git a/core/src/test/resources/org/apache/struts2/views/freemarker/something.ftl b/core/src/test/resources/org/apache/struts2/views/freemarker/something.ftl new file mode 100644 index 000000000..2ca0b5c50 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/freemarker/something.ftl @@ -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