Search code examples
xmlmuleesbmultipartform-dataendpoint

Forwarding a multipart request trough mule esb


i am using a http outbound endpoint to call a separate service that needs to process a file that is sent to mule in a multipart request. Here is an example

<http:outbound-endpoint connector-ref="serviceConnector"
                address="http://${serviceHost}:${servicePort}/upload"
                method="POST"
                responseTimeout="${endpointTimeout}"
                mimeType="multipart/form-data">

The problem i am having is that when this service is called i am getting a FileUploadException saying that the request was reject because no multipart boundary has been found.

I have been experimenting with this problem and looking at different questions but none of them seems to resolve the issue.

Mule ESB and "multipart/form-data"

Mule http:outbound-endpoint + multipart/form-data

https://www.mulesoft.org/jira/browse/MULE-6917

I have also tried altering the connector like explained in this question: Send file to Mule inbound-endpoint but no luck.

Any ideas?

Thanks


Solution

  • Finally this problem was solved by creating a custom processor that was parsing the incoming request, splitting it into parts and manually reading the bytes of the parts input stream and setting the read bytes as a data source that is attached to the message. The code:

     InputStream in = message.getPayload( InputStream.class );
     MultiPartInputStream multiIn = new MultiPartInputStream( in, contentType, null );
    try
        {
            Collection<Part> parts = multiIn.getParts();
    
            for ( Part part : parts )
            {
               if ( part instanceof MultiPart )
                    {
                        is = part.getInputStream();
                        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                        byte[] isByteArray = new byte[1024];
                        int numberRead;
                        while ( (numberRead = is.read( isByteArray ) ) != -1 ){
                            buffer.write(isByteArray, 0, numberRead);
                        }
                        byte[] bytesRead = buffer.toByteArray();
                        DataHandler attachment = new DataHandler(new ByteArrayDataSource( bytesRead, "application/octet-stream")); 
                        message.addOutboundAttachment( part.getName(), attachment );
                    }
             }
       }...handle exceptions etc...
    

    Hopefully this helps someone!