camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > FTP
Date Wed, 14 Apr 2010 13:37:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1520/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/FTP">FTP</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~arjanm">Arjan
Moraal</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="FTP-FTP%2FSFTPComponentCamel1.xonly"></a>FTP/SFTP
Component - <b>Camel 1.x only</b></h2>

<p>This component provides access to remote file systems over the FTP and SFTP protocols.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-cometd&lt;/artifactId&gt;
    &lt;version&gt;x.x.x&lt;/version&gt;
    &lt;!-- use the same version as your Camel core version --&gt;
&lt;/dependency&gt;
</pre>
</div></div>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Using Camel
2.x</b><br /><p>If you are using Camel 2.x then see the FTP documentation
at his <a href="/confluence/display/CAMEL/FTP2" title="FTP2">link</a>.<br/>
This documentation is only for Camel 1.x.</p></td></tr></table></div>


<h3><a name="FTP-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ftp:<span class="code-comment">//[username@]hostname[:port]/filename[?options]
</span>sftp:<span class="code-comment">//[username@]hostname[:port]/filename[?options]</span>
</pre>
</div></div>

<p>Where <b>filename</b> represents the underlying file name or directory.
Can contain nested folders.<br/>
The <b>username</b> is currently only possible to provide in the hostname parameter.</p>

<p>If no <b>username</b> is provided then <tt>anonymous</tt>
login is attempted using no password.<br/>
If no <b>port</b> number is provided, Camel will provide default values according
to the protocol (ftp = 21, sftp = 22).</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="FTP-URIOptions"></a>URI Options</h3>

<div class="confluenceTableSmall"><table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>directory</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Indicates whether or not the given file name should be interpreted
by default as a directory or file (as it sometimes hard to be sure with some FTP servers).
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>password</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Specifies the password to use to log in to the remote file
system. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>binary</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Specifies the file transfer mode <tt>BINARY</tt>
or <tt>ASCII</tt>. Default is <tt>ASCII</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClientConfig</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.5: Reference to a bean in the registry as a <tt><a
href="http://commons.apache.org/net/apidocs/org/apache/commons/net/ftp/FTPClientConfig.html"
rel="nofollow">org.apache.commons.net.ftp.FTPClientConfig</a></tt> class. Use
this option if you need to configure the client according to the FTP Server date format, locale,
timezone, platform etc. See the javadoc <tt><a href="http://commons.apache.org/net/apidocs/org/apache/commons/net/ftp/FTPClientConfig.html"
rel="nofollow">FTPClientConfig</a></tt> for more documentation.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.recursive</tt> </td>
<td class='confluenceTd'> <tt>true</tt>/<tt>false</tt> </td>
<td class='confluenceTd'> If a directory, will look for changes in files in all the
sub-directories. Is <tt>true</tt> by default for Camel 1.4 or older. Will change
to <tt>false</tt> by default value as of Camel 1.5. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.setNames</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>@deprecated</b> Used by FTPConsumer. If <tt>true</tt>,
Camel will use the filename the file has on the FTP server. The filename is stored on the
In message in the header, <tt>FileComponent.HEADER_FILE_NAME</tt>. <b>Note:</b>
In Camel 1.4 the default value has changed to <tt>true</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.delay</tt> </td>
<td class='confluenceTd'> <tt>500</tt> </td>
<td class='confluenceTd'> Delay in milliseconds between each poll. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.initialDelay</tt> </td>
<td class='confluenceTd'> <tt>1000</tt> </td>
<td class='confluenceTd'> Milliseconds before polling starts. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.userFixedDelay</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Set to <tt>true</tt> to use fixed delay between
polls, otherwise fixed rate is used. See <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html"
rel="nofollow">ScheduledExecutorService</a> in JDK for details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.regexPattern</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Used by FTPConsumer. Regular expression to use for matching
files when consuming. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.exclusiveReadLock</tt> </td>
<td class='confluenceTd'> <tt>false</tt></td>
<td class='confluenceTd'> Camel 1.5: Used by FTPConsumer. If set to <tt>true</tt>,
Camel will only poll the ftp files if it has exclusive read access to the file (that is, the
file is not in the process of being written). Camel will wait until exclusive access is granted,
testing once every second (where the test consists of Camel attempting to rename the file).
If set to <tt>false</tt>, Camel will poll the file even if it is in the process
of being written. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.deleteFile</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Camel 1.5: Used by FTPConsumer. Flag to set if the consumed
file should be deleted after it has been downloaded. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.moveNamePrefix</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.5: Used by FTPConsumer. The prefix string prepended
to the filename when moving it. For example, to move processed files into the <tt>done</tt>
directory, set this value to <tt>done/</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.moveNamePostfix</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.5: Used by FTPConsumer. The postfix string appended
to the filename when moving it. For example to rename processed files from <tt>foo</tt>
to <tt>foo.old</tt>, set this value to <tt>.old</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.excludedNamePrefix</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.5: Used by FTPConsumer. Used to exclude files, if
filename starts with the given prefix. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.excludedNamePostfix</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.5: Used by FTPConsumer. Used to exclude files, if
filename ends with the given postfix. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.timestamp</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Camel 1.5: <b>@deprecated</b> will be removed
in Camel 2.0. This option is only for backwards compatability. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>expression</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.5: Use an expression to dynamically set the filename.
This allows you very easily to set dynamic pattern style filenames. If an expression is set,
it takes precedence over the <tt>org.apache.camel.file.name</tt> header. (Note:
The header can itself also be an expression.) The <tt>expression</tt> option supports
both <tt>String</tt> and <tt>Expression</tt> types. If the expression
is a <tt>String</tt> type, it is <b>always</b> evaluated using the
<a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a>.
If the expression is an <tt>Expression</tt> type, then the specified <tt>Expression</tt>
type is used. This allows you, for instance, to use <a href="/confluence/display/CAMEL/OGNL"
title="OGNL">OGNL</a> expressions. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>passiveMode</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Camel 1.6.0: Specifies whether to use a passive mode connections.
Default is active mode (<tt>false</tt>). This feature is only for regular FTP,
not SFTP. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>knownHosts</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.6.0: Sets the <tt>known_hosts</tt> file,
so that the SFTP endpoint can do host key verification. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>privateKeyFile</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.6.0: Set the private key file to that the SFTP endpoint
can do private key verification. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>privateKeyFilePassphrase</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Camel 1.6.0: Set the private key file passphrase to that the
SFTP endpoint can do private key verification. </td>
</tr>
</tbody></table></div>

