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.activation.DataHandler;
025: import javax.activation.DataSource;
026: import javax.imageio.ImageIO;
027: import javax.imageio.stream.FileImageInputStream;
028: import javax.imageio.stream.ImageInputStream;
029: import javax.xml.bind.JAXBContext;
030: import javax.xml.namespace.QName;
031: import javax.xml.ws.Dispatch;
032: import javax.xml.ws.Service;
033: import javax.xml.ws.Service.Mode;
034: import javax.xml.ws.soap.SOAPBinding;
035:
036: import junit.framework.TestCase;
037: import org.apache.axis2.jaxws.provider.DataSourceImpl;
038: import org.apache.axis2.jaxws.TestLogger;
039: import org.test.mtom.ImageDepot;
040: import org.test.mtom.ObjectFactory;
041: import org.test.mtom.SendImage;
042: import org.test.mtom.SendImageResponse;
043:
044: public class MtomSampleTests extends TestCase {
045:
046: private static final QName QNAME_SERVICE = new QName(
047: "urn://mtom.test.org", "MtomSampleService");
048: private static final QName QNAME_PORT = new QName(
049: "urn://mtom.test.org", "MtomSample");
050: private static final String URL_ENDPOINT = "http://localhost:8080/axis2/services/MtomSampleService";
051: private static final String IMAGE_DIR = System.getProperty(
052: "basedir", ".")
053: + "/" + "test-resources" + File.separator + "image";
054:
055: /*
056: * Enable attachment Optimization through the SOAPBinding method
057: * -- setMTOMEnabled([true|false])
058: * Using SOAP11
059: */
060: public void testSendImageAttachmentAPI11() throws Exception {
061: TestLogger.logger.debug("----------------------------------");
062: TestLogger.logger.debug("test: " + getName());
063:
064: String imageResourceDir = IMAGE_DIR;
065:
066: //Create a DataSource from an image
067: File file = new File(imageResourceDir + File.separator
068: + "test.jpg");
069: ImageInputStream fiis = new FileImageInputStream(file);
070: Image image = ImageIO.read(fiis);
071: DataSource imageDS = new DataSourceImpl("image/jpeg",
072: "test.jpg", image);
073:
074: //Create a DataHandler with the String DataSource object
075: DataHandler dataHandler = new DataHandler(imageDS);
076:
077: //Store the data handler in ImageDepot bean
078: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
079: imageDepot.setImageData(dataHandler);
080:
081: SendImage request = new ObjectFactory().createSendImage();
082: request.setInput(imageDepot);
083:
084: //Create the necessary JAXBContext
085: JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
086:
087: // Create the JAX-WS client needed to send the request
088: Service service = Service.create(QNAME_SERVICE);
089: service.addPort(QNAME_PORT, SOAPBinding.SOAP11HTTP_BINDING,
090: URL_ENDPOINT);
091: Dispatch<Object> dispatch = service.createDispatch(QNAME_PORT,
092: jbc, Mode.PAYLOAD);
093:
094: //Enable attachment optimization
095: SOAPBinding binding = (SOAPBinding) dispatch.getBinding();
096: binding.setMTOMEnabled(true);
097:
098: SendImageResponse response = (SendImageResponse) dispatch
099: .invoke(request);
100:
101: assertNotNull(response);
102: assertNotNull(response.getOutput().getImageData());
103: }
104:
105: /*
106: * Enable attachment optimization using the SOAP11 binding
107: * property for MTOM.
108: */
109: public void testSendImageAttachmentProperty11() throws Exception {
110: TestLogger.logger.debug("----------------------------------");
111: TestLogger.logger.debug("test: " + getName());
112:
113: String imageResourceDir = IMAGE_DIR;
114:
115: //Create a DataSource from an image
116: File file = new File(imageResourceDir + File.separator
117: + "test.jpg");
118: ImageInputStream fiis = new FileImageInputStream(file);
119: Image image = ImageIO.read(fiis);
120: DataSource imageDS = new DataSourceImpl("image/jpeg",
121: "test.jpg", image);
122:
123: //Create a DataHandler with the String DataSource object
124: DataHandler dataHandler = new DataHandler(imageDS);
125:
126: //Store the data handler in ImageDepot bean
127: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
128: imageDepot.setImageData(dataHandler);
129:
130: SendImage request = new ObjectFactory().createSendImage();
131: request.setInput(imageDepot);
132:
133: //Create the necessary JAXBContext
134: JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
135:
136: // Create the JAX-WS client needed to send the request with soap 11 binding
137: // property for MTOM
138: Service service = Service.create(QNAME_SERVICE);
139: service.addPort(QNAME_PORT,
140: SOAPBinding.SOAP11HTTP_MTOM_BINDING, URL_ENDPOINT);
141: Dispatch<Object> dispatch = service.createDispatch(QNAME_PORT,
142: jbc, Mode.PAYLOAD);
143:
144: SendImageResponse response = (SendImageResponse) dispatch
145: .invoke(request);
146:
147: assertNotNull(response);
148: assertNotNull(response.getOutput().getImageData());
149: }
150:
151: /*
152: * Enable attachment optimization using both the SOAP11 binding
153: * property for MTOM and the Binding API
154: */
155: public void testSendImageAttachmentAPIProperty11() throws Exception {
156: TestLogger.logger.debug("----------------------------------");
157: TestLogger.logger.debug("test: " + getName());
158:
159: String imageResourceDir = IMAGE_DIR;
160:
161: //Create a DataSource from an image
162: File file = new File(imageResourceDir + File.separator
163: + "test.jpg");
164: ImageInputStream fiis = new FileImageInputStream(file);
165: Image image = ImageIO.read(fiis);
166: DataSource imageDS = new DataSourceImpl("image/jpeg",
167: "test.jpg", image);
168:
169: //Create a DataHandler with the String DataSource object
170: DataHandler dataHandler = new DataHandler(imageDS);
171:
172: //Store the data handler in ImageDepot bean
173: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
174: imageDepot.setImageData(dataHandler);
175:
176: SendImage request = new ObjectFactory().createSendImage();
177: request.setInput(imageDepot);
178:
179: //Create the necessary JAXBContext
180: JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
181:
182: // Create the JAX-WS client needed to send the request with soap 11 binding
183: // property for MTOM
184: Service service = Service.create(QNAME_SERVICE);
185: service.addPort(QNAME_PORT,
186: SOAPBinding.SOAP11HTTP_MTOM_BINDING, URL_ENDPOINT);
187: Dispatch<Object> dispatch = service.createDispatch(QNAME_PORT,
188: jbc, Mode.PAYLOAD);
189:
190: //Enable attachment optimization
191: SOAPBinding binding = (SOAPBinding) dispatch.getBinding();
192: binding.setMTOMEnabled(true);
193:
194: SendImageResponse response = (SendImageResponse) dispatch
195: .invoke(request);
196:
197: assertNotNull(response);
198: assertNotNull(response.getOutput().getImageData());
199: }
200:
201: /*
202: * Enable attachment optimization using both the SOAP12 binding
203: * property for MTOM
204: *
205: * Sending SOAP12 message to SOAP11 endpoint will correctly result in exception
206: *
207: */
208: public void testSendImageAttachmentProperty12() throws Exception {
209: TestLogger.logger.debug("----------------------------------");
210: TestLogger.logger.debug("test: " + getName());
211:
212: String imageResourceDir = IMAGE_DIR;
213:
214: //Create a DataSource from an image
215: File file = new File(imageResourceDir + File.separator
216: + "test.jpg");
217: ImageInputStream fiis = new FileImageInputStream(file);
218: Image image = ImageIO.read(fiis);
219: DataSource imageDS = new DataSourceImpl("image/jpeg",
220: "test.jpg", image);
221:
222: //Create a DataHandler with the String DataSource object
223: DataHandler dataHandler = new DataHandler(imageDS);
224:
225: //Store the data handler in ImageDepot bean
226: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
227: imageDepot.setImageData(dataHandler);
228:
229: SendImage request = new ObjectFactory().createSendImage();
230: request.setInput(imageDepot);
231:
232: //Create the necessary JAXBContext
233: JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
234:
235: // Create the JAX-WS client needed to send the request with soap 11 binding
236: // property for MTOM
237: Service service = Service.create(QNAME_SERVICE);
238: service.addPort(QNAME_PORT,
239: SOAPBinding.SOAP12HTTP_MTOM_BINDING, URL_ENDPOINT);
240: Dispatch<Object> dispatch = service.createDispatch(QNAME_PORT,
241: jbc, Mode.PAYLOAD);
242:
243: try {
244: SendImageResponse response = (SendImageResponse) dispatch
245: .invoke(request);
246: fail("Was expecting an exception due to sending SOAP12 message to SOAP11 endpoint.");
247: } catch (Exception e) {
248: assertNotNull(e);
249: }
250: /*
251: assertNotNull(response);
252: assertNotNull(response.getOutput().getImageData());
253: */
254: }
255:
256: /*
257: * Enable attachment optimization using both the SOAP12 binding API
258: * for MTOM
259: *
260: * Sending SOAP12 message to SOAP11 endpoint will correctly result in exception
261: *
262: */
263: public void testSendImageAttachmentAPI12() throws Exception {
264: TestLogger.logger.debug("----------------------------------");
265: TestLogger.logger.debug("test: " + getName());
266:
267: String imageResourceDir = IMAGE_DIR;
268:
269: //Create a DataSource from an image
270: File file = new File(imageResourceDir + File.separator
271: + "test.jpg");
272: ImageInputStream fiis = new FileImageInputStream(file);
273: Image image = ImageIO.read(fiis);
274: DataSource imageDS = new DataSourceImpl("image/jpeg",
275: "test.jpg", image);
276:
277: //Create a DataHandler with the String DataSource object
278: DataHandler dataHandler = new DataHandler(imageDS);
279:
280: //Store the data handler in ImageDepot bean
281: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
282: imageDepot.setImageData(dataHandler);
283:
284: SendImage request = new ObjectFactory().createSendImage();
285: request.setInput(imageDepot);
286:
287: //Create the necessary JAXBContext
288: JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
289:
290: // Create the JAX-WS client needed to send the request with soap 11 binding
291: // property for MTOM
292: Service service = Service.create(QNAME_SERVICE);
293: service.addPort(QNAME_PORT, SOAPBinding.SOAP12HTTP_BINDING,
294: URL_ENDPOINT);
295: Dispatch<Object> dispatch = service.createDispatch(QNAME_PORT,
296: jbc, Mode.PAYLOAD);
297:
298: //Enable attachment optimization
299: SOAPBinding binding = (SOAPBinding) dispatch.getBinding();
300: binding.setMTOMEnabled(true);
301:
302: try {
303: SendImageResponse response = (SendImageResponse) dispatch
304: .invoke(request);
305: fail("Was expecting an exception due to sending SOAP12 message to SOAP11 endpoint.");
306: } catch (Exception e) {
307: assertNotNull(e);
308: }
309: /*
310: assertNotNull(response);
311: assertNotNull(response.getOutput().getImageData());
312: */
313: }
314:
315: }
|