KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > encoding > soapenc > MimePartSerializer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 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 "SOAP" 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) 2000, 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.soap.encoding.soapenc;
59
60 import java.beans.*;
61 import java.io.*;
62 import java.util.*;
63 import java.lang.reflect.*;
64 import org.w3c.dom.*;
65 import org.apache.soap.util.*;
66 import org.apache.soap.util.xml.*;
67 import org.apache.soap.util.mime.*;
68 import org.apache.soap.*;
69 import org.apache.soap.rpc.*;
70 import javax.activation.*;
71 import javax.mail.*;
72 import javax.mail.internet.*;
73
74 /**
75  * A <code>MimePartSerializer</code> can be used to serialize
76  * Java's InputStream, JavaMail's MimeBodyPart and
77  * Java Activation Framework's DataSource and DataHandler objects
78  * from/to multipart Mime attachments to the SOAP message.<p>
79  * Inside the SOAP message body, the reference looks like:<br>
80  * <code>&lt;elemName HREF="cid:foo"&gt;</code><br>
81  * where "foo" is the URLEncoded name of the Content-ID of the mime part,
82  * or alternatively, an absolute or relative URI referring to the
83  * Content-Location of the part.<p>
84  * The class always deserializes to a DataHandler, which provides
85  * an InputStream, a DataSource with a Content-Type, the content
86  * as an object, and allows to write the data to an OutputStream.
87  *
88  * @author Wouter Cloetens (wcloeten@raleigh.ibm.com)
89  */

90 public class MimePartSerializer implements Serializer, Deserializer {
91     public void marshall(String JavaDoc inScopeEncStyle, Class JavaDoc javaType, Object JavaDoc src,
92                        Object JavaDoc context, Writer sink, NSStack nsStack,
93                        XMLJavaMappingRegistry xjmr, SOAPContext ctx)
94         throws IllegalArgumentException JavaDoc, IOException {
95         nsStack.pushScope();
96
97         if ((src != null) &&
98             !(src instanceof InputStream) &&
99             !(src instanceof DataSource) &&
100             !(src instanceof MimeBodyPart) &&
101             !(src instanceof DataHandler))
102             throw new IllegalArgumentException JavaDoc("Tried to pass a '" +
103                  src.getClass().toString() + "' to MimePartSerializer");
104
105         if (src == null) {
106             SoapEncUtils.generateNullStructure(inScopeEncStyle, Object JavaDoc.class,
107                                                null, sink, nsStack, xjmr);
108         } else {
109             // get a MimeBodyPart out of the various possible input types
110
DataSource ds = null;
111             DataHandler dh = null;
112             MimeBodyPart bp = null;
113             if (src instanceof InputStream)
114                 ds = new ByteArrayDataSource((InputStream)src,
115                                              "application/octet-stream");
116             else if (src instanceof DataSource)
117                 ds = (DataSource)src;
118             if (ds != null)
119                 dh = new DataHandler(ds);
120             else if (src instanceof DataHandler)
121                 dh = (DataHandler)src;
122             if (dh != null) {
123                 bp = new MimeBodyPart();
124                 try {
125                     bp.setDataHandler(dh);
126                 } catch(MessagingException me) {
127                     throw new IllegalArgumentException JavaDoc(
128                         "Invalid InputStream/DataSource/DataHandler: " + me);
129                 }
130             } else if (src instanceof MimeBodyPart) {
131                 bp = (MimeBodyPart)src;
132             }
133             // by now we must logically have a valid MimeBodyPart
134

135             // set a unique content-ID
136
String JavaDoc cid = null;
137             try {
138                 cid = bp.getContentID();
139             } catch(MessagingException me) {
140             }
141             if (cid == null) {
142             cid = MimeUtils.getUniqueValue();
143             try {
144                 bp.setHeader(Constants.HEADER_CONTENT_ID, '<' + cid + '>');
145             } catch(MessagingException me) {
146                throw new IllegalArgumentException JavaDoc("Could not set Content-ID: "
147                                                   + me);
148             }
149             }
150
151             // add the part to the context
152
try {
153                 ctx.addBodyPart(bp);
154             } catch(MessagingException me) {
155                throw new IllegalArgumentException JavaDoc("Could not add attachment: "
156                                                   + me);
157             }
158
159             // Now write the XML element.
160
sink.write('<' + context.toString());
161
162             // Write the URLEncoded reference.
163
sink.write(' ' + Constants.ATTR_REFERENCE + " =\"cid:"
164                        + java.net.URLEncoder.encode(cid) + '"');
165
166             sink.write("/>");
167         }
168
169         nsStack.popScope();
170     }
171
172     public Bean unmarshall(String JavaDoc inScopeEncStyle, QName elementType,
173                            Node src, XMLJavaMappingRegistry xjmr,
174                            SOAPContext ctx)
175         throws IllegalArgumentException JavaDoc {
176
177         Element paramEl = (Element)src;
178
179         DataHandler dh = null;
180         if (!SoapEncUtils.isNull(paramEl)) {
181             String JavaDoc uri = paramEl.getAttribute(Constants.ATTR_REFERENCE);
182
183             try {
184                 MimeBodyPart bp = null;
185                 try {
186                     bp = ctx.findBodyPart(uri);
187                 } catch(NullPointerException JavaDoc npe) {
188                 } catch(ClassCastException JavaDoc cce) {
189                 }
190                 if (bp == null) {
191                     throw new IllegalArgumentException JavaDoc(
192                       "Attachment tag \"" + paramEl.getTagName()
193                       + "\" refers to a Mime attachment with label \""
194                       + uri + "\" which could not be found.");
195                 } else
196                     dh = bp.getDataHandler();
197             } catch(MessagingException me) {
198                 throw new IllegalArgumentException JavaDoc(
199                     "Failed to read attachment for tag \""
200                     + paramEl.getTagName()
201                     + "\" with label \"" + uri + "\": " + me);
202             }
203         }
204         return new Bean(javax.activation.DataHandler JavaDoc.class, dh);
205     }
206 }
207
Popular Tags