You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Mickael Marrache <Mi...@xconnect.net> on 2012/10/14 12:40:21 UTC

Customize HTTP response for REST service

Hi again

I have a question about returning responses to HTTP requests in the context of a RESTful web service.

This is my method declaration in the WADL:

<method id="saveA" name="PUT">
   <request>
      <representation mediaType="application/xml" element="ns1:A" />
   </request>
   <response status="200">
       <param name="header1" style="header" type="xsd:string" />
   </response>
</method>

After generating the code, I get the following method signature:

public void saveA(AType atype);

If the PUT operation performed well, I would like to return an HTTP response with status code 200 and an HTTP header containing a certain value. This is indicated in the WADL, but I don't see anything in the code. Since the method saveA doesn't return anything, where is the right place to customize the response (i.e. setting the desired status code and header)?

Also, what if for a given request, different responses may be returned according to how the operation  performed? Should I add multiple responses in the method element (in the WADL)?

Thanks,
Mickael

Re: Customize HTTP response for REST service

Posted by Sergey Beryozkin <sb...@gmail.com>.
Issue has been fixed on the trunk so far:
https://issues.apache.org/jira/browse/CXF-4573

also, 'noVoidForEmptyResponses' switch has been added, 'void' needs to 
stay as a default option for now to avoid breaking the already existing code

thanks for the feedback so far,
Sergey
On 16/10/12 12:05, Sergey Beryozkin wrote:
> There's a bug with the generator losing the namespace info for types
> from imported (but not included) schemas - will be fix in next CXF
> releases, I'm glad tMap is becoming quite handy too, in cases where the
> generator itself is buggy...
>
> Thanks, Sergey
>
> On 16/10/12 11:20, Sergey Beryozkin wrote:
>> On 15/10/12 18:47, Mickael Marrache wrote:
>>> I use tMap in order for WADL2Java to generate the method saveA with
>>> the parameter of type AType. Without using tMap, I get the following
>>> method signature: void saveA() which is obviously not what I expect.
>>> So, my question was why do I need to use tMap at all? Getting a valid
>>> method signature (i.e. with the parameter of type AType) should be
>>> what I get by default, right?
>>>
>> You are getting saveA() generated because as I mentioned in the previous
>> email JAXB does not generate A and in fact anything in the 'rest'
>> package - I've no idea why, I guess it needs some types definitions.
>>
>> FYI, if I update a WADL you sent me to have saveA refer to base:AType, I
>> get saveA(AType).
>>
>> Now, if I update the parent schema like this:
>>
>> <element name="A" type="nsrest:AAType" />
>> <complexType name="AAType">
>> <complexContent>
>> <extension base="nsbase:BType">
>> <sequence>
>> <element name="a1" type="string" />
>> </sequence>
>> </extension>
>> </complexContent>
>> </complexType>
>>
>> then I get saveA(AAType)
>>
>> and I make AAType anonymous then I do get saveA(A).
>>
>> I wonder if the generator does not handle links to the types from
>> foreign schemas - will have a look
>>
>>
>>> I'll look the the MessageContext for the response.
>>>
>> One thing which I've completely forgotten about. Whenever you have an
>> exception case - throw WebApplicationException with a populated
>> Response. That might help a bit.
>>
>> Sergey
>>
>>> Thanks
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>>> Sent: Monday, October 15, 2012 4:05 PM
>>> To: users@cxf.apache.org
>>> Subject: Re: Customize HTTP response for REST service
>>>
>>> On 15/10/12 13:40, Mickael Marrache wrote:
>>>> Yes, I can generate my method as expected using the tMap argument.
>>>> But, I don't understand what I'm doing (i.e. the purpose of tMap) and
>>>> why I need to do that.
>>>> The expected behavior would be to add a method parameter (else how to
>>>> access the information the client sent?), why do I need to add extra
>>>> configuration to make this work?
>>>>
>>>
>>> I'm gettinga bit confused.
>>> What exactly are you using tMap for ?
>>>
>>>> Concerning the returned responses:
>>>>
>>>> In the case the HTTP response's body is empty, the operation method
>>>> returns nothing (void). Even if I build an instance of Response, how
>>>> to return it?
>>>
>>> As I said I will consider making the code generator configurable so
>>> that 'Response' is returned instead of 'void'
>>>
>>>> Concerning the use of filters, I don't understand how it can help
>>>> since the decision about which response to send (i.e. status code,
>>>> various HTTP headers...) is taken inside the body of the processing
>>>> method. I don't have this decision information inside the filter.
>>>
>>> At the moment, 'void' is generated, hence we have to think about the
>>> workarounds. If using the filter does not suit, then experiment with
>>> injecting CXF MessageContext (into the implementation class field) and
>>> set Message.PROTOCOL_HEADERS (of type Map<String, List<String>>) on
>>> this context - that may help
>>>
>>>>
>>>> In the case my HTTP response's body should include a XML
>>>> representation, the generated method returns an instance of the class
>>>> corresponding to the XML type (e.g. AType according to my previous
>>>> example). So, there is no Response instance returned (at least inside
>>>> the method), and I get the same issue.
>>>>
>>>> The solution seems to be accessing some sort of context inside the
>>>> method, and customize the returned response through this context, but
>>>> I don't know if there is such a construct?
>>>
>>> Indeed, hope using MessageContext can help for now.
>>>
>>> However if you have a case where a response may or may not contain the
>>> response body then 'tMap' may be actually the 'best friend'. have
>>> representation linking to a schema element but use tMap to get the
>>> generator to include 'Response' instead of the type generated by JAXB
>>> compiler.
>>>
>>> The case which is not supported well is when you know that it is
>>> always an empty response but may be the status and headers may vary,
>>> in this case we will need to add a configuration property for 'Response'
>>> generated instead of 'void'
>>>
>>> Cheers, Sergey
>>>>
>>>> Thanks,
>>>> Mickael
>>>>
>>>>
>>>> -----Original Message-----
>>>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>>>> Sent: Monday, October 15, 2012 11:40 AM
>>>> To: users@cxf.apache.org
>>>> Subject: Re: Customize HTTP response for REST service
>>>>
>>>> Hi
>>>> On 14/10/12 11:40, Mickael Marrache wrote:
>>>>> Hi again
>>>>>
>>>>> I have a question about returning responses to HTTP requests in the
>>>>> context of a RESTful web service.
>>>>>
>>>>> This is my method declaration in the WADL:
>>>>>
>>>>> <method id="saveA" name="PUT">
>>>>> <request>
>>>>> <representation mediaType="application/xml" element="ns1:A" />
>>>>> </request>
>>>>> <response status="200">
>>>>> <param name="header1" style="header" type="xsd:string" />
>>>>> </response>
>>>>> </method>
>>>>>
>>>>> After generating the code, I get the following method signature:
>>>>>
>>>>> public void saveA(AType atype);
>>>>>
>>>>
>>>> Does the method is generated as expected ? (referring to your other
>>>> email re PUT method)
>>>>
>>>>> If the PUT operation performed well, I would like to return an HTTP
>>>>> response with status code 200 and an HTTP header containing a
>>>>> certain value. This is indicated in the WADL, but I don't see
>>>>> anything in the code. Since the method saveA doesn't return
>>>>> anything, where is the right place to customize the response (i.e.
>>>>> setting the desired status code and header)?
>>>>>
>>>> Typically a Response would be returned from the method in such cases,
>>>> built like this:
>>>>
>>>> Response.status(myStatus).header(key, value).build();
>>>>
>>>> I wonder if the generator should generate 'Response' by default in
>>>> such cases, where no response representation is expected...Or this
>>>> should be configurable at least, I'll look into it.
>>>>
>>>> In meantime, add ResponseHandler filter (on CXF 2.6.x) or
>>>> ContainerResponseFilter (on CXF 2.7.0) and add a required header from
>>>> there...
>>>>
>>>>> Also, what if for a given request, different responses may be
>>>>> returned according to how the operation performed? Should I add
>>>>> multiple responses in the method element (in the WADL)?
>>>>
>>>> At the moment the simplest thing to do is to
>>>>
>>>> <response status="200 400 401">
>>>>
>>>> etc
>>>>
>>>> Creating multiple responses is also valid but as far as the code
>>>> generation is concerned, it won't make any difference, given that at
>>>> the code level the relevant exceptions are all runtime ones. However,
>>>> it can be better for the reader of WADL because you can attach
>>>> specific descriptions to different response elements...
>>>>
>>>> Cheers, Sergey
>>>>
>>>>>
>>>>> Thanks,
>>>>> Mickael
>>>>>
>>>>
>>>>
>>>> --
>>>> Sergey Beryozkin
>>>>
>>>> Talend Community Coders
>>>> http://coders.talend.com/
>>>>
>>>> Blog: http://sberyozkin.blogspot.com
>>>
>>>
>>> --
>>> Sergey Beryozkin
>>>
>>> Talend Community Coders
>>> http://coders.talend.com/
>>>
>>> Blog: http://sberyozkin.blogspot.com
>>
>>
>
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: Customize HTTP response for REST service

