You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Satyam Maloo <ma...@gmail.com> on 2014/03/27 11:00:17 UTC

Extract the value from property

The input looks like below,

<book author="ABC" type="Children">
    <id>123</id>
    <name>XYZ</name>
</book>
I have set the above in property in an xml route as:

<camel:setProperty propertyName="REQUEST">
    <camel:xpath>/node()</camel:xpath>
</camel:setProperty>

Then I do some other processing and based on the new response. I want to
extract the value of an author(i.e. ABC) from this Property and compare it
with a element's text string from the response.

I tried a few ways using camel:xpath and camel:simple but am not able to
extract the value from property.

What is the correct way to extract this property?



-----
Satyam
--
View this message in context: http://camel.465427.n5.nabble.com/Extract-the-value-from-property-tp5749428.html
Sent from the Camel - Users mailing list archive at Nabble.com.

AW: AW: Extract the value from property

Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
Just replace "setHeader" with "setProperty" ...

Jan



package de.materne.camel.test;

import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class XPathTest extends CamelTestSupport {

    @Test
    public void check() throws InterruptedException {
        MockEndpoint mock = getMockEndpoint("mock:end");
        mock.expectedMessageCount(1);
        mock.expectedMessagesMatches(PredicateBuilder.and(
            propertyIs("author", "ABC"),
            propertyIs("type", "Children"),
            propertyIs("id", "123"),
            propertyIs("name", "XYZ")
        ));

        String xml = "<book author=\"ABC\"
type=\"Children\"><id>123</id><name>XYZ</name></book>";
        template.sendBody("direct:in", xml);

        assertMockEndpointsSatisfied();
    }

    private Predicate propertyIs(final String propertyKey, final String
expectedValue) {
        return new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object actualValue = exchange.getProperty(propertyKey);
                return actualValue != null &&
actualValue.equals(expectedValue);
            }
        };
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:in")
                    .setProperty("author", xpath("/book/@author",
String.class))
                    .setProperty("type", xpath("/book/@type", String.class))
                    .setProperty("id", xpath("/book/id", String.class))
                    .setProperty("name", xpath("/book/name", String.class))
                    .to("mock:end");
            }
        };
    }
}

> -----Ursprüngliche Nachricht-----
> Von: Satyam Maloo [mailto:maloosatyam@gmail.com]
> Gesendet: Donnerstag, 27. März 2014 13:49
> An: users@camel.apache.org
> Betreff: Re: AW: Extract the value from property
> 
> Thanks for replying, but I know how it is done with header. I want the
> solution using property.
> 
> 
> 
> -----
> Satyam
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Extract-the-value-from-property-
> tp5749428p5749435.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: AW: Extract the value from property

Posted by Satyam Maloo <ma...@gmail.com>.
Thanks for replying, but I know how it is done with header. I want the
solution using property.



-----
Satyam
--
View this message in context: http://camel.465427.n5.nabble.com/Extract-the-value-from-property-tp5749428p5749435.html
Sent from the Camel - Users mailing list archive at Nabble.com.

AW: Extract the value from property

Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
Haven't tried SpringDSL, but this works for me.

Jan


package de.materne.camel.test;

import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class XPathTest extends CamelTestSupport {

    @Test
    public void check() throws InterruptedException {
        MockEndpoint mock = getMockEndpoint("mock:end");
        mock.expectedMessageCount(1);
        mock.expectedMessagesMatches(PredicateBuilder.and(
            headerIs("author", "ABC"),
            headerIs("type", "Children"),
            headerIs("id", "123"),
            headerIs("name", "XYZ")
        ));

        String xml = "<book author=\"ABC\"
type=\"Children\"><id>123</id><name>XYZ</name></book>";
        template.sendBody("direct:in", xml);

        assertMockEndpointsSatisfied();
    }

    private Predicate headerIs(final String headerKey, final String
expectedValue) {
        return new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                Object actualValue = exchange.getIn().getHeader(headerKey);
                return actualValue != null &&
actualValue.equals(expectedValue);
            }
        };
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:in")
                    .setHeader("author", xpath("/book/@author",
String.class))
                    .setHeader("type", xpath("/book/@type", String.class))
                    .setHeader("id", xpath("/book/id", String.class))
                    .setHeader("name", xpath("/book/name", String.class))
                    .to("mock:end");
            }
        };
    }
}


> -----Ursprüngliche Nachricht-----
> Von: Satyam Maloo [mailto:maloosatyam@gmail.com]
> Gesendet: Donnerstag, 27. März 2014 11:00
> An: users@camel.apache.org
> Betreff: Extract the value from property
> 
> The input looks like below,
> 
> <book author="ABC" type="Children">
>     <id>123</id>
>     <name>XYZ</name>
> </book>
> I have set the above in property in an xml route as:
> 
> <camel:setProperty propertyName="REQUEST">
>     <camel:xpath>/node()</camel:xpath>
> </camel:setProperty>
> 
> Then I do some other processing and based on the new response. I want
> to extract the value of an author(i.e. ABC) from this Property and
> compare it with a element's text string from the response.
> 
> I tried a few ways using camel:xpath and camel:simple but am not able
> to extract the value from property.
> 
> What is the correct way to extract this property?
> 
> 
> 
> -----
> Satyam
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Extract-the-value-from-property-
> tp5749428.html
> Sent from the Camel - Users mailing list archive at Nabble.com.