KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > dsn > MultipartReport


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)MultipartReport.java 1.5 06/03/09
24  *
25  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.dsn;
29
30 import java.io.*;
31 import java.util.Vector JavaDoc;
32
33 import javax.activation.*;
34 import javax.mail.*;
35 import javax.mail.internet.*;
36
37 /**
38  * A multipart/report message content, as defined in
39  * <A HREF="http://www.ietf.org/rfc/rfc3462.txt">RFC 3462</A>.
40  * A multipart/report content is a container for mail reports
41  * of any kind, and is most often used to return a delivery
42  * status report. This class only supports that most common
43  * usage. <p>
44  *
45  * A MultipartReport object is a special type of MimeMultipart
46  * object with a restricted set of body parts. A MultipartReport
47  * object contains:
48  * <ul>
49  * <li>[Required] A human readable text message describing the
50  * reason the report was generated.</li>
51  * <li>[Required] A {@link DeliveryStatus} object containing the
52  * details for why the report was generated.</li>
53  * <li>[Optional] A returned copy of the entire message, or just
54  * its headers, which caused the generation of this report.
55  * </ul>
56  * Many of the normal MimeMultipart operations are restricted to
57  * ensure that the MultipartReport object always follows this
58  * structure.
59  */

60 public class MultipartReport extends MimeMultipart {
61     protected boolean constructed; // true when done with constructor
62

63     /**
64      * Construct a multipart/report object with no content.
65      */

66     public MultipartReport() throws MessagingException {
67     super("report");
68     // always at least two body parts
69
MimeBodyPart mbp = new MimeBodyPart();
70     setBodyPart(mbp, 0);
71     mbp = new MimeBodyPart();
72     setBodyPart(mbp, 1);
73     constructed = true;
74     }
75
76     /**
77      * Construct a multipart/report object with the specified plain
78      * text and delivery status to be returned to the user.
79      */

80     public MultipartReport(String JavaDoc text, DeliveryStatus status)
81                 throws MessagingException {
82     super("report");
83     ContentType ct = new ContentType(contentType);
84     ct.setParameter("report-type", "delivery-status");
85     contentType = ct.toString();
86     MimeBodyPart mbp = new MimeBodyPart();
87     mbp.setText(text);
88     setBodyPart(mbp, 0);
89     mbp = new MimeBodyPart();
90     mbp.setContent(status, "message/delivery-status");
91     setBodyPart(mbp, 1);
92     constructed = true;
93     }
94
95     /**
96      * Construct a multipart/report object with the specified plain
97      * text, delivery status, and original message to be returned to the user.
98      */

99     public MultipartReport(String JavaDoc text, DeliveryStatus status,
100                 MimeMessage msg) throws MessagingException {
101     this(text, status);
102     if (msg != null) {
103         MimeBodyPart mbp = new MimeBodyPart();
104         mbp.setContent(msg, "message/rfc822");
105         setBodyPart(mbp, 2);
106     }
107     }
108
109     /**
110      * Construct a multipart/report object with the specified plain
111      * text, delivery status, and headers from the original message
112      * to be returned to the user.
113      */

114     public MultipartReport(String JavaDoc text, DeliveryStatus status,
115                 InternetHeaders hdr) throws MessagingException {
116     this(text, status);
117     if (hdr != null) {
118         MimeBodyPart mbp = new MimeBodyPart();
119         mbp.setContent(new MessageHeaders(hdr), "text/rfc822-headers");
120         setBodyPart(mbp, 2);
121     }
122     }
123
124     /**
125      * Constructs a MultipartReport object and its bodyparts from the
126      * given DataSource. <p>
127      *
128      * @param ds DataSource, can be a MultipartDataSource
129      */

130     public MultipartReport(DataSource ds) throws MessagingException {
131     super(ds);
132     parse();
133     constructed = true;
134     /*
135      * Can't fail to construct object because some programs just
136      * want to treat this as a Multipart and examine the parts.
137      *
138     if (getCount() < 2 || getCount() > 3) // XXX allow extra parts
139         throw new MessagingException(
140         "Wrong number of parts in multipart/report: " + getCount());
141      */

142     }
143
144     /**
145      * Get the plain text to be presented to the user, if there is any.
146      * Rarely, the message may contain only HTML text, or no text at
147      * all. If the text body part of this multipart/report object is
148      * of type text/plain, or if it is of type multipart/alternative
149      * and contains a text/plain part, the text from that part is
150      * returned. Otherwise, null is return and the {@link #getTextBodyPart
151      * getTextBodyPart} method may be used to extract the data.
152      */

153     public synchronized String JavaDoc getText() throws MessagingException {
154     try {
155         BodyPart bp = getBodyPart(0);
156         if (bp.isMimeType("text/plain"))
157         return (String JavaDoc)bp.getContent();
158         if (bp.isMimeType("multipart/alternative")) {
159         Multipart mp = (Multipart)bp.getContent();
160         for (int i = 0; i < mp.getCount(); i++) {
161             bp = mp.getBodyPart(i);
162             if (bp.isMimeType("text/plain"))
163             return (String JavaDoc)bp.getContent();
164         }
165         }
166     } catch (IOException ex) {
167         throw new MessagingException("Exception getting text content", ex);
168     }
169     return null;
170     }
171
172     /**
173      * Set the message to be presented to the user as just a text/plain
174      * part containing the specified text.
175      */

176     public synchronized void setText(String JavaDoc text) throws MessagingException {
177     MimeBodyPart mbp = new MimeBodyPart();
178     mbp.setText(text);
179     setBodyPart(mbp, 0);
180     }
181
182     /**
183      * Return the body part containing the message to be presented to
184      * the user, usually just a text/plain part.
185      */

186     public synchronized MimeBodyPart getTextBodyPart()
187                 throws MessagingException {
188     return (MimeBodyPart)getBodyPart(0);
189     }
190
191     /**
192      * Set the body part containing the text to be presented to the
193      * user. Usually this a text/plain part, but it might also be
194      * a text/html part or a multipart/alternative part containing
195      * text/plain and text/html parts. Any type is allowed here
196      * but these types are most common.
197      */

198     public synchronized void setTextBodyPart(MimeBodyPart mbp)
199                 throws MessagingException {
200     setBodyPart(mbp, 0);
201     }
202
203     /**
204      * Get the delivery status associated with this multipart/report.
205      */

206     public synchronized DeliveryStatus getDeliveryStatus()
207                 throws MessagingException {
208     if (getCount() < 2)
209         return null;
210     BodyPart bp = getBodyPart(1);
211     if (!bp.isMimeType("message/delivery-status"))
212         return null;
213     try {
214         return (DeliveryStatus)bp.getContent();
215     } catch (IOException ex) {
216         throw new MessagingException("IOException getting DeliveryStatus",
217                     ex);
218     }
219     }
220
221     /**
222      * Set the delivery status associated with this multipart/report.
223      */

224     public synchronized void setDeliveryStatus(DeliveryStatus status)
225                 throws MessagingException {
226     MimeBodyPart mbp = new MimeBodyPart();
227     mbp.setContent(status, "message/delivery-status");
228     setBodyPart(mbp, 2);
229     ContentType ct = new ContentType(contentType);
230     ct.setParameter("report-type", "delivery-status");
231     contentType = ct.toString();
232     }
233
234     /**
235      * Get the original message that is being returned along with this
236      * multipart/report. If no original message is included, null is
237      * returned. In some cases only the headers of the original
238      * message will be returned as an object of type MessageHeaders.
239      */

240     public synchronized MimeMessage getReturnedMessage()
241                 throws MessagingException {
242     if (getCount() < 3)
243         return null;
244     BodyPart bp = getBodyPart(2);
245     if (!bp.isMimeType("message/rfc822") &&
246         !bp.isMimeType("text/rfc822-headers"))
247         return null;
248     try {
249         return (MimeMessage)bp.getContent();
250     } catch (IOException ex) {
251         throw new MessagingException("IOException getting ReturnedMessage",
252                     ex);
253     }
254     }
255
256     /**
257      * Set the original message to be returned as part of the
258      * multipart/report. If msg is null, any previously set
259      * returned message or headers is removed.
260      */

261     public synchronized void setReturnedMessage(MimeMessage msg)
262                 throws MessagingException {
263     if (msg == null) {
264         BodyPart part = (BodyPart)parts.elementAt(2);
265         super.removeBodyPart(2);
266         return;
267     }
268     MimeBodyPart mbp = new MimeBodyPart();
269     if (msg instanceof MessageHeaders)
270         mbp.setContent(msg, "text/rfc822-headers");
271     else
272         mbp.setContent(msg, "message/rfc822");
273     setBodyPart(mbp, 2);
274     }
275
276     private synchronized void setBodyPart(BodyPart part, int index)
277                 throws MessagingException {
278     if (parts == null) // XXX - can never happen?
279
parts = new Vector JavaDoc();
280
281     super.removeBodyPart(index);
282     super.addBodyPart(part, index);
283     }
284
285
286     // Override Multipart methods to preserve integrity of multipart/report.
287

288     /**
289      * Set the subtype. Throws MessagingException.
290      *
291      * @param subtype Subtype
292      * @exception MessagingException always; can't change subtype
293      */

294     public synchronized void setSubType(String JavaDoc subtype)
295             throws MessagingException {
296     throw new MessagingException("Can't change subtype of MultipartReport");
297     }
298
299     /**
300      * Remove the specified part from the multipart message.
301      * Not allowed on a multipart/report object.
302      *
303      * @param part The part to remove
304      * @exception MessagingException always
305      */

306     public boolean removeBodyPart(BodyPart part) throws MessagingException {
307     throw new MessagingException(
308         "Can't remove body parts from multipart/report");
309     }
310
311     /**
312      * Remove the part at specified location (starting from 0).
313      * Not allowed on a multipart/report object.
314      *
315      * @param index Index of the part to remove
316      * @exception MessagingException always
317      */

318     public void removeBodyPart(int index) throws MessagingException {
319     throw new MessagingException(
320         "Can't remove body parts from multipart/report");
321     }
322
323     /**
324      * Adds a Part to the multipart.
325      * Not allowed on a multipart/report object.
326      *
327      * @param part The Part to be appended
328      * @exception MessagingException always
329      */

330     public synchronized void addBodyPart(BodyPart part)
331         throws MessagingException {
332     // Once constructor is done, don't allow this anymore.
333
if (!constructed)
334         super.addBodyPart(part);
335     else
336         throw new MessagingException(
337         "Can't add body parts to multipart/report 1");
338     }
339
340     /**
341      * Adds a BodyPart at position <code>index</code>.
342      * Not allowed on a multipart/report object.
343      *
344      * @param part The BodyPart to be inserted
345      * @param index Location where to insert the part
346      * @exception MessagingException always
347      */

348     public synchronized void addBodyPart(BodyPart part, int index)
349                 throws MessagingException {
350     throw new MessagingException(
351         "Can't add body parts to multipart/report 2");
352     }
353 }
354
Popular Tags