Posted by Sergey Beryozkin <sb...@gmail.com>.
There's a bug with the generator losing the namespace info for types 
from imported (but not included) schemas - will be fix in next CXF 
releases, I'm glad tMap is becoming quite handy too, in cases where the 
generator itself is buggy...

Thanks, Sergey

On 16/10/12 11:20, Sergey Beryozkin wrote:
> On 15/10/12 18:47, Mickael Marrache wrote:
>> I use tMap in order for WADL2Java to generate the method saveA with
>> the parameter of type AType. Without using tMap, I get the following
>> method signature: void saveA() which is obviously not what I expect.
>> So, my question was why do I need to use tMap at all? Getting a valid
>> method signature (i.e. with the parameter of type AType) should be
>> what I get by default, right?
>>
> You are getting saveA() generated because as I mentioned in the previous
> email JAXB does not generate A and in fact anything in the 'rest'
> package - I've no idea why, I guess it needs some types definitions.
>
> FYI, if I update a WADL you sent me to have saveA refer to base:AType, I
> get saveA(AType).
>
> Now, if I update the parent schema like this:
>
> <element name="A" type="nsrest:AAType" />
> <complexType name="AAType">
> <complexContent>
> <extension base="nsbase:BType">
> <sequence>
> <element name="a1" type="string" />
> </sequence>
> </extension>
> </complexContent>
> </complexType>
>
> then I get saveA(AAType)
>
> and I make AAType anonymous then I do get saveA(A).
>
> I wonder if the generator does not handle links to the types from
> foreign schemas - will have a look
>
>
>> I'll look the the MessageContext for the response.
>>
> One thing which I've completely forgotten about. Whenever you have an
> exception case - throw WebApplicationException with a populated
> Response. That might help a bit.
>
> Sergey
>
>> Thanks
>>
>>
>>
>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>> Sent: Monday, October 15, 2012 4:05 PM
>> To: users@cxf.apache.org
>> Subject: Re: Customize HTTP response for REST service
>>
>> On 15/10/12 13:40, Mickael Marrache wrote:
>>> Yes, I can generate my method as expected using the tMap argument.
>>> But, I don't understand what I'm doing (i.e. the purpose of tMap) and
>>> why I need to do that.
>>> The expected behavior would be to add a method parameter (else how to
>>> access the information the client sent?), why do I need to add extra
>>> configuration to make this work?
>>>
>>
>> I'm gettinga bit confused.
>> What exactly are you using tMap for ?
>>
>>> Concerning the returned responses:
>>>
>>> In the case the HTTP response's body is empty, the operation method
>>> returns nothing (void). Even if I build an instance of Response, how
>>> to return it?
>>
>> As I said I will consider making the code generator configurable so
>> that 'Response' is returned instead of 'void'
>>
>>> Concerning the use of filters, I don't understand how it can help
>>> since the decision about which response to send (i.e. status code,
>>> various HTTP headers...) is taken inside the body of the processing
>>> method. I don't have this decision information inside the filter.
>>
>> At the moment, 'void' is generated, hence we have to think about the
>> workarounds. If using the filter does not suit, then experiment with
>> injecting CXF MessageContext (into the implementation class field) and
>> set Message.PROTOCOL_HEADERS (of type Map<String, List<String>>) on
>> this context - that may help
>>
>>>
>>> In the case my HTTP response's body should include a XML
>>> representation, the generated method returns an instance of the class
>>> corresponding to the XML type (e.g. AType according to my previous
>>> example). So, there is no Response instance returned (at least inside
>>> the method), and I get the same issue.
>>>
>>> The solution seems to be accessing some sort of context inside the
>>> method, and customize the returned response through this context, but
>>> I don't know if there is such a construct?
>>
>> Indeed, hope using MessageContext can help for now.
>>
>> However if you have a case where a response may or may not contain the
>> response body then 'tMap' may be actually the 'best friend'. have
>> representation linking to a schema element but use tMap to get the
>> generator to include 'Response' instead of the type generated by JAXB
>> compiler.
>>
>> The case which is not supported well is when you know that it is
>> always an empty response but may be the status and headers may vary,
>> in this case we will need to add a configuration property for 'Response'
>> generated instead of 'void'
>>
>> Cheers, Sergey
>>>
>>> Thanks,
>>> Mickael
>>>
>>>
>>> -----Original Message-----
>>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>>> Sent: Monday, October 15, 2012 11:40 AM
>>> To: users@cxf.apache.org
>>> Subject: Re: Customize HTTP response for REST service
>>>
>>> Hi
>>> On 14/10/12 11:40, Mickael Marrache wrote:
>>>> Hi again
>>>>
>>>> I have a question about returning responses to HTTP requests in the
>>>> context of a RESTful web service.
>>>>
>>>> This is my method declaration in the WADL:
>>>>
>>>> <method id="saveA" name="PUT">
>>>> <request>
>>>> <representation mediaType="application/xml" element="ns1:A" />
>>>> </request>
>>>> <response status="200">
>>>> <param name="header1" style="header" type="xsd:string" />
>>>> </response>
>>>> </method>
>>>>
>>>> After generating the code, I get the following method signature:
>>>>
>>>> public void saveA(AType atype);
>>>>
>>>
>>> Does the method is generated as expected ? (referring to your other
>>> email re PUT method)
>>>
>>>> If the PUT operation performed well, I would like to return an HTTP
>>>> response with status code 200 and an HTTP header containing a
>>>> certain value. This is indicated in the WADL, but I don't see
>>>> anything in the code. Since the method saveA doesn't return
>>>> anything, where is the right place to customize the response (i.e.
>>>> setting the desired status code and header)?
>>>>
>>> Typically a Response would be returned from the method in such cases,
>>> built like this:
>>>
>>> Response.status(myStatus).header(key, value).build();
>>>
>>> I wonder if the generator should generate 'Response' by default in
>>> such cases, where no response representation is expected...Or this
>>> should be configurable at least, I'll look into it.
>>>
>>> In meantime, add ResponseHandler filter (on CXF 2.6.x) or
>>> ContainerResponseFilter (on CXF 2.7.0) and add a required header from
>>> there...
>>>
>>>> Also, what if for a given request, different responses may be
>>>> returned according to how the operation performed? Should I add
>>>> multiple responses in the method element (in the WADL)?
>>>
>>> At the moment the simplest thing to do is to
>>>
>>> <response status="200 400 401">
>>>
>>> etc
>>>
>>> Creating multiple responses is also valid but as far as the code
>>> generation is concerned, it won't make any difference, given that at
>>> the code level the relevant exceptions are all runtime ones. However,
>>> it can be better for the reader of WADL because you can attach
>>> specific descriptions to different response elements...
>>>
>>> Cheers, Sergey
>>>
>>>>
>>>> Thanks,
>>>> Mickael
>>>>
>>>
>>>
>>> --
>>> Sergey Beryozkin
>>>
>>> Talend Community Coders
>>> http://coders.talend.com/
>>>
>>> Blog: http://sberyozkin.blogspot.com
>>
>>
>> --
>> Sergey Beryozkin
>>
>> Talend Community Coders
>> http://coders.talend.com/
>>
>> Blog: http://sberyozkin.blogspot.com
>
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: Customize HTTP response for REST service

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 15/10/12 18:47, Mickael Marrache wrote:
> I use tMap in order for WADL2Java to generate the method saveA with the parameter of type AType. Without using tMap, I get the following method signature: void saveA() which is obviously not what I expect.
> So, my question was why do I need to use tMap at all? Getting a valid method signature (i.e. with the parameter of type AType) should be what I get by default, right?
>
You are getting saveA() generated because as I mentioned in the previous 
email JAXB does not generate A and in fact anything in the 'rest' 
package - I've no idea why, I guess it needs some types definitions.

