javasoapapache-synapsewso2-esb

Add attachment to SOAP message in Custom Mediator, WSO2 ESB


I am trying to implement a custom mediator in WSO2 ESB and what I am trying to achieve is that the mediator must take the file path as input and then add it to the SOAP message as an attachment.

The mediator code I have written so far, gets the attachment path and prints the SOAP message. Now I have skimmed through the documentation of MessageContext interface and I can see that we can add/remove elements to SOAP message, etc but I can not figure out how to add attachment in the SOAP message. Any ideas?

import javax.activation.FileDataSource;
import org.apache.axiom.soap.SOAPBody;
import org.apache.synapse.MessageContext; 
import org.apache.synapse.mediators.AbstractMediator;

public class SoapModifier extends AbstractMediator { 

private String AttachmentFilePath;  

public boolean mediate(MessageContext context) { 
    context.setDoingSWA(true);
    FileDataSource fileDataSource = new FileDataSource(AttachmentFilePath);
    SOAPBody soapBody = context.getEnvelope().getBody();
    System.out.println("Message Being Processed : " + context.toString());
    return true;
}

public String getAttachmentFilePath(){
    return AttachmentFilePath;
}

public void setAttachmentFilePath(String path){
    AttachmentFilePath = path;
}
}

Solution

  • This might help Attachments API in Apache Axis2