Getting Filename without extension : camel - file

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('.'))")

Related

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.

Dynamically change Apache Camel file options

I wish to change the file (or ftp) Camel component behaviour according to some value, for intance the value of a header.
I see in the Camel File documentation I can dynamically change the "CamelFilename" header, but ca we do the same with other options such as "fileexist" ?
My goal is to use only 1 FTP producer to write all files, some of them can be overriden and the others can't.
You can use Camel's .toD(..)
More info: http://camel.apache.org/message-endpoint.html#MessageEndpoint-DynamicTo

Validate Camel route programmatically

I'm working on a logging solution where Camel routes are defined at runtime with a Java DSL String. I wonder if there's a way to check programmatically some errors such as components not found in the route. The only option I was able to find is catching the org.apache.camel.ResolveEndpointFailedException and dig into the error message. Is there a better way to validate the route?
Just to give an example, it would be good to ascertain if a route syntax is completely wrong or if just a component wasn't found so that I can output a message e.g. "install ftp component".
You can use Fabric8 Camel Maven Plugin (http://fabric8.io/guide/camelMavenPlugin.html) for validating Camel endpoints in the source code.
Look at this article by Claus Ibsen to get more information : https://blog.fabric8.io/cheers-fabric8-camel-maven-plugin-to-validate-camel-endpoints-from-source-code-8768aff76b41#.wcji8hfdg

Camel Sftp - Download Multiple Files

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.

Is there anything wrong with using html files without the ".html" extension?

I was wondering if there is anything wrong with using html and php files without extensions.
For example, if I was to upload a file with an extension, I would get to by using a URL like this:
http://yoursite.com/randompage.html
If I used a file without an extension, I would use a URL like this:
http://yoursite.com/randompage
I know that this can't be the preferred method because it leaves the file without a way to be identified, but is there anything that would stop the site from working properly?
What you want to do is done with url-rewriting .
Basically, you will add a rewrite rule to your web server, so when he receives a request for url yoursite.com/randompage he will change it to yoursite.com/randompage.html. You will find a lot of examples on the web if you google for "mod_rewrite examples" or "url rewrite examples".
Document types are sent from the webserver in http headers, so it is perfectly possible to do what you are asking.
For instance when using Apache, to tell it that any file with no extension is html, in the 'conf' file you can say:
DefaultType text/html
More details at Apache The Definitive Guide
This point to concrete document (randompage.html in this case):
http://yoursite.com/randompage.html
This point to default document in directory:
http://yoursite.com/randompage/
Default document is set in you webserver. This could be for example: index.html, default.html, index.php, default.apsx,...

Resources