struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Burton Rhodes <burtonrho...@gmail.com>
Subject Re: Unable to change content-type with Interceptor?
Date Sun, 30 Dec 2012 20:04:39 GMT
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.

/**
 * 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;
        }
    }


}



On Sun, Dec 30, 2012 at 12:09 PM, Lukasz Lenart <lukaszlenart@apache.org>wrote:

> 2012/12/30 Burton Rhodes <burtonrhodes@gmail.com>:
> >         HttpServletResponse response = (HttpServletResponse)
> > invocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
>
> Maybe try to use:
> HttpServletRequest request = ServletActionContext.getRequest();
>
>
> Regards
> --
> Ɓ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
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message