I'm not sure what I'm doing wrong, but I cannot for the life of me change
the content-type of the response using an interceptor. My interceptor code
is below. The example below seems a bit absurd, but eventually I will add
code to change the content-type dynamically from "application/json" to
"text/plain" depending on what the request will "accept" (this is to fix an
IE bug related to ajax file uploads). What am I missing here? Many thanks
in advance.
<!-- struts.xml snippit -->
<package name="jsonSecure" namespace="/app/json" extends="json-default">
<interceptors>
<interceptor name="jsonDynamicContentType"
class="com.afs.web.interceptor.JsonDynamicContentTypeInterceptor"/>
<interceptor-stack name="dynamicContentType">
<interceptor-ref name="json"/>
<interceptor-ref name="jsonDynamicContentType"/>
</interceptor-stack>
</interceptors>
<action name="TeamFileJson_upload"
class="com.afs.web.action.json.TeamFileJsonAction" method="upload">
<interceptor-ref name="dynamicContentType"/>
<result type="json"/>
</action>
<!-- Custom Interceptor -->
public class JsonDynamicContentTypeInterceptor extends AbstractInterceptor
{
public String intercept(ActionInvocation invocation) throws Exception {
// Go down the chain first
String result = invocation.invoke();
// Change the content-type of the response
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = (HttpServletResponse)
invocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
response.setContentType("text/plain");
// This doesn't work either
response.setHeader("Content-Type","text/plain");
// Continue the chain
return result;
}
}