You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Gabo Manuel <km...@solegysystems.com> on 2008/11/07 06:31:20 UTC

[OutInterceptor][HttpHeader] Adding Authorization-Info entry to http header

Hi All,

I created an out interceptor, subclassing AbstractPhaseInterceptor. The 
interceptor is invoked fine, however, changes to the response header 
doesn't seem to reflect. The handleMessage is quite straightforward:

    public void handleMessage(Message message) throws Fault {
        Map<String, List<String>> responseHeaders = (Map<String, 
List<String>>)message.get(Message.PROTOCOL_HEADERS);
        logger.info("out message headers: " + responseHeaders);
       
        if (responseHeaders != null) {
            responseHeaders.put("Authentication-Info", Arrays.asList(new 
String[]{"nextnonce="+generateNonce()}));
        }
        logger.info("out message headers: " + responseHeaders);
    }

I have tried several phases:
1. SETUP -- the responseHeader is null
2. PRE_PROTOCOL - POST_PROTOCOL -- the changes to the header does not 
reflect.

Again, my thanks for the assistance.

Gabo



Re: [OutInterceptor][HttpHeader] Adding Authorization-Info entry to http header

Posted by Gabo Manuel <km...@solegysystems.com>.
Hi Sergey,

 > If you put your CXF interceptor at POST_PROTOCOL level then it should 
work.

I have tried both PRE_PROTOCOL and POST_PROTOCOL, the handleMessage() of 
the out handler does get executed but as per tcpmon, the header is never 
included. I tried PROTOCOL, but the handleMessage() was never executed.

 > Implementing ResponseHandler filter is another option

You have mentioned this before in other threads, however, I am hesitant 
since as per user guide the interfaces are to change in the short term. 
(http://cwiki.apache.org/CXF20DOC/jax-rs-jsr-311.html#JAX-RS%28JSR-311%29-Filters). 
But I'll give it a shot.

Gabo


Re: [OutInterceptor][HttpHeader] Adding Authorization-Info entry to http header

Posted by Gabo Manuel <km...@solegysystems.com>.
Hi Sergey,

 > so I'll try to write one and see what happens.

Sorry for the late reply. I tried this again with the recent snapshot 
and the headers are still lost unless I abort the interceptor chain.

The implementation I have extends AbstractPhaseInterceptor<Message>. 
Phase is at Phase.POST_PROTOCOL. Code to set the header is as follows:

        Map<String, List<String>> responseHeaders = (Map<String, 
List<String>>)message.get(Message.PROTOCOL_HEADERS);
       
        if (responseHeaders == null) {
            responseHeaders = new HashMap<String, List<String>>();
            message.put(Message.PROTOCOL_HEADERS, responseHeaders);
        }
       
        responseHeaders.put("header1", Arrays.asList(new 
String[]{"headerValue"}));
        logger.debug("out message headers: " + responseHeaders);

then I do something similar to what was done here:

http://www.nabble.com/-OutInterceptor--HttpHeader--Adding-Authorization-Info-entry-to-http-header-td20375003.html

On the other hand, I was able to successfully retain the headers using a 
ResponseHandler implementation with the following statement:

        MultivaluedMap<String, Object> map = response.getMetadata();
        map.add("header1", "headerValue");
        //I think it is worth noting in the docs it is this easy to 
modify the headers :D

So I guess I will be using the the ResponseHandler from now on. At least 
for header post-processing.

One thing though, with messages the expected value for headers is a 
List<String>, while with Response it is just String. Would it be 
possible to adjust one so they would expect the same object?

Again, my thanks.

Gabo

Sergey Beryozkin wrote:
> Hi Gabo
>
>> Hi Sergey,
>>
>> Is there a resource that shows how the filter is integrated with the 
>> rest of the application? I mean, where do I state that I want this 
>> filter used, i.e. how would the spring configuration file look like?
>
> I've updated the documentation a bit to show how filters can be 
> configured from Spring. As they're considered
> as JAXRS providres by CXF JAXRS runtime, they're registered the same 
> way as other JAXRS providers (message body readers/writers, exception 
> mappers. context handlers, etc)
>
>
>>
>> As of now, what I did is to set the phase at POST_PROTOCOL and force 
>> the chain to abort and flush whatever it already currently has. I'm 
>> thinking that skipping the USER_ and POST_ STREAM is not a good idea, 
>> no? Does the trick tho.
>
> I guess what I don't understand is whether your POST_PROTOCOL CXF 
> interceptor invoked before JAXRSOutInterceptor or after. My 
> understanding is that it has to be invoked after, so I don't get why 
> the headers are lost if a message is not aborted and preserved if it 
> is. Perhaps AbstractHttpDestination swallows custom headers ? but we 
> do have tests confirming it's not...I don't have tests at the moment 
> involving JAXRS and CXF interceptors, so I'll try to write one and see 
> what happens.
>
> Cheers, Sergey
>

Re: [OutInterceptor][HttpHeader] Adding Authorization-Info entry to http header

Posted by Sergey Beryozkin <se...@progress.com>.
Hi Gabo

> Hi Sergey,
>
> Is there a resource that shows how the filter is integrated with the rest of the application? I mean, where do I state that I want 
> this filter used, i.e. how would the spring configuration file look like?

I've updated the documentation a bit to show how filters can be configured from Spring. As they're considered
as JAXRS providres by CXF JAXRS runtime, they're registered the same way as other JAXRS providers (message body readers/writers, 
exception mappers. context handlers, etc)