FYI, if I update a WADL you sent me to have saveA refer to base:AType, I 
get saveA(AType).

Now, if I update the parent schema like this:

<element name="A" type="nsrest:AAType" />
		<complexType name="AAType">
             <complexContent>
                   <extension base="nsbase:BType">
                         <sequence>
                               <element name="a1" type="string" />
                        </sequence>
                  </extension>
            </complexContent>
      </complexType>

then I get saveA(AAType)

and I make AAType anonymous then I do get saveA(A).

I wonder if the generator does not handle links to the types from 
foreign schemas - will have a look


> I'll look the the MessageContext for the response.
>
One thing which I've completely forgotten about. Whenever you have an 
exception case - throw WebApplicationException with a populated 
Response. That might help a bit.

Sergey

> Thanks
>
>
>
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Monday, October 15, 2012 4:05 PM
> To: users@cxf.apache.org
> Subject: Re: Customize HTTP response for REST service
>
> On 15/10/12 13:40, Mickael Marrache wrote:
>> Yes, I can generate my method as expected using the tMap argument. But, I don't understand what I'm doing (i.e. the purpose of tMap) and why I need to do that.
>> The expected behavior would be to add a method parameter (else how to access the information the client sent?), why do I need to add extra configuration to make this work?
>>
>
> I'm gettinga bit confused.
> What exactly are you using tMap for ?
>
>> Concerning the returned responses:
>>
>> In the case the HTTP response's body is empty, the operation method returns nothing (void). Even if I build an instance of Response, how to return it?
>
> As I said I will consider making the code generator configurable so that 'Response' is returned instead of 'void'
>
>> Concerning the use of filters, I don't understand how it can help since the decision about which response to send (i.e. status code, various HTTP headers...) is taken inside the body of the processing method. I don't have this decision information inside the filter.
>
> At the moment, 'void' is generated, hence we have to think about the workarounds. If using the filter does not suit, then experiment with injecting CXF MessageContext (into the implementation class field) and set Message.PROTOCOL_HEADERS (of type Map<String, List<String>>) on this context - that may help
>
>>
>> In the case my HTTP response's body should include a XML representation, the generated method returns an instance of the class corresponding to the XML type (e.g. AType according to my previous example). So, there is no Response instance returned (at least inside the method), and I get the same issue.
>>
>> The solution seems to be accessing some sort of context inside the method, and customize the returned response through this context, but I don't know if there is such a construct?
>
> Indeed, hope using MessageContext can help for now.
>
> However if you have a case where a response may or may not contain the response body then 'tMap' may be actually the 'best friend'. have representation linking to a schema element but use tMap to get the generator to include 'Response' instead of the type generated by JAXB compiler.
>
> The case which is not supported well is when you know that it is always an empty response but may be the status and headers may vary, in this case we will need to add a configuration property for 'Response'
> generated instead of 'void'
>
> Cheers, Sergey
>>
>> Thanks,
>> Mickael
>>
>>
>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>> Sent: Monday, October 15, 2012 11:40 AM
>> To: users@cxf.apache.org
>> Subject: Re: Customize HTTP response for REST service
>>
>> Hi
>> On 14/10/12 11:40, Mickael Marrache wrote:
>>> Hi again
>>>
>>> I have a question about returning responses to HTTP requests in the context of a RESTful web service.
>>>
>>> This is my method declaration in the WADL:
>>>
>>> <method id="saveA" name="PUT">
>>>       <request>
>>>          <representation mediaType="application/xml" element="ns1:A" />
>>>       </request>
>>>       <response status="200">
>>>           <param name="header1" style="header" type="xsd:string" />
>>>       </response>
>>> </method>
>>>
>>> After generating the code, I get the following method signature:
>>>
>>> public void saveA(AType atype);
>>>
>>
>> Does the method is generated as expected ? (referring to your other
>> email re PUT method)
>>
>>> If the PUT operation performed well, I would like to return an HTTP response with status code 200 and an HTTP header containing a certain value. This is indicated in the WADL, but I don't see anything in the code. Since the method saveA doesn't return anything, where is the right place to customize the response (i.e. setting the desired status code and header)?
>>>
>> Typically a Response would be returned from the method in such cases, built like this:
>>
>> Response.status(myStatus).header(key, value).build();
>>
>> I wonder if the generator should generate 'Response' by default in such cases, where no response representation is expected...Or this should be configurable at least, I'll look into it.
>>
>> In meantime, add ResponseHandler filter (on CXF 2.6.x) or ContainerResponseFilter (on CXF 2.7.0) and add a required header from there...
>>
>>> Also, what if for a given request, different responses may be returned according to how the operation  performed? Should I add multiple responses in the method element (in the WADL)?
>>
>> At the moment the simplest thing to do is to
>>
>> <response status="200 400 401">
>>
>> etc
>>
>> Creating multiple responses is also valid but as far as the code generation is concerned, it won't make any difference, given that at the code level the relevant exceptions are all runtime ones. However, it can be better for the reader of WADL because you can attach specific descriptions to different response elements...
>>
>> Cheers, Sergey
>>
>>>
>>> Thanks,
>>> Mickael
>>>
>>
>>
>> --
>> Sergey Beryozkin
>>
>> Talend Community Coders
>> http://coders.talend.com/
>>
>> Blog: http://sberyozkin.blogspot.com
>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>
> Blog: http://sberyozkin.blogspot.com


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

