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 <lukaszlen...@apache.org>wrote:

> 2012/12/30 Burton Rhodes <burtonrho...@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-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>
>

Reply via email to