001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package samples.services;
020:
021: import org.apache.axiom.om.OMText;
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.om.OMFactory;
024: import org.apache.axiom.om.OMNamespace;
025: import org.apache.axiom.attachments.Attachments;
026: import org.apache.axis2.context.MessageContext;
027: import org.apache.axis2.wsdl.WSDLConstants;
028:
029: import javax.activation.DataHandler;
030: import javax.activation.FileDataSource;
031: import javax.xml.namespace.QName;
032: import java.io.*;
033:
034: public class MTOMSwASampleService {
035:
036: private static final int BUFFER = 2048;
037:
038: public OMElement uploadFileUsingMTOM(OMElement request)
039: throws Exception {
040:
041: OMText binaryNode = (OMText) request.getFirstChildWithName(
042: new QName("http://services.samples/xsd", "request"))
043: .getFirstChildWithName(
044: new QName("http://services.samples/xsd",
045: "image")).getFirstOMChild();
046: DataHandler dataHandler = (DataHandler) binaryNode
047: .getDataHandler();
048: InputStream is = dataHandler.getInputStream();
049:
050: File tempFile = File.createTempFile("mtom-", ".gif");
051: FileOutputStream fos = new FileOutputStream(tempFile);
052: BufferedOutputStream dest = new BufferedOutputStream(fos,
053: BUFFER);
054:
055: byte data[] = new byte[BUFFER];
056: int count;
057: while ((count = is.read(data, 0, BUFFER)) != -1) {
058: dest.write(data, 0, count);
059: }
060:
061: dest.flush();
062: dest.close();
063: System.out.println("Wrote MTOM content to temp file : "
064: + tempFile.getAbsolutePath());
065:
066: OMFactory factory = request.getOMFactory();
067: OMNamespace ns = factory.createOMNamespace(
068: "http://services.samples/xsd", "m0");
069: OMElement payload = factory.createOMElement(
070: "uploadFileUsingMTOMResponse", ns);
071: OMElement response = factory.createOMElement("response", ns);
072: OMElement image = factory.createOMElement("image", ns);
073:
074: FileDataSource fileDataSource = new FileDataSource(tempFile);
075: dataHandler = new DataHandler(fileDataSource);
076: OMText textData = factory.createOMText(dataHandler, true);
077: image.addChild(textData);
078: response.addChild(image);
079: payload.addChild(response);
080:
081: MessageContext outMsgCtx = MessageContext
082: .getCurrentMessageContext().getOperationContext()
083: .getMessageContext(
084: WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
085: outMsgCtx.setProperty(
086: org.apache.axis2.Constants.Configuration.ENABLE_MTOM,
087: org.apache.axis2.Constants.VALUE_TRUE);
088:
089: return payload;
090: }
091:
092: public OMElement uploadFileUsingSwA(OMElement request)
093: throws Exception {
094:
095: String imageContentId = request.getFirstChildWithName(
096: new QName("http://services.samples/xsd", "request"))
097: .getFirstChildWithName(
098: new QName("http://services.samples/xsd",
099: "imageId")).getText();
100:
101: MessageContext msgCtx = MessageContext
102: .getCurrentMessageContext();
103: Attachments attachment = msgCtx.getAttachmentMap();
104: DataHandler dataHandler = attachment
105: .getDataHandler(imageContentId);
106: File tempFile = File.createTempFile("swa-", ".gif");
107: FileOutputStream fos = new FileOutputStream(tempFile);
108: dataHandler.writeTo(fos);
109: fos.flush();
110: fos.close();
111: System.out.println("Wrote SwA attachment to temp file : "
112: + tempFile.getAbsolutePath());
113:
114: MessageContext outMsgCtx = msgCtx.getOperationContext()
115: .getMessageContext(
116: WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
117: outMsgCtx.setProperty(
118: org.apache.axis2.Constants.Configuration.ENABLE_SWA,
119: org.apache.axis2.Constants.VALUE_TRUE);
120:
121: OMFactory factory = request.getOMFactory();
122: OMNamespace ns = factory.createOMNamespace(
123: "http://services.samples/xsd", "m0");
124: OMElement payload = factory.createOMElement(
125: "uploadFileUsingSwAResponse", ns);
126: OMElement response = factory.createOMElement("response", ns);
127: OMElement imageId = factory.createOMElement("imageId", ns);
128:
129: FileDataSource fileDataSource = new FileDataSource(tempFile);
130: dataHandler = new DataHandler(fileDataSource);
131: imageContentId = outMsgCtx.addAttachment(dataHandler);
132: imageId.setText(imageContentId);
133: response.addChild(imageId);
134: payload.addChild(response);
135:
136: return payload;
137: }
138:
139: public void oneWayUploadUsingMTOM(OMElement element)
140: throws Exception {
141:
142: OMText binaryNode = (OMText) element.getFirstOMChild();
143: DataHandler dataHandler = (DataHandler) binaryNode
144: .getDataHandler();
145: InputStream is = dataHandler.getInputStream();
146:
147: File tempFile = File.createTempFile("mtom-", ".gif");
148: FileOutputStream fos = new FileOutputStream(tempFile);
149: BufferedOutputStream dest = new BufferedOutputStream(fos,
150: BUFFER);
151:
152: byte data[] = new byte[BUFFER];
153: int count;
154: while ((count = is.read(data, 0, BUFFER)) != -1) {
155: dest.write(data, 0, count);
156: }
157:
158: dest.flush();
159: dest.close();
160: System.out.println("Wrote to file : "
161: + tempFile.getAbsolutePath());
162: }
163: }
|