<h4><a name="FTP-Examples"></a>Examples</h4>
<p><tt><a href="ftp://someone@someftpserver.com/public/upload/images/holiday2008?password=secret&amp;binary=true"
rel="nofollow">ftp://someone@someftpserver.com/public/upload/images/holiday2008?password=secret&amp;binary=true</a></tt><br/>
<tt><a href="ftp://someoneelse@someotherftpserver.co.uk:12049/reports/2008/budget.txt?password=secret&amp;binary=false&amp;directory=false"
rel="nofollow">ftp://someoneelse@someotherftpserver.co.uk:12049/reports/2008/budget.txt?password=secret&amp;binary=false&amp;directory=false</a></tt><br/>
<tt><a href="ftp://publicftpserver.com/download" rel="nofollow">ftp://publicftpserver.com/download</a></tt></p>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Timestamp</b><br
/><p>In Camel 1.4 or older the FTP consumer uses an internal timestamp for last polling.
This timestamp is used to match for new remote files: if remote file modified timestamp &gt;
last poll timestamp =&gt; file can be consumed.</p>

<p>In Camel 1.5 this algorithm has been disabled by default, as it is not reliable over
the FTP protocol. FTP Servers only return file modified timestamps using <tt>hh:mm</tt>
(not seconds). And of course the clocks between the client and server can also be out of sync.
Bottom line is that timestamp check for FTP protocol should <b>not</b> be used.
That is why this feature is marked as <b>@deprecated</b> and will be removed in
Camel 2.0.</p>

