camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > FTP2
Date Tue, 01 Jun 2010 08:17:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/FTP2">FTP2</a></h2>
    <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (5)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Maven users will need to
add the following dependency to their {{pom.xml}} for this component: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{code<span
class="diff-added-chars"style="background-color: #dfd;">:xml</span>}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >&lt;dependency&gt; <br>
   &lt;groupId&gt;org.apache.camel&lt;/groupId&gt; <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>h4. limitations <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
option *readLock* can be used to force Camel *not* to consume files that is currently in the
progress of being written. However, this option is turned off by default, as it requires that
the user has write access. And there is only a few options supported for FTP. <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">The
option *readLock* can be used to force Camel *not* to consume files that is currently in the
progress of being written. However, this option is turned off by default, as it requires that
the user has write access.</span> There are other solutions to avoid consuming files
that are currently being written over FTP; for instance, you can write to a temporary destination
and move the file after it has been written. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>When moving files using
{{move}} or {{preMove}} option the files are restricted to the FTP_ROOT folder. That prevents
you from moving files outside the FTP area. If you want to move files to another area you
can use soft links and move files into a soft linked folder. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h4. Consuming a remote FTPS server
(implicit SSL) and client authentication <br>{code} <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">from(&quot;ftps://admin@localhost:2222/public/camel?password=admin&amp;securityProtocol=SSL&amp;isImplicit=true<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">&amp;ftpClient.keyStore.file=./src/test/resources/server.jks&amp;ftpClient.keyStore.password=password&amp;ftpClient.keyStore.keyPassword=password&quot;)</span></span>
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
     &amp;ftpClient.keyStore.file=./src/test/resources/server.jks <br>      &amp;ftpClient.keyStore.password=password&amp;ftpClient.keyStore.keyPassword=password&quot;)
<br></td></tr>
            <tr><td class="diff-unchanged" >  .to(&quot;bean:foo&quot;);
<br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="FTP2-FTP%2FSFTP%2FFTPSComponentCamel2.0onwards"></a>FTP/SFTP/FTPS
Component - <b>Camel 2.0 onwards</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-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-ftp<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</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
1.x</b><br />If you are using Camel 1.x then see this <a href="/confluence/display/CAMEL/FTP"
title="FTP">link</a> for documentation.<br/>
This page is only for Camel 2.0 or newer.</td></tr></table></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 FTPS</b><br
/>The FTPS component is only available in Camel 2.2 or newer.<br/>
FTPS (also known as FTP Secure) is an extension to FTP that adds support for the Transport
Layer Security (TLS) and the Secure Sockets Layer (SSL) cryptographic protocols.</td></tr></table></div>

<h3><a name="FTP2-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]/directoryname[?options]
</span>sftp:<span class="code-comment">//[username@]hostname[:port]/directoryname[?options]
</span>ftps:<span class="code-comment">//[username@]hostname[:port]/directoryname[?options]</span>
</pre>
</div></div>

