View Javadoc

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