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 org.apache.axis2.jaxws.sample;
020:
021: import java.awt.Image;
022: import java.io.File;
023:
024: import javax.imageio.ImageIO;
025: import javax.xml.bind.JAXBContext;
026: import javax.xml.namespace.QName;
027: import javax.xml.ws.Dispatch;
028: import javax.xml.ws.Service;
029: import javax.xml.ws.soap.SOAPBinding;
030:
031: import junit.framework.TestCase;
032:
033: import org.apache.axis2.jaxws.sample.mtom1.ImageDepot;
034: import org.apache.axis2.jaxws.sample.mtom1.ObjectFactory;
035: import org.apache.axis2.jaxws.sample.mtom1.Invoke;
036: import org.apache.axis2.jaxws.sample.mtom1.SendImageResponse;
037: import org.apache.axis2.jaxws.TestLogger;
038: import org.w3._2005._05.xmlmime.Base64Binary;
039:
040: import com.sun.xml.bind.v2.runtime.unmarshaller.Base64Data;
041:
042: public class MtomSampleByteArrayTests extends TestCase {
043:
044: private static final QName QNAME_SERVICE = new QName(
045: "urn://mtom1.sample.jaxws.axis2.apache.org",
046: "SendImageService");
047: private static final QName QNAME_PORT = new QName(
048: "urn://mtom1.sample.jaxws.axis2.apache.org",
049: "sendImageSoap");
050: private static final String URL_ENDPOINT = "http://localhost:8080/axis2/services/SendImageService";
051: private static final String IMAGE_DIR = System.getProperty(
052: "basedir", ".")
053: + File.separator
054: + "test-resources"
055: + File.separator
056: + "image";
057:
058: /*
059: * Enable attachment Optimization through the SOAPBinding method
060: * -- setMTOMEnabled([true|false])
061: * Using SOAP11
062: */
063: public void testAttachmentByteArrayAPI11() throws Exception {
064: TestLogger.logger.debug("----------------------------------");
065: TestLogger.logger.debug("test: " + getName());
066:
067: String imageResourceDir = IMAGE_DIR;
068:
069: Service svc = Service.create(QNAME_SERVICE);
070: svc.addPort(QNAME_PORT, SOAPBinding.SOAP11HTTP_BINDING,
071: URL_ENDPOINT);
072:
073: JAXBContext jbc = JAXBContext
074: .newInstance("org.apache.axis2.jaxws.sample.mtom1");
075: Dispatch<Object> dispatch = svc.createDispatch(QNAME_PORT, jbc,
076: Service.Mode.PAYLOAD);
077:
078: SOAPBinding binding = (SOAPBinding) dispatch.getBinding();
079: binding.setMTOMEnabled(true);
080:
081: Image image = ImageIO.read(new File(imageResourceDir
082: + File.separator + "test.jpg"));
083: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
084: imageDepot.setImageData(image);
085: setText(imageDepot);
086:
087: //Create a request bean with imagedepot bean as value
088: ObjectFactory factory = new ObjectFactory();
089: Invoke request = factory.createInvoke();
090: request.setInput(imageDepot);
091:
092: SendImageResponse response = (SendImageResponse) dispatch
093: .invoke(request);
094:
095: assertNotNull(response);
096: assertNotNull(response.getOutput().getImageData());
097: checkText(response.getOutput());
098: }
099:
100: /*
101: * Disable attachment Optimization through the SOAPBinding method
102: * -- setMTOMEnabled([true|false])
103: * Using SOAP11
104: */
105: public void testAttachmentByteArrayAPI11_ClientSendsNonOptimizedMTOM()
106: throws Exception {
107: TestLogger.logger.debug("----------------------------------");
108: TestLogger.logger.debug("test: " + getName());
109:
110: String imageResourceDir = IMAGE_DIR;
111:
112: Service svc = Service.create(QNAME_SERVICE);
113: svc.addPort(QNAME_PORT, SOAPBinding.SOAP11HTTP_BINDING,
114: URL_ENDPOINT);
115:
116: JAXBContext jbc = JAXBContext
117: .newInstance("org.apache.axis2.jaxws.sample.mtom1");
118: Dispatch<Object> dispatch = svc.createDispatch(QNAME_PORT, jbc,
119: Service.Mode.PAYLOAD);
120:
121: SOAPBinding binding = (SOAPBinding) dispatch.getBinding();
122: binding.setMTOMEnabled(false); // Disabling MTOM optimization on client, but server will respond with optimized MTOM
123:
124: Image image = ImageIO.read(new File(imageResourceDir
125: + File.separator + "test.jpg"));
126: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
127: imageDepot.setImageData(image);
128: setText(imageDepot);
129:
130: //Create a request bean with imagedepot bean as value
131: ObjectFactory factory = new ObjectFactory();
132: Invoke request = factory.createInvoke();
133: request.setInput(imageDepot);
134:
135: SendImageResponse response = (SendImageResponse) dispatch
136: .invoke(request);
137:
138: assertNotNull(response);
139: assertNotNull(response.getOutput().getImageData());
140: checkText(response.getOutput());
141: }
142:
143: /*
144: * Enable attachment optimization using the SOAP11 binding
145: * property for MTOM.
146: */
147: public void testAttachmentByteArrayProperty11() throws Exception {
148: TestLogger.logger.debug("----------------------------------");
149: TestLogger.logger.debug("test: " + getName());
150:
151: String imageResourceDir = IMAGE_DIR;
152:
153: Service svc = Service.create(QNAME_SERVICE);
154: svc.addPort(QNAME_PORT, SOAPBinding.SOAP11HTTP_MTOM_BINDING,
155: URL_ENDPOINT);
156:
157: JAXBContext jbc = JAXBContext
158: .newInstance("org.apache.axis2.jaxws.sample.mtom1");
159: Dispatch<Object> dispatch = svc.createDispatch(QNAME_PORT, jbc,
160: Service.Mode.PAYLOAD);
161:
162: Image image = ImageIO.read(new File(imageResourceDir
163: + File.separator + "test.jpg"));
164: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
165: imageDepot.setImageData(image);
166: setText(imageDepot);
167:
168: //Create a request bean with imagedepot bean as value
169: ObjectFactory factory = new ObjectFactory();
170: Invoke request = factory.createInvoke();
171: request.setInput(imageDepot);
172:
173: SendImageResponse response = (SendImageResponse) dispatch
174: .invoke(request);
175:
176: assertNotNull(response);
177: assertNotNull(response.getOutput().getImageData());
178: checkText(response.getOutput());
179:
180: }
181:
182: private static final String text = "Binary Attachments are radical";
183:
184: private void setText(ImageDepot depot) {
185:
186: Base64Binary binary = new Base64Binary();
187: binary.setContentType("");
188: binary.setValue(text.getBytes());
189: depot.setTextData(binary);
190: }
191:
192: private void checkText(ImageDepot depot) {
193: Base64Binary binary = depot.getTextData();
194: assertTrue(binary != null);
195: String contentType = binary.getContentType();
196: assertTrue("".equals(contentType));
197: byte[] bytes = binary.getValue();
198: assertTrue(bytes != null);
199: String theText = new String(bytes);
200: assertTrue(text.equals(theText));
201: }
202:
203: }
|