Search code examples
apacheapache-camelspring-dslcamel-ftp

Apache Camel - File Transfer on Particular Route Call


I'm trying to create a webservice which, when called look into a local directory picks up files from there and upload to the ftp server.

I'm able to create a simple route which picks file from local directory and uploads to ftp server below is the code :

<route>
        <from uri="file://D:\\FTPTest?noop=true&amp;delay=2000" />
        <to uri="ftp://[email protected]:21/public_html/EnterpriseProject?password=password123#"/>
        <to uri="bean:myBean?method=test" />
    </route>

But, I want to this file transfer to be called when a particular route is called via restlet webservice is called, I tried with the following code, but it didn't work :

<route>
        <from uri="direct:fileTransferRoute" />
            <to uri="file://D:\\FTPTest?noop=true&amp;delay=2000" />
            <to uri="ftp://[email protected]:21/public_html/EnterpriseProject?password=password123#"/>
        </route>

The above route is called by restlet from following route :

<route>
<from
            uri="restlet:http://0.0.0.0:9080/csitec/{serviceName}?restletMethod=post" />
        <process ref="serviceRouteProcessor" />
        <toD uri="direct:${in.header.nextRoute}" />

    </route>

Here's the code of my serviceRouteProcessor :

public void process(Exchange exchange) throws Exception {
    String body = exchange.getIn().getBody(String.class);
    String serviceName = exchange.getIn().getHeader(Constants.SERVICE_NAME).toString();
    String nextRoute = serviceName+Constants.NEXT_ROUTE_APPENDER;
    exchange.getOut().setHeader(Constants.NEXT_ROUTE, nextRoute);
    exchange.getOut().setBody(body);
}

Please help me and suggest the changes needs to be done to make it work like this.


Solution

  • You should try the pollEnrich feature of content-enricher

    In the example section you can find a example regarding files.

    Your route should look something like this(I work only with camel java dsl, so this a bit xml pseudo code):

    <route>
        <from uri="direct:fileTransferRoute" />
            <pollEnrich uri="file://D:\\FTPTest?fileName=data.txt....." />
            <to uri="ftp://[email protected]:21/public_html/EnterpriseProject?password=password123#"/>
        </route>