RE: Customize HTTP response for REST service

Posted by Mickael Marrache <Mi...@xconnect.net>.
I use tMap in order for WADL2Java to generate the method saveA with the parameter of type AType. Without using tMap, I get the following method signature: void saveA() which is obviously not what I expect.
So, my question was why do I need to use tMap at all? Getting a valid method signature (i.e. with the parameter of type AType) should be what I get by default, right?

I'll look the the MessageContext for the response.

Thanks



-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozkin@gmail.com] 
Sent: Monday, October 15, 2012 4:05 PM
To: users@cxf.apache.org
Subject: Re: Customize HTTP response for REST service

On 15/10/12 13:40, Mickael Marrache wrote:
> Yes, I can generate my method as expected using the tMap argument. But, I don't understand what I'm doing (i.e. the purpose of tMap) and why I need to do that.
> The expected behavior would be to add a method parameter (else how to access the information the client sent?), why do I need to add extra configuration to make this work?
>

I'm gettinga bit confused.
What exactly are you using tMap for ?

> Concerning the returned responses:
>
> In the case the HTTP response's body is empty, the operation method returns nothing (void). Even if I build an instance of Response, how to return it?

As I said I will consider making the code generator configurable so that 'Response' is returned instead of 'void'

>Concerning the use of filters, I don't understand how it can help since the decision about which response to send (i.e. status code, various HTTP headers...) is taken inside the body of the processing method. I don't have this decision information inside the filter.