<p>We encourage you to use a different strategy for matching new remote files: such
as deleting or moving the file after download.</p></td></tr></table></div>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>FTP does
not support concurrency for same endpoint</b><br /><p>In Camel 1.x both
the FTP consumer and FTP producer (created from the same endpoint) does not support concurrency
(the backing FTP client is not thread safe).</p></td></tr></table></div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>More examples</b><br
/><p>This component is an extension of the <a href="/confluence/display/CAMEL/File"
title="File">File</a> component. So there could be more samples and details on the
<a href="/confluence/display/CAMEL/File" title="File">File</a> component page
as well.</p></td></tr></table></div>

<h3><a name="FTP-NewdefaultbehaviorforFTP%2FSFTPConsumersinCamel1.5"></a>New
default behavior for FTP/SFTP-Consumers in Camel 1.5</h3>
<p>The consumer will always skip any file which name starts with a dot, such as <tt>".",
".camel", ".m2" or ".groovy"</tt>. Only files (not directories) is matched for valid
filename if options such as: <tt>consumer.regexPattern, consumer.excludeNamePrefix,
consumer.excludeNamePostfix</tt> is used.</p>

<p>The consumer recursive option will be changed from <b>true</b> to <b>false</b>
as the default value. We don't feel that Camel out-of-the-box should recursive poll.</p>

<p>The consumer will <b>not</b> use timestamp algorithm for determine if
a remote file is a new file - see warning section above. To use the old behavior of Camel
1.4 or older you can use the option <tt>consumer.timestamp=true</tt>.</p>

<h3><a name="FTP-ExclusiveReadLock"></a>Exclusive Read Lock</h3>
<p>The option <b>readLock</b> can be used to force Camel <b>not</b>
to consume files that is currently in the progress of being written. However this option is
default turned off, as it requires that the user has write access. There are other solutions
to avoid consuming files that are currently being written over FTP, for instance you can write
the a temporary destination and move the file after it has been written. </p>

<h3><a name="FTP-MessageHeaders"></a>Message Headers</h3>

<p>The following message headers can be used to affect the behavior of the component</p>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> org.apache.camel.file.name </td>
<td class='confluenceTd'> Specifies the output file name (relative to the endpoint directory)
to be used for the output message when sending to the endpoint. If this is not present and
no expression either then a generated message Id is used as filename instead.  </td>
</tr>
<tr>
<td class='confluenceTd'> org.apache.camel.file.name.produced </td>
<td class='confluenceTd'> New in Camel 1.5: The actual absolute filepath (path + name)
for the output file that was written. This header is set by Camel and its purpose is providing
end-users the name of the file that was written. </td>
</tr>
<tr>
<td class='confluenceTd'> file.remote.host </td>
<td class='confluenceTd'> The hostname of the remote server </td>
</tr>
<tr>
<td class='confluenceTd'> file.remote.name </td>
<td class='confluenceTd'> The name of the file consumed from the remote server </td>
</tr>
<tr>
<td class='confluenceTd'> file.remote.fullName </td>
<td class='confluenceTd'> The fullname of the file consumed from the remote server </td>
</tr>
</tbody></table>

<h3><a name="FTP-Consumerproperties"></a>Consumer properties</h3>

<p>When using FTPConsumer (downloading files from a FTP Server) the consumer specific
properties from the <a href="/confluence/display/CAMEL/File" title="File">File</a>
component should be prefixed with "consumer.". For example the delay option from File Component
should be specified as "consumer.delay=30000" in the URI. See the samples or some of the unit
tests of this component.</p>

<h3><a name="FTP-FilenameExpression"></a>Filename Expression</h3>
<p>In Camel 1.5 we have support for setting the filename using an expression. This can
be set either using the <b>expression</b> option or as a string based <a href="/confluence/display/CAMEL/File+Language"
title="File Language">File Language</a> expression in the <tt>org.apache.camel.file.name</tt>
header. See the <a href="/confluence/display/CAMEL/File+Language" title="File Language">File
Language</a> for some samples.</p>

<h3><a name="FTP-Camel1.xKnownissues"></a>Camel 1.x Known issues</h3>

<p>See the timestamp warning.</p>

<p>When consuming files (downloading) you must use type conversation to either String
or to InputStream for ASCII and BINARY file types. <br/>
In Camel 1.4 this is fixed, as there are build in type converters for the ASCII and BINARY
file types, meaning that you do not need the convertBodyTo expression.</p>

