KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mime > MimeImpl


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 JavaDoc;
61 import java.io.File JavaDoc;
62 import java.io.IOException JavaDoc;
63 import java.io.InputStream JavaDoc;
64
65 import javax.activation.DataHandler JavaDoc;
66 import javax.activation.FileDataSource JavaDoc;
67 import javax.swing.ImageIcon JavaDoc;
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 JavaDoc dataHandlerToString(DataHandler JavaDoc dh) {
79         try {
80             InputStream JavaDoc is = dh.getInputStream();
81             byte[] bBuff = new byte[is.available()];
82             is.read(bBuff);
83             String JavaDoc sBuff = new String JavaDoc(bBuff);
84             return sBuff;
85         } catch (IOException JavaDoc ioe) {
86             ioe.printStackTrace();
87             return null;
88         }
89     }
90
91     public DataHandler JavaDoc stringToDataHandler(String JavaDoc buff) {
92         try {
93             FileDataSource JavaDoc fds = getTempFile();
94             fds.getOutputStream().write(buff.getBytes());
95             DataHandler JavaDoc dh = new DataHandler JavaDoc(fds);
96             return dh;
97         } catch (IOException JavaDoc ioe) {
98             ioe.printStackTrace();
99             return null;
100         }
101     }
102
103     public String JavaDoc plainTextToString(DataHandler JavaDoc ds) {
104         try {
105             InputStream JavaDoc is = ds.getInputStream();
106             byte[] bBuff = new byte[is.available()];
107             is.read(bBuff);
108             String JavaDoc sBuff = new String JavaDoc(bBuff);
109             return sBuff;
110         } catch (IOException JavaDoc ioe) {
111             ioe.printStackTrace();
112             return null;
113         }
114     }
115
116     public DataHandler JavaDoc stringToPlainText(String JavaDoc buff) {
117         try {
118             FileDataSource JavaDoc fds = getTempFile();
119             fds.getOutputStream().write(buff.getBytes());
120             return new DataHandler JavaDoc(fds);
121         } catch (IOException JavaDoc ioe) {
122             ioe.printStackTrace();
123             return null;
124         }
125     }
126
127     public DataHandler JavaDoc bounceImage(DataHandler JavaDoc ds) {
128         try {
129             InputStream JavaDoc 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 JavaDoc fds = getTempFile();
149             fds.getOutputStream().write(bBuff);
150             return new DataHandler JavaDoc(fds);
151         } catch (Exception JavaDoc e) {
152             e.printStackTrace();
153             return null;
154         } finally {
155             WSIFFrame.close();
156         }
157     }
158
159     public DataHandler JavaDoc bounceImage2(DataHandler JavaDoc ds) {
160         return bounceImage(ds);
161     }
162
163     //TODO: See bugzilla bug 15837
164
public DataHandler JavaDoc bounceImage4(boolean b, DataHandler JavaDoc 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 JavaDoc orMultiMimeParts(DataHandler JavaDoc dh) {
176         return dh.getContentType();
177     }
178
179     public String JavaDoc andMultiMimeParts(DataHandler JavaDoc dh1, DataHandler JavaDoc dh2) {
180         try {
181             InputStream JavaDoc is1 = dh1.getInputStream();
182             byte[] bBuff1 = new byte[is1.available()];
183             is1.read(bBuff1);
184             StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc(new String JavaDoc(bBuff1));
185             
186             InputStream JavaDoc is2 = dh2.getInputStream();
187             byte[] bBuff2 = new byte[is2.available()];
188             is2.read(bBuff2);
189             sBuff.append(new String JavaDoc(bBuff2));
190             
191             return sBuff.toString();
192         } catch (IOException JavaDoc 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 JavaDoc noContent(String JavaDoc s) {
203         return s;
204     }
205
206     public String JavaDoc typeStar(DataHandler JavaDoc dh) {
207         return dataHandlerToString(dh);
208     }
209
210     public String JavaDoc soapBodyParts1(boolean shouldBounce, DataHandler JavaDoc dh) {
211         return "1";
212     }
213
214     public String JavaDoc soapBodyParts2(DataHandler JavaDoc dh) {
215         return "2";
216     }
217
218     public String JavaDoc soapBodyParts3(boolean shouldBounce) {
219         return "3";
220     }
221
222     public String JavaDoc soapBodyParts4() {
223         return "4";
224     }
225
226     public String JavaDoc arrayOfBinary(DataHandler JavaDoc dh) {
227         return dataHandlerToString(dh);
228     }
229
230     public DataHandler JavaDoc optionalSoapBody(DataHandler JavaDoc ds) {
231         return bounceImage(ds);
232     }
233
234     public String JavaDoc mixMimeParts(
235         String JavaDoc pos1,
236         DataHandler JavaDoc dh1,
237         String JavaDoc pos2,
238         DataHandler JavaDoc dh2,
239         String JavaDoc pos3)
240     {
241         return pos1
242             + dataHandlerToString(dh1)
243             + pos2
244             + dataHandlerToString(dh2)
245             + pos3;
246     }
247
248     /* ******************* ERRORS *********************** */
249     
250     public String JavaDoc badNoPart(DataHandler JavaDoc dh) {
251         return dataHandlerToString(dh);
252     }
253
254     public String JavaDoc badPart(DataHandler JavaDoc dh) {
255         return dataHandlerToString(dh);
256     }
257     
258     public String JavaDoc badNested(DataHandler JavaDoc dh) {
259         return dataHandlerToString(dh);
260     }
261
262     public String JavaDoc badMixSoapMime(DataHandler JavaDoc dh) {
263         return dataHandlerToString(dh);
264     }
265
266     public String JavaDoc badMultipleSoapBodies(DataHandler JavaDoc dh) {
267         return dataHandlerToString(dh);
268     }
269
270     public String JavaDoc badSoapBodyType(DataHandler JavaDoc dh) {
271         return dataHandlerToString(dh);
272     }
273
274     /* ***************** UTILITY METHODS *************** */
275     
276     private FileDataSource JavaDoc getTempFile() throws IOException JavaDoc {
277         File JavaDoc f = File.createTempFile("WSIFMimeTest", "txt");
278         f.deleteOnExit();
279         return new FileDataSource JavaDoc(f);
280     }
281 }
282
Popular Tags