1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package samples.services; 20 21 import org.apache.axiom.om.OMText; 22 import org.apache.axiom.om.OMElement; 23 import org.apache.axiom.om.OMFactory; 24 import org.apache.axiom.om.OMNamespace; 25 import org.apache.axiom.attachments.Attachments; 26 import org.apache.axis2.context.MessageContext; 27 import org.apache.axis2.wsdl.WSDLConstants; 28 29 import javax.activation.DataHandler; 30 import javax.activation.FileDataSource; 31 import javax.xml.namespace.QName; 32 import java.io; 33 34 public class MTOMSwASampleService { 35 36 private static final int BUFFER = 2048; 37 38 public OMElement uploadFileUsingMTOM(OMElement request) throws Exception { 39 40 OMText binaryNode = (OMText) request. 41 getFirstChildWithName(new QName("http://services.samples/xsd", "request")). 42 getFirstChildWithName(new QName("http://services.samples/xsd", "image")). 43 getFirstOMChild(); 44 DataHandler dataHandler = (DataHandler) binaryNode.getDataHandler(); 45 InputStream is = dataHandler.getInputStream(); 46 47 File tempFile = File.createTempFile("mtom-", ".gif"); 48 FileOutputStream fos = new FileOutputStream(tempFile); 49 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); 50 51 byte data[] = new byte[BUFFER]; 52 int count; 53 while ((count = is.read(data, 0, BUFFER)) != -1) { 54 dest.write(data, 0, count); 55 } 56 57 dest.flush(); 58 dest.close(); 59 System.out.println("Wrote MTOM content to temp file : " + tempFile.getAbsolutePath()); 60 61 OMFactory factory = request.getOMFactory(); 62 OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); 63 OMElement payload = factory.createOMElement("uploadFileUsingMTOMResponse", ns); 64 OMElement response = factory.createOMElement("response", ns); 65 OMElement image = factory.createOMElement("image", ns); 66 67 FileDataSource fileDataSource = new FileDataSource(tempFile); 68 dataHandler = new DataHandler(fileDataSource); 69 OMText textData = factory.createOMText(dataHandler, true); 70 image.addChild(textData); 71 response.addChild(image); 72 payload.addChild(response); 73 74 MessageContext outMsgCtx = MessageContext.getCurrentMessageContext() 75 .getOperationContext().getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE); 76 outMsgCtx.setProperty( 77 org.apache.axis2.Constants.Configuration.ENABLE_MTOM, 78 org.apache.axis2.Constants.VALUE_TRUE); 79 80 return payload; 81 } 82 83 public OMElement uploadFileUsingSwA(OMElement request) throws Exception { 84 85 String imageContentId = request. 86 getFirstChildWithName(new QName("http://services.samples/xsd", "request")). 87 getFirstChildWithName(new QName("http://services.samples/xsd", "imageId")). 88 getText(); 89 90 MessageContext msgCtx = MessageContext.getCurrentMessageContext(); 91 Attachments attachment = msgCtx.getAttachmentMap(); 92 DataHandler dataHandler = attachment.getDataHandler(imageContentId); 93 File tempFile = File.createTempFile("swa-", ".gif"); 94 FileOutputStream fos = new FileOutputStream(tempFile); 95 dataHandler.writeTo(fos); 96 fos.flush(); 97 fos.close(); 98 System.out.println("Wrote SwA attachment to temp file : " + tempFile.getAbsolutePath()); 99 100 MessageContext outMsgCtx = msgCtx.getOperationContext(). 101 getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE); 102 outMsgCtx.setProperty( 103 org.apache.axis2.Constants.Configuration.ENABLE_SWA, 104 org.apache.axis2.Constants.VALUE_TRUE); 105 106 OMFactory factory = request.getOMFactory(); 107 OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); 108 OMElement payload = factory.createOMElement("uploadFileUsingSwAResponse", ns); 109 OMElement response = factory.createOMElement("response", ns); 110 OMElement imageId = factory.createOMElement("imageId", ns); 111 112 FileDataSource fileDataSource = new FileDataSource(tempFile); 113 dataHandler = new DataHandler(fileDataSource); 114 imageContentId = outMsgCtx.addAttachment(dataHandler); 115 imageId.setText(imageContentId); 116 response.addChild(imageId); 117 payload.addChild(response); 118 119 return payload; 120 } 121 122 public void oneWayUploadUsingMTOM(OMElement element) throws Exception { 123 124 OMText binaryNode = (OMText) element.getFirstOMChild(); 125 DataHandler dataHandler = (DataHandler) binaryNode.getDataHandler(); 126 InputStream is = dataHandler.getInputStream(); 127 128 File tempFile = File.createTempFile("mtom-", ".gif"); 129 FileOutputStream fos = new FileOutputStream(tempFile); 130 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); 131 132 byte data[] = new byte[BUFFER]; 133 int count; 134 while ((count = is.read(data, 0, BUFFER)) != -1) { 135 dest.write(data, 0, count); 136 } 137 138 dest.flush(); 139 dest.close(); 140 System.out.println("Wrote to file : " + tempFile.getAbsolutePath()); 141 } 142 }