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:
020: package samples.userguide;
021:
022: import org.apache.axiom.om.*;
023: import org.apache.axiom.soap.SOAPEnvelope;
024: import org.apache.axiom.soap.SOAPFactory;
025: import org.apache.axiom.soap.SOAPBody;
026: import org.apache.axiom.attachments.Attachments;
027: import org.apache.axis2.client.ServiceClient;
028: import org.apache.axis2.client.Options;
029: import org.apache.axis2.client.OperationClient;
030: import org.apache.axis2.addressing.EndpointReference;
031: import org.apache.axis2.Constants;
032: import org.apache.axis2.wsdl.WSDLConstants;
033: import org.apache.axis2.context.MessageContext;
034:
035: import javax.activation.FileDataSource;
036: import javax.activation.DataHandler;
037: import javax.xml.namespace.QName;
038: import java.io.*;
039:
040: public class MTOMSwAClient {
041:
042: private static final int BUFFER = 2048;
043:
044: private static String getProperty(String name, String def) {
045: String result = System.getProperty(name);
046: if (result == null || result.length() == 0) {
047: result = def;
048: }
049: return result;
050: }
051:
052: public static void main(String[] args) throws Exception {
053:
054: String targetEPR = getProperty("opt_url",
055: "http://localhost:8080/soap/MTOMSwASampleService");
056: String fileName = getProperty("opt_file",
057: "./../../repository/conf/sample/resources/mtom/asf-logo.gif");
058: String mode = getProperty("opt_mode", "mtom");
059:
060: if (args.length > 0)
061: mode = args[0];
062: if (args.length > 1)
063: targetEPR = args[1];
064: if (args.length > 2)
065: fileName = args[2];
066:
067: if ("mtom".equals(mode)) {
068: sendUsingMTOM(fileName, targetEPR);
069: } else if ("swa".equals(mode)) {
070: sendUsingSwA(fileName, targetEPR);
071: }
072: }
073:
074: public static MessageContext sendUsingSwA(String fileName,
075: String targetEPR) throws IOException {
076:
077: Options options = new Options();
078: options.setTo(new EndpointReference(targetEPR));
079: options.setAction("urn:uploadFileUsingSwA");
080: options.setProperty(Constants.Configuration.ENABLE_SWA,
081: Constants.VALUE_TRUE);
082:
083: ServiceClient sender = new ServiceClient();
084: sender.setOptions(options);
085: OperationClient mepClient = sender
086: .createClient(ServiceClient.ANON_OUT_IN_OP);
087:
088: MessageContext mc = new MessageContext();
089:
090: System.out.println("Sending file : " + fileName + " as SwA");
091: FileDataSource fileDataSource = new FileDataSource(new File(
092: fileName));
093: DataHandler dataHandler = new DataHandler(fileDataSource);
094: String attachmentID = mc.addAttachment(dataHandler);
095:
096: SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
097: SOAPEnvelope env = factory.getDefaultEnvelope();
098: OMNamespace ns = factory.createOMNamespace(
099: "http://services.samples/xsd", "m0");
100: OMElement payload = factory.createOMElement(
101: "uploadFileUsingSwA", ns);
102: OMElement request = factory.createOMElement("request", ns);
103: OMElement imageId = factory.createOMElement("imageId", ns);
104: imageId.setText(attachmentID);
105: request.addChild(imageId);
106: payload.addChild(request);
107: env.getBody().addChild(payload);
108: mc.setEnvelope(env);
109:
110: mepClient.addMessageContext(mc);
111: mepClient.execute(true);
112: MessageContext response = mepClient
113: .getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
114:
115: SOAPBody body = response.getEnvelope().getBody();
116: String imageContentId = body.getFirstChildWithName(
117: new QName("http://services.samples/xsd",
118: "uploadFileUsingSwAResponse"))
119: .getFirstChildWithName(
120: new QName("http://services.samples/xsd",
121: "response")).getFirstChildWithName(
122: new QName("http://services.samples/xsd",
123: "imageId")).getText();
124:
125: Attachments attachment = response.getAttachmentMap();
126: dataHandler = attachment.getDataHandler(imageContentId);
127: File tempFile = File.createTempFile("swa-", ".gif");
128: FileOutputStream fos = new FileOutputStream(tempFile);
129: dataHandler.writeTo(fos);
130: fos.flush();
131: fos.close();
132:
133: System.out.println("Saved response to file : "
134: + tempFile.getAbsolutePath());
135:
136: return response;
137: }
138:
139: public static OMElement sendUsingMTOM(String fileName,
140: String targetEPR) throws IOException {
141: OMFactory factory = OMAbstractFactory.getOMFactory();
142: OMNamespace ns = factory.createOMNamespace(
143: "http://services.samples/xsd", "m0");
144: OMElement payload = factory.createOMElement(
145: "uploadFileUsingMTOM", ns);
146: OMElement request = factory.createOMElement("request", ns);
147: OMElement image = factory.createOMElement("image", ns);
148:
149: System.out.println("Sending file : " + fileName + " as MTOM");
150: FileDataSource fileDataSource = new FileDataSource(new File(
151: fileName));
152: DataHandler dataHandler = new DataHandler(fileDataSource);
153: OMText textData = factory.createOMText(dataHandler, true);
154: image.addChild(textData);
155: request.addChild(image);
156: payload.addChild(request);
157:
158: ServiceClient serviceClient = new ServiceClient();
159: Options options = new Options();
160: options.setTo(new EndpointReference(targetEPR));
161: options.setAction("urn:uploadFileUsingMTOM");
162: options.setProperty(Constants.Configuration.ENABLE_MTOM,
163: Constants.VALUE_TRUE);
164:
165: serviceClient.setOptions(options);
166: OMElement response = serviceClient.sendReceive(payload);
167:
168: OMText binaryNode = (OMText) response.getFirstChildWithName(
169: new QName("http://services.samples/xsd", "response"))
170: .getFirstChildWithName(
171: new QName("http://services.samples/xsd",
172: "image")).getFirstOMChild();
173: dataHandler = (DataHandler) binaryNode.getDataHandler();
174: InputStream is = dataHandler.getInputStream();
175:
176: File tempFile = File.createTempFile("mtom-", ".gif");
177: FileOutputStream fos = new FileOutputStream(tempFile);
178: BufferedOutputStream dest = new BufferedOutputStream(fos,
179: BUFFER);
180:
181: byte data[] = new byte[BUFFER];
182: int count;
183: while ((count = is.read(data, 0, BUFFER)) != -1) {
184: dest.write(data, 0, count);
185: }
186:
187: dest.flush();
188: dest.close();
189: System.out.println("Saved response to file : "
190: + tempFile.getAbsolutePath());
191: return response;
192: }
193: }
|