<p>Where <b>directoryname</b> represents the underlying 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, ftps = 2222).</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="FTP2-URIOptions"></a>URI Options</h3>
<p>The options below are exclusive for the <a href="/confluence/display/CAMEL/FTP2"
title="FTP2">FTP2</a> component. </p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<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>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, BINARY or ASCII. Default
is ASCII (<tt>false</tt>). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>disconnect</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.2:</b> Whether or not to disconnect
from remote FTP server right after use. Can be used for both consumer and producer. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>localWorkDirectory</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> When consuming, a local work directory can be used to store
the remote file content directly in local files, to avoid loading the content into memory.
This is beneficial, if you consume a very big remote file and thus can conserve memory. See
below for more details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>passiveMode</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>FTP and FTPS only</b>: Specifies whether to
use passive mode connections. Default is active mode (<tt>false</tt>). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>securityProtocol</tt> </td>
<td class='confluenceTd'> <tt>TLS</tt> </td>
<td class='confluenceTd'> <b>FTPS only:</b> Sets the underlying security
protocol. The following values are defined:<br class="atl-forced-newline" />
<tt>TLS</tt>: Transport Layer Security<br class="atl-forced-newline" />
<tt>SSL</tt>: Secure Sockets Layer </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>isImplicit</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>FTPS only:</b> Sets the security mode(implicit/explicit).
Default is explicit (<tt>false</tt>). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>knownHostsFile</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>SFTP only:</b> 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'> <b>SFTP only:</b> 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'> <b>SFTP only:</b> Set the private key file passphrase
to that the SFTP endpoint can do private key verification. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>strictHostKeyChecking</tt> </td>
<td class='confluenceTd'> <tt>no</tt> </td>
<td class='confluenceTd'> <b>SFTP only:</b> <b>Camel 2.2:</b>
Sets whether to use strict host key checking. Possible values are: <tt>no</tt>,
<tt>yes</tt> and <tt>ask</tt>. <tt>ask</tt> does not make
sense to use as Camel cannot answer the question for you as its meant for human intervention.
<b>Note:</b> The default in Camel 2.1 and below was <tt>ask</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maximumReconnectAttempts</tt> </td>
<td class='confluenceTd'> 3 </td>
<td class='confluenceTd'> Specifies the maximum reconnect attempts Camel performs when
it tries to connect to the remote FTP server. Use 0 to disable this behavior. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>reconnectDelay</tt> </td>
<td class='confluenceTd'> 1000 </td>
<td class='confluenceTd'> Delay in millis Camel will wait before performing a reconnect
attempt. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>FTP and FTPS Only:</b> <b>Camel 2.1:</b>
Allows you to use a custom <tt>org.apache.commons.net.ftp.FTPClient</tt> instance.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClientConfig</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>FTP and FTPS Only:</b> <b>Camel 2.1:</b>
Allows you to use a custom <tt>org.apache.commons.net.ftp.FTPClientConfig</tt>
instance. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.trustStore.file</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the trust store file, so
that the FTPS client can look up for trusted certificates. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.trustStore.type</tt> </td>
<td class='confluenceTd'> <tt>JKS</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the trust store type. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.trustStore.algorithm</tt> </td>
<td class='confluenceTd'> <tt>SunX509</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the trust store algorithm.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.trustStore.password</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the trust store password.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.keyStore.file</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the key store file, so
that the FTPS client can look up for the private certificate. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.keyStore.type</tt> </td>
<td class='confluenceTd'> <tt>JKS</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the key store type. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.keyStore.algorithm</tt> </td>
<td class='confluenceTd'> <tt>SunX509</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the key store algorithm.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.keyStore.password</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the key store password.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ftpClient.keyStore.keyPassword</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>FTPS Only:</b> Sets the private key password.
</td>
</tr>
</tbody></table>
</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>FTPS component
default trust store</b><br />By default, the FTPS component trust store accept
all certificates. If you only want trust selective certificates, you have to configure the
trust store with the <tt>ftpClient.trustStore.xxx</tt> options or by configuring
a custom <tt>ftpClient</tt>.</td></tr></table></div>

<p>You can configure additional options on the <tt>ftpClient</tt> and <tt>ftpClientConfig</tt>
from the URI directly by using the <tt>ftpClient.</tt> or <tt>ftpClientConfig.</tt>
prefix.</p>

<p>For example to set the <tt>setDataTimeout</tt> on the <tt>FTPClient</tt>
to 30 seconds you can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"ftp:<span class="code-comment">//foo@myserver?password=secret&amp;ftpClient.dataTimeout=30000"</span>).to(<span
class="code-quote">"bean:foo"</span>);</span>
</pre>
</div></div>

