import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axiom.om.OMText; import org.apache.axiom.soap.SOAP12Constants; import org.apache.axiom.soap.SOAP11Constants; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.xml.namespace.QName; import java.io.File; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import java.io.StringReader; public class MTOMClient { public static void main(String[] args) throws XMLStreamException { PrivacyServiceClient client = new PrivacyServiceClient(); try { OMElement result = client.testEchoXMLSync("test1.xml"); } catch (Exception e) { e.printStackTrace(); } } private OMElement createEnvelope(String fileName) throws Exception { DataHandler expectedDH; OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x"); OMElement data = fac.createOMElement("Data", omNs); File dataFile = new File(fileName); FileDataSource dataSource = new FileDataSource(dataFile); expectedDH = new DataHandler(dataSource); OMText textData = fac.createOMText(expectedDH, true); data.addChild(textData); return data; } public OMElement testEchoXMLSync(String fileName) throws Exception { EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MTOMServiceWS"); // QName operationName = new QName("MTOMService"); OMElement payload = createEnvelope(fileName); Options options = new Options(); options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI); // options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); options.setTo(targetEPR); // enabling MTOM in the client side options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); return sender.sendReceive(payload); } }