Source code: mime/MimeImpl.java
1 /*
2 * The Apache Software License, Version 1.1
3 *
4 *
5 * Copyright (c) 2002 The Apache Software Foundation. All rights
6 * reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgment:
22 * "This product includes software developed by the
23 * Apache Software Foundation (http://www.apache.org/)."
24 * Alternately, this acknowledgment may appear in the software itself,
25 * if and wherever such third-party acknowledgments normally appear.
26 *
27 * 4. The names "WSIF" and "Apache Software Foundation" must
28 * not be used to endorse or promote products derived from this
29 * software without prior written permission. For written
30 * permission, please contact apache@apache.org.
31 *
32 * 5. Products derived from this software may not be called "Apache",
33 * nor may "Apache" appear in their name, without prior written
34 * permission of the Apache Software Foundation.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the Apache Software Foundation and was
52 * originally based on software copyright (c) 2001, 2002, International
53 * Business Machines, Inc., http://www.apache.org. For more
54 * information on the Apache Software Foundation, please see
55 * <http://www.apache.org/>.
56 */
57
58 package mime;
59
60 import java.awt.Image;
61 import java.io.File;
62 import java.io.IOException;
63 import java.io.InputStream;
64
65 import javax.activation.DataHandler;
66 import javax.activation.FileDataSource;
67 import javax.swing.ImageIcon;
68
69 import util.TestUtilities;
70
71 /**
72 * Mime service used by MimeTest
73 * @author Mark Whitlock
74 */
75
76 public class MimeImpl {
77
78 public String dataHandlerToString(DataHandler dh) {
79 try {
80 InputStream is = dh.getInputStream();
81 byte[] bBuff = new byte[is.available()];
82 is.read(bBuff);
83 String sBuff = new String(bBuff);
84 return sBuff;
85 } catch (IOException ioe) {
86 ioe.printStackTrace();
87 return null;
88 }
89 }
90
91 public DataHandler stringToDataHandler(String buff) {
92 try {
93 FileDataSource fds = getTempFile();
94 fds.getOutputStream().write(buff.getBytes());
95 DataHandler dh = new DataHandler(fds);
96 return dh;
97 } catch (IOException ioe) {
98 ioe.printStackTrace();
99 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(
235 String pos1,
236 DataHandler dh1,
237 String pos2,
238 DataHandler dh2,
239 String pos3)
240 {
241 return pos1
242 + dataHandlerToString(dh1)
243 + pos2
244 + dataHandlerToString(dh2)
245 + pos3;
246 }
247
248 /* ******************* ERRORS *********************** */
249
250 public String badNoPart(DataHandler dh) {
251 return dataHandlerToString(dh);
252 }
253
254 public String badPart(DataHandler dh) {
255 return dataHandlerToString(dh);
256 }
257
258 public String badNested(DataHandler dh) {
259 return dataHandlerToString(dh);
260 }
261
262 public String badMixSoapMime(DataHandler dh) {
263 return dataHandlerToString(dh);
264 }
265
266 public String badMultipleSoapBodies(DataHandler dh) {
267 return dataHandlerToString(dh);
268 }
269
270 public String badSoapBodyType(DataHandler dh) {
271 return dataHandlerToString(dh);
272 }
273
274 /* ***************** UTILITY METHODS *************** */
275
276 private FileDataSource getTempFile() throws IOException {
277 File f = File.createTempFile("WSIFMimeTest", "txt");
278 f.deleteOnExit();
279 return new FileDataSource(f);
280 }
281 }