Setting some header with current file name with Camel - apache-camel

I am trying to set a header, "CamelAwsS3Key", with the current file name, before uploading the file to Amazon S3, but I am not able to get the current file name, here is the Spring snippet:
...
<to uri="file:D:/camel_out?fileName=foo_$simple{date:now:yyyyMMddHHmmss}.xml" />
<setHeader headerName="CamelAwsS3Key">
<simple>${file:onlyname}</simple>
</setHeader>
<to uri="aws-s3://bucket?accessKey=xxx&secretKey=yyy" />
Here is the message history (I removed useless columns):
...
[to2 ] [file:D:/camel_out?fileName=foo_$simple{date:now:yyyyMMddHHmmss}.xml]
[setHeader1] [setHeader[CamelAwsS3Key] ]
[to3 ] [aws-s3://bucket?accessKey=xxx&secretKey=xxxxxx ]
But the header is not set... From the log:
Exchange[
...
Headers{... CamelAwsS3Key=null, CamelFileName=null, CamelFileNameProduced=D:\camel_out\foo_20150622124308.xml ...}
...
]
It works if I use a constant instead (the file is uploaded), like:
<setHeader headerName="CamelAwsS3Key">
<constant>test</constant>
</setHeader>
How to set a header with the current file name?
Thanks!

The simple <simple>${file:onlyname}</simple> is only when you consume from a file endpoint, eg from file.
To file producer (eg to file) only has one header which is file name produced:
http://camel.apache.org/maven/current/camel-core/apidocs/src-html/org/apache/camel/Exchange.html#line.125
which is also what you can see from your logs. You will need to write some code to extract only the name part of file named produced if you only want that, as the name currently includes paths. Though we could introduce a FILE_ONLYNAME_PRODUCED header in the camel file producer.
You are welcome to log such ENH request at: http://camel.apache.org/support

Related

Logic App Function to remove file extension

I have a logic app in Azure. The trigger is to check when an email arrives into a specific folder in a mailbox. An email may contain 1 or more attachments.
Once triggered, I have an HTTP request that gets sent to a SOAP service. The idea is that I want to check if the filename exists at the SOAP Service.
Its all working perfectly, with the exception, when I reference the filename from the trigger, it includes the file extension. I need to somehow ignore the ".PDF" part of the filename
Below is the XML I post to the SOAP service. Lets say the filename is 12345.pdf, then I need /UniversalEvent/Event/ContextCollection/Context/Value to = "12345" and not "12345.pdf":
<UniversalEvent xmlns="http://www.cargowise.com/Schemas/Universal/2011/11" version="1.1">
<Event>
<DataContext>
<DataTargetCollection>
<DataTarget>
<Type>ForwardingShipment</Type>
</DataTarget>
</DataTargetCollection>
</DataContext>
<EventTime>#{utcNow()}</EventTime>
<EventType>Z77</EventType>
<EventReference>Requesting Shipment ID</EventReference>
<IsEstimate>false</IsEstimate>
<ContextCollection>
<Context>
<Type>HAWBNumber</Type>
<Value>#{items('For_each')?['name']}</Value>
</Context>
</ContextCollection>
</Event>
</UniversalEvent>
do you have any suggestions on what function to use to achieve this?
You can change the expression #{items('For_each')?['name']} in your xml to:
#{substring(items('For_each')?['name'], 0, indexOf(items('For_each')?['name'], '.'))}

Setting timeout for pollenrich using configuration property

I am using pollenrich in my code to get the message from the queue:
<pollEnrich uri="activemq:queueName" timeout="5000"/>
Now, I want to read the timeout value from config file declared in etc folder.
Something like this:
<pollEnrich uri="file:inbox?fileName=data.txt" timeout="{{readTimeout}}"/>
While doing so, I am getting the following error:
org.xml.sax.SAXParseException : cvc-datatype-valid.1.2.1: '{{readTimeout}}' is not a valid value for 'integer'
This error only comes for pollenrich and nowhere else in my code. I am able to use other properties from config file in the same camel-context.
e.g.,
<from uri="timer://TestTimer?period={{timer.interval}}&delay={{startupDelay}}/>
See the documentation at: http://camel.apache.org/using-propertyplaceholder.html at the section titled Using property placeholders for any kind of attribute in the XML DSL

Camel pollEnrich and xml 'prettyPrint'

I am attempting to use Camel's pollEnrich feature, but it is not behaving as I would like... I'm not saying it's broken, but wondering if there is a way to get the behavior I desire. That is, I have an XML (blueprint) defined route that goes something like this:
<route>
<from uri="direct:a" />
<pollEnrich uri="http:www.somewebsite.com?format=application/xml" />
<to uri="log:com.acme?level=WARN&showStreams=true" />
</route>
Now, the response normally comes back just fine (e.g., in a web browser). The problem seems to be that it is not just on one line, and for some reason Camel reads each line, sequentially into the same buffer, starting at character zero... so what we end up with is one messy line in the output from the pollEnrich. That is, the to uri="log... line prints messages like:
2015-05-26 13:55:26,379 | WARN | a.distr.topic.B] | contentEnrich |
? ? | 142 - org.apache.camel.camel-core - 2.12.0.redhat-610379 |
Exchange[ExchangePattern: InOnly, BodyType:
org.apache.camel.converter.stream.InputStreamCache, Body:
<?xml versi</ElementStatus> ]pe></Status>nd>gin>ys for this element.</Reason>>ame>
(last line vertically offset for emphasis)
I cannot seem to find a way to tell Camel that the result will be in 'prettPrint' format... Anyone know how? The documentation seems to suggest that this option does not exist--in which case, I'd consider this to be a bug... though I suppose a person could argue that a custom aggregation strategy should be used (and I'd disagree with that person, citing the simplicity of this case) :)
UPDATE#1: even using org.apache.camel.processor.aggregate.UseLatestAggregationStrategy produces the same effect. (i.e., usage as below)
<bean id="latestStrat"
class="org.apache.camel.processor.aggregate.UseLatestAggregationStrategy" />
<route>
<from uri="direct:a" />
<pollEnrich uri="http:www.somewebsite.com?format=application/xml" strategyRef="latestStrat" />
<to uri="log:com.acme?level=WARN&showStreams=true" />
</route>
...going to cross fingers and try org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy, but am guessing there is a configuration limitation with Camel always treating EOL characters as message delimiters.
UPDATE #2 - additional information:
The REST(GET) response received (tested with wget) has blank lines and null fields--but no carriage returns (^M). I've tried both the http and http4 components--same result. There is a leading <?xml version="1.0" encoding="UTF-8"?>, but no namespace/style info. I also just noticed that tab characters have been used for the pretty-ish indents. In sum, the response looks like:
<?xml version="1.0" encoding="UTF-8"?><ElementStatus>
<Flag>false</Flag>
<CODE>XYZ</CODE>
<Locale>Western</Locale>
...
(again, where the whitespace indenting has been done with tabs--AND the blank lines have a few tabs too)
...so the "answer" is that this is an apparent limitation of (or bug within) the log component's "showStreams" logic. I implemented Processor in a <bean>, routed the Exchange output from my pollEnrich to that <bean>, and logged the contents instead, and that matches exactly the output from wget.
FYI: this is camel-paxlogging (2.12.0.redhat-610379) - not sure what underlying version of camel that corresponds to, as I don't seem to have a jboss-parent-2.12.0 pom in my maven repo--which is strange, since I have other jboss-parent poms--and the red hat documentation doesn't seem to get into version composition.
FYI#2: and on a related note, when I use GroupedExchangeAggregationStrategy it does produce a List<Exchange>, BUT it behaves effectively the same as UseLatestAggregationStrategy -- i.e., 'grouped' produces a one-item List<Exchange> that only has the pollEnrich result, where 'latest' produces a standalone Exchange object that has only the pollEnrich result. Seems like an error in either GroupedExchangeAggregationStrategy or pollEnrich ... but this will likely be the topic of my next Stack-post.