>
> As of now, what I did is to set the phase at POST_PROTOCOL and force the chain to abort and flush whatever it already currently 
> has. I'm thinking that skipping the USER_ and POST_ STREAM is not a good idea, no? Does the trick tho.

I guess what I don't understand is whether your POST_PROTOCOL CXF interceptor invoked before JAXRSOutInterceptor or after. My 
understanding is that it has to be invoked after, so I don't get why the headers are lost if a message is not aborted and preserved 
if it is. Perhaps AbstractHttpDestination swallows custom headers ? but we do have tests confirming it's not...I don't have tests at 
the moment involving JAXRS and CXF interceptors, so I'll try to write one and see what happens.

Cheers, Sergey

>
> Gabo
>
> Sergey Beryozkin wrote:
>> Hi Gabo,
>>
>> Sorry, missed your message this morning.
>> If you put your CXF interceptor at POST_PROTOCOL level then it should work.
>>
>> Can you debug it and see what happens ?
>>
>> JAXRSOutInterceptor sits at the MARSHALL phase and POST_PROTOCOL should be handled after it.
>> Now that you mentioned it I reckon there's a bug in JAXRSOutInterceptor in that if you add headers from a cxf out interceptor 
>> after the invocation has returned but before JAXRSOutInterceptor is invoked then such headers might be lost/overriden - but I 
>> need to verify if it's the case.
>>
>> Implementing ResponseHandler filter is another option, but doing it at POST_PROTOCOL with CXF out interceptor should work too...
>>
>> Cheers, Sergey
>> 


Re: [OutInterceptor][HttpHeader] Adding Authorization-Info entry to http header

Posted by Gabo Manuel <km...@solegysystems.com>.
Hi Sergey,

Is there a resource that shows how the filter is integrated with the 
rest of the application? I mean, where do I state that I want this 
filter used, i.e. how would the spring configuration file look like?

As of now, what I did is to set the phase at POST_PROTOCOL and force the 
chain to abort and flush whatever it already currently has. I'm thinking 
that skipping the USER_ and POST_ STREAM is not a good idea, no? Does 
the trick tho.

Gabo

Sergey Beryozkin wrote:
> Hi Gabo,
>
> Sorry, missed your message this morning.
> If you put your CXF interceptor at POST_PROTOCOL level then it should 
> work.
>
> Can you debug it and see what happens ?
>
> JAXRSOutInterceptor sits at the MARSHALL phase and POST_PROTOCOL 
> should be handled after it.
> Now that you mentioned it I reckon there's a bug in 
> JAXRSOutInterceptor in that if you add headers from a cxf out 
> interceptor after the invocation has returned but before 
> JAXRSOutInterceptor is invoked then such headers might be 
> lost/overriden - but I need to verify if it's the case.
>
> Implementing ResponseHandler filter is another option, but doing it at 
> POST_PROTOCOL with CXF out interceptor should work too...
>
> Cheers, Sergey
>

Re: [OutInterceptor][HttpHeader] Adding Authorization-Info entry to http header

Posted by Sergey Beryozkin <se...@progress.com>.
Hi Gabo,

Sorry, missed your message this morning.
If you put your CXF interceptor at POST_PROTOCOL level then it should work.

Can you debug it and see what happens ?

JAXRSOutInterceptor sits at the MARSHALL phase and POST_PROTOCOL should be handled after it.
Now that you mentioned it I reckon there's a bug in JAXRSOutInterceptor in that if you add headers from a cxf out interceptor after 
the invocation has returned but before JAXRSOutInterceptor is invoked then such headers might be lost/overriden - but I need to 
verify if it's the case.

Implementing ResponseHandler filter is another option, but doing it at POST_PROTOCOL with CXF out interceptor should work too...

Cheers, Sergey

> Hi All,
>
> I created an out interceptor, subclassing AbstractPhaseInterceptor. The interceptor is invoked fine, however, changes to the 
> response header doesn't seem to reflect. The handleMessage is quite straightforward:
>
>    public void handleMessage(Message message) throws Fault {
>        Map<String, List<String>> responseHeaders = (Map<String, List<String>>)message.get(Message.PROTOCOL_HEADERS);
>        logger.info("out message headers: " + responseHeaders);
>       if (responseHeaders != null) {
>            responseHeaders.put("Authentication-Info", Arrays.asList(new String[]{"nextnonce="+generateNonce()}));
>        }
>        logger.info("out message headers: " + responseHeaders);
>    }
>
> I have tried several phases:
> 1. SETUP -- the responseHeader is null
> 2. PRE_PROTOCOL - POST_PROTOCOL -- the changes to the header does not reflect.
>
> Again, my thanks for the assistance.
>
> Gabo
>
>