<p>You can mix and match and have use both prefixes, for example to configure date format
or timezones.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"ftp:<span class="code-comment">//foo@myserver?password=secret&amp;ftpClient.dataTimeout=30000&amp;ftpClientConfig.serverLanguageCode=fr"</span>).to(<span
class="code-quote">"bean:foo"</span>);</span>
</pre>
</div></div>

<p>You can have as many of these options as you like.</p>

<p>See the documentation of the <a href="http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClientConfig.html"
class="external-link" rel="nofollow">Apache Commons FTP FTPClientConfig</a> for possible
options and more details.<br/>
And as well for <a href="http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html"
class="external-link" rel="nofollow">Apache Commons FTP FTPClient</a>.</p>

<p>If you do not like having many and long configuration in the url you can refer to
the <tt>ftpClient</tt> or <tt>ftpClientConfig</tt> to use by letting
Camel lookup in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
for it.</p>

<p>For example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   &lt;bean id=<span class="code-quote">"myConfig"</span> class=<span class="code-quote">"org.apache.commons.net.ftp.FTPClientConfig"</span>&gt;
       &lt;property name=<span class="code-quote">"lenientFutureDates"</span>
value=<span class="code-quote">"<span class="code-keyword">true</span>"</span>/&gt;
       &lt;property name=<span class="code-quote">"serverLanguageCode"</span>
value=<span class="code-quote">"fr"</span>/&gt;
   &lt;/bean&gt;
</pre>
</div></div>

<p>And then let Camel lookup this bean when you use the # notation in the url.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"ftp:<span class="code-comment">//foo@myserver?password=secret&amp;ftpClientConfig=#myConfig"</span>).to(<span
class="code-quote">"bean:foo"</span>);</span>
</pre>
</div></div>


<h3><a name="FTP2-MoreURIoptions"></a>More URI options</h3>
<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>See <a href="/confluence/display/CAMEL/File2"
title="File2">File2</a> as all the options there also applies for this component.</td></tr></table></div>

<h4><a name="FTP2-Examples"></a>Examples</h4>
<p><tt><a href="ftp://someone@someftpserver.com/public/upload/images/holiday2008?password=secret&amp;binary=true"
class="external-link" 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/password=secret&amp;binary=false"
class="external-link" rel="nofollow">ftp://someoneelse@someotherftpserver.co.uk:12049/reports/2008/password=secret&amp;binary=false</a></tt><br/>
<tt><a href="ftp://publicftpserver.com/download" class="external-link" 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>FTP Consumer
does not support concurrency</b><br />The FTP consumer (with the same endpoint)
does not support concurrency (the backing FTP client is not thread safe).<br/>
You can use multiple FTP consumers to poll from different endpoints. It is only a single endpoint
that does not support concurrent consumers.

<p>The FTP producer does <b>not</b> have this issue, it supports concurrency.</p>

<p>In the future we will <a href="https://issues.apache.org/activemq/browse/CAMEL-1682"
class="external-link" rel="nofollow">add consumer pooling to Camel</a> to allow this
consumer to support concurrency as well.</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 information</b><br
/>This component is an extension of the <a href="/confluence/display/CAMEL/File2" title="File2">File2</a>
component. So there are more samples and details on the <a href="/confluence/display/CAMEL/File2"
title="File2">File2</a> component page.</td></tr></table></div>

<h3><a name="FTP2-Defaultwhenconsumingfiles"></a>Default when consuming
files</h3>
<p>The <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> consumer
will by default leave the consumed files untouched on the remote FTP server. You have to configure
it explicit if you want it to delete the files or move them to another location. For example
you can use <tt>delete=true</tt> to delete the files, or use <tt>move=.done</tt>
to move the files into a hidden done sub directory.</p>

<p>The regular <a href="/confluence/display/CAMEL/File2" title="File2">File</a>
consumer is different as it will by default move files to a <tt>.camel</tt> sub
directory. The reason Camel does <b>not</b> do this by default for the FTP consumer
is that it may lack permissions by default to be able to move or delete files.</p>