<p>In Camel 1.4 or below Camel FTPConsumer will poll files regardless if the file is
currently being written. See the <b>consumer.exclusiveReadLock</b> option.</p>

<p>Also in Camel 1.3 since setNames is default <b>false</b> then you must
explicitly set the filename using the setHeader expression when consuming from FTP directly
to File.<br/>
The code below illustrates this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">private</span> <span class="code-object">String</span>
ftpUrl = <span class="code-quote">"ftp:<span class="code-comment">//camelrider@localhost:21/<span
class="code-keyword">public</span>/downloads?password=admin&amp;binary=<span
class="code-keyword">false</span>"</span>;
</span><span class="code-keyword">private</span> <span class="code-object">String</span>
fileUrl = <span class="code-quote">"file:myfolder/?append=<span class="code-keyword">false</span>&amp;noop=<span
class="code-keyword">true</span>"</span>;

<span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
    <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span>
Exception {
        from(ftpUrl).setHeader(FileComponent.HEADER_FILE_NAME, constant(<span class="code-quote">"downloaded.txt"</span>)).convertBodyTo(<span
class="code-object">String</span>.class).to(fileUrl);
    }
};
</pre>
</div></div>

<p>Or you can set the option to <b>true</b> as illustrated below:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">private</span> <span class="code-object">String</span>
ftpUrl = <span class="code-quote">"ftp:<span class="code-comment">//camelrider@localhost:21/<span
class="code-keyword">public</span>/downloads?password=admin&amp;binary=<span
class="code-keyword">false</span>&amp;consumer.setNames=<span class="code-keyword">true</span>"</span>;
</span><span class="code-keyword">private</span> <span class="code-object">String</span>
fileUrl = <span class="code-quote">"file:myfolder/?append=<span class="code-keyword">false</span>&amp;noop=<span
class="code-keyword">true</span>"</span>;

<span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
    <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span>
Exception {
        from(ftpUrl).convertBodyTo(<span class="code-object">String</span>.class).to(fileUrl);
    }
};
</pre>
</div></div>

<h3><a name="FTP-Sample"></a>Sample</h3>

<p>In the sample below we setup Camel to download all the reports from the FTP server
once every hour (60 min) as BINARY content and store it as files on the local file system.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">protected</span> RouteBuilder
createRouteBuilder() <span class="code-keyword">throws</span> Exception {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span>
Exception {
            <span class="code-comment">// we use a delay of 60 minutes (eg. once pr.
hour we poll the FTP server
</span>            <span class="code-object">long</span> delay = 60 * 60
* 1000L;

            <span class="code-comment">// from the given FTP server we poll (= download)
all the files
</span>            <span class="code-comment">// from the <span class="code-keyword">public</span>/reports
folder as BINARY types and store <span class="code-keyword">this</span> as files
</span>            <span class="code-comment">// in a local directory. Camel will
use the filenames from the FTPServer
</span>
            <span class="code-comment">// notice that the FTPConsumer properties must
be prefixed with <span class="code-quote">"consumer."</span> in the URL
</span>            <span class="code-comment">// the delay parameter is from the
FileConsumer component so we should use consumer.delay as
</span>            <span class="code-comment">// the URI parameter name. The FTP
Component is an extension of the File Component.
</span>            from(<span class="code-quote">"ftp:<span class="code-comment">//scott@localhost/<span
class="code-keyword">public</span>/reports?password=tiger&amp;binary=<span
class="code-keyword">true</span>&amp;consumer.delay="</span> + delay).
</span>                to(<span class="code-quote">"file:<span class="code-comment">//target/test-reports"</span>);
</span>        }
    };
}
</pre>
</div></div>

<p>And the route using Spring DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;route&gt;</span>
     <span class="code-tag">&lt;from uri=<span class="code-quote">"ftp://scott@localhost/public/reports?password=tiger&amp;amp;binary=true&amp;amp;consumer.delay=60000"</span>/&gt;</span>
     <span class="code-tag">&lt;to uri=<span class="code-quote">"file://target/test-reports"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h4><a name="FTP-Usingexpressionforfilenames"></a>Using expression for filenames</h4>

