KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > test > mock > javaxmail > MockMimeMessage


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20
21
22 package org.apache.james.test.mock.javaxmail;
23
24 import javax.mail.internet.MimeMessage JavaDoc;
25 import javax.mail.internet.InternetHeaders JavaDoc;
26 import javax.mail.internet.InternetAddress JavaDoc;
27 import javax.mail.*;
28 import javax.mail.search.SearchTerm JavaDoc;
29 import javax.activation.DataHandler JavaDoc;
30 import java.util.*;
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.io.UnsupportedEncodingException JavaDoc;
36
37 public class MockMimeMessage extends MimeMessage JavaDoc {
38
39     private final List m_fromAddresses = new ArrayList();
40     private Address JavaDoc m_senderAddress;
41     private final List m_toRecepients = new ArrayList();
42     private final List m_ccRecepients = new ArrayList();
43     private final List m_bccRecepients = new ArrayList();
44     private final List m_replyToAddresses = new ArrayList();
45     private String JavaDoc m_subject;
46     private int m_iMessageNumber;
47     private boolean m_bIsExpunged;
48     private Object JavaDoc m_content;
49     private Date m_sentDate;
50     private String JavaDoc[] m_contentLanguage;
51     private String JavaDoc m_fileName;
52     private DataHandler JavaDoc m_dataHandler;
53     private HashMap m_contentHeaders = new HashMap();
54     private Flags m_setFlags = new Flags();
55     private boolean m_doMatch;
56
57     public MockMimeMessage() throws MessagingException {
58         super((Session)null);
59     }
60
61     public MockMimeMessage(int messageNumber) throws MessagingException {
62         super((Session)null);
63         m_iMessageNumber = messageNumber;
64     }
65
66     public MockMimeMessage(MimeMessage JavaDoc mimeMessage) throws MessagingException {
67         super(mimeMessage); // trivial implementation
68
}
69
70     public Address JavaDoc[] getFrom() throws MessagingException {
71         return (Address JavaDoc[])m_fromAddresses.toArray();
72     }
73
74     public void setFrom(Address JavaDoc address) throws MessagingException {
75         m_fromAddresses.clear();
76         m_fromAddresses.add(address);
77     }
78
79     public void setFrom() throws MessagingException {
80         m_fromAddresses.clear();
81         m_fromAddresses.add(InternetAddress.getLocalAddress(null));
82     }
83
84     public void addFrom(Address JavaDoc[] addresses) throws MessagingException {
85         m_fromAddresses.add(addresses);
86     }
87
88     public Address JavaDoc getSender() throws MessagingException {
89         return m_senderAddress;
90     }
91
92     public void setSender(Address JavaDoc address) throws MessagingException {
93         m_senderAddress = address;
94     }
95
96     public Address JavaDoc[] getRecipients(Message.RecipientType JavaDoc recipientType) throws MessagingException {
97         List recipientsList = getRecipientsList(recipientType);
98         List recipientAddresses = new ArrayList();
99         for (Iterator iterator = recipientsList.iterator(); iterator.hasNext();) {
100             String JavaDoc recipient = (String JavaDoc) iterator.next();
101             recipientAddresses.add(new InternetAddress JavaDoc(recipient));
102         }
103         return (Address JavaDoc[]) (recipientAddresses.toArray(new Address JavaDoc[]{}));
104     }
105
106     private List getRecipientsList(Message.RecipientType JavaDoc recipientType) {
107         if (Message.RecipientType.TO.equals(recipientType)) return m_toRecepients;
108         if (Message.RecipientType.CC.equals(recipientType)) return m_ccRecepients;
109         if (Message.RecipientType.BCC.equals(recipientType)) return m_bccRecepients;
110         return null;
111     }
112     
113     public Address JavaDoc[] getAllRecipients() throws MessagingException {
114         List allRecipients = new ArrayList();
115         allRecipients.addAll(m_toRecepients);
116         allRecipients.addAll(m_ccRecepients);
117         allRecipients.addAll(m_bccRecepients);
118         return (Address JavaDoc[]) allRecipients.toArray();
119     }
120
121     public void setRecipients(Message.RecipientType JavaDoc recipientType, Address JavaDoc[] addresses) throws MessagingException {
122         getRecipientsList(recipientType).addAll(Arrays.asList(addresses));
123     }
124
125     public void setRecipients(Message.RecipientType JavaDoc recipientType, String JavaDoc recipient) throws MessagingException {
126         getRecipientsList(recipientType).add(recipient);
127     }
128
129     public void addRecipients(Message.RecipientType JavaDoc recipientType, Address JavaDoc[] addresses) throws MessagingException {
130         getRecipientsList(recipientType).addAll(Arrays.asList(addresses));
131     }
132
133     public void addRecipients(Message.RecipientType JavaDoc recipientType, String JavaDoc recipient) throws MessagingException {
134         getRecipientsList(recipientType).add(recipient);
135     }
136
137     public Address JavaDoc[] getReplyTo() throws MessagingException {
138         return (Address JavaDoc[]) m_replyToAddresses.toArray();
139     }
140
141     public void setReplyTo(Address JavaDoc[] addresses) throws MessagingException {
142         m_replyToAddresses.addAll(Arrays.asList(addresses));
143     }
144
145     public String JavaDoc getSubject() throws MessagingException {
146         return m_subject;
147     }
148
149     public void setSubject(String JavaDoc subject) throws MessagingException {
150         m_subject = subject;
151     }
152
153     public void setSubject(String JavaDoc subject, String JavaDoc charset) throws MessagingException {
154         if (subject == null)
155         {
156             m_subject = null;
157             return;
158         }
159         try {
160             m_subject = new String JavaDoc(subject.getBytes(charset));
161         } catch (UnsupportedEncodingException JavaDoc e) {
162             throw new MessagingException("setting subject failed", e);
163         }
164     }
165
166     public Date getSentDate() throws MessagingException {
167         return m_sentDate;
168     }
169
170     public void setSentDate(Date date) throws MessagingException {
171         m_sentDate = date;
172     }
173
174     public Date getReceivedDate() throws MessagingException {
175         return null; // trivial implementation
176
}
177
178     public int getSize() throws MessagingException {
179         return -1; // trivial implementation
180
}
181
182     public int getLineCount() throws MessagingException {
183         return -1; // trivial implementation
184
}
185
186     public String JavaDoc getContentType() throws MessagingException {
187         return getHeader("Content-Type", null);
188     }
189
190     public boolean isMimeType(String JavaDoc mimeType) throws MessagingException {
191         return mimeType.startsWith(getContentType());
192     }
193
194     public String JavaDoc getDisposition() throws MessagingException {
195         return getHeader("Content-Disposition", null);
196     }
197
198     public void setDisposition(String JavaDoc disposition) throws MessagingException {
199         setHeader("Content-Disposition", disposition);
200     }
201
202     public String JavaDoc getEncoding() throws MessagingException {
203         return getHeader("Content-Transfer-Encoding", null);
204     }
205
206     public String JavaDoc getContentID() throws MessagingException {
207         return getHeader("Content-ID", null);
208     }
209
210     public void setContentID(String JavaDoc contentID) throws MessagingException {
211         setHeader("Content-ID", contentID);
212     }
213
214     public String JavaDoc getContentMD5() throws MessagingException {
215         return getHeader("Content-MD5", null);
216     }
217
218     public void setContentMD5(String JavaDoc value) throws MessagingException {
219         setHeader("Content-MD5", value);
220     }
221
222     public String JavaDoc getDescription() throws MessagingException {
223         return getHeader("Content-Description", null);
224     }
225
226     public void setDescription(String JavaDoc description) throws MessagingException {
227         setHeader("Content-Description", description);
228     }
229
230     public void setDescription(String JavaDoc description, String JavaDoc charset) throws MessagingException {
231         try {
232             setDescription(new String JavaDoc(description.getBytes(charset)));
233         } catch (UnsupportedEncodingException JavaDoc e) {
234             throw new MessagingException("setting description failed", e);
235         }
236     }
237
238     public String JavaDoc[] getContentLanguage() throws MessagingException {
239         return m_contentLanguage;
240     }
241
242     public void setContentLanguage(String JavaDoc[] contentLanguage) throws MessagingException {
243         m_contentLanguage = contentLanguage;
244     }
245
246     public String JavaDoc getMessageID() throws MessagingException {
247         return "ID-" + m_iMessageNumber; // trivial implementation
248
}
249
250     public String JavaDoc getFileName() throws MessagingException {
251         return m_fileName;
252     }
253
254     public void setFileName(String JavaDoc fileName) throws MessagingException {
255         m_fileName = fileName;
256     }
257
258     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, MessagingException {
259         return null; // trivial implementation
260
}
261
262     protected InputStream JavaDoc getContentStream() throws MessagingException {
263         return null; // trivial implementation
264
}
265
266     public InputStream JavaDoc getRawInputStream() throws MessagingException {
267         if (m_content instanceof String JavaDoc) {
268             return new ByteArrayInputStream JavaDoc(m_content.toString().getBytes());
269         }
270         throw new UnsupportedOperationException JavaDoc("Unimplementated method");
271     }
272
273     public synchronized DataHandler JavaDoc getDataHandler() throws MessagingException {
274         return m_dataHandler;
275     }
276
277     public synchronized void setDataHandler(DataHandler JavaDoc dataHandler) throws MessagingException {
278         m_dataHandler = dataHandler;
279     }
280
281     public Object JavaDoc getContent() throws IOException JavaDoc, MessagingException {
282         return m_content;
283     }
284
285     public void setContent(Object JavaDoc object, String JavaDoc mimeType) throws MessagingException {
286         m_content = object; // trivial implementation
287
}
288
289     public void setText(String JavaDoc string) throws MessagingException {
290         setContent(string, "text/plain");
291     }
292
293     public void setText(String JavaDoc string, String JavaDoc charset) throws MessagingException {
294         try {
295             setContent(new String JavaDoc(string.getBytes(charset)) , "text/plain");
296         } catch (UnsupportedEncodingException JavaDoc e) {
297             throw new MessagingException("setting text content failed", e);
298         }
299     }
300
301     public void setContent(Multipart multipart) throws MessagingException {
302         m_content = multipart;
303     }
304
305     public Message JavaDoc reply(boolean b) throws MessagingException {
306         return new MockMimeMessage(this); // trivial implementation
307
}
308
309     public void writeTo(OutputStream JavaDoc outputStream) throws IOException JavaDoc, MessagingException {
310         ; // trivial implementation
311
}
312
313     public void writeTo(OutputStream JavaDoc outputStream, String JavaDoc[] strings) throws IOException JavaDoc, MessagingException {
314         ; // trivial implementation
315
}
316
317     public String JavaDoc[] getHeader(String JavaDoc name) throws MessagingException {
318         String JavaDoc value = (String JavaDoc) m_contentHeaders.get(name);
319         if (value == null) return null;
320         return new String JavaDoc[] {value};
321     }
322
323     public String JavaDoc getHeader(String JavaDoc name, String JavaDoc delimiter) throws MessagingException {
324         String JavaDoc[] header = getHeader(name);
325         if (header == null || header.length == 0) return null;
326         return header[0];
327     }
328
329     public void setHeader(String JavaDoc name, String JavaDoc value) throws MessagingException {
330         addHeader(name, value);
331     }
332
333     public void addHeader(String JavaDoc name, String JavaDoc value) throws MessagingException {
334         m_contentHeaders.put(name, value);
335     }
336
337     public void removeHeader(String JavaDoc name) throws MessagingException {
338         m_contentHeaders.remove(name);
339     }
340
341     public Enumeration getAllHeaders() throws MessagingException {
342         return Collections.enumeration(m_contentHeaders.values());
343     }
344
345     public Enumeration getMatchingHeaders(String JavaDoc[] names) throws MessagingException {
346         ArrayList matchingHeaders = new ArrayList();
347         for (int i = 0; i < names.length; i++) {
348             String JavaDoc name = names[i];
349             String JavaDoc value = getHeader(name, null);
350             if (value == null) continue;
351             matchingHeaders.add(value);
352         }
353         return Collections.enumeration(matchingHeaders);
354     }
355
356     public Enumeration getNonMatchingHeaders(String JavaDoc[] names) throws MessagingException {
357         List existingHeaders = Arrays.asList(names);
358
359         ArrayList nonMatchingHeaders = new ArrayList();
360         
361         Iterator iterator = m_contentHeaders.keySet().iterator();
362         while (iterator.hasNext()) {
363             String JavaDoc name = (String JavaDoc) iterator.next();
364             if (existingHeaders.contains(name)) continue;
365             String JavaDoc value = getHeader(name, null);
366             if (value == null) continue;
367             nonMatchingHeaders.add(value);
368         }
369         return Collections.enumeration(nonMatchingHeaders);
370     }
371
372     public void addHeaderLine(String JavaDoc headerLine) throws MessagingException {
373         int separatorIndex = headerLine.indexOf(":");
374         if (separatorIndex < 0) throw new MessagingException("header line does not conform to standard");
375         
376         addHeader(headerLine.substring(0,separatorIndex), headerLine.substring(separatorIndex,headerLine.length()));
377     }
378
379     public Enumeration getAllHeaderLines() throws MessagingException {
380         return Collections.enumeration(getHeadersAsStrings(m_contentHeaders));
381     }
382
383     private ArrayList getHeadersAsStrings(HashMap contentHeaders) {
384         ArrayList headerLines = new ArrayList();
385         Iterator iterator = contentHeaders.keySet().iterator();
386         while (iterator.hasNext()) {
387             String JavaDoc key = (String JavaDoc) iterator.next();
388             String JavaDoc value = (String JavaDoc) contentHeaders.get(key);
389             headerLines.add(key + ":" + value);
390         }
391         return headerLines;
392     }
393
394     public Enumeration getMatchingHeaderLines(String JavaDoc[] names) throws MessagingException {
395         ArrayList matchingHeaders = new ArrayList();
396         for (int i = 0; i < names.length; i++) {
397             String JavaDoc name = names[i];
398             String JavaDoc value = getHeader(name, null);
399             if (value == null) continue;
400             matchingHeaders.add(name + ":" + value);
401         }
402         return Collections.enumeration(matchingHeaders);
403     }
404
405     public Enumeration getNonMatchingHeaderLines(String JavaDoc[] names) throws MessagingException {
406         List existingHeaders = names != null ? Arrays.asList(names) : null;
407
408         ArrayList nonMatchingHeaders = new ArrayList();
409         
410         Iterator iterator = m_contentHeaders.keySet().iterator();
411         while (iterator.hasNext()) {
412             String JavaDoc name = (String JavaDoc) iterator.next();
413             if (existingHeaders!=null && existingHeaders.contains(name)) continue;
414             String JavaDoc value = getHeader(name, null);
415             if (value == null) continue;
416             nonMatchingHeaders.add(name + ":" + value);
417         }
418         return Collections.enumeration(nonMatchingHeaders);
419     }
420
421     public synchronized Flags getFlags() throws MessagingException {
422         return new Flags(m_setFlags);
423     }
424
425     public synchronized boolean isSet(Flags.Flag flag) throws MessagingException {
426         return m_setFlags.contains(flag);
427     }
428
429     public synchronized void setFlags(Flags flags, boolean set) throws MessagingException {
430         if (set) m_setFlags.add(flags);
431         else m_setFlags.remove(flags);
432     }
433
434     public void saveChanges() throws MessagingException {
435         ; // trivial implementation
436
}
437
438     protected void updateHeaders() throws MessagingException {
439         ; // trivial implementation
440
}
441
442     protected InternetHeaders JavaDoc createInternetHeaders(InputStream JavaDoc inputStream) throws MessagingException {
443         return new InternetHeaders JavaDoc();
444     }
445
446     public void setRecipient(Message.RecipientType JavaDoc recipientType, Address JavaDoc address) throws MessagingException {
447         setRecipients(recipientType, new Address JavaDoc[]{address});
448     }
449
450     public void addRecipient(Message.RecipientType JavaDoc recipientType, Address JavaDoc address) throws MessagingException {
451         setRecipients(recipientType, new Address JavaDoc[]{address});
452     }
453
454     public void setFlag(Flags.Flag flag, boolean set) throws MessagingException {
455         if (set) m_setFlags.add(flag);
456         else m_setFlags.remove(flag);
457     }
458
459     public int getMessageNumber() {
460         return m_iMessageNumber;
461     }
462
463     protected void setMessageNumber(int i) {
464         m_iMessageNumber = i;
465     }
466
467     public Folder getFolder() {
468         return null;
469     }
470
471     public boolean isExpunged() {
472         return m_bIsExpunged;
473     }
474
475     protected void setExpunged(boolean b) {
476         m_bIsExpunged = b;
477     }
478
479     public void setShouldMatch(boolean doMatch) {
480         m_doMatch = doMatch;
481     }
482     
483     public boolean match(SearchTerm JavaDoc searchTerm) throws MessagingException {
484         return m_doMatch;
485     }
486 }
487
Popular Tags