You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Bruno Dusausoy <br...@yp5.be> on 2011/05/27 11:37:36 UTC
CXFRS custom response
Hi,
As previously stated[1] I'm creating a RESTful webservice, defined by this :
public class RestComputationService implements ComputationService {
@POST
@Path("/computation")
@Consumes("text/xml")
@Produces("text/xml")
@Override
public Response compute(Request request) {
return null;
}
}
Request and Response are quite misleading, they are *not* the one found
in the JAXRS package, they are generated from a schema.
I'd like to handle an exception like I would in CXF, that is something
like this :
Category cat = (Category) getCategoryDAO().getCategory(id);
if (cat == null) {
ResponseBuilder builder =
Response.status(Status.BAD_REQUEST);
builder.type("application/xml");
builder.entity("<error>Category Not Found</error>");
throw new WebApplicationException(builder.build());
}
I've read in "Camel in Action" that a possible way to do this is to use
something similar to this :
public class FailureResponseProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
Exception.class);
StringBuilder sb = new StringBuilder();
sb.append("ERROR: ");
sb.append(e.getMessage());
sb.append("\nBODY: ");
sb.append(body);
exchange.getIn().setBody(sb.toString());
}
}
But if I do this with my CXFRS webservice, I get a
CamelExecutionException caused by a InvalidPayloadException caused
itself by a NoTypeConversionAvailableException.
Indeed, what I put in the body (that is, a String) cannot be transformed
to an object of type Response by JAXB.
So, question, how can I return a custom response ?
Can I bypass JAXB and directly access the CXF rsServer in order to
respond to the client and tell Camel that it doesn't have to do anything
else - in other words, telling Camel its work is finished for this
request ?
Am I doing right or is my design completely flawed ?
Regards.
[1]
http://camel.465427.n5.nabble.com/CXFRS-and-XML-validation-td4428899.html
--
Bruno Dusausoy
YP5 Software
--
Pensez environnement : limitez l'impression de ce mail.
Please don't print this e-mail unless you really need to.
Re: CXFRS custom response
Posted by Willem Jiang <wi...@gmail.com>.
Hi,
You can do the same thing as you do in the CXF.
You just need to throw the WebApplicationException from the processor,
camel-cxfrs will take care of it, but if you set the repsonse message
back to the message body, camel-cxfrs will try to turn it into a
Response object and send it back as a result to CXFRS invoker.
Please check out the unit test of CxfRsConsumerTest[1] for more information.
[1]
https://svn.apache.org/repos/asf/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
On 5/27/11 5:37 PM, Bruno Dusausoy wrote:
> Hi,
>
> As previously stated[1] I'm creating a RESTful webservice, defined by
> this :
>
> public class RestComputationService implements ComputationService {
>
> @POST
> @Path("/computation")
> @Consumes("text/xml")
> @Produces("text/xml")
> @Override
> public Response compute(Request request) {
> return null;
> }
> }
>
> Request and Response are quite misleading, they are *not* the one found
> in the JAXRS package, they are generated from a schema.
>
> I'd like to handle an exception like I would in CXF, that is something
> like this :
>
> Category cat = (Category) getCategoryDAO().getCategory(id);
> if (cat == null) {
> ResponseBuilder builder =
> Response.status(Status.BAD_REQUEST);
> builder.type("application/xml");
> builder.entity("<error>Category Not Found</error>");
> throw new WebApplicationException(builder.build());
> }
>
> I've read in "Camel in Action" that a possible way to do this is to use
> something similar to this :
>
> public class FailureResponseProcessor implements Processor {
> public void process(Exchange exchange) throws Exception {
> String body = exchange.getIn().getBody(String.class);
> Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
> Exception.class);
> StringBuilder sb = new StringBuilder();
> sb.append("ERROR: ");
> sb.append(e.getMessage());
> sb.append("\nBODY: ");
> sb.append(body);
> exchange.getIn().setBody(sb.toString());
> }
> }
>
> But if I do this with my CXFRS webservice, I get a
> CamelExecutionException caused by a InvalidPayloadException caused
> itself by a NoTypeConversionAvailableException.
> Indeed, what I put in the body (that is, a String) cannot be transformed
> to an object of type Response by JAXB.
>
> So, question, how can I return a custom response ?
> Can I bypass JAXB and directly access the CXF rsServer in order to
> respond to the client and tell Camel that it doesn't have to do anything
> else - in other words, telling Camel its work is finished for this
> request ?
> Am I doing right or is my design completely flawed ?
>
> Regards.
> [1]
> http://camel.465427.n5.nabble.com/CXFRS-and-XML-validation-td4428899.html
--
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.javaeye.com (Chinese)
Twitter: willemjiang
Weibo: willemjiang
Re: CXFRS custom response
Posted by Claus Ibsen <cl...@gmail.com>.
Hi
I have created a ticket to track this
https://issues.apache.org/jira/browse/CAMEL-4039
On Fri, May 27, 2011 at 11:37 AM, Bruno Dusausoy <br...@yp5.be> wrote:
> Hi,
>
> As previously stated[1] I'm creating a RESTful webservice, defined by this :
>
> public class RestComputationService implements ComputationService {
>
> @POST
> @Path("/computation")
> @Consumes("text/xml")
> @Produces("text/xml")
> @Override
> public Response compute(Request request) {
> return null;
> }
> }
>
> Request and Response are quite misleading, they are *not* the one found in
> the JAXRS package, they are generated from a schema.
>
> I'd like to handle an exception like I would in CXF, that is something like
> this :
>
> Category cat = (Category) getCategoryDAO().getCategory(id);
> if (cat == null) {
> ResponseBuilder builder =
> Response.status(Status.BAD_REQUEST);
> builder.type("application/xml");
> builder.entity("<error>Category Not Found</error>");
> throw new WebApplicationException(builder.build());
> }
>
> I've read in "Camel in Action" that a possible way to do this is to use
> something similar to this :
>
> public class FailureResponseProcessor implements Processor {
> public void process(Exchange exchange) throws Exception {
> String body = exchange.getIn().getBody(String.class);
> Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
> Exception.class);
> StringBuilder sb = new StringBuilder();
> sb.append("ERROR: ");
> sb.append(e.getMessage());
> sb.append("\nBODY: ");
> sb.append(body);
> exchange.getIn().setBody(sb.toString());
> }
> }
>
> But if I do this with my CXFRS webservice, I get a CamelExecutionException
> caused by a InvalidPayloadException caused itself by a
> NoTypeConversionAvailableException.
> Indeed, what I put in the body (that is, a String) cannot be transformed to
> an object of type Response by JAXB.
>
> So, question, how can I return a custom response ?
> Can I bypass JAXB and directly access the CXF rsServer in order to respond
> to the client and tell Camel that it doesn't have to do anything else - in
> other words, telling Camel its work is finished for this request ?
> Am I doing right or is my design completely flawed ?
>
> Regards.
> [1]
> http://camel.465427.n5.nabble.com/CXFRS-and-XML-validation-td4428899.html
> --
> Bruno Dusausoy
> YP5 Software
> --
> Pensez environnement : limitez l'impression de ce mail.
> Please don't print this e-mail unless you really need to.
>
--
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/