Contact Us 1-800-596-4880

@InboundAttachments Annotation

Mule runtime engine version 3.8 reached its End of Life on November 16, 2021. For more information, contact your Customer Success Manager to determine how to migrate to the latest Mule version.

This annotation controls how the current message inbound attachments are passed into a method. The annotation supports, Map, List, single attachment, wildcards and optional entries. It can be used on component entry points and @Transformer methods.

Accessing Message Attachments

A message in Mule can optionally have attachments. THe modules in Mule that support attachments are HTTP, Email, Axis and VM. If you need to access the attachments, add a parameter to the component method signature or transformer method signature annotated with the @InboundAttachments annotation -

The '*' indicates that all headers should be returned.

@Transformer
public Person xmlToOrder(@Payload Document data,
             @InboundAttachments("*") Map<String, DataHandler> headers)

Note that the Map type will be Map<String, DataHandler>. The attahcment information can be queried from the javax.activation.DataHandler instance.

Wildcard expressions can be used to match a subset of attachments too -

@Transformer
public Person xmlToOrder(@Payload Document data,
                  @InboundAttachments("*.pdf") Map<String, DataHandler> headers)

Alternatively, you can specify a single header name and just return the value of that attachment:

public class OrderService {
   public OrderConfirmation processOrder(@Payload InputStream data,
             @InboundAttachments("shipping-info.doc") DataHandler attachment) {
         //do stuff
    }
}

To receive a subset of specific attachments, you can list them as comma-separated values:

public class OrderService {
   public OrderConfirmation processOrder(@Payload InputStream data,
         @InboundAttachments("shipping-info.doc, invoice.pdf") Map<String, DataHandler> attachments) {
         //do stuff
    }
}

By default an error will be thrown if a listed attachment is not on the response. To avoid this error, mark the header as optional with '?'. For example, shipping-info.doc is optional in the following code:

public class OrderService {
   public OrderConfirmation processOrder(@Payload InputStream data,
           @InboundAttachments("shipping-info.doc?, invoice.pdf") Map headers) {
         //do stuff
    }
}

If the return type for the @InboundAttachments param is a java.util.List, just the values will be returned.

public class OrderService {
   public OrderConfirmation processOrder(@Payload InputStream data,
        @InboundAttachments("shipping-info.doc?, invoice.pdf") List<DataHandler> attachments) {
         //do stuff
    }
}