KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > protocol > ENVELOPE


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  * @(#)ENVELOPE.java 1.17 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.imap.protocol;
29
30 import java.util.Vector JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.io.UnsupportedEncodingException JavaDoc;
33 import java.text.ParseException JavaDoc;
34 import javax.mail.internet.InternetAddress JavaDoc;
35 import javax.mail.internet.AddressException JavaDoc;
36 import javax.mail.internet.MailDateFormat JavaDoc;
37 import javax.mail.internet.MimeUtility JavaDoc;
38 import com.sun.mail.iap.*;
39
40 /**
41  * The ENEVELOPE item of an IMAP FETCH response.
42  *
43  * @author John Mani
44  * @author Bill Shannon
45  */

46
47 public class ENVELOPE implements Item {
48     
49     // IMAP item name
50
public static char[] name = {'E','N','V','E','L','O','P','E'};
51     public int msgno;
52
53     public Date JavaDoc date = null;
54     public String JavaDoc subject;
55     public InternetAddress JavaDoc[] from;
56     public InternetAddress JavaDoc[] sender;
57     public InternetAddress JavaDoc[] replyTo;
58     public InternetAddress JavaDoc[] to;
59     public InternetAddress JavaDoc[] cc;
60     public InternetAddress JavaDoc[] bcc;
61     public String JavaDoc inReplyTo;
62     public String JavaDoc messageId;
63
64     // Used to parse dates
65
private static MailDateFormat JavaDoc mailDateFormat = new MailDateFormat JavaDoc();
66     
67     public ENVELOPE(FetchResponse r) throws ParsingException {
68     msgno = r.getNumber();
69
70     r.skipSpaces();
71
72     if (r.readByte() != '(')
73         throw new ParsingException("ENVELOPE parse error");
74     
75     String JavaDoc s = r.readString();
76     if (s != null) {
77         try {
78         date = mailDateFormat.parse(s);
79         } catch (Exception JavaDoc pex) {
80         // We need to be *very* tolerant about bogus dates (and
81
// there's lot of 'em around), so we ignore any
82
// exception (including RunTimeExceptions) and just let
83
// date be null.
84
}
85     }
86
87     subject = r.readString();
88     from = parseAddressList(r);
89     sender = parseAddressList(r);
90     replyTo = parseAddressList(r);
91     to = parseAddressList(r);
92     cc = parseAddressList(r);
93     bcc = parseAddressList(r);
94     inReplyTo = r.readString();
95     messageId = r.readString();
96
97     if (r.readByte() != ')')
98         throw new ParsingException("ENVELOPE parse error");
99     }
100
101     private InternetAddress JavaDoc[] parseAddressList(Response r)
102         throws ParsingException {
103     r.skipSpaces(); // skip leading spaces
104

105     byte b = r.readByte();
106     if (b == '(') {
107         Vector JavaDoc v = new Vector JavaDoc();
108
109         do {
110         IMAPAddress a = new IMAPAddress(r);
111         // if we see an end-of-group address at the top, ignore it
112
if (!a.isEndOfGroup())
113             v.addElement(a);
114         } while (r.peekByte() != ')');
115
116         // skip the terminating ')' at the end of the addresslist
117
r.skip(1);
118
119         InternetAddress JavaDoc[] a = new InternetAddress JavaDoc[v.size()];
120         v.copyInto(a);
121         return a;
122     } else if (b == 'N' || b == 'n') { // NIL
123
r.skip(2); // skip 'NIL'
124
return null;
125     } else
126         throw new ParsingException("ADDRESS parse error");
127     }
128 }
129
130 class IMAPAddress extends InternetAddress JavaDoc {
131     private boolean group = false;
132     private InternetAddress JavaDoc[] grouplist;
133     private String JavaDoc groupname;
134
135     private static final long serialVersionUID = -3835822029483122232L;
136
137     IMAPAddress(Response r) throws ParsingException {
138         r.skipSpaces(); // skip leading spaces
139

140         if (r.readByte() != '(')
141             throw new ParsingException("ADDRESS parse error");
142
143         encodedPersonal = r.readString();
144
145         r.readString(); // throw away address_list
146
String JavaDoc mb = r.readString();
147     String JavaDoc host = r.readString();
148         if (r.readByte() != ')') // skip past terminating ')'
149
throw new ParsingException("ADDRESS parse error");
150
151     if (host == null) {
152         // it's a group list, start or end
153
group = true;
154         groupname = mb;
155         if (groupname == null) // end of group list
156
return;
157         // Accumulate a group list. The members of the group
158
// are accumulated in a Vector and the corresponding string
159
// representation of the group is accumulated in a StringBuffer.
160
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
161         sb.append(groupname).append(':');
162         Vector JavaDoc v = new Vector JavaDoc();
163         while (r.peekByte() != ')') {
164         IMAPAddress a = new IMAPAddress(r);
165         if (a.isEndOfGroup()) // reached end of group
166
break;
167         if (v.size() != 0) // if not first element, need a comma
168
sb.append(',');
169         sb.append(a.toString());
170         v.addElement(a);
171         }
172         sb.append(';');
173         address = sb.toString();
174         grouplist = new IMAPAddress[v.size()];
175         v.copyInto(grouplist);
176     } else {
177         if (mb == null || mb.length() == 0)
178         address = host;
179         else if (host == null || host.length() == 0)
180         address = mb;
181         else
182         address = mb + "@" + host;
183     }
184
185     }
186
187     boolean isEndOfGroup() {
188     return group && groupname == null;
189     }
190
191     public boolean isGroup() {
192     return group;
193     }
194
195     public InternetAddress JavaDoc[] getGroup(boolean strict) throws AddressException JavaDoc {
196     if (grouplist == null)
197         return null;
198     return (InternetAddress JavaDoc[])grouplist.clone();
199     }
200 }
201
Popular Tags