1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.mail.internet;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.StringTokenizer;
32
33 import org.apache.geronimo.mail.util.ASCIIUtil;
34 import org.apache.geronimo.mail.util.RFC2231Encoder;
35 import org.apache.geronimo.mail.util.SessionUtil;
36
37
38
39
40
41
42
43
44 public class ParameterList {
45 private static final String MIME_ENCODEPARAMETERS = "mail.mime.encodeparameters";
46 private static final String MIME_DECODEPARAMETERS = "mail.mime.decodeparameters";
47 private static final String MIME_DECODEPARAMETERS_STRICT = "mail.mime.decodeparameters.strict";
48
49 private static final int HEADER_SIZE_LIMIT = 76;
50
51 private Map _parameters = new HashMap();
52
53 private boolean encodeParameters = false;
54 private boolean decodeParameters = false;
55 private boolean decodeParametersStrict = false;
56
57 public ParameterList() {
58
59 getInitialProperties();
60 }
61
62 public ParameterList(String list) throws ParseException {
63
64 getInitialProperties();
65
66 HeaderTokenizer tokenizer = new HeaderTokenizer(list, HeaderTokenizer.MIME);
67 while (true) {
68 HeaderTokenizer.Token token = tokenizer.next();
69
70 switch (token.getType()) {
71
72 case HeaderTokenizer.Token.EOF:
73 return;
74
75
76
77 case ';':
78
79 token = tokenizer.next();
80
81 if (token.getType() == HeaderTokenizer.Token.EOF) {
82 return;
83 }
84
85 if (token.getType() != HeaderTokenizer.Token.ATOM) {
86 throw new ParseException("Invalid parameter name: " + token.getValue());
87 }
88
89
90 String name = token.getValue().toLowerCase();
91
92 token = tokenizer.next();
93
94
95 if (token.getType() != '=') {
96 throw new ParseException("Missing '='");
97 }
98
99
100 token = tokenizer.next();
101
102 if (token.getType() != HeaderTokenizer.Token.ATOM && token.getType() != HeaderTokenizer.Token.QUOTEDSTRING) {
103 throw new ParseException("Invalid parameter value: " + token.getValue());
104 }
105
106 String value = token.getValue();
107 String decodedValue = null;
108
109
110
111 if (decodeParameters && name.endsWith("*")) {
112
113 name = name.substring(0, name.length() - 1);
114
115 RFC2231Encoder decoder = new RFC2231Encoder(HeaderTokenizer.MIME);
116
117 try {
118
119 decodedValue = decoder.decode(value);
120 } catch (Exception e) {
121
122
123 if (decodeParametersStrict) {
124 throw new ParseException("Invalid RFC2231 encoded parameter");
125 }
126 }
127 _parameters.put(name, new ParameterValue(name, decodedValue, value));
128 }
129 else {
130 _parameters.put(name, new ParameterValue(name, value));
131 }
132
133 break;
134
135 default:
136 throw new ParseException("Missing ';'");
137
138 }
139 }
140 }
141
142
143
144
145
146 private void getInitialProperties() {
147 decodeParameters = SessionUtil.getBooleanProperty(MIME_DECODEPARAMETERS, false);
148 decodeParametersStrict = SessionUtil.getBooleanProperty(MIME_DECODEPARAMETERS_STRICT, false);
149 encodeParameters = SessionUtil.getBooleanProperty(MIME_ENCODEPARAMETERS, true);
150 }
151
152 public int size() {
153 return _parameters.size();
154 }
155
156 public String get(String name) {
157 ParameterValue value = (ParameterValue)_parameters.get(name.toLowerCase());
158 if (value != null) {
159 return value.value;
160 }
161 return null;
162 }
163
164 public void set(String name, String value) {
165 name = name.toLowerCase();
166 _parameters.put(name, new ParameterValue(name, value));
167 }
168
169 public void set(String name, String value, String charset) {
170 name = name.toLowerCase();
171
172 if (encodeParameters && !ASCIIUtil.isAscii(value)) {
173 ByteArrayOutputStream out = new ByteArrayOutputStream();
174
175 try {
176 RFC2231Encoder encoder = new RFC2231Encoder(HeaderTokenizer.MIME);
177
178
179 byte[] valueBytes = value.getBytes(MimeUtility.javaCharset(charset));
180
181
182 out.write(charset.getBytes());
183 out.write('\'');
184 out.write('\'');
185 encoder.encode(valueBytes, 0, valueBytes.length, out);
186
187
188 _parameters.put(name, new ParameterValue(name, value, new String(out.toByteArray())));
189 return;
190
191 } catch (Exception e) {
192
193 }
194 }
195
196 _parameters.put(name, new ParameterValue(name, value));
197 }
198
199 public void remove(String name) {
200 _parameters.remove(name);
201 }
202
203 public Enumeration getNames() {
204 return Collections.enumeration(_parameters.keySet());
205 }
206
207 public String toString() {
208
209 return toString(0);
210 }
211
212 public String toString(int used) {
213 StringBuffer stringValue = new StringBuffer();
214
215 Iterator values = _parameters.values().iterator();
216
217 while (values.hasNext()) {
218 ParameterValue parm = (ParameterValue)values.next();
219
220 String name = parm.getEncodedName();
221 String value = parm.toString();
222
223
224 stringValue.append("; ");
225 used += 2;
226
227
228 if ((used + name.length() + value.length() + 1) > HEADER_SIZE_LIMIT) {
229
230 stringValue.append("\r\n\t");
231
232
233 used = 8;
234 }
235
236 stringValue.append(name);
237 stringValue.append("=");
238
239 used += name.length() + 1;
240
241
242
243 if (used + value.length() > HEADER_SIZE_LIMIT) {
244 String foldedValue = MimeUtility.fold(used, value);
245
246 stringValue.append(foldedValue);
247
248
249 int lastLineBreak = foldedValue.lastIndexOf('\n');
250
251 if (lastLineBreak != -1) {
252 used = foldedValue.length() - lastLineBreak + 1;
253 }
254 else {
255 used += foldedValue.length();
256 }
257 }
258 else {
259
260 stringValue.append(value);
261 used += value.length();
262 }
263 }
264
265 return stringValue.toString();
266 }
267
268
269
270
271
272 class ParameterValue {
273 public String name;
274 public String value;
275 public String encodedValue;
276
277 public ParameterValue(String name, String value) {
278 this.name = name;
279 this.value = value;
280 this.encodedValue = null;
281 }
282
283 public ParameterValue(String name, String value, String encodedValue) {
284 this.name = name;
285 this.value = value;
286 this.encodedValue = encodedValue;
287 }
288
289 public String toString() {
290 if (encodedValue != null) {
291 return MimeUtility.quote(encodedValue, HeaderTokenizer.MIME);
292 }
293 return MimeUtility.quote(value, HeaderTokenizer.MIME);
294 }
295
296 public String getEncodedName() {
297 if (encodedValue != null) {
298 return name + "*";
299 }
300 return name;
301 }
302 }
303 }