1 16 package sample.mtom.client; 17 18 import org.apache.axis2.Constants; 19 import org.apache.axis2.addressing.AddressingConstants; 20 import org.apache.axis2.addressing.EndpointReference; 21 import org.apache.axis2.attachments.ImageDataSource; 22 import org.apache.axis2.attachments.JDK13IO; 23 import org.apache.axis2.clientapi.Call; 24 import org.apache.axis2.om.*; 25 import org.apache.axis2.om.impl.llom.OMTextImpl; 26 27 import javax.activation.DataHandler ; 28 import javax.xml.namespace.QName ; 29 import java.awt.*; 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 33 34 public class MTOMClientModel { 35 private File inputFile = null; 36 37 private EndpointReference targetEPR = new EndpointReference(AddressingConstants.WSA_TO, 38 "http://127.0.0.1:8080/axis2/services/MyService"); 39 40 private QName operationName = new QName ("mtomSample"); 41 42 43 public MTOMClientModel() { 44 45 } 46 47 private OMElement createEnvelope(String fileName) throws Exception { 48 49 DataHandler expectedDH; 50 OMFactory fac = OMAbstractFactory.getOMFactory(); 51 OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my"); 52 53 OMElement data = fac.createOMElement("mtomSample", omNs); 54 OMElement image = fac.createOMElement("image", omNs); 55 Image expectedImage; 56 expectedImage = new JDK13IO() 57 .loadImage(new FileInputStream (inputFile)); 58 59 ImageDataSource dataSource = new ImageDataSource("test.jpg", 60 expectedImage); 61 expectedDH = new DataHandler (dataSource); 62 OMText textData = fac.createText(expectedDH, true); 63 image.addChild(textData); 64 65 OMElement imageName = fac.createOMElement("fileName", omNs); 66 if (fileName != null) { 67 imageName.setText(fileName); 68 } 69 data.addChild(image); 70 data.addChild(imageName); 71 72 return data; 73 74 } 75 76 public OMElement testEchoXMLSync(String fileName) throws Exception { 77 78 OMElement payload = createEnvelope(fileName); 79 80 Call call = new Call(); 81 call.setTo(targetEPR); 82 call.set(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE); 84 call.setTransportInfo(Constants.TRANSPORT_HTTP, 85 Constants.TRANSPORT_HTTP, false); 86 OMElement result = (OMElement) call.invokeBlocking(operationName 87 .getLocalPart(), payload); 88 89 return result; 90 } 91 92 93 public void setTargetEPR(String targetEPR) { 94 this.targetEPR = new EndpointReference(AddressingConstants.WSA_TO, 95 targetEPR); 96 97 } 98 99 100 public void setInputFile(File inputFile) { 101 this.inputFile = inputFile; 102 } 103 } 104 | Popular Tags |