1 16 17 package org.apache.axis2.mtom; 18 19 22 import java.awt.Image ; 23 import java.io.FileOutputStream ; 24 import java.io.InputStream ; 25 26 import javax.activation.DataHandler ; 27 import javax.xml.namespace.QName ; 28 29 import junit.framework.TestCase; 30 31 import org.apache.axis2.Constants; 32 import org.apache.axis2.addressing.AddressingConstants; 33 import org.apache.axis2.addressing.EndpointReference; 34 import org.apache.axis2.attachments.ImageDataSource; 35 import org.apache.axis2.attachments.JDK13IO; 36 import org.apache.axis2.context.MessageContext; 37 import org.apache.axis2.context.ServiceContext; 38 import org.apache.axis2.description.ServiceDescription; 39 import org.apache.axis2.engine.AxisConfiguration; 40 import org.apache.axis2.engine.Echo; 41 import org.apache.axis2.integration.UtilServer; 42 import org.apache.axis2.om.OMAbstractFactory; 43 import org.apache.axis2.om.OMElement; 44 import org.apache.axis2.om.OMFactory; 45 import org.apache.axis2.om.OMNamespace; 46 import org.apache.axis2.om.OMText; 47 import org.apache.axis2.om.impl.llom.OMTextImpl; 48 import org.apache.axis2.soap.SOAPFactory; 49 import org.apache.axis2.util.Utils; 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 public class EchoRawMTOMTest extends TestCase { 54 private EndpointReference targetEPR = new EndpointReference( 55 AddressingConstants.WSA_TO, "http://127.0.0.1:" 56 + (UtilServer.TESTING_PORT) 57 + "/axis/services/EchoXMLService/echoOMElement"); 58 59 private Log log = LogFactory.getLog(getClass()); 60 61 private QName serviceName = new QName ("EchoXMLService"); 62 63 private QName operationName = new QName ("echoOMElement"); 64 65 private QName transportName = new QName ("http://localhost/my", 66 "NullTransport"); 67 68 private String imageInFileName = "img/test.jpg"; 69 70 private String imageOutFileName = "mtom/img/testOut.jpg"; 71 72 private AxisConfiguration engineRegistry; 73 74 private MessageContext mc; 75 76 private ServiceContext serviceContext; 77 78 private ServiceDescription service; 79 80 private boolean finish = false; 81 82 public EchoRawMTOMTest() { 83 super(EchoRawMTOMTest.class.getName()); 84 } 85 86 public EchoRawMTOMTest(String testName) { 87 super(testName); 88 } 89 90 protected void setUp() throws Exception { 91 UtilServer.start(Constants.TESTING_PATH + "MTOM-enabledRepository"); 92 service = Utils.createSimpleService(serviceName, Echo.class.getName(), 93 operationName); 94 UtilServer.deployService(service); 95 serviceContext = UtilServer.getConfigurationContext() 96 .createServiceContext(service.getName()); 97 } 98 99 protected void tearDown() throws Exception { 100 UtilServer.unDeployService(serviceName); 101 UtilServer.stop(); 102 } 103 104 private OMElement createEnvelope() throws Exception { 105 106 DataHandler expectedDH; 107 OMFactory fac = OMAbstractFactory.getOMFactory(); 108 OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my"); 109 OMElement rpcWrapEle = fac.createOMElement("echoOMElement", omNs); 110 OMElement data = fac.createOMElement("data", omNs); 111 Image expectedImage; 112 expectedImage = new JDK13IO() 113 .loadImage(getResourceAsStream("org/apache/axis2/mtom/test.jpg")); 114 115 ImageDataSource dataSource = new ImageDataSource("test.jpg", 116 expectedImage); 117 expectedDH = new DataHandler (dataSource); 118 OMTextImpl textData = new OMTextImpl(expectedDH, true); 119 data.addChild(textData); 120 rpcWrapEle.addChild(data); 123 return rpcWrapEle; 124 125 } 126 127 public void testEchoXMLSync() throws Exception { 128 SOAPFactory fac = OMAbstractFactory.getSOAP11Factory(); 129 130 OMElement payload = createEnvelope(); 131 132 org.apache.axis2.clientapi.Call call = new org.apache.axis2.clientapi.Call(); 133 call.setTo(targetEPR); 134 call.set(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE); 135 call.setTransportInfo(Constants.TRANSPORT_HTTP, 136 Constants.TRANSPORT_HTTP, false); 137 138 OMElement result = (OMElement) call.invokeBlocking(operationName 139 .getLocalPart(), payload); 140 OMElement ele = (OMElement) result.getFirstChild(); 143 OMText binaryNode = (OMText) ele.getFirstChild(); 144 DataHandler actualDH; 145 actualDH = binaryNode.getDataHandler(); 146 Image actualObject = new JDK13IO().loadImage(actualDH.getDataSource() 147 .getInputStream()); 148 FileOutputStream imageOutStream = new FileOutputStream ("testout.jpg"); 149 new JDK13IO().saveImage("image/jpeg", actualObject, imageOutStream); 150 151 } 152 153 private InputStream getResourceAsStream(String path) { 154 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 155 return cl.getResourceAsStream(path); 156 } 157 } | Popular Tags |