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/06 05:38:01 UTC

[JAX-RS][Exchange] Accessing the in/out message in service implementation

Hi All,

I was hoping to have some things set in the handlers, e.g. username 
passed in the Authorization token; and have that username (or a java 
bean) available when the method is finally invoked.

I've read somewhere that the class org.apache.cxf.message.Exchange would 
have the in and out Message available for JAX-RS services, however, I am 
not sure how to get the said object. I have tried having the following 
as method parameter or as class variable to no avail:

@Context
Exchange ex

As a parameter, the handlers are able to finish, but when it comes to 
invocation of the actual method/endpoint I get this exception:
<ns1:faultstring 
xmlns:ns1="http://cxf.apache.org/bindings/xformat">java.lang.IllegalArgumentException: 
wrong number of arguments</ns1:faultstring>

As a class variable, the said object is always null.

Again, my thanks.

Gabo

Re: [JAX-RS][Exchange] Accessing the in/out message in service implementation

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

Just to make it simpler.. What would be the counterpart of 
WebServiceContext for ReST services?

Again, my thanks.

Gabo

Gabo Manuel wrote:
> Hi All,
>
> I was hoping to have some things set in the handlers, e.g. username 
> passed in the Authorization token; and have that username (or a java 
> bean) available when the method is finally invoked.
>
> I've read somewhere that the class org.apache.cxf.message.Exchange 
> would have the in and out Message available for JAX-RS services, 
> however, I am not sure how to get the said object. I have tried having 
> the following as method parameter or as class variable to no avail:
>
> @Context
> Exchange ex
>
> As a parameter, the handlers are able to finish, but when it comes to 
> invocation of the actual method/endpoint I get this exception:
> <ns1:faultstring 
> xmlns:ns1="http://cxf.apache.org/bindings/xformat">java.lang.IllegalArgumentException: 
> wrong number of arguments</ns1:faultstring>
>
> As a class variable, the said object is always null.
>
> Again, my thanks.
>
> Gabo
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com 
> Version: 8.0.175 / Virus Database: 270.9.0/1770 - Release Date: 11/5/2008 5:36 PM
>
>   

Re: [JAX-RS][Exchange] Accessing the in/out message in service implementation

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

HttpHeaders.getRequestHeader(key) returns List<String> and indeed, internal Message keeps a Map<String, List<String>> to represent 
headers which have been parsed earlier as the raw header value might have multiple parameters seperated by';'.

If you have a single value header and you'd like to avoid explicitly dealing with Lists you might want to consider using
MultivaluedMap :

//creates a header
HttpHeaders.getRequestHeaders().putFirst(key)

// retrieves its single value
HttpHeaders.getRequestHeaders().getFirst(key)

Cheers, Sergey

----- Original Message ----- 
From: "Gabo Manuel" <km...@solegysystems.com>
To: <us...@cxf.apache.org>
Sent: Thursday, November 06, 2008 9:52 AM
Subject: Re: [JAX-RS][Exchange] Accessing the in/out message in service implementation


> Hi All,
>
> Just some work-around I did. I used the http header as an "all-purpose"
> map. The only quirk I find here is that the header should be a List,
> otherwise HttpHeaders.getHeader(key) causes a ClassCastException. Why
> does it do so?
>
> Gabo
>
> Gabo Manuel wrote:
>> Hi All,
>>
>> I was hoping to have some things set in the handlers, e.g. username
>> passed in the Authorization token; and have that username (or a java
>> bean) available when the method is finally invoked.
>>
>> I've read somewhere that the class org.apache.cxf.message.Exchange
>> would have the in and out Message available for JAX-RS services,
>> however, I am not sure how to get the said object. I have tried having
>> the following as method parameter or as class variable to no avail:
>>
>> @Context
>> Exchange ex
>>
>> As a parameter, the handlers are able to finish, but when it comes to
>> invocation of the actual method/endpoint I get this exception:
>> <ns1:faultstring
>> xmlns:ns1="http://cxf.apache.org/bindings/xformat">java.lang.IllegalArgumentException:
>> wrong number of arguments</ns1:faultstring>
>>
>> As a class variable, the said object is always null.
>>
>> Again, my thanks.
>>
>> Gabo
>> ------------------------------------------------------------------------
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - http://www.avg.com
>> Version: 8.0.175 / Virus Database: 270.9.0/1770 - Release Date: 11/5/2008 5:36 PM
>>
>>
> 


Re: [JAX-RS][Exchange] Accessing the in/out message in service implementation

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

Just some work-around I did. I used the http header as an "all-purpose" 
map. The only quirk I find here is that the header should be a List, 
otherwise HttpHeaders.getHeader(key) causes a ClassCastException. Why 
does it do so?

Gabo

Gabo Manuel wrote:
> Hi All,
>
> I was hoping to have some things set in the handlers, e.g. username 
> passed in the Authorization token; and have that username (or a java 
> bean) available when the method is finally invoked.
>
> I've read somewhere that the class org.apache.cxf.message.Exchange 
> would have the in and out Message available for JAX-RS services, 
> however, I am not sure how to get the said object. I have tried having 
> the following as method parameter or as class variable to no avail:
>
> @Context
> Exchange ex
>
> As a parameter, the handlers are able to finish, but when it comes to 
> invocation of the actual method/endpoint I get this exception:
> <ns1:faultstring 
> xmlns:ns1="http://cxf.apache.org/bindings/xformat">java.lang.IllegalArgumentException: 
> wrong number of arguments</ns1:faultstring>
>
> As a class variable, the said object is always null.
>
> Again, my thanks.
>
> Gabo
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com 
> Version: 8.0.175 / Virus Database: 270.9.0/1770 - Release Date: 11/5/2008 5:36 PM
>
>   

Re: [JAX-RS][Exchange] Accessing the in/out message in service implementation

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

If you implement a CXF JAXRS RequestFilter then input message is available as an input parameter.
If you implement a CXF JAXRS ResponseFilter then output message is available as an output parameter.

If you do both JAXRS and JAXWS then you might want to consider using CXF in/out interceptors, those which have 
handleMessage(Message) method.

It's not possible an internal CXF Exchange be injected as part of JAXRS invocation - you might be able to get to it from a JAXWS 
WebServiceContext somehow but not from JAXRS.

Additionally, if you'd like to modify an output message only you might want to consider doing it indirectly, using JAXRS Response 
class....

Cheers, Sergey

> Hi All,
>
> I was hoping to have some things set in the handlers, e.g. username passed in the Authorization token; and have that username (or 
> a java bean) available when the method is finally invoked.
>
> I've read somewhere that the class org.apache.cxf.message.Exchange would have the in and out Message available for JAX-RS 
> services, however, I am not sure how to get the said object. I have tried having the following as method parameter or as class 
> variable to no avail:
>
> @Context
> Exchange ex
>
> As a parameter, the handlers are able to finish, but when it comes to invocation of the actual method/endpoint I get this 
> exception:
> <ns1:faultstring xmlns:ns1="http://cxf.apache.org/bindings/xformat">java.lang.IllegalArgumentException: wrong number of 
> arguments</ns1:faultstring>
>
> As a class variable, the said object is always null.
>
> Again, my thanks.
>
> Gabo