camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Mueller (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > JCIFS
Date Tue, 27 Aug 2013 20:18:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/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="https://cwiki.apache.org/confluence/display/CAMEL/JCIFS">JCIFS</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="JCIFS-JCIFSComponent"></a>JCIFS Component </h2>

<p>This component provides access to remote file systems over the CIFS/SMB networking
protocol. </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="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;"> 
&lt;dependency&gt; 
  &lt;groupId&gt;org.apache-extras.camel-extra&lt;/groupId&gt; 
  &lt;artifactId&gt;camel-jcifs&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='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>Consuming from remote
server</b><br />Make sure you read the section titled <em>Default when consuming
files</em> further below for details related to consuming files.</td></tr></table></div>


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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;"> 
smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]]]][?options] 
</pre>
</div></div> 

<p>Where <b>share</b> represents the share to connect to and <b>dir</b>
is optionaly any underlying directory. Can contain nested folders. </p>

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

<p>This component uses the <a href="http://jcifs.samba.org/" class="external-link"
rel="nofollow">JCIFS</a> library for the actual CIFS/SMB work.</p>

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

<p>The options below are exclusive for the JCIFS 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'> <b>Mandatory</b> Specifies the password to use
to log in to the remote file system. </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>
</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>More options</b><br
/>See <a href="/confluence/display/CAMEL/File2" title="File2">File</a> for
more options as all the options from <a href="/confluence/display/CAMEL/File2" title="File2">File</a>
is inherited.</td></tr></table></div> 

<p>For example to set the <tt>localWorkDirectory</tt> to ''/tmp'' you can
do: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;"> 
from("smb://foo@myserver.example.com/sharename?password=secret&amp;localWorkDirectory=/tmp")
  .to("bean:foo"); 
</pre>
</div></div> 


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

<h3><a name="JCIFS-Examples"></a>Examples </h3>

<p><tt><a href="smb://foo@myserver.example.com/sharename?password=secret" class="external-link"
rel="nofollow">smb://foo@myserver.example.com/sharename?password=secret</a></tt>
<br/>
<tt><a href="smb://companydomain;foo@myserver.company.com/sharename?password=secret"
class="external-link" rel="nofollow">smb://companydomain;foo@myserver.company.com/sharename?password=secret</a></tt>
</p>

<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">File</a>
component. So there are more samples and details on the <a href="/confluence/display/CAMEL/File2"
title="File2">File</a> component page.</td></tr></table></div>


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

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

<div class="confluenceTableSmall"><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>
</div> 


<h3><a name="JCIFS-UsingLocalWorkDirectory"></a>Using Local Work Directory
</h3>

<p>Camel JCIFS supports consuming from remote 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 JCIFS will store to a local file with the same name as the remote file. 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 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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;"> 
from("smb://foo@myserver.example.com/sharename?password=secret&amp;localWorkDirectory=/tmp")
  .to("file://inbox"); 
</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="JCIFS-Samples"></a>Samples </h3>

<p>In the sample below we set up Camel to download all the reports from the SMB/CIFS
server once every hour (60 min) 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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
protected RouteBuilder createRouteBuilder() throws Exception {
  return new RouteBuilder() {
    public void configure() throws Exception {
      // we use a delay of 60 minutes (eg. once pr. hour) we poll the server
      long delay = 60 * 60 * 1000L;
 
      // from the given server we poll (= download) all the files
      // from the public/reports folder and store this as files
      // in a local directory. Camel will use the filenames from the server
      from("smb://foo@myserver.example.com/public/reports?password=secret&amp;delay="
+ delay)
        .to("file://target/test-reports");
    }
  };
}

from("smb://foo@myserver.example.com/sharename?password=secret&amp;amp;delay=60000")
  .to("file://target/test-reports")
</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="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;"> 
&lt;route&gt; 
  &lt;from uri="smb://foo@myserver.example.com/sharename?password=secret&amp;amp;delay=60000"/&gt;

  &lt;to uri="file://target/test-reports"/&gt; 
&lt;/route&gt; 
</pre>
</div></div> 

<h3><a name="JCIFS-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 built 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">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
public class MyFileFilter&lt;T&gt; implements GenericFileFilter&lt;T&gt; {

    public boolean accept(GenericFile&lt;T&gt; file) {
        // we only want report files 
        return file.getFileName().startsWith("report");
    }
}
]]></script>
</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 defined
in the spring XML file: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;"> 
&lt;!-- define our sorter as a plain spring bean --&gt; 
&lt;bean id="myFilter" class="com.mycompany.MyFileFilter"/&gt; 

&lt;route&gt; 
  &lt;from uri="smb://foo@myserver.example.com/sharename?password=secret&amp;amp;filter=#myFilter"/&gt;

  &lt;to uri="bean:processInbox"/&gt; 
&lt;/route&gt; 
</pre>
</div></div> 


<h3><a name="JCIFS-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.springsource.org/spring/docs/3.0.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>&#42;</tt> matches zero or more characters</li>
	<li><tt>&#42;&#42;</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="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;camelContext xmlns="http://camel.apache.org/schema/spring"&gt;
  &lt;template id="camelTemplate"/&gt;

  &lt;!-- use myFilter as filter to allow setting ANT paths for which files to scan for
--&gt;
  &lt;endpoint id="mySMBEndpoint" uri="smb://foo@myserver.example.com/sharename?password=secret&amp;amp;recursive=true&amp;amp;filter=#myAntFilter"/&gt;

  &lt;route&gt;
    &lt;from ref="mySMBEndpoint"/&gt;
    &lt;to uri="mock:result"/&gt;
  &lt;/route&gt;
&lt;/camelContext&gt;
 
&lt;!-- we use the AntPathMatcherRemoteFileFilter to use ant paths for includes and exclude
--&gt;
&lt;bean id="myAntFilter" class="org.apache.camel.component.file.AntPathMatcherGenericFileFilter"&gt;
  &lt;!-- include any files in the sub folder that has day in the name --&gt;
  &lt;property name="includes" value="**/subfolder/**/*day*"/&gt;
  &lt;!-- exclude all files with bad in name or .xml files. Use comma to separate multiple
excludes --&gt;
  &lt;property name="excludes" value="**/*bad*,**/*.xml"/&gt;
&lt;/bean&gt;
</pre>
</div></div>

<h3><a name="JCIFS-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;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/JCIFS">View Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/JCIFS?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message