Search code examples
apache-cameljbossfuse

how to append file content in velocity in apache camel


I am trying to write a simple sample :) on Apache-camel velocity. It is supposed to pick up a file and insert its content in a pre-written text (in .vm file). But it just puts the file name in the place. Here is my scripts.

blueprint.xml

    <camelContext id="_context1" xmlns="http://camel.apache.org/schema/blueprint">
    <route id="_route1">
        <from id="_from1" uri="file:work/velocityFileInput"/>
        <setHeader headerName="receiver" id="_setHeader1">
            <simple>sir</simple>
        </setHeader>
        <setHeader headerName="senderName" id="_setHeader1">
            <simple>Magfa co.</simple>
        </setHeader>
        <setHeader headerName="meetingAddress" id="_setHeader1">
            <simple> Tehran , Iran</simple>
        </setHeader>
        <setHeader headerName="senderPersonalName" id="_setHeader1">
            <simple>Ehsan Zangeneh</simple>
        </setHeader>          
        <to id="_to1" uri="velocity:email.vm"/>
        <log message="${body}"></log>
    </route>
</camelContext>

and here is my .vm file content

Hello dear ${headers.receiver},
This letter is sent from ${headers.senderName} to inform you that you are
invited to the meeting in the address of '${headers.meetingAddress}'.
The meeting is about
${in}
Best regards
${headers.senderPersonalName} 

instead of ${in}, I also tried ${body} which didnt work.


Solution

  • Try to convert the body to string after retrieving content from URI the file.

    Might this work, Your route will be like this

    <route id="_route1">
     <from id="_from1" uri="file:work/velocityFileInput"/>
     <convertBodyTo id="_convertBodyFileContent" type="java.lang.String"/>
     <setHeader headerName="receiver" id="_setHeader1">
      <simple>sir</simple>
     </setHeader>
     <setHeader headerName="senderName" id="_setHeader1">
      <simple>Magfa co.</simple>
     </setHeader>
     <setHeader headerName="meetingAddress" id="_setHeader1">
       <simple> Tehran , Iran</simple>
     </setHeader>
     <setHeader headerName="senderPersonalName" id="_setHeader1">
      <simple>Ehsan Zangeneh</simple>
     </setHeader>          
     <to id="_to1" uri="velocity:email.vm"/>
     <log message="${body}"></log>
    </route>
    

    and here is your .vm file content

    Hello dear ${headers.receiver},
    This letter is sent from ${headers.senderName} to inform you that you are
    invited to the meeting in the address of '${headers.meetingAddress}'.
    The meeting is about
    ${body}
    Best regards
    ${headers.senderPersonalName}