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.attachments;
020:
021: import java.awt.*;
022: import java.io.ByteArrayOutputStream;
023: import java.io.File;
024:
025: import javax.activation.DataHandler;
026: import javax.activation.DataSource;
027: import javax.imageio.ImageIO;
028: import javax.imageio.stream.FileImageInputStream;
029: import javax.imageio.stream.ImageInputStream;
030: import javax.xml.bind.JAXBContext;
031:
032: import junit.framework.TestCase;
033: import org.apache.axiom.om.OMAbstractFactory;
034: import org.apache.axiom.om.OMElement;
035: import org.apache.axiom.om.OMFactory;
036: import org.apache.axiom.om.OMNamespace;
037: import org.apache.axiom.om.OMOutputFormat;
038: import org.apache.axiom.om.OMText;
039: import org.apache.axiom.soap.SOAPBody;
040: import org.apache.axiom.soap.SOAPEnvelope;
041: import org.apache.axiom.soap.SOAPFactory;
042: import org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory;
043: import org.apache.axis2.jaxws.message.Block;
044: import org.apache.axis2.jaxws.message.Message;
045: import org.apache.axis2.jaxws.message.Protocol;
046: import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
047: import org.apache.axis2.jaxws.message.factory.BlockFactory;
048: import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
049: import org.apache.axis2.jaxws.message.factory.MessageFactory;
050: import org.apache.axis2.jaxws.provider.DataSourceImpl;
051: import org.apache.axis2.jaxws.registry.FactoryRegistry;
052: import org.apache.axis2.jaxws.TestLogger;
053: import org.test.mtom.ImageDepot;
054: import org.test.mtom.ObjectFactory;
055: import org.test.mtom.SendImage;
056:
057: public class MTOMSerializationTests extends TestCase {
058:
059: private DataSource imageDS;
060:
061: public void setUp() throws Exception {
062: String imageResourceDir = System.getProperty("basedir", ".")
063: + "/" + "test-resources" + File.separator + "image";
064:
065: //Create a DataSource from an image
066: File file = new File(imageResourceDir + File.separator
067: + "test.jpg");
068: ImageInputStream fiis = new FileImageInputStream(file);
069: Image image = ImageIO.read(fiis);
070: imageDS = new DataSourceImpl("image/jpeg", "test.jpg", image);
071: }
072:
073: public MTOMSerializationTests(String name) {
074: super (name);
075: }
076:
077: /*
078: * Simulate building up an OM that is sourced from JAXB and contains
079: * binary data that should be optimized when serialized.
080: */
081: public void testPlainOMSerialization() throws Exception {
082: TestLogger.logger
083: .debug("---------------------------------------");
084: TestLogger.logger.debug("test: " + getName());
085:
086: OMElement payload = createPayload();
087:
088: OMOutputFormat format = new OMOutputFormat();
089: format.setDoOptimize(true);
090: format.setSOAP11(true);
091:
092: ByteArrayOutputStream baos = new ByteArrayOutputStream();
093: payload.serializeAndConsume(baos, format);
094:
095: TestLogger.logger.debug("==================================");
096: TestLogger.logger.debug(baos.toString());
097: TestLogger.logger.debug("==================================");
098: }
099:
100: /*
101: * Simulate building up an OM SOAPEnvelope that has the contents of
102: * the body sourced from JAXB and contains binary data that should be
103: * optimized when serialized.
104: */
105: public void testSoapOMSerialization() throws Exception {
106: TestLogger.logger
107: .debug("---------------------------------------");
108: TestLogger.logger.debug("test: " + getName());
109:
110: OMElement payload = createPayload();
111:
112: SOAPFactory factory = new SOAP11Factory();
113: SOAPEnvelope env = factory.createSOAPEnvelope();
114: SOAPBody body = factory.createSOAPBody(env);
115:
116: body.addChild(payload);
117:
118: OMOutputFormat format = new OMOutputFormat();
119: format.setDoOptimize(true);
120: format.setSOAP11(true);
121:
122: ByteArrayOutputStream baos = new ByteArrayOutputStream();
123: env.serializeAndConsume(baos, format);
124:
125: TestLogger.logger.debug("==================================");
126: TestLogger.logger.debug(baos.toString());
127: TestLogger.logger.debug("==================================");
128: }
129:
130: public void testMTOMAttachmentWriter() throws Exception {
131: TestLogger.logger
132: .debug("---------------------------------------");
133: TestLogger.logger.debug("test: " + getName());
134:
135: //Create a DataHandler with the String DataSource object
136: DataHandler dataHandler = new DataHandler(imageDS);
137:
138: //Store the data handler in ImageDepot bean
139: ImageDepot imageDepot = new ObjectFactory().createImageDepot();
140: imageDepot.setImageData(dataHandler);
141:
142: //JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
143: JAXBBlockContext context = new JAXBBlockContext(SendImage.class
144: .getPackage().getName());
145:
146: //Create a request bean with imagedepot bean as value
147: ObjectFactory factory = new ObjectFactory();
148: SendImage request = factory.createSendImage();
149: request.setInput(imageDepot);
150:
151: BlockFactory blkFactory = (JAXBBlockFactory) FactoryRegistry
152: .getFactory(JAXBBlockFactory.class);
153: Block block = blkFactory.createFrom(request, context, null);
154:
155: MessageFactory msgFactory = (MessageFactory) FactoryRegistry
156: .getFactory(MessageFactory.class);
157: Message msg = msgFactory.create(Protocol.soap11);
158:
159: msg.setBodyBlock(block);
160:
161: msg.setMTOMEnabled(true);
162:
163: SOAPEnvelope soapOM = (SOAPEnvelope) msg.getAsOMElement();
164:
165: OMOutputFormat format = new OMOutputFormat();
166: format.setDoOptimize(true);
167: format.setSOAP11(true);
168:
169: ByteArrayOutputStream baos = new ByteArrayOutputStream();
170: soapOM.serializeAndConsume(baos, format);
171:
172: TestLogger.logger.debug("==================================");
173: TestLogger.logger.debug(baos.toString());
174: TestLogger.logger.debug("==================================");
175: }
176:
177: private OMElement createPayload() {
178: //Create a DataHandler with the String DataSource object
179: DataHandler dataHandler = new DataHandler(imageDS);
180:
181: OMFactory fac = OMAbstractFactory.getOMFactory();
182: OMNamespace omNs = fac.createOMNamespace("urn://mtom.test.org",
183: "mtom");
184:
185: OMElement sendImage = fac.createOMElement("sendImage", omNs);
186:
187: OMElement input = fac.createOMElement("input", omNs);
188: sendImage.addChild(input);
189:
190: OMElement imageData = fac.createOMElement("imageData", omNs);
191: input.addChild(imageData);
192:
193: OMText binaryData = fac.createOMText(dataHandler, true);
194: imageData.addChild(binaryData);
195:
196: return sendImage;
197: }
198: }
|