Mule - how to get the name of the file created in an outbound endpoint

I have a Mule application that is writing a file in an outbound endpoint, with the config below:
<file:outbound-endpoint path="${Outbound}" outputPattern="outputFile_#[function:datestamp:yyyyMMddHHmmss].csv" doc:name="Output File"/>
Following this point in the flow I need to display a log message along the lines of "Successfully created file {filename}".
The issue I'm having is that I can't find a way of displaying the name of the file I have just created. I could put: Successfully created file outputFile_#[function:datestamp:yyyyMMddHHmmss].csv, but there is a chance that the datestamp may differ by one second.
Is there a way in Mule that I can display the name of the file I've just written?
UPDATE
Following the response from #til_b, I've achieved this using the following:
<set-variable value="outputFile_#[function:datestamp:yyyyMMddHHmmss].csv" variableName="Filename" doc:name="Variable"/>
<file:outbound-endpoint path="${Outbound}" outputPattern="#[variable:Filename]" doc:name="Output File"/>
<logger level="INFO" message="Successfully created file #[variable:Filename]" doc:name="Logger" />
I dont know about mule, but when i encounter such a problem while programming i store the generated filename in a variable, and then use that variable to actually create the file and display the message.
In Pseudocode:
var filename = #yyyymmddhhMMss.csv
create_file(filename)
log_message(filename + ' successfully created')

