struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lukasz Lenart <lukaszlen...@apache.org>
Subject Re: Unable to change content-type with Interceptor?
Date Mon, 31 Dec 2012 17:41:05 GMT
2012/12/30 Burton Rhodes <burtonrhodes@gmail.com>:
> Lukasz -
>
> Thanks for all your help.  You got me on the right track and it appears the
> json Result was what was overriding and setting the content type.  As a
> result, I have overridden the JSONResult class to use the "dynamic"
> content-Type.

You're welcome :-)

> /**
>  * Custom json result type to set Content-Type response header dynamically
> to either 'application/json' or 'plain/text'
>  * according to what the request header identifies in the "Accept" header
>  *
>  * (this is to fix browser wanting to download the json response on an ajax
> submit)
>  *
>  * @author Burton Rhodes
>  *
>  */
> public class JsonDynamicContentTypeResult extends JSONResult {
>
>     private static final Logger LOG =
> LoggerFactory.getLogger(JsonDynamicContentTypeResult.class);
>
>     @Override
>     public void execute(ActionInvocation invocation) throws Exception {
>
>         ActionContext actionContext = invocation.getInvocationContext();
>         HttpServletRequest request = (HttpServletRequest)
> actionContext.get(StrutsStatics.HTTP_REQUEST);
>         HttpServletResponse response = (HttpServletResponse)
> actionContext.get(StrutsStatics.HTTP_RESPONSE);
>
>         // Set Content-Type according to what the request will "accept"
>         if (request.getHeader("Accept")!=null &&
> request.getHeader("Accept").toLowerCase().contains("application/json")) {
>             this.setContentType("application/json");
>         } else {
>             // Default to text/plain
>             this.setContentType("text/plain");
>         }
>
>         try {
>             Object rootObject;
>             rootObject = readRootObject(invocation);
>             writeToResponse(response, createJSONString(request,
> rootObject), enableGzip(request));
>         } catch (IOException exception) {
>             LOG.error(exception.getMessage(), exception);
>             throw exception;
>         }
>     }
>
>
> }

Right now contentType isn't dynamically resolved, please register an
issue and I'll add such option to JSONResult so you will be able to
specify contentType via <param/> tag, eg.

<result type="json">
    <param name="contentType">${resolveContentType}</param>
</result>

and the action's resolvedContentType method will be called to obtain
valid contentType


Happy New Year :-)
-- 
Ɓukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message