Search code examples
apache-camelapache-servicemix

Apache camel exec message body


I have the following route:

            <route id="import">
                    <from uri="file:/var/inputfolder?delete=true"/>
                            <log message="Importing ${file:name} started."/>
                            <setProperty propertyName="keepbody">
                                    <simple>${body}</simple>
                            </setProperty>
                            <setHeader headerName="BrandId">
                                    <simple>${file:name}</simple>
                            </setHeader>
                            <setHeader headerName="CamelExecCommandArgs">
                                    <simple>${file:absolute.path} ${header.BrandId.toString.split("_")[0]} Zip -a erase</simple>
                            </setHeader>
                    <to uri="exec://./transfile.php?workingDir=/usr/bin/&amp;args&amp;useStderrOnEmptyStdout=true"/>
                            <log message="stdout/stderr of /usr/bin/transfile.php:\n ${body}"/>

                    <choice>
                            <when>
                                    <simple>${bodyAs(String)} contains 'Return OK'</simple>
                                    <log message="Importing ${file:name} finished."/>
                            </when>
                            <otherwise>
                                    <log message="Importing ${file:name} failed."/>
                                    <setBody>
                                          <simple>${exchangeProperty.keepbody}</simple>
                                    </setBody>
                                    <to uri="file:/var/rejected"/>
                            </otherwise>
                    </choice>

            </route>

The route checks the folder /var/inputfolder for files. If it finds any, it executes the transfile.php script, and uses the file name in its arguments. If the output of this execution does not contain the string 'Return OK', the stdout will be put in a file with the original filename into the folder /var/rejected. But I would like to achive, that in the /var/rejected folder the file with its original content appears, not the with output of the execution. I know, that this is the normal behavior of the exec component

https://camel.apache.org/exec.html

but how can I change this to implement the previously mentioned?

Thanks in advance!


Solution

  • You need to store the message body as an exchange property, and then replace the body afterwards the exec for example

    <setProperty propertyName="keepBody">
      <simple>${body}</simple>
    </setProperty>
    

    And then restore it afterwards

    <setBody>
     <simple>${exchangeProperty.keepBody}</simple>
    </setBody>
    

    Notice depending on how old Apache Camel you use, then the name exchangeProperty may be an older name such as property. See more at: http://camel.apache.org/simple

    We are thinking about adding a push/pop to the Camel DSL to make this easier in the future: https://issues.apache.org/jira/browse/CAMEL-8958