Camel Sftp - Download Multiple Files - apache-camel

I am able to successfully download one/all files from a sftp directory using the following uri in canmel route definition
Download all files
"sftp://userName#serverName/directoryName?knownHostsFile=./known_hosts&privateKeyFile=./id_rsa&proxy=#proxy&noop=true"
Download one file
"sftp://userName#serverName/directoryName?knownHostsFile=./known_hosts&privateKeyFile=./id_rsa&proxy=#proxy&noop=true&fileName=one.txt"
My requirement is to download a specific list of files = one.text, two.text.
How can I pass list of fileNames to the Camel route? Preferablly I am looking for a solution where in I can specify something like the below
"sftp://userName#serverName/directoryName?knownHostsFile=./known_hosts&privateKeyFile=./id_rsa&proxy=#proxy&noop=true&fileName=one.txt,two.txt"

The Camel FTP component extends the File component and many of the options from file is also applicable for the FTP component, so read this page
http://camel.apache.org/file2
You can for example use include to specify a regular expression that matches the files you want. Or implement a custom filter class and use the filter option, etc.

Related

How to configure retrieving folder once when camel start?

My requirement as below,
Starting camel,then processing all of the files with file component ,which is located some folder once.this route is just used once.
So how to configure with spring DSL?
Thanks advance.
Split your requirement as follow
Starting camel
Your research item, cover this will be off-topic in stackoverflow
processing all of the files with file component
Use file component by from, i.e. from("file://...")
is located some folder once
Set a proper parent directory and then use recursive option in file component to enable sub-directories lookup. For further control, you may check following options minDepth, maxDepth, filterDirectory, filter
this route is just used once
Use repeatCount option in file component to control fire count, i.e. repeatCount=1
Combine them together, you have
from("file://path/to/parent/directory?repeatCount=1&recursive=true")
... // follow by your route logic

Apache Camel File component base path

How do I manage to configure the File component of my Camel Context, so that all directory paths provided to its endpoints are prepended with some base path?
For example, if someone writes
file:input/customer12?include=.*\.csv
it will effectively be
file:/usr/local/share/app/exchange/input/customer12?include=.*\.csv
For example, I get the component during the Camel Context initialization like this:
FileComponent file = CAMELCONTEXT.getComponent("file", FileComponent.class);
What do I do next? createComponentConfiguration()?
addition: It's a standalone cli app which I want to be runnable from any directory
The easiest solution is start your app under folder /usr/local/share/app/exchange , but this is not an option as state in addition requirement
If you have a layer between your user input and your code, then you could inject the path. For example, Java DSL with RouteBuilder.
The last solution is override Camel's file component in component class before it create the actual endpoint.

Getting Filename without extension : camel

I have header with filename with extension like this
<setHeader>
<simple>Test.txt</simple>
</setHeader>
I need to fetch only filename ie. Test alone in another header. Can anyone help me to achieve this.
Thanks.
You have multiple options. The examples take the full filename from a header and write the filename without extension into another header.
You write a Java Bean to use full Java power (for example Apache Commons FilenameUtils.getBaseName) and call it from your route. See this Camel documentation how to inject header values into your bean methods. In the route you call the bean like this
.setHeader("filename", method(beanReference, "methodName"))
Or you add camel-groovy to your dependencies to get more scripting power than with Camel Simple. Then you can do it directly in the Camel route
setHeader("filename").groovy("request.headers.get('fullFilename')
.take(request.headers.get('fullFilename').lastIndexOf('.'))")

Dynamic mapping for BeanIO in Camel on windows

I working on CSV to CSV file conversion using beanIO camel compnnet, is there any way or workaround to load mapping files on fly or at runtime in marshal /unmarshal endpoint in camel route.
Hard coding of files is working for me but not the with placeholder
.unmarshal(new BeanIODataFormat("file://C://Camel//Dev//mapping//unmarshlling//inputMapping.xml", "xyz"))
OR
.to("dataformat:beanio:unmarshal?mapping='file://C://Camel//Dev//mapping//unmarshlling//inputMapping.xml'?streamName=xyz")
is working but
.unmarshal(new BeanIODataFormat("{{file name}}", "xyz"))
OR
.to("dataformat:beanio:unmarshal?mapping=("{{file name}}"?streamName=xyz")
Is not working for me , I want to put mapping file as in put and which will load from external folder please help

Apache Camel / String template component / Template Groups

In Apache Camel, is it possible to use template groups (c.f. https://theantlrguy.atlassian.net/wiki/display/ST4/Group+file+syntax) using the string-template component ?
I would like to load a template group file to be able to used the named templates defined there and use them in the template file refered to by a string-template endpoint.
There does not seem to be any means to configure the string-template endpoint or component to define where to load a group file.
Any ideas on whether this is possible or not and if so how ?
Is there a reason for needing the template groups and ST4? According to the String-Template Component page:
string-template:templateName[?options]
Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template.
I would think you could put all your template files in the same folder and treat that as your group.

Resources