.convertBodyTo(String.class)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

.convertBodyTo(String.class)

jpcook01
Hi,

I have a simple routebuilder configured to read an xml message from a JMS and then split the message:

from("seda:horseRacingResults_all_finished_ceefax")
  .splitter(xpath("/myXml")).convertBodyTo(String.class)
  .setBody(groovy("'<myNewXml>'+request.body+'</myNewXml>'"))
  .to("mock:afterSplit");

It seems that the groovy component doesn't like xml bodies so I have converted it to a String. However in the source xml message there are escaped characters such as: ¼ fraction 1/4 ¼ ¼ which I need to preserve. I wondered if convertBodyTo(String.class) should escape characters?

Would appreciate any feedback.

Thanks
Jonathan
Reply | Threaded
Open this post in threaded view
|

Re: .convertBodyTo(String.class)

jstrachan
On 09/01/2008, jpcook <[hidden email]> wrote:

>
> Hi,
>
> I have a simple routebuilder configured to read an xml message from a JMS
> and then split the message:
>
> from("seda:horseRacingResults_all_finished_ceefax")
>   .splitter(xpath("/myXml")).convertBodyTo(String.class)
>   .setBody(groovy("'<myNewXml>'+request.body+'</myNewXml>'"))
>   .to("mock:afterSplit");
>
> It seems that the groovy component doesn't like xml bodies so I have
> converted it to a String. However in the source xml message there are
> escaped characters such as: ¼ fraction 1/4 &frac14; &#188; which I need to
> preserve. I wondered if convertBodyTo(String.class) should escape
> characters?

You probably need to XML-encode the request.body in the Groovy
expression when making some XML (as the convertTo(String.class) could
generate some valid XML).

You could always write a little helper method to do the conversion in
Java if you like...

from("seda:horseRacingResults_all_finished_ceefax")
 .splitter(xpath("/myXml")).beanRef("myBean").
 .to("mock:afterSplit");


where you register "myBean" as...

public class MyBean {
  public String toXml(String body) {
    return "'<myNewXml>" + someEncodeMethod(body) + "</myNewXml>'";
  }
}

--
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://open.iona.com
Reply | Threaded
Open this post in threaded view
|

Re: .convertBodyTo(String.class)

jpcook01
I'm having the error:
Need to specify class name in environment or system property, or as an applet
parameter, or in an application resource file:  java.naming.factory.initial

But how do you register your bean using the DefaultCamelContext, you can get at the
registry but this only has lookup methods.

I looked at the unit test BeanWithPropertiesAndHeadersAndBodyInjectionTest.java and
this does something with JndiContext.

Jonathan