FreemarkerServlet override Content-Type previously set in a Filter
Generates text that depends on changing data (like dynamic HTML).
Brought to you by:
revusky
FreemarkerServlet override Content-Type previously set in a Filter
proposed solution: insert the code below after line 427.
The strategy should be:
1) look if a custom attribute content_type exists
2) if response Content-Type was already set by an external source (e.g. a filter) then leve it alone and use it
3) if default content type miss character encoding add it and use the calculated content type
4) if default content type have character encoding use the default setting
Object attrContentType = template.getCustomAttribute("content_type");
if(attrContentType != null) {
response.setContentType(attrContentType.toString());
}
else {
String ct = response.getContentType();
if (ct != null) {
response.setContentType(ct);
}
else {
if (noCharsetInContentType) {
response.setContentType(
contentType + "; charset=" + template.getEncoding());
} else {
response.setContentType(contentType);
}
}
}
I guess some other users expect FreeMarker to override the content-type, and certainly at this point many are already relying on that behavior even if they don't realize that. Still this could be an option. However, I'm not sure how to detect if something has already set the content type, or it's just the default of the Servlet container. Is the default always null per Servlet specification?
Ticket moved from /p/freemarker/bugs/393/