KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)DeliveryStatus.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.*;
32
33 import javax.activation.*;
34 import javax.mail.*;
35 import javax.mail.internet.*;
36
37 import com.sun.mail.util.LineOutputStream; // XXX
38

39 /**
40  * A message/delivery-status message content, as defined in
41  * <A HREF="http://www.ietf.org/rfc/rfc3464.txt">RFC 3464</A>.
42  */

43 public class DeliveryStatus {
44
45     private static boolean debug = false;
46
47     static {
48     try {
49         String JavaDoc s = System.getProperty("mail.dsn.debug");
50         // default to false
51
debug = s != null && !s.equalsIgnoreCase("false");
52     } catch (SecurityException JavaDoc sex) {
53         // ignore it
54
}
55     }
56
57     /**
58      * The DSN fields for the message.
59      */

60     protected InternetHeaders messageDSN;
61
62     /**
63      * The DSN fields for each recipient.
64      */

65     protected InternetHeaders[] recipientDSN;
66
67     /**
68      * Construct a delivery status notification with no content.
69      */

70     public DeliveryStatus() throws MessagingException {
71     messageDSN = new InternetHeaders();
72     recipientDSN = new InternetHeaders[0];
73     }
74
75     /**
76      * Construct a delivery status notification by parsing the
77      * supplied input stream.
78      */

79     public DeliveryStatus(InputStream is)
80                 throws MessagingException, IOException {
81     messageDSN = new InternetHeaders(is);
82     if (debug)
83         System.out.println("DSN: got messageDSN");
84     Vector v = new Vector();
85     try {
86         while (is.available() > 0) {
87         InternetHeaders h = new InternetHeaders(is);
88         if (debug)
89             System.out.println("DSN: got recipientDSN");
90         v.addElement(h);
91         }
92     } catch (EOFException ex) {
93         if (debug)
94         System.out.println("DSN: got EOFException");
95     }
96     if (debug)
97         System.out.println("DSN: recipientDSN size " + v.size());
98     recipientDSN = new InternetHeaders[v.size()];
99     v.copyInto(recipientDSN);
100     }
101
102     /**
103      * Return all the per-message fields in the delivery status notification.
104      * The fields are defined as:
105      *
106      * <pre>
107      * per-message-fields =
108      * [ original-envelope-id-field CRLF ]
109      * reporting-mta-field CRLF
110      * [ dsn-gateway-field CRLF ]
111      * [ received-from-mta-field CRLF ]
112      * [ arrival-date-field CRLF ]
113      * *( extension-field CRLF )
114      * </pre>
115      */

116     // XXX - could parse each of these fields
117
public InternetHeaders getMessageDSN() {
118     return messageDSN;
119     }
120
121     /**
122      * Set the per-message fields in the delivery status notification.
123      */

124     public void setMessageDSN(InternetHeaders messageDSN) {
125     this.messageDSN = messageDSN;
126     }
127
128     /**
129      * Return the number of recipients for which we have
130      * per-recipient delivery status notification information.
131      */

132     public int getRecipientDSNCount() {
133     return recipientDSN.length;
134     }
135
136     /**
137      * Return the delivery status notification information for
138      * the specified recipient.
139      */

140     public InternetHeaders getRecipientDSN(int n) {
141     return recipientDSN[n];
142     }
143
144     /**
145      * Add deliver status notification information for another
146      * recipient.
147      */

148     public void addRecipientDSN(InternetHeaders h) {
149     InternetHeaders[] rh = new InternetHeaders[recipientDSN.length + 1];
150     System.arraycopy(recipientDSN, 0, rh, 0, recipientDSN.length);
151     recipientDSN = rh;
152     recipientDSN[recipientDSN.length - 1] = h;
153     }
154
155     public void writeTo(OutputStream os)
156                 throws IOException, MessagingException {
157     // see if we already have a LOS
158
LineOutputStream los = null;
159     if (os instanceof LineOutputStream) {
160         los = (LineOutputStream) os;
161     } else {
162         los = new LineOutputStream(os);
163     }
164
165     writeInternetHeaders(messageDSN, los);
166     los.writeln();
167     for (int i = 0; i < recipientDSN.length; i++) {
168         writeInternetHeaders(recipientDSN[i], los);
169         los.writeln();
170     }
171     }
172
173     private static void writeInternetHeaders(InternetHeaders h,
174                 LineOutputStream los) throws IOException {
175     Enumeration e = h.getAllHeaderLines();
176     try {
177         while (e.hasMoreElements())
178         los.writeln((String JavaDoc)e.nextElement());
179     } catch (MessagingException mex) {
180         Exception JavaDoc ex = mex.getNextException();
181         if (ex instanceof IOException)
182         throw (IOException)ex;
183         else
184         throw new IOException("Exception writing headers: " + mex);
185     }
186     }
187
188     public String JavaDoc toString() {
189     return "DeliveryStatus: Reporting-MTA=" +
190         messageDSN.getHeader("Reporting-MTA", null) + ", #Recipients=" +
191         recipientDSN.length;
192     }
193 }
194
Popular Tags