KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > soap > apacheaxis > MIMEHelper


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 org.apache.wsif.providers.soap.apacheaxis;
59
60 import java.awt.Image JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.InputStream JavaDoc;
63
64 import javax.activation.DataHandler JavaDoc;
65 import javax.mail.MessagingException JavaDoc;
66 import javax.mail.internet.MimeMultipart JavaDoc;
67 import javax.swing.ImageIcon JavaDoc;
68 import javax.xml.namespace.QName JavaDoc;
69 import javax.xml.soap.SOAPException JavaDoc;
70 import javax.xml.transform.dom.DOMSource JavaDoc;
71 import javax.xml.transform.sax.SAXSource JavaDoc;
72 import javax.xml.transform.stream.StreamSource JavaDoc;
73
74 import org.apache.axis.attachments.AttachmentPart;
75 import org.apache.axis.client.Call;
76 import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
77 import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
78 import org.apache.wsif.WSIFException;
79 import org.apache.wsif.WSIFMessage;
80 import org.apache.wsif.logging.Trc;
81
82 /**
83  * The sole purpose of this class is to collect all the code that uses
84  * attachement classes outside of the WSIFOperation_ApacheAxis class
85  * so that there is not a runtime requirement on activation.jar and
86  * mail.jar unless attachements are actually being used.
87  *
88  * @author Ant Elder <ant.elder@uk.ibm.com>
89  */

90 public class MIMEHelper {
91
92     public static void registerAttachmentType(Call call, QName JavaDoc type) {
93         call.registerTypeMapping(
94             DataHandler JavaDoc.class,
95             type,
96             JAFDataHandlerSerializerFactory.class,
97             JAFDataHandlerDeserializerFactory.class);
98     }
99
100     public static boolean addAttachementIfMIMEPart(Call call, Object JavaDoc o) {
101         Trc.entry(null, call, o);
102         boolean ok;
103         
104         if (o instanceof DataHandler JavaDoc) {
105             call.addAttachmentPart(new AttachmentPart((DataHandler JavaDoc)o));
106             ok = true;
107         } else {
108             ok = false;
109         }
110                     
111         Trc.exit(new Boolean JavaDoc(ok));
112         return ok;
113     }
114
115     public static AttachmentPart getAttachementPart(Object JavaDoc o) throws WSIFException {
116         Trc.entry(null, o);
117         AttachmentPart ap = null;
118         if (o instanceof DataHandler JavaDoc) {
119             ap = new AttachmentPart((DataHandler JavaDoc) o);
120         } else {
121             throw new WSIFException("Object is not a DataHandler: " + o);
122         }
123         Trc.exit(ap);
124         return ap;
125     }
126
127     public static void setMIMEMessagePart(
128         WSIFMessage msg,
129         String JavaDoc name,
130         Object JavaDoc value,
131         Class JavaDoc type)
132         throws WSIFException {
133         Trc.entry(null, msg, name, value, type);
134
135         Class JavaDoc valueType = value == null ? null : value.getClass();
136         try {
137             if (valueType != null
138                 && DataHandler JavaDoc.class.equals(type)
139                 && AttachmentPart.class.isAssignableFrom(valueType)) {
140                 AttachmentPart ap = (AttachmentPart) value;
141                 DataHandler JavaDoc dh = ap.getDataHandler();
142                 msg.setObjectPart(name, dh);
143             } else if (
144             // Attachments that are Strings, Images, etc are unsupported at present.
145
// Attachments can only be DataHandlers.
146
valueType
147                 != null
148                     && (String JavaDoc.class.equals(type)
149                         || Image JavaDoc.class.equals(type)
150                         || StreamSource JavaDoc.class.equals(type)
151                         || DOMSource JavaDoc.class.equals(type)
152                         || SAXSource JavaDoc.class.equals(type)
153                         || MimeMultipart JavaDoc.class.equals(type))
154                     && AttachmentPart.class.isAssignableFrom(valueType)) {
155
156                 AttachmentPart ap = (AttachmentPart) value;
157                 InputStream JavaDoc is = ap.getDataHandler().getInputStream();
158
159                 if (String JavaDoc.class.equals(type)) {
160                     byte[] bBuff = new byte[is.available()];
161                     is.read(bBuff);
162                     msg.setObjectPart(name, new String JavaDoc(bBuff));
163                 } else if (Image JavaDoc.class.equals(type)) {
164                     byte[] bBuff = new byte[is.available()];
165                     is.read(bBuff);
166                     msg.setObjectPart(name, new ImageIcon JavaDoc(bBuff).getImage());
167                 } else if (StreamSource JavaDoc.class.equals(type))
168                     // Warning: this next line of code has never been tested.
169
msg.setObjectPart(name, new StreamSource JavaDoc(is));
170                 else if (DOMSource JavaDoc.class.equals(type))
171                     throw new WSIFException("DOMSource is not supported");
172                 else if (SAXSource JavaDoc.class.equals(type))
173                     throw new WSIFException("SAXSource is not supported");
174                 else if (MimeMultipart JavaDoc.class.equals(type))
175                     // Warning: this next line of code has never been tested.
176
msg.setObjectPart(
177                         name,
178                         new MimeMultipart JavaDoc(ap.getDataHandler().getDataSource()));
179             } else if (
180                 valueType != null
181                     && type != null // will be null for async responses
182
&& !type.isPrimitive()
183                     && !(type.isAssignableFrom(valueType))) {
184                 throw new WSIFException(
185                     "return value "
186                         + value
187                         + " has unexpected type "
188                         + valueType
189                         + " instead of "
190                         + type);
191             } else
192                 msg.setObjectPart(name, value);
193         } catch (SOAPException JavaDoc se) {
194             Trc.exception(se);
195             throw new WSIFException(
196                 "WSIFOperation_ApacheAxis.setMessagePart messageName="
197                     + (msg.getName() == null ? "null" : msg.getName())
198                     + " partName="
199                     + name
200                     + " caught "
201                     + se);
202         } catch (IOException JavaDoc ioe) {
203             Trc.exception(ioe);
204             throw new WSIFException(
205                 "WSIFOperation_ApacheAxis.setMessagePart messageName="
206                     + (msg.getName() == null ? "null" : msg.getName())
207                     + " partName="
208                     + name
209                     + " caught "
210                     + ioe);
211         } catch (MessagingException JavaDoc me) {
212             Trc.exception(me);
213             throw new WSIFException(
214                 "WSIFOperation_ApacheAxis.setMessagePart messageName="
215                     + (msg.getName() == null ? "null" : msg.getName())
216                     + " partName="
217                     + name
218                     + " caught "
219                     + me);
220         }
221
222         Trc.exit();
223     }
224
225 }
226
Popular Tags