On Thu, Dec 17, 2009 at 9:34 AM, titexe <[email protected]> wrote:
>
> thank you for your answers,
>
> I am in testing phase, the solutions you have proposed
>
> Claus excuse me, I have to put your code (in which file camel.xml or
> activemq.xml) and in each part?
>
No just proves the point that Stephen was wrong that you you CAN set
the resultType when using xpath with XML.
Such as:
<xpath resultType="java.lang.String">foo/bar/text()</xpath>
> thank you
>
>
>
> Claus Ibsen-2 wrote:
>>
>> On Thu, Dec 17, 2009 at 3:25 AM, Stephen Gargan <[email protected]>
>> wrote:
>>> I'm guessing that you expected to receive the 'ALLAH' string at your
>>> endpoint and were receiving a DeferredCDATASectionImpl object instead is
>>> this correct? The unit test below illustrates what is happening (it uses
>>> the
>>> java DSL with the first of the 3 routes being pretty much the same as
>>> what
>>> your spring xml route is defining).
>>>
>>> The test 'testFailsDueToTypeConversion' shows that what you actually
>>> receive
>>> from the xpath split is a instance of DeferredCDATASectionImpl. I'm
>>> guessing
>>> you expected this to be a String and were trying to use value() to make
>>> it
>>> so. There are a couple of ways to do this as the rest of the test shows.
>>>
>>> First and easiest is to use a result type in your xpath expression, e.g.
>>> xpath("/request/header/source/text()", String.class), this will correctly
>>> extract the data from the DeferredCDATASectionImpl object. Unfortunately,
>>> you cannot currently specify a result type in an XML route, though It
>>> would
>>> be an easy thing to add so maybe you should open a Jira ticket.
>>>
>>
>> Yes you can specify that in Spring XML. There is an attribute for that.
>>
>> <xs:complexType name="xPathExpression">
>> <xs:simpleContent>
>> <xs:extension base="tns:namespaceAwareExpression">
>> <xs:attribute name="resultType" type="xs:string"/>
>> </xs:extension>
>> </xs:simpleContent>
>> </xs:complexType>
>>
>>> Secondly you could use an intermediate bean to grab the data from the
>>> DeferredCDATASectionImpl and pass it on e.g. 'testWithBeanConversion.'
>>>
>>> Third and probably the best would be to add an explicit type converter
>>> for
>>> it. The test 'testWithRegisteredConverter' shows how using the
>>> CDataTypeConverter bean below. There is excellent documentation on Type
>>> converters at the Camel website
>>> http://camel.apache.org/type-converter.html
>>>
>>> Hope this helps,
>>>
>>> ste
>>>
>>> public class CDataXPathTest extends ContextTestSupport
>>> {
>>> �...@override
>>> protected RouteBuilder createRouteBuilder() throws Exception
>>> {
>>> return new RouteBuilder()
>>> {
>>> �...@override
>>> public void configure() throws Exception
>>> {
>>>
>>>
>>> from("direct:xpath-no-conversion").split().xpath("/request/header/source/text()").to("mock:test");
>>>
>>> from("direct:xpath-with-result-type").split().xpath("/request/header/source/text()",
>>> String.class).to("mock:test");
>>>
>>> from("direct:xpath-with-bean-converter").split().xpath("/request/header/source/text()").bean(new
>>> MySourceProcessor()).to("mock:test");
>>> }
>>> };
>>> }
>>>
>>> public void testFailsDueToTypeConversion() throws Exception
>>> {
>>> MockEndpoint mock = getMockEndpoint("mock:test");
>>> mock.expectedMessageCount(1);
>>>
>>> String xml =
>>> "<request><header><source><![CDATA[ALLAH]]></source></header></request>";
>>> sendBody("direct:xpath-no-conversion", xml);
>>>
>>> mock.await();
>>> mock.assertIsSatisfied();
>>>
>>> Object receieved =
>>> ExchangeHelper.getMandatoryInBody(mock.getReceivedExchanges().get(0));
>>> assertIsInstanceOf(DeferredCDATASectionImpl.class, receieved);
>>> }
>>>
>>>
>>> public void testWithResultTypeConversion() throws Exception
>>> {
>>> MockEndpoint mock = getMockEndpoint("mock:test");
>>> mock.expectedBodiesReceived("ALLAH");
>>>
>>> String xml =
>>> "<request><header><source><![CDATA[ALLAH]]></source></header></request>";
>>> sendBody("direct:xpath-with-result-type", xml);
>>>
>>> mock.await();
>>> mock.assertIsSatisfied();
>>> }
>>>
>>> public void testWithBeanConversion() throws Exception
>>> {
>>> MockEndpoint mock = getMockEndpoint("mock:test");
>>> mock.expectedBodiesReceived("ALLAH");
>>>
>>> String xml =
>>> "<request><header><source><![CDATA[ALLAH]]></source></header></request>";
>>> sendBody("direct:xpath-with-bean-converter", xml);
>>>
>>> mock.await();
>>> mock.assertIsSatisfied();
>>> }
>>>
>>> public void testWithRegisteredConverter() throws Exception
>>> {
>>> context.getTypeConverterRegistry().addTypeConverter(String.class,
>>> DeferredCDATASectionImpl.class, new CDataTypeConverter());
>>> MockEndpoint mock = getMockEndpoint("mock:test");
>>> mock.expectedBodiesReceived("ALLAH");
>>>
>>> String xml =
>>> "<request><header><source><![CDATA[ALLAH]]></source></header></request>";
>>> sendBody("direct:xpath-no-conversion", xml);
>>>
>>> mock.await();
>>> mock.assertIsSatisfied();
>>> }
>>>
>>> public static class MySourceProcessor
>>> {
>>> public String handleCdata(DeferredCDATASectionImpl payload)
>>> {
>>> return payload.getData();
>>> }
>>> }
>>>
>>> public static class CDataTypeConverter implements TypeConverter
>>> {
>>> public <T> T convertTo(Class<T> type, Object value) {
>>> DeferredCDATASectionImpl cdata =
>>> (DeferredCDATASectionImpl)value;
>>> return (T) cdata.getData();
>>> }
>>>
>>> public <T> T convertTo(Class<T> type, Exchange exchange, Object
>>> value) {
>>> return convertTo(type, value);
>>> }
>>>
>>> public <T> T mandatoryConvertTo(Class<T> type, Object value) {
>>> return convertTo(type, value);
>>> }
>>>
>>> public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange,
>>> Object value) {
>>> return convertTo(type, value);
>>> }
>>> }
>>> }
>>>
>>> On Wed, Dec 16, 2009 at 9:27 AM, titexe <[email protected]> wrote:
>>>
>>>>
>>>> hello,
>>>>
>>>> my xpath query does not work, when I put the text value () to finish:
>>>>
>>>> /request/header/source/text()
>>>>
>>>> body of my message :
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <request>
>>>> <header>
>>>> <source><![CDATA[ALLAH]]></source>
>>>> </header>
>>>> </request>
>>>>
>>>>
>>>> my route :
>>>>
>>>> <route>
>>>> <from uri="activemq:queue:IN1"/>
>>>> <split>
>>>> <xpath>/request/header/source/text()</ xpath>
>>>>
>>>> <t uri="activemq:queue:XML"/>
>>>> </ Split>
>>>> <t uri="activemq:queue:RESULT"/>
>>>> </ route>
>>>>
>>>> any idea?
>>>>
>>>> thanks in advance,
>>>>
>>>> titexe
>>>> --
>>>> View this message in context:
>>>> http://old.nabble.com/camel-%3A-xpath---text%28%29-tp26815065p26815065.html
>>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>>
>>>>
>>>
>>
>>
>>
>> --
>> Claus Ibsen
>> Apache Camel Committer
>>
>> Author of Camel in Action: http://www.manning.com/ibsen/
>> Open Source Integration: http://fusesource.com
>> Blog: http://davsclaus.blogspot.com/
>> Twitter: http://twitter.com/davsclaus
>>
>>
>
> --
> View this message in context:
> http://old.nabble.com/camel-%3A-xpath---text%28%29-tp26815065p26824799.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
--
Claus Ibsen
Apache Camel Committer
Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus