0

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;
}
}
Community
  • 1
  • 1
Amna Tariq
  • 135
  • 1
  • 9

1 Answers1

0

This might help Attachments API in Apache Axis2

Nadeeshaan
  • 356
  • 5
  • 15
  • Thanks! The article suggests using the Message Context interface of Apache Axis 2 but the custom mediator gives in the Message Context object of Apache Synapse. So I solved it by casting the message context to Apache Axis 2. – Amna Tariq Aug 03 '17 at 12:39
  • So the code addition will be like that: `DataHandler dataHandler = new DataHandler(fileDataSource); org.apache.axis2.context.MessageContext mc = ((Axis2MessageContext) context).getAxis2MessageContext(); String contentid = mc.addAttachment(dataHandler);` – Amna Tariq Aug 03 '17 at 12:47