<h4><a name="FTP2-limitations"></a>limitations</h4>
<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 turned off by default, as it requires that the user has write access. And there is only
a few options supported for FTP.<br/>
There are other solutions to avoid consuming files that are currently being written over FTP;
for instance, you can write to a temporary destination and move the file after it has been
written. </p>

<p>When moving files using <tt>move</tt> or <tt>preMove</tt>
option the files are restricted to the FTP_ROOT folder. That prevents you from moving files
outside the FTP area. If you want to move files to another area you can use soft links and
move files into a soft linked folder.</p>

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

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

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileName</tt> </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 the filename instead.  </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileNameProduced</tt> </td>
<td class='confluenceTd'> 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'> <tt>CamelFileBatchIndex</tt> </td>
<td class='confluenceTd'> Current index out of total number of files being consumed
in this batch. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileBatchSize</tt> </td>
<td class='confluenceTd'> Total number of files being consumed in this batch. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileHost</tt> </td>
<td class='confluenceTd'> The remote hostname. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileLocalWorkPath</tt> </td>
<td class='confluenceTd'> Path to the local work file, if local work directory is used.
</td>
</tr>
</tbody></table>
</div>


<h3><a name="FTP2-UsingLocalWorkDirectory"></a>Using Local Work Directory</h3>
<p>Camel supports consuming from remote FTP servers and downloading the files directly
into a local work directory. This avoids reading the entire remote file content into memory
as it is streamed directly into the local file using <tt>FileOutputStream</tt>.
</p>

<p>Camel will store to a local file with the same name as the remote file, though with
<tt>.inprogress</tt> as extension while the file is being downloaded. Afterwards,
the file is renamed to remove the <tt>.inprogress</tt> suffix. And finally, when
the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
is complete the local file is deleted. </p>

<p>So if you want to download files from a remote FTP server and store it as files then
you need to route to a file endpoint such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"ftp:<span class="code-comment">//someone@someserver.com?password=secret&amp;localWorkDirectory=/tmp"</span>).to(<span
class="code-quote">"file://inbox"</span>);</span>
</pre>
</div></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>Optimization by renaming
work file</b><br />The route above is ultra efficient as it avoids reading the
entire file content into memory. It will download the remote file directly to a local file
stream. The <tt>java.io.File</tt> handle is then used as the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> body. The file producer leverages this fact and can
work directly on the work file <tt>java.io.File</tt> handle and perform a <tt>java.io.File.rename</tt>
to the target filename. As Camel knows it's a local work file, it can optimize and use a rename
instead of a file copy, as the work file is meant to be deleted anyway.</td></tr></table></div>

<h3><a name="FTP2-Samples"></a>Samples</h3>