At the moment, 'void' is generated, hence we have to think about the workarounds. If using the filter does not suit, then experiment with injecting CXF MessageContext (into the implementation class field) and set Message.PROTOCOL_HEADERS (of type Map<String, List<String>>) on this context - that may help

>
> In the case my HTTP response's body should include a XML representation, the generated method returns an instance of the class corresponding to the XML type (e.g. AType according to my previous example). So, there is no Response instance returned (at least inside the method), and I get the same issue.
>
> The solution seems to be accessing some sort of context inside the method, and customize the returned response through this context, but I don't know if there is such a construct?

Indeed, hope using MessageContext can help for now.

However if you have a case where a response may or may not contain the response body then 'tMap' may be actually the 'best friend'. have representation linking to a schema element but use tMap to get the generator to include 'Response' instead of the type generated by JAXB compiler.

The case which is not supported well is when you know that it is always an empty response but may be the status and headers may vary, in this case we will need to add a configuration property for 'Response' 
generated instead of 'void'

Cheers, Sergey
>
> Thanks,
> Mickael
>
>
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Monday, October 15, 2012 11:40 AM
> To: users@cxf.apache.org
> Subject: Re: Customize HTTP response for REST service
>
> Hi
> On 14/10/12 11:40, Mickael Marrache wrote:
>> Hi again
>>
>> I have a question about returning responses to HTTP requests in the context of a RESTful web service.
>>
>> This is my method declaration in the WADL:
>>
>> <method id="saveA" name="PUT">
>>      <request>
>>         <representation mediaType="application/xml" element="ns1:A" />
>>      </request>
>>      <response status="200">
>>          <param name="header1" style="header" type="xsd:string" />
>>      </response>
>> </method>
>>
>> After generating the code, I get the following method signature:
>>
>> public void saveA(AType atype);
>>
>
> Does the method is generated as expected ? (referring to your other 
> email re PUT method)
>
>> If the PUT operation performed well, I would like to return an HTTP response with status code 200 and an HTTP header containing a certain value. This is indicated in the WADL, but I don't see anything in the code. Since the method saveA doesn't return anything, where is the right place to customize the response (i.e. setting the desired status code and header)?
>>
> Typically a Response would be returned from the method in such cases, built like this:
>
> Response.status(myStatus).header(key, value).build();
>
> I wonder if the generator should generate 'Response' by default in such cases, where no response representation is expected...Or this should be configurable at least, I'll look into it.
>
> In meantime, add ResponseHandler filter (on CXF 2.6.x) or ContainerResponseFilter (on CXF 2.7.0) and add a required header from there...
>
>> Also, what if for a given request, different responses may be returned according to how the operation  performed? Should I add multiple responses in the method element (in the WADL)?
>
> At the moment the simplest thing to do is to
>
> <response status="200 400 401">
>
> etc
>
> Creating multiple responses is also valid but as far as the code generation is concerned, it won't make any difference, given that at the code level the relevant exceptions are all runtime ones. However, it can be better for the reader of WADL because you can attach specific descriptions to different response elements...
>
> Cheers, Sergey
>
>>
>> Thanks,
>> Mickael
>>
>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>
> Blog: http://sberyozkin.blogspot.com


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: Customize HTTP response for REST service

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 15/10/12 13:40, Mickael Marrache wrote:
> Yes, I can generate my method as expected using the tMap argument. But, I don't understand what I'm doing (i.e. the purpose of tMap) and why I need to do that.
> The expected behavior would be to add a method parameter (else how to access the information the client sent?), why do I need to add extra configuration to make this work?
>

