You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by Conrad O'Dea <co...@iona.com> on 2006/11/01 21:54:33 UTC

setting HTTP response headers

Hi,

I'm porting a RESTy service from Celtix 1.something to CXF and have  
noticed some differences in the way the HTTP protocol headers are  
handled.

For example, in a service implemented using a Provider, I've tried  
getting the headers from the MessageContext like this:

	ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);

This returns null though.  Calling:

	Map<String, List<String>>hdrs = ctx.get 
(org.apache.cxf.message.Message.PROTOCOL_HEADERS);

works though and I can get the headers ok.

I also need to set the response headers and should be able to set a  
header like this:

       Map<String, List<String>>hdrs = ctx.get 
(javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
       hdrs.put(headerName, headerValue);

and have it added to the outgoing response.  However, as above using  
the MessageContext property does not return anything.  I've tried  
using the org.apache.cxf.message.Message property like this

       Map<String, List<String>>hdrs = ctx.get 
(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
       hdrs.put(headerName, headerValue);

but there's no sign that the header has been correctly added.  Is  
there some other way to set a HTTP header on the response?

thanks
Conrad



Re: setting HTTP response headers

Posted by Willem Jiang <ni...@iona.com>.
Hi Conrad,

I had done some work of the context mapping in CXF,  I can give you some 
explanations.

First, CXF has a jax-ws front end to wrap all the things about the 
jax-ws, so I implemented an mapper which get the connection between 
jax-ws context and rt-core message context.
Current CXF jax-ws front end mapper just implement a limited mapping of 
BindingProvider and MessageContext's defined properties to rt-core, that 
is why you can't use 
javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS to get the 
request headers.

Second, I went though the code and found the http header which you want 
to pass to the response wouldn't take any effect because you just set on 
the context ( which we get from  inMessage's context)and it would not 
effect to outMessage's context in current code.

So I will fill an JIRA and look into it today :)

Thanks,

Willem.
Conrad O'Dea wrote:

> Hi,
>
> I'm porting a RESTy service from Celtix 1.something to CXF and have  
> noticed some differences in the way the HTTP protocol headers are  
> handled.
>
> For example, in a service implemented using a Provider, I've tried  
> getting the headers from the MessageContext like this:
>
>     ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);
>
> This returns null though.  Calling:
>
>     Map<String, List<String>>hdrs = ctx.get 
> (org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>
> works though and I can get the headers ok.
>
> I also need to set the response headers and should be able to set a  
> header like this:
>
>       Map<String, List<String>>hdrs = ctx.get 
> (javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
>       hdrs.put(headerName, headerValue);
>
> and have it added to the outgoing response.  However, as above using  
> the MessageContext property does not return anything.  I've tried  
> using the org.apache.cxf.message.Message property like this
>
>       Map<String, List<String>>hdrs = ctx.get 
> (org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>       hdrs.put(headerName, headerValue);
>
> but there's no sign that the header has been correctly added.  Is  
> there some other way to set a HTTP header on the response?
>
> thanks
> Conrad
>
>
>


Re: setting HTTP response headers

Posted by Willem Jiang <ni...@iona.com>.
Hi Conrad,

I had done some work of the context mapping in CXF,  I can give you some
explanations.

First, CXF has a jax-ws front end to wrap all the things about the
jax-ws, so I implemented an mapper which get the connection between
jax-ws context and rt-core message context.
Current CXF jax-ws front end mapper just implement a limited mapping of
BindingProvider and MessageContext's defined properties to rt-core, that
is why you can't use
javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS to get the
request headers.

Second, I went though the code and found the http header which you want
to pass to the response wouldn't take any effect because you just set on
the context ( which we get from  inMessage's context)and it would not
effect to outMessage's context in current code.

So I will fill an JIRA and look into it today :)

Thanks,

Willem.
Conrad O'Dea wrote:

> Hi,
>
> I'm porting a RESTy service from Celtix 1.something to CXF and have  
> noticed some differences in the way the HTTP protocol headers are  
> handled.
>
> For example, in a service implemented using a Provider, I've tried  
> getting the headers from the MessageContext like this:
>
>     ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);
>
> This returns null though.  Calling:
>
>     Map<String, List<String>>hdrs = ctx.get 
> (org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>
> works though and I can get the headers ok.
>
> I also need to set the response headers and should be able to set a  
> header like this:
>
>       Map<String, List<String>>hdrs = ctx.get 
> (javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
>       hdrs.put(headerName, headerValue);
>
> and have it added to the outgoing response.  However, as above using  
> the MessageContext property does not return anything.  I've tried  
> using the org.apache.cxf.message.Message property like this
>
>       Map<String, List<String>>hdrs = ctx.get 
> (org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>       hdrs.put(headerName, headerValue);
>
> but there's no sign that the header has been correctly added.  Is  
> there some other way to set a HTTP header on the response?
>
> thanks
> Conrad
>
>
>



Re: setting HTTP response headers

Posted by Freeman Fang <fr...@iona.com>.
Hi Conrad,

Thanks very much

That's very helpful

Freeman


Conrad O'Dea wrote:

>On Thu, 2006-11-02 at 10:31 +0000, Conrad O'Dea wrote:
>  
>
>>On Thu, 2006-11-02 at 17:41 +0800, Freeman Fang wrote:
>>    
>>
>>>If you can provide your test case, that would be great for us to write 
>>>the test.
>>>      
>>>
>>I'll put this together now...
>>
>>    
>>
>
>Here's a really simple provider that attempts to set a test header.  The
>main line GETs the message from the provider and checks if the header is
>set.
>  
>
>------------------------------------------------------------------------
>
>package test;
>
>import java.io.StringReader;
>import java.net.HttpURLConnection;
>import java.net.URL;
>import java.util.ArrayList;
>import java.util.List;
>import java.util.Map;
>
>import javax.annotation.Resource;
>import javax.xml.transform.Source;
>import javax.xml.transform.stream.StreamSource;
>import javax.xml.ws.BindingType;
>import javax.xml.ws.Endpoint;
>import javax.xml.ws.Provider;
>import javax.xml.ws.Service;
>import javax.xml.ws.ServiceMode;
>import javax.xml.ws.WebServiceContext;
>import javax.xml.ws.WebServiceProvider;
>import javax.xml.ws.handler.MessageContext;
>
>@WebServiceProvider()
>@ServiceMode(value = Service.Mode.PAYLOAD)
>//@BindingType(value = "http://cxf.apache.org/bindings/xformat")
>@BindingType(value = "http://celtix.objectweb.org/bindings/xmlformat")
>public class HeaderTest implements Provider<Source> {
>    
>    public static final String TEST_HEADER_NAME = "Test";
>    
>    @Resource
>    WebServiceContext wsCtx; 
>    
>    @SuppressWarnings("unchecked")
>    public Source invoke(Source arg0) {
>        MessageContext ctx = wsCtx.getMessageContext();
>
>        List<String> hdr = new ArrayList<String>();
>        hdr.add("test-value");
>        Map<String, List<String>> hdrs = (Map<String, List<String>>)ctx.get(MessageContext.HTTP_RESPONSE_HEADERS);
>            
>        if (hdrs != null) {
>            hdrs.put(TEST_HEADER_NAME, hdr);
>        } 
>        return new StreamSource(new StringReader("<foo/>"));
>    }
>
>    public static void main(String[] args) throws Exception {
>        try { 
>            URL url = new URL("http://localhost:9999");
>            System.out.println("publishing endpoint");
>            Endpoint.publish(url.toString(), new HeaderTest());
>            System.out.println("server ready");
>            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
>
>            conn.connect();
>            boolean ok = false;
>            Map<String, List<String>> hdrs = conn.getHeaderFields();
>            for (String hdrName : hdrs.keySet()) {
>                List<String> value = hdrs.get(hdrName);
>                System.out.println(hdrName + ": " + value);
>                if (TEST_HEADER_NAME.equals(hdrName)) {
>                    ok = true;
>                }
>            }
>            System.out.println("Test " + (ok ? "PASSED" : "FAILED"));
>        } finally {
>            System.exit(0);
>        }
>    }
>}
>  
>


-- 
Freeman Fang
Software Engineer

IONA Asia Pacific Software Development Center
No.2 Floor A Unit Information Center
Zhongguancun Software Park Haidian District,
Beijing, P.R.China

Tel.: +86-10-82825151 -  ex. 551
Fax: +86-10-8282-5210
freeman.fang@iona.com
-------------------------------------------------
Making Software Work Together TM




Re: setting HTTP response headers

Posted by Conrad O'Dea <co...@iona.com>.
On Thu, 2006-11-02 at 10:31 +0000, Conrad O'Dea wrote:
> On Thu, 2006-11-02 at 17:41 +0800, Freeman Fang wrote:
> > 
> > If you can provide your test case, that would be great for us to write 
> > the test.
> 
> I'll put this together now...
> 

Here's a really simple provider that attempts to set a test header.  The
main line GETs the message from the provider and checks if the header is
set.

Re: setting HTTP response headers

Posted by Conrad O'Dea <co...@iona.com>.
On Thu, 2006-11-02 at 17:41 +0800, Freeman Fang wrote:
> Hi Conrad,
> 
> If you can provide your test case, that would be great for us to write 
> the test.

I'll put this together now...


Re: setting HTTP response headers

Posted by Freeman Fang <fr...@iona.com>.
Hi Conrad,

If you can provide your test case, that would be great for us to write 
the test.

Thanks very much

Cheers

Freeman

Conrad O'Dea wrote:

>Cool thanks. 
>
>Is there anything I can do to help?
>
>thanks
>Conrad 
>
>On Thu, 2006-11-02 at 16:16 +0800, Freeman Fang wrote:
>  
>
>>Hi Conrad,
>>
>>I donot think we can get outMessage from EndpointImpl so far.
>>And Willem give more reasonable explaination for it
>>======================
>>Hi Conrad,
>>
>>I had done some work of the context mapping in CXF,  I can give you some
>>explanations.
>>
>>First, CXF has a jax-ws front end to wrap all the things about the
>>jax-ws, so I implemented an mapper which get the connection between
>>jax-ws context and rt-core message context.
>>Current CXF jax-ws front end mapper just implement a limited mapping of
>>BindingProvider and MessageContext's defined properties to rt-core, that
>>is why you can't use
>>javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS to get the
>>request headers.
>>
>>Second, I went though the code and found the http header which you want
>>to pass to the response wouldn't take any effect because you just set on
>>the context ( which we get from  inMessage's context)and it would not
>>effect to outMessage's context in current code.
>>=================================
>>
>>A jira task https://issues.apache.org/jira/browse/CXF-197 already 
>>created to track this issue and Willem will fix it very soon
>>
>>Thanks very much
>>
>>Freeman
>> 
>>Conrad O'Dea wrote:
>>
>>    
>>
>>>Hi Freeman, 
>>>
>>>On Thu, 2006-11-02 at 11:04 +0800, Freeman Fang wrote:
>>>
>>> 
>>>
>>>      
>>>
>>>>I guess you need set headers into  outMessage which is  instance of 
>>>>org.apache.cxf.message.Message
>>>>
>>>>outMessage.put(Message.PROTOCOL_HEADERS, hdrs);
>>>>
>>>>I just paste some code from AbstractHTTPDestination.java  and 
>>>>JettyHTTPDestination.java where do setHeaders work
>>>>
>>>>protected void setHeaders(Message message) {
>>>>   Map<String, List<String>> requestHeaders = new HashMap<String, 
>>>>List<String>>();
>>>>   copyRequestHeaders(message, requestHeaders);
>>>>   message.put(Message.PROTOCOL_HEADERS, requestHeaders);
>>>>   ....
>>>>}
>>>>   
>>>>
>>>>        
>>>>
>>>thanks for that.  The code I'm writing is basically JAX-WS (although
>>>becoming more cxf specific).  Is it possible to get from the
>>>EndpointImpl to the outMessage?
>>>
>>>
>>> 
>>>
>>>      
>>>
>>>>   protected void copyRequestHeaders(Message message, Map<String, 
>>>>List<String>> headers) {
>>>>       HttpRequest req = (HttpRequest)message.get(HTTP_REQUEST);
>>>>       for (Enumeration e = req.getFieldNames(); e.hasMoreElements();) {
>>>>           String fname = (String)e.nextElement();
>>>>           List<String> values;
>>>>           if (headers.containsKey(fname)) {
>>>>               values = headers.get(fname);
>>>>           } else {
>>>>               values = new ArrayList<String>();
>>>>               headers.put(fname, values);
>>>>           }
>>>>           for (Enumeration e2 = req.getFieldValues(fname); 
>>>>e2.hasMoreElements();) {
>>>>               String val = (String)e2.nextElement();
>>>>               values.add(val);
>>>>           }
>>>>       }
>>>>   }
>>>>Thanks very much
>>>>
>>>>Freeman
>>>>
>>>>
>>>>Conrad O'Dea wrote:
>>>>
>>>>   
>>>>
>>>>        
>>>>
>>>>>Hi,
>>>>>
>>>>>I'm porting a RESTy service from Celtix 1.something to CXF and have  
>>>>>noticed some differences in the way the HTTP protocol headers are  
>>>>>handled.
>>>>>
>>>>>For example, in a service implemented using a Provider, I've tried  
>>>>>getting the headers from the MessageContext like this:
>>>>>
>>>>>   ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);
>>>>>
>>>>>This returns null though.  Calling:
>>>>>
>>>>>   Map<String, List<String>>hdrs = ctx.get 
>>>>>(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>>>>>
>>>>>works though and I can get the headers ok.
>>>>>
>>>>>I also need to set the response headers and should be able to set a  
>>>>>header like this:
>>>>>
>>>>>     Map<String, List<String>>hdrs = ctx.get 
>>>>>(javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
>>>>>     hdrs.put(headerName, headerValue);
>>>>>
>>>>>and have it added to the outgoing response.  However, as above using  
>>>>>the MessageContext property does not return anything.  I've tried  
>>>>>using the org.apache.cxf.message.Message property like this
>>>>>
>>>>>     Map<String, List<String>>hdrs = ctx.get 
>>>>>(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>>>>>     hdrs.put(headerName, headerValue);
>>>>>
>>>>>but there's no sign that the header has been correctly added.  Is  
>>>>>there some other way to set a HTTP header on the response?
>>>>>
>>>>>thanks
>>>>>Conrad
>>>>>
>>>>>
>>>>>     
>>>>>
>>>>>          
>>>>>
>>>>   
>>>>
>>>>        
>>>>
>>> 
>>>
>>>      
>>>
>>    
>>
>
>  
>


-- 
Freeman Fang
Software Engineer

IONA Asia Pacific Software Development Center
No.2 Floor A Unit Information Center
Zhongguancun Software Park Haidian District,
Beijing, P.R.China

Tel.: +86-10-82825151 -  ex. 551
Fax: +86-10-8282-5210
freeman.fang@iona.com
-------------------------------------------------
Making Software Work Together TM




Re: setting HTTP response headers

Posted by Willem Jiang <ni...@iona.com>.
Hi Conrad,

I had found a way to fix this issue, by mapping the context information 
with  exchange which is connected both inMessage and outMessage in the 
ProviderInvoker and JAXWSMethodInvoker.
I almost finish it, and need to write some test code. Can you send me 
your REST Server code?  Then I can put it into the systest code.
Chrees,

Willem
Conrad O'Dea wrote:

>Cool thanks. 
>
>Is there anything I can do to help?
>
>thanks
>Conrad 
>
>On Thu, 2006-11-02 at 16:16 +0800, Freeman Fang wrote:
>  
>
>>Hi Conrad,
>>
>>I donot think we can get outMessage from EndpointImpl so far.
>>And Willem give more reasonable explaination for it
>>======================
>>Hi Conrad,
>>
>>I had done some work of the context mapping in CXF,  I can give you some
>>explanations.
>>
>>First, CXF has a jax-ws front end to wrap all the things about the
>>jax-ws, so I implemented an mapper which get the connection between
>>jax-ws context and rt-core message context.
>>Current CXF jax-ws front end mapper just implement a limited mapping of
>>BindingProvider and MessageContext's defined properties to rt-core, that
>>is why you can't use
>>javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS to get the
>>request headers.
>>
>>Second, I went though the code and found the http header which you want
>>to pass to the response wouldn't take any effect because you just set on
>>the context ( which we get from  inMessage's context)and it would not
>>effect to outMessage's context in current code.
>>=================================
>>
>>A jira task https://issues.apache.org/jira/browse/CXF-197 already 
>>created to track this issue and Willem will fix it very soon
>>
>>Thanks very much
>>
>>Freeman
>> 
>>Conrad O'Dea wrote:
>>
>>    
>>
>>>Hi Freeman, 
>>>
>>>On Thu, 2006-11-02 at 11:04 +0800, Freeman Fang wrote:
>>>
>>> 
>>>
>>>      
>>>
>>>>I guess you need set headers into  outMessage which is  instance of 
>>>>org.apache.cxf.message.Message
>>>>
>>>>outMessage.put(Message.PROTOCOL_HEADERS, hdrs);
>>>>
>>>>I just paste some code from AbstractHTTPDestination.java  and 
>>>>JettyHTTPDestination.java where do setHeaders work
>>>>
>>>>protected void setHeaders(Message message) {
>>>>   Map<String, List<String>> requestHeaders = new HashMap<String, 
>>>>List<String>>();
>>>>   copyRequestHeaders(message, requestHeaders);
>>>>   message.put(Message.PROTOCOL_HEADERS, requestHeaders);
>>>>   ....
>>>>}
>>>>   
>>>>
>>>>        
>>>>
>>>thanks for that.  The code I'm writing is basically JAX-WS (although
>>>becoming more cxf specific).  Is it possible to get from the
>>>EndpointImpl to the outMessage?
>>>
>>>
>>> 
>>>
>>>      
>>>
>>>>   protected void copyRequestHeaders(Message message, Map<String, 
>>>>List<String>> headers) {
>>>>       HttpRequest req = (HttpRequest)message.get(HTTP_REQUEST);
>>>>       for (Enumeration e = req.getFieldNames(); e.hasMoreElements();) {
>>>>           String fname = (String)e.nextElement();
>>>>           List<String> values;
>>>>           if (headers.containsKey(fname)) {
>>>>               values = headers.get(fname);
>>>>           } else {
>>>>               values = new ArrayList<String>();
>>>>               headers.put(fname, values);
>>>>           }
>>>>           for (Enumeration e2 = req.getFieldValues(fname); 
>>>>e2.hasMoreElements();) {
>>>>               String val = (String)e2.nextElement();
>>>>               values.add(val);
>>>>           }
>>>>       }
>>>>   }
>>>>Thanks very much
>>>>
>>>>Freeman
>>>>
>>>>
>>>>Conrad O'Dea wrote:
>>>>
>>>>   
>>>>
>>>>        
>>>>
>>>>>Hi,
>>>>>
>>>>>I'm porting a RESTy service from Celtix 1.something to CXF and have  
>>>>>noticed some differences in the way the HTTP protocol headers are  
>>>>>handled.
>>>>>
>>>>>For example, in a service implemented using a Provider, I've tried  
>>>>>getting the headers from the MessageContext like this:
>>>>>
>>>>>   ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);
>>>>>
>>>>>This returns null though.  Calling:
>>>>>
>>>>>   Map<String, List<String>>hdrs = ctx.get 
>>>>>(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>>>>>
>>>>>works though and I can get the headers ok.
>>>>>
>>>>>I also need to set the response headers and should be able to set a  
>>>>>header like this:
>>>>>
>>>>>     Map<String, List<String>>hdrs = ctx.get 
>>>>>(javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
>>>>>     hdrs.put(headerName, headerValue);
>>>>>
>>>>>and have it added to the outgoing response.  However, as above using  
>>>>>the MessageContext property does not return anything.  I've tried  
>>>>>using the org.apache.cxf.message.Message property like this
>>>>>
>>>>>     Map<String, List<String>>hdrs = ctx.get 
>>>>>(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>>>>>     hdrs.put(headerName, headerValue);
>>>>>
>>>>>but there's no sign that the header has been correctly added.  Is  
>>>>>there some other way to set a HTTP header on the response?
>>>>>
>>>>>thanks
>>>>>Conrad
>>>>>
>>>>>
>>>>>     
>>>>>
>>>>>          
>>>>>
>>>>   
>>>>
>>>>        
>>>>
>>> 
>>>
>>>      
>>>
>>    
>>
>
>
>  
>


Re: setting HTTP response headers

Posted by Conrad O'Dea <co...@iona.com>.
Cool thanks. 

Is there anything I can do to help?

thanks
Conrad 

On Thu, 2006-11-02 at 16:16 +0800, Freeman Fang wrote:
> Hi Conrad,
> 
> I donot think we can get outMessage from EndpointImpl so far.
> And Willem give more reasonable explaination for it
> ======================
> Hi Conrad,
> 
> I had done some work of the context mapping in CXF,  I can give you some
> explanations.
> 
> First, CXF has a jax-ws front end to wrap all the things about the
> jax-ws, so I implemented an mapper which get the connection between
> jax-ws context and rt-core message context.
> Current CXF jax-ws front end mapper just implement a limited mapping of
> BindingProvider and MessageContext's defined properties to rt-core, that
> is why you can't use
> javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS to get the
> request headers.
> 
> Second, I went though the code and found the http header which you want
> to pass to the response wouldn't take any effect because you just set on
> the context ( which we get from  inMessage's context)and it would not
> effect to outMessage's context in current code.
> =================================
> 
> A jira task https://issues.apache.org/jira/browse/CXF-197 already 
> created to track this issue and Willem will fix it very soon
> 
> Thanks very much
> 
> Freeman
>  
> Conrad O'Dea wrote:
> 
> >Hi Freeman, 
> >
> >On Thu, 2006-11-02 at 11:04 +0800, Freeman Fang wrote:
> >
> >  
> >
> >>I guess you need set headers into  outMessage which is  instance of 
> >>org.apache.cxf.message.Message
> >>
> >>outMessage.put(Message.PROTOCOL_HEADERS, hdrs);
> >>
> >>I just paste some code from AbstractHTTPDestination.java  and 
> >>JettyHTTPDestination.java where do setHeaders work
> >>
> >>protected void setHeaders(Message message) {
> >>    Map<String, List<String>> requestHeaders = new HashMap<String, 
> >>List<String>>();
> >>    copyRequestHeaders(message, requestHeaders);
> >>    message.put(Message.PROTOCOL_HEADERS, requestHeaders);
> >>    ....
> >>}
> >>    
> >>
> >
> >thanks for that.  The code I'm writing is basically JAX-WS (although
> >becoming more cxf specific).  Is it possible to get from the
> >EndpointImpl to the outMessage?
> >
> >
> >  
> >
> >> 
> >>    protected void copyRequestHeaders(Message message, Map<String, 
> >>List<String>> headers) {
> >>        HttpRequest req = (HttpRequest)message.get(HTTP_REQUEST);
> >>        for (Enumeration e = req.getFieldNames(); e.hasMoreElements();) {
> >>            String fname = (String)e.nextElement();
> >>            List<String> values;
> >>            if (headers.containsKey(fname)) {
> >>                values = headers.get(fname);
> >>            } else {
> >>                values = new ArrayList<String>();
> >>                headers.put(fname, values);
> >>            }
> >>            for (Enumeration e2 = req.getFieldValues(fname); 
> >>e2.hasMoreElements();) {
> >>                String val = (String)e2.nextElement();
> >>                values.add(val);
> >>            }
> >>        }
> >>    }
> >>Thanks very much
> >>
> >>Freeman
> >>
> >>
> >>Conrad O'Dea wrote:
> >>
> >>    
> >>
> >>>Hi,
> >>>
> >>>I'm porting a RESTy service from Celtix 1.something to CXF and have  
> >>>noticed some differences in the way the HTTP protocol headers are  
> >>>handled.
> >>>
> >>>For example, in a service implemented using a Provider, I've tried  
> >>>getting the headers from the MessageContext like this:
> >>>
> >>>    ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);
> >>>
> >>>This returns null though.  Calling:
> >>>
> >>>    Map<String, List<String>>hdrs = ctx.get 
> >>>(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
> >>>
> >>>works though and I can get the headers ok.
> >>>
> >>>I also need to set the response headers and should be able to set a  
> >>>header like this:
> >>>
> >>>      Map<String, List<String>>hdrs = ctx.get 
> >>>(javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
> >>>      hdrs.put(headerName, headerValue);
> >>>
> >>>and have it added to the outgoing response.  However, as above using  
> >>>the MessageContext property does not return anything.  I've tried  
> >>>using the org.apache.cxf.message.Message property like this
> >>>
> >>>      Map<String, List<String>>hdrs = ctx.get 
> >>>(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
> >>>      hdrs.put(headerName, headerValue);
> >>>
> >>>but there's no sign that the header has been correctly added.  Is  
> >>>there some other way to set a HTTP header on the response?
> >>>
> >>>thanks
> >>>Conrad
> >>>
> >>>
> >>>      
> >>>
> >>    
> >>
> >
> >  
> >
> 
> 


Re: setting HTTP response headers

Posted by Freeman Fang <fr...@iona.com>.
Hi Conrad,

I donot think we can get outMessage from EndpointImpl so far.
And Willem give more reasonable explaination for it
======================
Hi Conrad,

I had done some work of the context mapping in CXF,  I can give you some
explanations.

First, CXF has a jax-ws front end to wrap all the things about the
jax-ws, so I implemented an mapper which get the connection between
jax-ws context and rt-core message context.
Current CXF jax-ws front end mapper just implement a limited mapping of
BindingProvider and MessageContext's defined properties to rt-core, that
is why you can't use
javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS to get the
request headers.

Second, I went though the code and found the http header which you want
to pass to the response wouldn't take any effect because you just set on
the context ( which we get from  inMessage's context)and it would not
effect to outMessage's context in current code.
=================================

A jira task https://issues.apache.org/jira/browse/CXF-197 already 
created to track this issue and Willem will fix it very soon

Thanks very much

Freeman
 
Conrad O'Dea wrote:

>Hi Freeman, 
>
>On Thu, 2006-11-02 at 11:04 +0800, Freeman Fang wrote:
>
>  
>
>>I guess you need set headers into  outMessage which is  instance of 
>>org.apache.cxf.message.Message
>>
>>outMessage.put(Message.PROTOCOL_HEADERS, hdrs);
>>
>>I just paste some code from AbstractHTTPDestination.java  and 
>>JettyHTTPDestination.java where do setHeaders work
>>
>>protected void setHeaders(Message message) {
>>    Map<String, List<String>> requestHeaders = new HashMap<String, 
>>List<String>>();
>>    copyRequestHeaders(message, requestHeaders);
>>    message.put(Message.PROTOCOL_HEADERS, requestHeaders);
>>    ....
>>}
>>    
>>
>
>thanks for that.  The code I'm writing is basically JAX-WS (although
>becoming more cxf specific).  Is it possible to get from the
>EndpointImpl to the outMessage?
>
>
>  
>
>> 
>>    protected void copyRequestHeaders(Message message, Map<String, 
>>List<String>> headers) {
>>        HttpRequest req = (HttpRequest)message.get(HTTP_REQUEST);
>>        for (Enumeration e = req.getFieldNames(); e.hasMoreElements();) {
>>            String fname = (String)e.nextElement();
>>            List<String> values;
>>            if (headers.containsKey(fname)) {
>>                values = headers.get(fname);
>>            } else {
>>                values = new ArrayList<String>();
>>                headers.put(fname, values);
>>            }
>>            for (Enumeration e2 = req.getFieldValues(fname); 
>>e2.hasMoreElements();) {
>>                String val = (String)e2.nextElement();
>>                values.add(val);
>>            }
>>        }
>>    }
>>Thanks very much
>>
>>Freeman
>>
>>
>>Conrad O'Dea wrote:
>>
>>    
>>
>>>Hi,
>>>
>>>I'm porting a RESTy service from Celtix 1.something to CXF and have  
>>>noticed some differences in the way the HTTP protocol headers are  
>>>handled.
>>>
>>>For example, in a service implemented using a Provider, I've tried  
>>>getting the headers from the MessageContext like this:
>>>
>>>    ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);
>>>
>>>This returns null though.  Calling:
>>>
>>>    Map<String, List<String>>hdrs = ctx.get 
>>>(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>>>
>>>works though and I can get the headers ok.
>>>
>>>I also need to set the response headers and should be able to set a  
>>>header like this:
>>>
>>>      Map<String, List<String>>hdrs = ctx.get 
>>>(javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
>>>      hdrs.put(headerName, headerValue);
>>>
>>>and have it added to the outgoing response.  However, as above using  
>>>the MessageContext property does not return anything.  I've tried  
>>>using the org.apache.cxf.message.Message property like this
>>>
>>>      Map<String, List<String>>hdrs = ctx.get 
>>>(org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>>>      hdrs.put(headerName, headerValue);
>>>
>>>but there's no sign that the header has been correctly added.  Is  
>>>there some other way to set a HTTP header on the response?
>>>
>>>thanks
>>>Conrad
>>>
>>>
>>>      
>>>
>>    
>>
>
>  
>


-- 
Freeman Fang
Software Engineer

IONA Asia Pacific Software Development Center
No.2 Floor A Unit Information Center
Zhongguancun Software Park Haidian District,
Beijing, P.R.China

Tel.: +86-10-82825151 -  ex. 551
Fax: +86-10-8282-5210
freeman.fang@iona.com
-------------------------------------------------
Making Software Work Together TM




Re: setting HTTP response headers

Posted by Conrad O'Dea <co...@iona.com>.
Hi Willem, 

On Fri, 2006-11-03 at 15:19 +0800, Willem Jiang wrote:

> You can use MessageContext to get request header and response header
> with the last CXF code in the repository.
> You can also get the response header from client side by using the
> response context.
> Please see the Systest of REST
> RestClientServerTest.testHttpGETDispatcher() for more detail.
> 

That works. 

thanks
Conrad 



Re: setting HTTP response headers

Posted by Willem Jiang <ni...@iona.com>.
Hi Conrad,

You can use MessageContext to get request header and response header
with the last CXF code in the repository.
You can also get the response header from client side by using the
response context.
Please see the Systest of REST
RestClientServerTest.testHttpGETDispatcher() for more detail.

Enjoy!

Willem.

Conrad O'Dea wrote:

>On Thu, 2006-11-02 at 10:00 +0000, Sergey Beryozkin wrote:
>  
>
>>Do we have to write this code oursellves or is it something which will end up in some common utility class ?
>>    
>>
>
>It's a workaround.  The response header should be settable through the
>MessageContext. 
>
>
>  
>
>>Cheers, Sergey
>>
>>
>>    
>>
>>>On Thu, 2006-11-02 at 07:48 +0000, Conrad O'Dea wrote:
>>>
>>>      
>>>
>>>>thanks for that.  The code I'm writing is basically JAX-WS (although
>>>>becoming more cxf specific).  Is it possible to get from the
>>>>EndpointImpl to the outMessage?
>>>>
>>>>        
>>>>
>>>I've managed to get the response header successfully set.  There's a
>>>path to the outbound Message through the MessageContext:
>>>
>>> WrappedMessageContext wmc = (WrappedMessageContext)ctx;
>>> Message msg = wmc.getWrappedMessage();
>>> Exchange xchg = msg.getExchange();
>>> Message outMsg = xchg.getOutMessage();
>>> respHeaders = (Map<String, List<String>>)outMsg.get(Message.PROTOCOL_HEADERS);
>>> respHeaders.put(headerName, headerValue);
>>>
>>>and then the headers I set appear in the HTTP response.
>>>
>>>
>>>
>>>      
>>>
>
>
>  
>



Re: setting HTTP response headers

Posted by Willem Jiang <ni...@iona.com>.
Hi Conrad,

You can use MessageContext to get request header and response header 
with the last CXF code in the repository.
You can also get the response header from client side by using the 
response context.
Please see the Systest of REST 
RestClientServerTest.testHttpGETDispatcher() for more detail.

Enjoy!

Willem.

Conrad O'Dea wrote:

>On Thu, 2006-11-02 at 10:00 +0000, Sergey Beryozkin wrote:
>  
>
>>Do we have to write this code oursellves or is it something which will end up in some common utility class ?
>>    
>>
>
>It's a workaround.  The response header should be settable through the
>MessageContext. 
>
>
>  
>
>>Cheers, Sergey
>>
>>
>>    
>>
>>>On Thu, 2006-11-02 at 07:48 +0000, Conrad O'Dea wrote:
>>>
>>>      
>>>
>>>>thanks for that.  The code I'm writing is basically JAX-WS (although
>>>>becoming more cxf specific).  Is it possible to get from the
>>>>EndpointImpl to the outMessage?
>>>>
>>>>        
>>>>
>>>I've managed to get the response header successfully set.  There's a
>>>path to the outbound Message through the MessageContext:
>>>
>>> WrappedMessageContext wmc = (WrappedMessageContext)ctx;
>>> Message msg = wmc.getWrappedMessage();
>>> Exchange xchg = msg.getExchange();
>>> Message outMsg = xchg.getOutMessage();
>>> respHeaders = (Map<String, List<String>>)outMsg.get(Message.PROTOCOL_HEADERS);
>>> respHeaders.put(headerName, headerValue);
>>>
>>>and then the headers I set appear in the HTTP response.
>>>
>>>
>>>
>>>      
>>>
>
>
>  
>


Re: setting HTTP response headers

Posted by Conrad O'Dea <co...@iona.com>.
On Thu, 2006-11-02 at 10:00 +0000, Sergey Beryozkin wrote:
> Do we have to write this code oursellves or is it something which will end up in some common utility class ?

It's a workaround.  The response header should be settable through the
MessageContext. 


> 
> Cheers, Sergey
> 
> 
> > On Thu, 2006-11-02 at 07:48 +0000, Conrad O'Dea wrote:
> > 
> >> thanks for that.  The code I'm writing is basically JAX-WS (although
> >> becoming more cxf specific).  Is it possible to get from the
> >> EndpointImpl to the outMessage?
> >> 
> > 
> > I've managed to get the response header successfully set.  There's a
> > path to the outbound Message through the MessageContext:
> > 
> >  WrappedMessageContext wmc = (WrappedMessageContext)ctx;
> >  Message msg = wmc.getWrappedMessage();
> >  Exchange xchg = msg.getExchange();
> >  Message outMsg = xchg.getOutMessage();
> >  respHeaders = (Map<String, List<String>>)outMsg.get(Message.PROTOCOL_HEADERS);
> >  respHeaders.put(headerName, headerValue);
> > 
> > and then the headers I set appear in the HTTP response.
> > 
> > 
> >


Re: setting HTTP response headers

Posted by Sergey Beryozkin <se...@iona.com>.
Do we have to write this code oursellves or is it something which will end up in some common utility class ?

Cheers, Sergey


> On Thu, 2006-11-02 at 07:48 +0000, Conrad O'Dea wrote:
> 
>> thanks for that.  The code I'm writing is basically JAX-WS (although
>> becoming more cxf specific).  Is it possible to get from the
>> EndpointImpl to the outMessage?
>> 
> 
> I've managed to get the response header successfully set.  There's a
> path to the outbound Message through the MessageContext:
> 
>  WrappedMessageContext wmc = (WrappedMessageContext)ctx;
>  Message msg = wmc.getWrappedMessage();
>  Exchange xchg = msg.getExchange();
>  Message outMsg = xchg.getOutMessage();
>  respHeaders = (Map<String, List<String>>)outMsg.get(Message.PROTOCOL_HEADERS);
>  respHeaders.put(headerName, headerValue);
> 
> and then the headers I set appear in the HTTP response.
> 
> 
>

Re: setting HTTP response headers

Posted by Conrad O'Dea <co...@iona.com>.
On Thu, 2006-11-02 at 07:48 +0000, Conrad O'Dea wrote:

> thanks for that.  The code I'm writing is basically JAX-WS (although
> becoming more cxf specific).  Is it possible to get from the
> EndpointImpl to the outMessage?
> 

I've managed to get the response header successfully set.  There's a
path to the outbound Message through the MessageContext:

  WrappedMessageContext wmc = (WrappedMessageContext)ctx;
  Message msg = wmc.getWrappedMessage();
  Exchange xchg = msg.getExchange();
  Message outMsg = xchg.getOutMessage();
  respHeaders = (Map<String, List<String>>)outMsg.get(Message.PROTOCOL_HEADERS);
  respHeaders.put(headerName, headerValue);

and then the headers I set appear in the HTTP response.




Re: setting HTTP response headers

Posted by Conrad O'Dea <co...@iona.com>.
Hi Freeman, 

On Thu, 2006-11-02 at 11:04 +0800, Freeman Fang wrote:

> I guess you need set headers into  outMessage which is  instance of 
> org.apache.cxf.message.Message
> 
> outMessage.put(Message.PROTOCOL_HEADERS, hdrs);
> 
> I just paste some code from AbstractHTTPDestination.java  and 
> JettyHTTPDestination.java where do setHeaders work
> 
> protected void setHeaders(Message message) {
>     Map<String, List<String>> requestHeaders = new HashMap<String, 
> List<String>>();
>     copyRequestHeaders(message, requestHeaders);
>     message.put(Message.PROTOCOL_HEADERS, requestHeaders);
>     ....
> }

thanks for that.  The code I'm writing is basically JAX-WS (although
becoming more cxf specific).  Is it possible to get from the
EndpointImpl to the outMessage?


>  
>     protected void copyRequestHeaders(Message message, Map<String, 
> List<String>> headers) {
>         HttpRequest req = (HttpRequest)message.get(HTTP_REQUEST);
>         for (Enumeration e = req.getFieldNames(); e.hasMoreElements();) {
>             String fname = (String)e.nextElement();
>             List<String> values;
>             if (headers.containsKey(fname)) {
>                 values = headers.get(fname);
>             } else {
>                 values = new ArrayList<String>();
>                 headers.put(fname, values);
>             }
>             for (Enumeration e2 = req.getFieldValues(fname); 
> e2.hasMoreElements();) {
>                 String val = (String)e2.nextElement();
>                 values.add(val);
>             }
>         }
>     }
> Thanks very much
> 
> Freeman
> 
> 
> Conrad O'Dea wrote:
> 
> > Hi,
> >
> > I'm porting a RESTy service from Celtix 1.something to CXF and have  
> > noticed some differences in the way the HTTP protocol headers are  
> > handled.
> >
> > For example, in a service implemented using a Provider, I've tried  
> > getting the headers from the MessageContext like this:
> >
> >     ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);
> >
> > This returns null though.  Calling:
> >
> >     Map<String, List<String>>hdrs = ctx.get 
> > (org.apache.cxf.message.Message.PROTOCOL_HEADERS);
> >
> > works though and I can get the headers ok.
> >
> > I also need to set the response headers and should be able to set a  
> > header like this:
> >
> >       Map<String, List<String>>hdrs = ctx.get 
> > (javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
> >       hdrs.put(headerName, headerValue);
> >
> > and have it added to the outgoing response.  However, as above using  
> > the MessageContext property does not return anything.  I've tried  
> > using the org.apache.cxf.message.Message property like this
> >
> >       Map<String, List<String>>hdrs = ctx.get 
> > (org.apache.cxf.message.Message.PROTOCOL_HEADERS);
> >       hdrs.put(headerName, headerValue);
> >
> > but there's no sign that the header has been correctly added.  Is  
> > there some other way to set a HTTP header on the response?
> >
> > thanks
> > Conrad
> >
> >
> 
> 


Re: setting HTTP response headers

Posted by Freeman Fang <fr...@iona.com>.
Hi Conrad,

I guess you need set headers into  outMessage which is  instance of 
org.apache.cxf.message.Message

outMessage.put(Message.PROTOCOL_HEADERS, hdrs);

I just paste some code from AbstractHTTPDestination.java  and 
JettyHTTPDestination.java where do setHeaders work

protected void setHeaders(Message message) {
    Map<String, List<String>> requestHeaders = new HashMap<String, 
List<String>>();
    copyRequestHeaders(message, requestHeaders);
    message.put(Message.PROTOCOL_HEADERS, requestHeaders);
    ....
}
 
    protected void copyRequestHeaders(Message message, Map<String, 
List<String>> headers) {
        HttpRequest req = (HttpRequest)message.get(HTTP_REQUEST);
        for (Enumeration e = req.getFieldNames(); e.hasMoreElements();) {
            String fname = (String)e.nextElement();
            List<String> values;
            if (headers.containsKey(fname)) {
                values = headers.get(fname);
            } else {
                values = new ArrayList<String>();
                headers.put(fname, values);
            }
            for (Enumeration e2 = req.getFieldValues(fname); 
e2.hasMoreElements();) {
                String val = (String)e2.nextElement();
                values.add(val);
            }
        }
    }
Thanks very much

Freeman


Conrad O'Dea wrote:

> Hi,
>
> I'm porting a RESTy service from Celtix 1.something to CXF and have  
> noticed some differences in the way the HTTP protocol headers are  
> handled.
>
> For example, in a service implemented using a Provider, I've tried  
> getting the headers from the MessageContext like this:
>
>     ctx.get(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS);
>
> This returns null though.  Calling:
>
>     Map<String, List<String>>hdrs = ctx.get 
> (org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>
> works though and I can get the headers ok.
>
> I also need to set the response headers and should be able to set a  
> header like this:
>
>       Map<String, List<String>>hdrs = ctx.get 
> (javax.xml.ws.handler.MessageContext.HTTP_RESPONSE_HEADERS);
>       hdrs.put(headerName, headerValue);
>
> and have it added to the outgoing response.  However, as above using  
> the MessageContext property does not return anything.  I've tried  
> using the org.apache.cxf.message.Message property like this
>
>       Map<String, List<String>>hdrs = ctx.get 
> (org.apache.cxf.message.Message.PROTOCOL_HEADERS);
>       hdrs.put(headerName, headerValue);
>
> but there's no sign that the header has been correctly added.  Is  
> there some other way to set a HTTP header on the response?
>
> thanks
> Conrad
>
>


-- 
Freeman Fang
Software Engineer

IONA Asia Pacific Software Development Center
No.2 Floor A Unit Information Center
Zhongguancun Software Park Haidian District,
Beijing, P.R.China

Tel.: +86-10-82825151 -  ex. 551
Fax: +86-10-8282-5210
freeman.fang@iona.com
-------------------------------------------------
Making Software Work Together TM