<p>In the sample below we set up 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;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="FTP2-ConsumingaremoteFTPSserver%28implicitSSL%29andclientauthentication"></a>Consuming
a remote FTPS server (implicit SSL) and client authentication</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from("ftps:<span class="code-comment">//admin@localhost:2222/<span class="code-keyword">public</span>/camel?password=admin&amp;securityProtocol=SSL&amp;isImplicit=<span
class="code-keyword">true</span>
</span>      &amp;ftpClient.keyStore.file=./src/test/resources/server.jks
      &amp;ftpClient.keyStore.password=password&amp;ftpClient.keyStore.keyPassword=password")
  .to(<span class="code-quote">"bean:foo"</span>);
</pre>
</div></div>

<h4><a name="FTP2-ConsumingaremoteFTPSserver%28explicitTLS%29andacustomtruststoreconfiguration"></a>Consuming
a remote FTPS server (explicit TLS) and a custom trust store configuration</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"ftps:<span class="code-comment">//admin@localhost:2222/<span
class="code-keyword">public</span>/camel?password=admin&amp;ftpClient.trustStore.file=./src/test/resources/server.jks&amp;ftpClient.trustStore.password=password"</span>)
</span>  .to(<span class="code-quote">"bean:foo"</span>);
</pre>
</div></div>

<h3><a name="FTP2-Filterusing%7B%7Borg.apache.camel.component.file.GenericFileFilter%7D%7D"></a>Filter
using <tt>org.apache.camel.component.file.GenericFileFilter</tt></h3>
<p>Camel supports pluggable filtering strategies. This strategy it to use the build
in <tt>org.apache.camel.component.file.GenericFileFilter</tt> in Java. You can
then configure the endpoint with such a filter to skip certain filters before being processed.</p>

<p>In the sample we have build our own filter that only accepts files starting with
report in the filename.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MyFileFilter
<span class="code-keyword">implements</span> GenericFileFilter {

    <span class="code-keyword">public</span> <span class="code-object">boolean</span>
accept(GenericFile file) {
        <span class="code-comment">// we only want report files 
</span>        <span class="code-keyword">return</span> file.getFileName().startsWith(<span
class="code-quote">"report"</span>);
    }
}
</pre>
</div></div>

<p>And then we can configure our route using the <b>filter</b> attribute
to reference our filter (using <tt>&#35;</tt> notation) that we have defines
in the spring XML file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
   <span class="code-tag"><span class="code-comment">&lt;!-- define our sorter
as a plain spring bean --&gt;</span></span>
   <span class="code-tag">&lt;bean id=<span class="code-quote">"myFilter"</span>
class=<span class="code-quote">"com.mycompany.MyFileFilter"</span>/&gt;</span>

  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"ftp://someuser@someftpserver.com?password=secret&amp;amp;filter=#myFilter"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:processInbox"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>


<h3><a name="FTP2-FilteringusingANTpathmatcher"></a>Filtering using ANT
path matcher</h3>
<p>The ANT path matcher is a filter that is shipped out-of-the-box in the <b>camel-spring</b>
jar. So you need to depend on <b>camel-spring</b> if you are using Maven.<br/>
The reason is that we leverage Spring's <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/util/AntPathMatcher.html"
class="external-link" rel="nofollow">AntPathMatcher</a> to do the actual matching.</p>

<p>The file paths are matched with the following rules:</p>
<ul class="alternate" type="square">
	<li><tt>?</tt> matches one character</li>
	<li><tt>*</tt> matches zero or more characters</li>
	<li><tt>**</tt> matches zero or more directories in a path</li>
</ul>


<p>The sample below demonstrates how to use it:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext xmlns=<span
class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;template id=<span class="code-quote">"camelTemplate"</span>/&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- use myFilter
as filter to allow setting ANT paths for which files to scan for --&gt;</span></span>
    <span class="code-tag">&lt;endpoint id=<span class="code-quote">"myFTPEndpoint"</span>
uri=<span class="code-quote">"ftp://admin@localhost:20123/antpath?password=admin&amp;amp;recursive=true&amp;amp;delay=10000&amp;amp;initialDelay=2000&amp;amp;filter=#myAntFilter"</span>/&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from ref=<span class="code-quote">"myFTPEndpoint"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- we use the AntPathMatcherRemoteFileFilter
to use ant paths for includes and exlucde --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"myAntFilter"</span>
class=<span class="code-quote">"org.apache.camel.component.file.AntPathMatcherGenericFileFilter"</span>&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- include and
file in the subfolder that has day in the name --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"includes"</span>
value=<span class="code-quote">"**/subfolder/**/*day*"</span>/&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- exclude all
files with bad in name or .xml files. Use comma to seperate multiple excludes --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"excludes"</span>
value=<span class="code-quote">"**/*bad*,**/*.xml"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<h3><a name="FTP2-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="FTP2-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>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/File2" title="File2">File2</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/FTP2">View Online</a>
        |
        <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=109470&revisedVersion=34&originalVersion=33">View
Changes</a>
                |
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/FTP2?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message