You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Olivier Roger <ol...@bsb.com> on 2010/04/19 15:44:49 UTC

Velocity component

Hello,

When trying to use the Velocity component I encountered a problem.
I have a Simple SQL query I would like to built using the Message Body (XML)

I was using an XSL file to perform the transformation by extracting the
values into the SQL query templace. 

<xsl:template match="/">INSERT INTO person VALUES('<xsl:value-of
select="ns0:person/ns0:username"/>','<xsl:value-of
select="ns0:person/ns0:firstName"/>','<xsl:value-of
select="ns0:person/ns0:lastName"/>','<xsl:value-of
select="ns0:person/ns0:mailAddress"/>','<xsl:value-of
select="ns0:person/ns0:password"/>','<xsl:value-of
select="ns0:person/ns0:address/ns0:zipCode"/>','<xsl:value-of
select="ns0:person/ns0:socialId"/>')</xsl:template>

However I think using Velocity would be easier to understand.

Is there a way to using the ${body.<xpath>} placeholder in the velocity
template? because at the moment I use the headers, which works, but is not
really practical for larger files.

Here is my expected VM file

INSERT INTO person
VALUES('${body.ns0:person/ns0:username}','${body.ns0:person/ns0:firstName}','${body.ns0:person/ns0:lastName}','${body.ns0:person/ns0:mailAddress}','${body.ns0:person/ns0:password}','${body.ns0:person/ns0:address/ns0:zipCode}','${body.ns0:person/ns0:socialId}')


I am also intressted in any other way to use the body values.

Thanks in avance,

Olivier
-- 
View this message in context: http://old.nabble.com/Velocity-component-tp28287678p28287678.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Velocity component

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Apr 22, 2010 at 3:59 PM, Olivier Roger <ol...@bsb.com> wrote:
>
> I could indeed unmarshal using JAXB but I would like to use as less Java as
> possible.
>
> I am facing an other problem now:
> When I extract the value using XPath like this:
>
> <setHeader
> headerName="firstname"><xpath>/ns1:person/ns1:firstName/text()</xpath></setHeader>
>
> and use it in the velocity template with:
>
> Hello ${headers.firstname}
>
> It produce this kind of result:
>
> Hello com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList@1acee78
>
> Is there some way to use set the header value as text ?

You have to tell the <xpath> that the resultType should be String

<xpath resultType="String">bla bla</xpath>

See setting result type
http://camel.apache.org/xpath


>
> Thanks in advance !
>
>
> Christian Mueller wrote:
>>
>> Hello Oliver!
>>
>> I think another possibility could be for you to use JAXB to unmarshal your
>> XML document into a java bean. Then you can use the following expression
>> in
>> your velocity template: $body.username
>>
>> Please have a look on this test:
>>
>> http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityBodyAsDomainObjectTest.java?view=markup
>>
>> and
>>
>> http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/resources/org/apache/camel/component/velocity/BodyAsDomainObject.vm?view=markup
>>
>> Regards,
>> Christian
>>
>>
>
> --
> View this message in context: http://old.nabble.com/Velocity-component-tp28287678p28329284.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

Re: Velocity component

Posted by Olivier Roger <ol...@bsb.com>.
I could indeed unmarshal using JAXB but I would like to use as less Java as
possible.

I am facing an other problem now:
When I extract the value using XPath like this:

<setHeader
headerName="firstname"><xpath>/ns1:person/ns1:firstName/text()</xpath></setHeader>

and use it in the velocity template with:

Hello ${headers.firstname}

It produce this kind of result:

Hello com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList@1acee78

Is there some way to use set the header value as text ?

Thanks in advance !


Christian Mueller wrote:
> 
> Hello Oliver!
> 
> I think another possibility could be for you to use JAXB to unmarshal your
> XML document into a java bean. Then you can use the following expression
> in
> your velocity template: $body.username
> 
> Please have a look on this test:
> 
> http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityBodyAsDomainObjectTest.java?view=markup
> 
> and
> 
> http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/resources/org/apache/camel/component/velocity/BodyAsDomainObject.vm?view=markup
> 
> Regards,
> Christian
> 
> 

-- 
View this message in context: http://old.nabble.com/Velocity-component-tp28287678p28329284.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Velocity component

Posted by Christian Müller <ch...@gmail.com>.
Hello Oliver!

I think another possibility could be for you to use JAXB to unmarshal your
XML document into a java bean. Then you can use the following expression in
your velocity template: $body.username

Please have a look on this test:

http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityBodyAsDomainObjectTest.java?view=markup

and

http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/resources/org/apache/camel/component/velocity/BodyAsDomainObject.vm?view=markup

Regards,
Christian

Re: Velocity component

Posted by Ashwin Karpe <as...@progress.com>.
Hi Olivier,

This is not possible at the moment with the velocity component since there
is no way to assume that the payload in the exchange body is an XML
fragment. It could be a straight text string or a GIF/JPEG.

But there is a workaround. If you know that payload to be XML, you could
create the right header elements on the route by adding values as needed to
the content header using a convenient processor and then use a velocity
template. 

If you feel strongly about this need, please create a JIRA entry requesting
this feature that would only work on XML based payloads sent in the body
element.

Cheers,

Ashwin...


Olivier Roger wrote:
> 
> Hello,
> 
> When trying to use the Velocity component I encountered a problem.
> I have a Simple SQL query I would like to built using the Message Body
> (XML)
> 
> I was using an XSL file to perform the transformation by extracting the
> values into the SQL query templace. 
> 
> <xsl:template match="/">INSERT INTO person VALUES('<xsl:value-of
> select="ns0:person/ns0:username"/>','<xsl:value-of
> select="ns0:person/ns0:firstName"/>','<xsl:value-of
> select="ns0:person/ns0:lastName"/>','<xsl:value-of
> select="ns0:person/ns0:mailAddress"/>','<xsl:value-of
> select="ns0:person/ns0:password"/>','<xsl:value-of
> select="ns0:person/ns0:address/ns0:zipCode"/>','<xsl:value-of
> select="ns0:person/ns0:socialId"/>')</xsl:template>
> 
> However I think using Velocity would be easier to understand.
> 
> Is there a way to using the ${body.<xpath>} placeholder in the velocity
> template? because at the moment I use the headers, which works, but is not
> really practical for larger files.
> 
> Here is my expected VM file
> 
> INSERT INTO person
> VALUES('${body.ns0:person/ns0:username}','${body.ns0:person/ns0:firstName}','${body.ns0:person/ns0:lastName}','${body.ns0:person/ns0:mailAddress}','${body.ns0:person/ns0:password}','${body.ns0:person/ns0:address/ns0:zipCode}','${body.ns0:person/ns0:socialId}')
> 
> 
> I am also intressted in any other way to use the body values.
> 
> Thanks in avance,
> 
> Olivier
> 


-----
--- 
Ashwin Karpe, Principal Consultant, PS - Opensource Center of Competence 
Progress Software Corporation
14 Oak Park Drive
Bedford, MA 01730
--- 
+1-972-304-9084 (Office) 
+1-972-971-1700 (Mobile) 
---- 
Blog: http://opensourceknowledge.blogspot.com/


-- 
View this message in context: http://old.nabble.com/Velocity-component-tp28287678p28287894.html
Sent from the Camel - Users mailing list archive at Nabble.com.