1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.geronimo.javamail.store.imap.connection;
19
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.mail.MessagingException;
27 import javax.mail.internet.ContentDisposition;
28 import javax.mail.internet.ContentType;
29
30
31 public class IMAPBodyStructure extends IMAPFetchDataItem {
32
33
34 public ContentType mimeType = new ContentType();
35
36 public ContentDisposition disposition = null;
37
38 public String contentID;
39 public String contentDescription;
40 public String transferEncoding;
41
42 public int bodySize;
43
44 public int lines = -1;
45
46
47 public IMAPBodyStructure[] parts;
48
49 public Map dispositionParameters;
50
51 public List languages;
52
53 public String md5Hash;
54
55
56 public IMAPEnvelope nestedEnvelope;
57 public IMAPBodyStructure nestedBody;
58
59
60 public IMAPBodyStructure(IMAPResponseTokenizer source) throws MessagingException {
61 super(BODYSTRUCTURE);
62 parseBodyStructure(source);
63 }
64
65
66 protected void parseBodyStructure(IMAPResponseTokenizer source) throws MessagingException {
67
68 source.checkLeftParen();
69
70
71
72 if (source.peek().getType() == '(') {
73 parseMultipartBodyStructure(source);
74 }
75 else {
76 parseSinglepartBodyStructure(source);
77 }
78 }
79
80
81 protected void parseMultipartBodyStructure(IMAPResponseTokenizer source) throws MessagingException {
82 mimeType.setPrimaryType("multipart");
83 ArrayList partList = new ArrayList();
84
85 do {
86
87 IMAPBodyStructure part = new IMAPBodyStructure(source);
88 partList.add(part);
89
90 } while (source.peek().getType() == '(');
91
92 parts = (IMAPBodyStructure[])partList.toArray(new IMAPBodyStructure[partList.size()]);
93
94
95 mimeType.setSubType(source.readString());
96
97 if (source.checkListEnd()) {
98 return;
99 }
100
101
102 if (source.checkListEnd()) {
103 return;
104 }
105
106 mimeType.setParameterList(source.readParameterList());
107
108
109 if (source.checkListEnd()) {
110 return;
111 }
112
113
114 parseMessageExtensions(source);
115 }
116
117
118 protected void parseSinglepartBodyStructure(IMAPResponseTokenizer source) throws MessagingException {
119
120 mimeType.setPrimaryType(source.readString());
121 mimeType.setSubType(source.readString());
122
123
124 mimeType.setParameterList(source.readParameterList());
125
126
127 contentID = source.readStringOrNil();
128 contentDescription = source.readStringOrNil();
129 transferEncoding = source.readStringOrNil();
130 bodySize = source.readInteger();
131
132
133
134 if (mimeType.match("message/rfc822")) {
135
136 nestedEnvelope = new IMAPEnvelope(source);
137 nestedBody = new IMAPBodyStructure(source);
138 lines = source.readInteger();
139 }
140
141 else if (mimeType.match("text/*")) {
142 lines = source.readInteger();
143 }
144
145
146 if (source.checkListEnd()) {
147 return;
148 }
149
150 md5Hash = source.readString();
151
152
153 parseMessageExtensions(source);
154 }
155
156
157
158
159
160
161
162 protected void parseMessageExtensions(IMAPResponseTokenizer source) throws MessagingException {
163
164
165 if (source.checkListEnd()) {
166 return;
167 }
168
169 disposition = new ContentDisposition();
170
171 disposition.setDisposition(source.readString());
172 disposition.setParameterList(source.readParameterList());
173
174
175 if (source.checkListEnd()) {
176 return;
177 }
178
179 languages = source.readStringList();
180
181
182
183
184 if (source.checkListEnd()) {
185 return;
186 }
187
188 source.readStringList();
189
190
191
192 while (source.notListEnd()) {
193 source.skipExtensionItem();
194 }
195 }
196
197
198
199
200
201
202
203 public boolean isMultipart() {
204 return parts != null;
205 }
206
207
208
209
210
211
212
213
214
215 public boolean isAttachedMessage() {
216 return !isMultipart() && mimeType.match("message/rfc822");
217 }
218 }
219