camel ftp route and file type converters

I am struggling with type conversion in my camel route for handling ftp files. My route looks like this (in Spring DSL):
<route id="processIncomingFtpFile" errorHandlerRef="parkingCitErrHandler">
<from uri="{{ftp.parkingcit.input.path}}"/>
<bean ref="ftpZipFileHandler"/>
<unmarshal ref="bindyCsvFormat"/>
<bean ref="parkingTicketsHandler"/>
<split>
<simple>${body}</simple>
<marshal ref="jaxbFormatter"/>
<convertBodyTo type="java.io.File"/>
<to uri="{{ftp.parkingcit.output.path}}"/>
</split>
</route>
And my handler signature looks like this:
public File handleIncomingFile(File incomingfile)...
However, this yields the following type conversion problem:
org.apache.camel.InvalidPayloadException: No body available of type: java.io.File but has value: RemoteFile[test.zip] of type: org.apache.camel.component.file.remote.RemoteFile on: test.zip. Caused by: No type converter available to convert from type: org.apache.camel.component.file.remote.RemoteFile to the required type: java.io.File with value RemoteFile[test.zip]. Exchange[test.zip]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.apache.camel.component.file.remote.RemoteFile to the required type: java.io.File with value RemoteFile[test.zip]]
My question is: should I be able to handle my ftp file in-memory, without explicitly telling camel to write it to disk, with type converters doing the work automagically behind the scenes for me? Or is what I am trying to do senseless, given that my handler wants a java.io.File as its input parameter, i.e. I must write the data to disk for this to work?
java.io.File is for file systems, and not for FTP files.
You would need to change the type in your bean method signature to either Camel's GenericFile or the actual type which is from the commons-net FTP client library that Camel uses.
hmm, I would recommend to work with a local file (I always do that with the ftp endpoint). There is an option in the ftp endpoint called localWorkDirectory that lets you define a directory to download the file ( typically, /tmp ) before further processing. The idea is to avoid any error due to network issue or being disconnected during process
Can you try it It's easy enough (just add &localWorkDirectory =mydir in the uri ) and it will dl the file for you
see https://camel.apache.org/ftp2.html.
Just make sure you have write right on the directory, of course

Resources