<p>In this sample we want to move consumed files to a backup folder using today's date
as a sub foldername. Notice that the move happens on the remote FTP server. If you want to
store the downloaded file on your local disk then route it to the <a href="/confluence/display/CAMEL/File"
title="File">File</a> component as the sample above illustrates.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(ftpUrl + <span class="code-quote">"&amp;expression=backup/${date:now:yyyyMMdd}/${file:name}"</span>).to(<span
class="code-quote">"..."</span>);
</pre>
</div></div>

<p>See <a href="/confluence/display/CAMEL/File+Language" title="File Language">File
Language</a> for more samples.</p>

<h4><a name="FTP-ConsumingaremoteFTPservertriggeredbyaroute"></a>Consuming
a remote FTP server triggered by a route</h4>
<p>The FTP consumer is built as a scheduled consumer to be used in the <b>from</b>
route. However if you want to start consuming from a FTP server triggered within a route it's
a bit cumbersome to do this in Camel 1.x (we plan to improve this in Camel 2.x). However it's
possible as this code below demonstrates.</p>

<p>In the sample we have a <a href="/confluence/display/CAMEL/SEDA" title="SEDA">SEDA</a>
queue where a message arrives that holds a message containing a filename to poll from a remote
FTP server. So we setup a basic FTP url as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// we use directory=<span
class="code-keyword">false</span> to indicate we only want to consume a single file
</span><span class="code-comment">// we use delay=5000 to use 5 sec delay between
pools to avoid polling a second time before we stop the consumer
</span><span class="code-comment">// <span class="code-keyword">this</span>
is because we only want to run a single poll and get the file
</span><span class="code-comment">// file=getme/ is the path to the folder where
the file is
</span><span class="code-keyword">private</span> <span class="code-object">String</span>
getUrl = <span class="code-quote">"ftp:<span class="code-comment">//admin@localhost:"</span>
+ port + <span class="code-quote">"?password=admin&amp;binary=<span class="code-keyword">false</span>&amp;directory=<span
class="code-keyword">false</span>&amp;consumer.delay=5000&amp;file=getme/"</span>;</span>
</pre>
</div></div>

<p>And then we have the route where we use <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> within the route so we can use Java code. In this
Java code we create the ftp consumer that downloads the file we want. And after the download
we can get the content of the file and put it in the original exchange that continues being
routed. As this is based on an unit test it routes to a <a href="/confluence/display/CAMEL/Mock"
title="Mock">Mock</a> endpoint.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"seda:start"</span>).process(<span
class="code-keyword">new</span> Processor() {
    <span class="code-keyword">public</span> void process(<span class="code-keyword">final</span>
Exchange exchange) <span class="code-keyword">throws</span> Exception {
        <span class="code-comment">// get the filename from our custome header we want
to get from a remote server
</span>        <span class="code-object">String</span> filename = exchange.getIn().getHeader(<span
class="code-quote">"myfile"</span>, <span class="code-object">String</span>.class);

        <span class="code-comment">// construct the total url <span class="code-keyword">for</span>
the ftp consumer
</span>        <span class="code-object">String</span> url = getUrl + filename;

        <span class="code-comment">// create a ftp endpoint
</span>        Endpoint ftp = context.getEndpoint(url);

        <span class="code-comment">// create a polling consumer so we can poll the remote
ftp file
</span>        PollingConsumer consumer = ftp.createPollingConsumer();
        consumer.start();
        <span class="code-comment">// receive the remote ftp without timeout
</span>        Exchange result = consumer.receive();
        <span class="code-comment">// we must stop the consumer
</span>        consumer.stop();

        <span class="code-comment">// the result is the response from the FTP consumer
(the downloaded file)
</span>        <span class="code-comment">// replace the outher exchange with
the content from the downloaded file
</span>        exchange.getIn().setBody(result.getIn().getBody());
    }
}).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<h3><a name="FTP-Debuglogging"></a>Debug logging</h3>
<p>This component has log level <b>TRACE</b> that can be helpful if you
have problems.</p>

<h3><a name="FTP-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/FTP">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=57232&revisedVersion=69&originalVersion=68">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/FTP?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message