Reading Attachments in Mule

#ICYMI: Let’s say there is a requirement to handle a multipart request. As the request is received by Mule, the attachments are saved in an object called InboundAttchments, which is part of Mule Message.

Configuration

* Create a Mule flow with an HTTP listener.
* First, to read the total attachments received. Drop a logger after HTTP and write #[message.inboundAttachments.size()] in the logger message.
* To read the contents of attachment(s). Add a For Each. It will loop over the inbound Attachments object and read them one by one. Give #[message.inboundAttachments] against collection in properties tab.
* Inside the For Each scope, place a logger to read the contents using #[message.payloadAs(java.lang.String)]. Reading the payload using  #[payload] will return an object.

Getting Attachment Metadata

* Attachment Name To get the name of attachment, we can use the variable key, which is auto-generated by Mule inside the For Each scope#[flowVars.key] or #[payload.dataSource.part.fileName]
* Attachment size (in Bytes) To get the attachment size, we have to read the payload and use the getBytes method #[message.payloadAs(java.lang.String).getBytes().length] or #[payload.dataSource.part.size]
* Content Type To get the content type of the attachment: #[payload.dataSource.part.contentType]

Flow XML

Thanks, and let me know your thoughts in the comments section! https://goo.gl/K1vZ7v #DataIntegration #ML

Mule 3.9: Separate Mule Info and Error Logs via Log4J2 Configuration

#ICYDK: In this article, I will explain how to separate the Mule application info and error logs into a separate log file. For instance, we have a Mule application that runs every 10 seconds to read an input file located on the local path /appdata/temp/input/persons.csv.

LOG4J2.XML Configuration

To separate the Mule INFO and ERROR logs, we need to define 2 separate RollingRandomAccessFile tags under Appenders in our log4j2.xml file located in src/main/resources. One for INFO logs and another for ERROR logs. We can set the filename of the log file as well as the log category (INFO, ERROR, WARN, DEBUG) that the file should contain which can be configured using ThresholdFilter Level. Lastly, we need to add the two RollingRandonAccessFile configuration to the loggers tag under AsyncRoot. https://goo.gl/wg6KbE #DataIntegration #ML