I'm gettinga bit confused.
What exactly are you using tMap for ?

> Concerning the returned responses:
>
> In the case the HTTP response's body is empty, the operation method returns nothing (void). Even if I build an instance of Response, how to return it?

As I said I will consider making the code generator configurable so that 
'Response' is returned instead of 'void'

>Concerning the use of filters, I don't understand how it can help since the decision about which response to send (i.e. status code, various HTTP headers...) is taken inside the body of the processing method. I don't have this decision information inside the filter.

At the moment, 'void' is generated, hence we have to think about the 
workarounds. If using the filter does not suit, then experiment with 
injecting CXF MessageContext (into the implementation class field) and 
set Message.PROTOCOL_HEADERS (of type Map<String, List<String>>) on this 
context - that may help

>
> In the case my HTTP response's body should include a XML representation, the generated method returns an instance of the class corresponding to the XML type (e.g. AType according to my previous example). So, there is no Response instance returned (at least inside the method), and I get the same issue.
>
> The solution seems to be accessing some sort of context inside the method, and customize the returned response through this context, but I don't know if there is such a construct?

Indeed, hope using MessageContext can help for now.

However if you have a case where a response may or may not contain the 
response body then 'tMap' may be actually the 'best friend'. have 
representation linking to a schema element but use tMap to get the 
generator to include 'Response' instead of the type generated by JAXB 
compiler.

The case which is not supported well is when you know that it is always 
an empty response but may be the status and headers may vary, in this 
case we will need to add a configuration property for 'Response' 
generated instead of 'void'

Cheers, Sergey
>
> Thanks,
> Mickael
>
>
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Monday, October 15, 2012 11:40 AM
> To: users@cxf.apache.org
> Subject: Re: Customize HTTP response for REST service
>
> Hi
> On 14/10/12 11:40, Mickael Marrache wrote:
>> Hi again
>>
>> I have a question about returning responses to HTTP requests in the context of a RESTful web service.
>>
>> This is my method declaration in the WADL:
>>
>> <method id="saveA" name="PUT">
>>      <request>
>>         <representation mediaType="application/xml" element="ns1:A" />
>>      </request>
>>      <response status="200">
>>          <param name="header1" style="header" type="xsd:string" />
>>      </response>
>> </method>
>>
>> After generating the code, I get the following method signature:
>>
>> public void saveA(AType atype);
>>
>
> Does the method is generated as expected ? (referring to your other email re PUT method)
>
>> If the PUT operation performed well, I would like to return an HTTP response with status code 200 and an HTTP header containing a certain value. This is indicated in the WADL, but I don't see anything in the code. Since the method saveA doesn't return anything, where is the right place to customize the response (i.e. setting the desired status code and header)?
>>
> Typically a Response would be returned from the method in such cases, built like this:
>
> Response.status(myStatus).header(key, value).build();
>
> I wonder if the generator should generate 'Response' by default in such cases, where no response representation is expected...Or this should be configurable at least, I'll look into it.
>
> In meantime, add ResponseHandler filter (on CXF 2.6.x) or ContainerResponseFilter (on CXF 2.7.0) and add a required header from there...
>
>> Also, what if for a given request, different responses may be returned according to how the operation  performed? Should I add multiple responses in the method element (in the WADL)?
>
> At the moment the simplest thing to do is to
>
> <response status="200 400 401">
>
> etc
>
> Creating multiple responses is also valid but as far as the code generation is concerned, it won't make any difference, given that at the code level the relevant exceptions are all runtime ones. However, it can be better for the reader of WADL because you can attach specific descriptions to different response elements...
>
> Cheers, Sergey
>
>>
>> Thanks,
>> Mickael
>>
>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>
> Blog: http://sberyozkin.blogspot.com


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

RE: Customize HTTP response for REST service

Posted by Mickael Marrache <Mi...@xconnect.net>.
Yes, I can generate my method as expected using the tMap argument. But, I don't understand what I'm doing (i.e. the purpose of tMap) and why I need to do that.
The expected behavior would be to add a method parameter (else how to access the information the client sent?), why do I need to add extra configuration to make this work?

