[ 
https://issues.apache.org/jira/browse/CXF-4891?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13607522#comment-13607522
 ] 

Sergey Beryozkin commented on CXF-4891:
---------------------------------------

Sure, I went ahead and merged the actual changes to 2.7.x - I thought I'd 
rather keep them on the trunk only but given that ContainerResponseFilter and 
WriterInterceptor chains are not run correctly on 2.7.x the changes have to 
make it into 2.7.x.

In CXF 2.7.4 you will be able to use JAX-RS 2.0 ContainerResponseFilter to 
check the entity and update the media type as needed - please give it a try 
with CXF 2.7.4-SNAPSHOT in a few days and reopen the JIRA if it does not work 
as expected.

Also, note that one can write a delegating MBW which will do exactly the same 
but indeed it will require more effort, for example:

{code:java}

public class MyWriter implements MessageBodyProvider {
   private static final String ACTIVE_JAXRS_PROVIDER_KEY = 
"active.jaxrs.provider";

   @Context
   private Providers providers;

   public void writeTo(Object obj, Class<?> type, Type genericType, 
Annotation[] anns, MediaType mt,
                        MultivaluedMap<String, Object> headers, OutputStream 
os) 
        throws IOException, WebApplicationException {

        // to avoid the recursive calls
        Message currentMessage = PhaseInterceptorChain.getCurrentMessage(); 
        currentMessage.put(ACTIVE_JAXRS_PROVIDER_KEY, this);
        
        MessageBodyWriter<T> r = null;
        try {
            r = mc.getProviders().getMessageBodyWriter(type, genericType, anns, 
mt);
        } finally {
            currentMessage.put("active.jaxrs.provider", null); 
        }
        // check the object, update mt as needed
        r.writeTo(obj, type, genericType, anns, mt, headers, os);  
   }

}
{code}

This will work on 2.7.3
                
> Cannot set content-type header
> ------------------------------
>
>                 Key: CXF-4891
>                 URL: https://issues.apache.org/jira/browse/CXF-4891
>             Project: CXF
>          Issue Type: Bug
>          Components: JAX-RS, Transports
>    Affects Versions: 2.6.2, 2.7.3
>            Reporter: Todd Orr
>
> From within an interceptor I am trying to alter the content-type simply to 
> add a few parameters to the response header. I have attempted multiple ways 
> to do so, across several Phases.
> Firstly, there are examples on the web of setting the header using 
> Message.put(Message.CONTENT_TYPE, "whatever"). This simply does not work. I 
> then wrote the header using aMessage.put(Message.PROTOCOL_HEADERS, 
> arrayOfWhatever). This appears to work, but it's a lie. In the CXF response 
> logging I see what appears to be what I want:
> {code}
> ID: 18
> Response-Code: 200
> Content-Type: text/plain;charset=UTF-8;version=1.0.0-SNAPSHOT
> Headers: {Expires=[-1], Cache-Control=[no-cache], Pragma=[no-cache], 
> Content-Type=[text/plain], Date=[Tue, 12 Mar 2013 20:37:43 GMT]}
> Payload: 1.0
> {code}
> However, this must be getting overwritten between this logging statement and 
> my client because the browser only displays text/plain. Thinking this was 
> just a browser error I used Firfox and curl, both resulted in the same value 
> of text/plain being returned.
> I have only been able to get the actual values changed by grabbing the 
> HTTPServletResponse using HttpServletResponse response = 
> (HttpServletResponse) m.get(AbstractHTTPDestination.HTTP_RESPONSE); and 
> setting the content-type via response.setHeader(HttpHeaders.CONTENT_TYPE, 
> contentType);. However, this doesn't work right. First of all, it won't work 
> if you attempt to write the content type after MARSHAL because the stream has 
> already been written to. And if you attempt to write the value before 
> MARSHAL, then when you attempt to pull the content-type from the Message, 
> you'll get a default content-type (I believe text/xml) value other than what 
> the body is. That is, if the content was marshalled to JSON, you will not get 
> application/json as expected.
> I have been over this for two days. I have attempted to use JAXRS handlers 
> with similar results. I simply do not see a way to affect the headers as I 
> need to.
> I originally ran against 2.6.2, but was hoping that 2.7.3 corrected this. 
> Unfortunately both version exhibit this behavior.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to