001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package mime;
059:
060: import java.awt.Image;
061: import java.io.File;
062: import java.io.IOException;
063: import java.io.InputStream;
064:
065: import javax.activation.DataHandler;
066: import javax.activation.FileDataSource;
067: import javax.swing.ImageIcon;
068:
069: import util.TestUtilities;
070:
071: /**
072: * Mime service used by MimeTest
073: * @author Mark Whitlock
074: */
075:
076: public class MimeImpl {
077:
078: public String dataHandlerToString(DataHandler dh) {
079: try {
080: InputStream is = dh.getInputStream();
081: byte[] bBuff = new byte[is.available()];
082: is.read(bBuff);
083: String sBuff = new String(bBuff);
084: return sBuff;
085: } catch (IOException ioe) {
086: ioe.printStackTrace();
087: return null;
088: }
089: }
090:
091: public DataHandler stringToDataHandler(String buff) {
092: try {
093: FileDataSource fds = getTempFile();
094: fds.getOutputStream().write(buff.getBytes());
095: DataHandler dh = new DataHandler(fds);
096: return dh;
097: } catch (IOException ioe) {
098: ioe.printStackTrace();
099: return null;
100: }
101: }
102:
103: public String plainTextToString(DataHandler ds) {
104: try {
105: InputStream is = ds.getInputStream();
106: byte[] bBuff = new byte[is.available()];
107: is.read(bBuff);
108: String sBuff = new String(bBuff);
109: return sBuff;
110: } catch (IOException ioe) {
111: ioe.printStackTrace();
112: return null;
113: }
114: }
115:
116: public DataHandler stringToPlainText(String buff) {
117: try {
118: FileDataSource fds = getTempFile();
119: fds.getOutputStream().write(buff.getBytes());
120: return new DataHandler(fds);
121: } catch (IOException ioe) {
122: ioe.printStackTrace();
123: return null;
124: }
125: }
126:
127: public DataHandler bounceImage(DataHandler ds) {
128: try {
129: InputStream is = ds.getInputStream();
130: byte[] bBuff = new byte[is.available()];
131: is.read(bBuff);
132:
133: // This commented out code displays the image
134: // to check that it is correct.
135: //
136: // Image im = new ImageIcon(bBuff).getImage();
137: // WSIFFrame.display(im, "Backend image");
138: // int t = 0;
139: // try {
140: // t = Integer.parseInt(
141: // TestUtilities.getWsifProperty("wsif.displaytime"));
142: // } finally {
143: // if (t <= 0)
144: // t = 2000;
145: // }
146: // Thread.sleep(t);
147:
148: FileDataSource fds = getTempFile();
149: fds.getOutputStream().write(bBuff);
150: return new DataHandler(fds);
151: } catch (Exception e) {
152: e.printStackTrace();
153: return null;
154: } finally {
155: WSIFFrame.close();
156: }
157: }
158:
159: public DataHandler bounceImage2(DataHandler ds) {
160: return bounceImage(ds);
161: }
162:
163: //TODO: See bugzilla bug 15837
164: public DataHandler bounceImage4(boolean b, DataHandler ds) {
165: // return bounceImage4(ds, b);
166: // }
167: //
168: // public DataHandler bounceImage4(DataHandler ds, boolean b) {
169: if (b && (ds != null))
170: return bounceImage(ds);
171: else
172: return null;
173: }
174:
175: public String orMultiMimeParts(DataHandler dh) {
176: return dh.getContentType();
177: }
178:
179: public String andMultiMimeParts(DataHandler dh1, DataHandler dh2) {
180: try {
181: InputStream is1 = dh1.getInputStream();
182: byte[] bBuff1 = new byte[is1.available()];
183: is1.read(bBuff1);
184: StringBuffer sBuff = new StringBuffer(new String(bBuff1));
185:
186: InputStream is2 = dh2.getInputStream();
187: byte[] bBuff2 = new byte[is2.available()];
188: is2.read(bBuff2);
189: sBuff.append(new String(bBuff2));
190:
191: return sBuff.toString();
192: } catch (IOException ioe) {
193: ioe.printStackTrace();
194: return null;
195: }
196: }
197:
198: // MUST IMPLEMENT ???
199: // multiOutMimeParts and multiInoutMimeParts are only
200: // invoked through the stubless interface
201:
202: public String noContent(String s) {
203: return s;
204: }
205:
206: public String typeStar(DataHandler dh) {
207: return dataHandlerToString(dh);
208: }
209:
210: public String soapBodyParts1(boolean shouldBounce, DataHandler dh) {
211: return "1";
212: }
213:
214: public String soapBodyParts2(DataHandler dh) {
215: return "2";
216: }
217:
218: public String soapBodyParts3(boolean shouldBounce) {
219: return "3";
220: }
221:
222: public String soapBodyParts4() {
223: return "4";
224: }
225:
226: public String arrayOfBinary(DataHandler dh) {
227: return dataHandlerToString(dh);
228: }
229:
230: public DataHandler optionalSoapBody(DataHandler ds) {
231: return bounceImage(ds);
232: }
233:
234: public String mixMimeParts(String pos1, DataHandler dh1,
235: String pos2, DataHandler dh2, String pos3) {
236: return pos1 + dataHandlerToString(dh1) + pos2
237: + dataHandlerToString(dh2) + pos3;
238: }
239:
240: /* ******************* ERRORS *********************** */
241:
242: public String badNoPart(DataHandler dh) {
243: return dataHandlerToString(dh);
244: }
245:
246: public String badPart(DataHandler dh) {
247: return dataHandlerToString(dh);
248: }
249:
250: public String badNested(DataHandler dh) {
251: return dataHandlerToString(dh);
252: }
253:
254: public String badMixSoapMime(DataHandler dh) {
255: return dataHandlerToString(dh);
256: }
257:
258: public String badMultipleSoapBodies(DataHandler dh) {
259: return dataHandlerToString(dh);
260: }
261:
262: public String badSoapBodyType(DataHandler dh) {
263: return dataHandlerToString(dh);
264: }
265:
266: /* ***************** UTILITY METHODS *************** */
267:
268: private FileDataSource getTempFile() throws IOException {
269: File f = File.createTempFile("WSIFMimeTest", "txt");
270: f.deleteOnExit();
271: return new FileDataSource(f);
272: }
273: }
|