Concerning the returned responses:

In the case the HTTP response's body is empty, the operation method returns nothing (void). Even if I build an instance of Response, how to return it? Concerning the use of filters, I don't understand how it can help since the decision about which response to send (i.e. status code, various HTTP headers...) is taken inside the body of the processing method. I don't have this decision information inside the filter.

In the case my HTTP response's body should include a XML representation, the generated method returns an instance of the class corresponding to the XML type (e.g. AType according to my previous example). So, there is no Response instance returned (at least inside the method), and I get the same issue.

The solution seems to be accessing some sort of context inside the method, and customize the returned response through this context, but I don't know if there is such a construct?

Thanks,
Mickael


-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozkin@gmail.com] 
Sent: Monday, October 15, 2012 11:40 AM
To: users@cxf.apache.org
Subject: Re: Customize HTTP response for REST service

Hi
On 14/10/12 11:40, Mickael Marrache wrote:
> Hi again
>
> I have a question about returning responses to HTTP requests in the context of a RESTful web service.
>
> This is my method declaration in the WADL:
>
> <method id="saveA" name="PUT">
>     <request>
>        <representation mediaType="application/xml" element="ns1:A" />
>     </request>
>     <response status="200">
>         <param name="header1" style="header" type="xsd:string" />
>     </response>
> </method>
>
> After generating the code, I get the following method signature:
>
> public void saveA(AType atype);
>

Does the method is generated as expected ? (referring to your other email re PUT method)

> If the PUT operation performed well, I would like to return an HTTP response with status code 200 and an HTTP header containing a certain value. This is indicated in the WADL, but I don't see anything in the code. Since the method saveA doesn't return anything, where is the right place to customize the response (i.e. setting the desired status code and header)?
>
Typically a Response would be returned from the method in such cases, built like this:

Response.status(myStatus).header(key, value).build();

I wonder if the generator should generate 'Response' by default in such cases, where no response representation is expected...Or this should be configurable at least, I'll look into it.

In meantime, add ResponseHandler filter (on CXF 2.6.x) or ContainerResponseFilter (on CXF 2.7.0) and add a required header from there...

> Also, what if for a given request, different responses may be returned according to how the operation  performed? Should I add multiple responses in the method element (in the WADL)?

At the moment the simplest thing to do is to

<response status="200 400 401">

etc

Creating multiple responses is also valid but as far as the code generation is concerned, it won't make any difference, given that at the code level the relevant exceptions are all runtime ones. However, it can be better for the reader of WADL because you can attach specific descriptions to different response elements...

Cheers, Sergey

>
> Thanks,
> Mickael
>


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: Customize HTTP response for REST service

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 14/10/12 11:40, Mickael Marrache wrote:
> Hi again
>
> I have a question about returning responses to HTTP requests in the context of a RESTful web service.
>
> This is my method declaration in the WADL:
>
> <method id="saveA" name="PUT">
>     <request>
>        <representation mediaType="application/xml" element="ns1:A" />
>     </request>
>     <response status="200">
>         <param name="header1" style="header" type="xsd:string" />
>     </response>
> </method>
>
> After generating the code, I get the following method signature:
>
> public void saveA(AType atype);
>

Does the method is generated as expected ? (referring to your other 
email re PUT method)

> If the PUT operation performed well, I would like to return an HTTP response with status code 200 and an HTTP header containing a certain value. This is indicated in the WADL, but I don't see anything in the code. Since the method saveA doesn't return anything, where is the right place to customize the response (i.e. setting the desired status code and header)?
>
Typically a Response would be returned from the method in such cases, 
built like this:

Response.status(myStatus).header(key, value).build();

I wonder if the generator should generate 'Response' by default in such 
cases, where no response representation is expected...Or this should be 
configurable at least, I'll look into it.

In meantime, add ResponseHandler filter (on CXF 2.6.x) or 
ContainerResponseFilter (on CXF 2.7.0) and add a required header from 
there...

> Also, what if for a given request, different responses may be returned according to how the operation  performed? Should I add multiple responses in the method element (in the WADL)?

At the moment the simplest thing to do is to

<response status="200 400 401">

etc

Creating multiple responses is also valid but as far as the code 
generation is concerned, it won't make any difference, given that at the 
code level the relevant exceptions are all runtime ones. However, it can 
be better for the reader of WADL because you can attach specific 
descriptions to different response elements...

Cheers, Sergey

>
> Thanks,
> Mickael
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com