Recently, I came across the following situation while working with : I needed to handle an http post that would carry not one but N > 1 .

If I were to do this back in the days where I didn’t know about such a thing called “Mule”, I would have needed to:

  • Handle a http stream
  • Identify all the parts in the message
  • Read each file
  • Clean up

Of course there’re libraries and frameworks that can help you with this, but all of them still require some level of understanding of the multipart request beneath.

And then came Mule into my life, and this task became as simple as navigating the properties of a MuleMessage interface. Let’s explain a little bit….

As you probably know, what the MuleESB does is to carry packages of information (messages) from one system to another, allowing for software integration without the need of the involved systems to know about each other, their transport, protocols or any API changes. Each of those packages of information are represented by a MuleMessage. This object acts as a facade to access of a great deal of information about the message including, headers, payload and attachments, but today I want to focus on a property called inbound attachments.

When the message is coming through an http post that sends N >= 1 files through a Multipart Request, each of those files will be automatically read by Mule and stored in the message under the inboundAttachments property.

Pretty cool uh? No more worrying about Multipart, streams or anything like it, you just need to access this property as a Key-Value pair where the key is the filename and the value is the content itself. MuleMessage also provides a  getInboundAttachmentNames() that returns all the keys.

So, let’s see a couple of examples. Suppose you want to retrieve the content of an expected file named foo.txt:

Now, let’s suppose that you want a flow that receives N >= 1 amount of attachments and logs all of them:

That was easy wasn’t it? If you’re thinking that this is pretty simple stuff compared to the average post in this blog, then mission accomplished! We have succeeded in demonstrate how easy it is to do every day chores using Mule. Please let us know if this post makes your life easier, that would really make us happy.

No related posts.

Interested in 3 days of knowledge sharing, hands-on labs, industry focused sessions, and plenty of networking? Register for the premier integration event, CONNECT London »

5 Responses to “Handling File Attachments: handling multipart requests in Mule”

Scott Banwart's Blog » Distributed Weekly 191 January 25th, 2013, 1:18 am

[...] Handling File Attachments: handling multipart requests in Mule [...]

Yogesh October 7th, 2013, 5:03 am

I am new to the mule. I am facing the same problem. help me

Few details :

Method in the connector :

@Processor
public Status addcontent(String inputXml, File file )
throws IOException {
// do something
}

Entry in the connector.xml.sample

Entry in the .mflow file

I have a html file will will submit the data :

Test

function submitUploadForm(){
var inputXml = ‘
‘;
document.getElementById(“addcontentXml”).value = inputXml ;
document.getElementById(“addcontentSubmit”).submit();
}

Upload Form

When i hit the request i get the following exception on the mule server which i run in mule studio.

INFO 2013-10-04 19:29:39,810 [[FusionliveTest].httpConnector.receiver.03] org.mule.module.xml.transformer.jaxb.JAXBContextResolver: No common Object of type ‘class javax.xml.bind.JAXBContext’ configured, creating a local one for: SimpleDataType{type=com.mclaren.fusionlive.common.Status, mimeType=’application/x-www-form-urlencoded’}, SimpleDataType{type=org.mule.api.transport.OutputHandler, mimeType=’*/*’}
ERROR 2013-10-04 19:30:45,175 [[FusionliveTest].httpConnector.receiver.03] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Could not find a transformer to transform “SimpleDataType{type=java.lang.String, mimeType=’*/*’}” to “SimpleDataType{type=java.io.File, mimeType=’*/*’}”.
Code : MULE_ERROR-236
——————————————————————————–
Exception stack is:
1. Could not find a transformer to transform “SimpleDataType{type=java.lang.String, mimeType=’*/*’}” to “SimpleDataType{type=java.io.File, mimeType=’*/*’}”. (org.mule.api.transformer.TransformerException)
org.mule.registry.MuleRegistryHelper:252 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
——————————————————————————–
Root Exception stack trace:
org.mule.api.transformer.TransformerException: Could not find a transformer to transform “SimpleDataType{type=java.lang.String, mimeType=’*/*’}” to “SimpleDataType{type=java.io.File, mimeType=’*/*’}”.
at org.mule.registry.MuleRegistryHelper.lookupTransformer(MuleRegistryHelper.java:252)
at org.mclaren.fusionlive.processors.AbstractExpressionEvaluator.transform(AbstractExpressionEvaluator.java:329)
at org.mclaren.fusionlive.processors.AbstractExpressionEvaluator.evaluateAndTransform(AbstractExpressionEvaluator.java:202)
+ 3 more (set debug level logging or ‘-Dmule.verbose.exceptions=true’ for everything)
********************************************************************************

Please put your suggestions if i am missing things..

Thanks in advance.

John June 16th, 2014, 8:12 am

I’ve been trying to use with this sample:

and

But message.inboundAttachments is always empty, I even try with soapui and doesn’t work.

Mariano Gonzalez June 17th, 2014, 10:25 am

Hello John,

Which version of Mule are you using? We spotted a bug around this in the 3.4.x series. Have you tried with 3.5.0?

Thanks

Leave a Comment