You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Burton Rhodes <bu...@gmail.com> on 2012/12/30 18:18:52 UTC

Unable to change content-type with Interceptor?

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;

    }

}

Re: Unable to change content-type with Interceptor?

Posted by Lukasz Lenart <lu...@apache.org>.
2012/12/30 Burton Rhodes <bu...@gmail.com>:
> Just tried that without success.  Oddly, even when I debug the code, the
> reponse.contentType is not changed immediately after the line:
> response.setContentType("text/plain").  I am wondering if the contentType
> cannot be set twice (or overridden once set)?

As mentioned here [1] you can change content-type till getWriter is
called or response committed

[1] http://docs.oracle.com/javaee/5/api/javax/servlet/ServletResponse.html#setContentType(java.lang.String)


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


Re: Unable to change content-type with Interceptor?

Posted by Burton Rhodes <bu...@gmail.com>.
Just tried that without success.  Oddly, even when I debug the code, the
reponse.contentType is not changed immediately after the line:
response.setContentType("text/plain").  I am wondering if the contentType
cannot be set twice (or overridden once set)?


On Sun, Dec 30, 2012 at 11:39 AM, Lukasz Lenart <lu...@apache.org>wrote:

> 2012/12/30 Burton Rhodes <bu...@gmail.com>:
> > 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>
>
> Did you try to change order of the interceptors? Interceptors are
> executed in FILO - First-In-Last-Out - which means json interceptor
> will be executed as the first one, but when action was executed and
> result will be forwarded back, json interceptor will be executed as
> the last one:
>
> json->jsonDynamicContentType->action->result->jsonDynamicContentType->json
>
> http://struts.apache.org/2.x/docs/interceptors.html
>
>
> 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
>
>

Re: Unable to change content-type with Interceptor?

Posted by Lukasz Lenart <lu...@apache.org>.
2012/12/30 Burton Rhodes <bu...@gmail.com>:
> 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>

Did you try to change order of the interceptors? Interceptors are
executed in FILO - First-In-Last-Out - which means json interceptor
will be executed as the first one, but when action was executed and
result will be forwarded back, json interceptor will be executed as
the last one:

json->jsonDynamicContentType->action->result->jsonDynamicContentType->json

http://struts.apache.org/2.x/docs/interceptors.html


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


Re: Unable to change content-type with Interceptor?

Posted by Lukasz Lenart <lu...@apache.org>.
2015-07-23 23:18 GMT+02:00 JOSE L MARTINEZ-AVIAL <jl...@gmail.com>:
> Hello all,
>    I was just looking for this option to override the content-type of the
> json result. Is there any fix for this?

Not sure what I have been referring to but JsonResult has contentType
param defined
https://github.com/apache/struts/blob/master/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java#L101


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


Re: Unable to change content-type with Interceptor?

Posted by JOSE L MARTINEZ-AVIAL <jl...@gmail.com>.
Hello all,
   I was just looking for this option to override the content-type of the
json result. Is there any fix for this?

  Thanks

JL

2012-12-31 12:41 GMT-05:00 Lukasz Lenart <lu...@apache.org>:

> 2012/12/30 Burton Rhodes <bu...@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
>
>

Re: Unable to change content-type with Interceptor?

Posted by Lukasz Lenart <lu...@apache.org>.
2012/12/30 Burton Rhodes <bu...@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


Re: Unable to change content-type with Interceptor?

Posted by Burton Rhodes <bu...@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.

/**
 * 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 <lu...@apache.org>wrote:

> 2012/12/30 Burton Rhodes <bu...@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
>
>

Re: Unable to change content-type with Interceptor?

Posted by Lukasz Lenart <lu...@apache.org>.
2012/12/30 Burton Rhodes <bu...@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