001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.test.mock.javaxmail;
019:
020: import javax.mail.internet.MimeMessage;
021: import javax.mail.internet.InternetHeaders;
022: import javax.mail.internet.InternetAddress;
023: import javax.mail.*;
024: import javax.mail.search.SearchTerm;
025: import javax.activation.DataHandler;
026: import java.util.*;
027: import java.io.InputStream;
028: import java.io.IOException;
029: import java.io.OutputStream;
030: import java.io.UnsupportedEncodingException;
031:
032: public class MockMimeMessage extends MimeMessage {
033:
034: private final List m_fromAddresses = new ArrayList();
035: private Address m_senderAddress;
036: private final List m_toRecepients = new ArrayList();
037: private final List m_ccRecepients = new ArrayList();
038: private final List m_bccRecepients = new ArrayList();
039: private final List m_replyToAddresses = new ArrayList();
040: private String m_subject;
041: private int m_iMessageNumber;
042: private boolean m_bIsExpunged;
043: private Object m_content;
044: private Date m_sentDate;
045: private String[] m_contentLanguage;
046: private String m_fileName;
047: private DataHandler m_dataHandler;
048: private HashMap m_contentHeaders = new HashMap();
049: private Flags m_setFlags = new Flags();
050: private boolean m_doMatch;
051:
052: public MockMimeMessage() throws MessagingException {
053: super ((Session) null);
054: }
055:
056: public MockMimeMessage(MimeMessage mimeMessage)
057: throws MessagingException {
058: super (mimeMessage); // trivial implementation
059: }
060:
061: public Address[] getFrom() throws MessagingException {
062: return (Address[]) m_fromAddresses.toArray();
063: }
064:
065: public void setFrom(Address address) throws MessagingException {
066: m_fromAddresses.clear();
067: m_fromAddresses.add(address);
068: }
069:
070: public void setFrom() throws MessagingException {
071: m_fromAddresses.clear();
072: m_fromAddresses.add(InternetAddress.getLocalAddress(null));
073: }
074:
075: public void addFrom(Address[] addresses) throws MessagingException {
076: m_fromAddresses.add(addresses);
077: }
078:
079: public Address getSender() throws MessagingException {
080: return m_senderAddress;
081: }
082:
083: public void setSender(Address address) throws MessagingException {
084: m_senderAddress = address;
085: }
086:
087: public Address[] getRecipients(Message.RecipientType recipientType)
088: throws MessagingException {
089: return (Address[]) getRecipientsList(recipientType).toArray();
090: }
091:
092: private List getRecipientsList(Message.RecipientType recipientType) {
093: if (Message.RecipientType.TO.equals(recipientType))
094: return m_toRecepients;
095: if (Message.RecipientType.CC.equals(recipientType))
096: return m_ccRecepients;
097: if (Message.RecipientType.BCC.equals(recipientType))
098: return m_bccRecepients;
099: return null;
100: }
101:
102: public Address[] getAllRecipients() throws MessagingException {
103: List allRecipients = new ArrayList();
104: allRecipients.addAll(m_toRecepients);
105: allRecipients.addAll(m_ccRecepients);
106: allRecipients.addAll(m_bccRecepients);
107: return (Address[]) allRecipients.toArray();
108: }
109:
110: public void setRecipients(Message.RecipientType recipientType,
111: Address[] addresses) throws MessagingException {
112: getRecipientsList(recipientType).addAll(
113: Arrays.asList(addresses));
114: }
115:
116: public void setRecipients(Message.RecipientType recipientType,
117: String recipient) throws MessagingException {
118: getRecipientsList(recipientType).add(recipient);
119: }
120:
121: public void addRecipients(Message.RecipientType recipientType,
122: Address[] addresses) throws MessagingException {
123: getRecipientsList(recipientType).addAll(
124: Arrays.asList(addresses));
125: }
126:
127: public void addRecipients(Message.RecipientType recipientType,
128: String recipient) throws MessagingException {
129: getRecipientsList(recipientType).add(recipient);
130: }
131:
132: public Address[] getReplyTo() throws MessagingException {
133: return (Address[]) m_replyToAddresses.toArray();
134: }
135:
136: public void setReplyTo(Address[] addresses)
137: throws MessagingException {
138: m_replyToAddresses.addAll(Arrays.asList(addresses));
139: }
140:
141: public String getSubject() throws MessagingException {
142: return m_subject;
143: }
144:
145: public void setSubject(String subject) throws MessagingException {
146: m_subject = subject;
147: }
148:
149: public void setSubject(String subject, String charset)
150: throws MessagingException {
151: if (subject == null) {
152: m_subject = null;
153: return;
154: }
155: try {
156: m_subject = new String(subject.getBytes(charset));
157: } catch (UnsupportedEncodingException e) {
158: throw new MessagingException("setting subject failed", e);
159: }
160: }
161:
162: public Date getSentDate() throws MessagingException {
163: return m_sentDate;
164: }
165:
166: public void setSentDate(Date date) throws MessagingException {
167: m_sentDate = date;
168: }
169:
170: public Date getReceivedDate() throws MessagingException {
171: return null; // trivial implementation
172: }
173:
174: public int getSize() throws MessagingException {
175: return -1; // trivial implementation
176: }
177:
178: public int getLineCount() throws MessagingException {
179: return -1; // trivial implementation
180: }
181:
182: public String getContentType() throws MessagingException {
183: return getHeader("Content-Type", null);
184: }
185:
186: public boolean isMimeType(String mimeType)
187: throws MessagingException {
188: return mimeType.startsWith(getContentType());
189: }
190:
191: public String getDisposition() throws MessagingException {
192: return getHeader("Content-Disposition", null);
193: }
194:
195: public void setDisposition(String disposition)
196: throws MessagingException {
197: setHeader("Content-Disposition", disposition);
198: }
199:
200: public String getEncoding() throws MessagingException {
201: return getHeader("Content-Transfer-Encoding", null);
202: }
203:
204: public String getContentID() throws MessagingException {
205: return getHeader("Content-ID", null);
206: }
207:
208: public void setContentID(String contentID)
209: throws MessagingException {
210: setHeader("Content-ID", contentID);
211: }
212:
213: public String getContentMD5() throws MessagingException {
214: return getHeader("Content-MD5", null);
215: }
216:
217: public void setContentMD5(String value) throws MessagingException {
218: setHeader("Content-MD5", value);
219: }
220:
221: public String getDescription() throws MessagingException {
222: return getHeader("Content-Description", null);
223: }
224:
225: public void setDescription(String description)
226: throws MessagingException {
227: setHeader("Content-Description", description);
228: }
229:
230: public void setDescription(String description, String charset)
231: throws MessagingException {
232: try {
233: setDescription(new String(description.getBytes(charset)));
234: } catch (UnsupportedEncodingException e) {
235: throw new MessagingException("setting description failed",
236: e);
237: }
238: }
239:
240: public String[] getContentLanguage() throws MessagingException {
241: return m_contentLanguage;
242: }
243:
244: public void setContentLanguage(String[] contentLanguage)
245: throws MessagingException {
246: m_contentLanguage = contentLanguage;
247: }
248:
249: public String getMessageID() throws MessagingException {
250: return "ID-" + m_iMessageNumber; // trivial implementation
251: }
252:
253: public String getFileName() throws MessagingException {
254: return m_fileName;
255: }
256:
257: public void setFileName(String fileName) throws MessagingException {
258: m_fileName = fileName;
259: }
260:
261: public InputStream getInputStream() throws IOException,
262: MessagingException {
263: return null; // trivial implementation
264: }
265:
266: protected InputStream getContentStream() throws MessagingException {
267: return null; // trivial implementation
268: }
269:
270: public InputStream getRawInputStream() throws MessagingException {
271: return null; // trivial implementation
272: }
273:
274: public synchronized DataHandler getDataHandler()
275: throws MessagingException {
276: return m_dataHandler;
277: }
278:
279: public synchronized void setDataHandler(DataHandler dataHandler)
280: throws MessagingException {
281: m_dataHandler = dataHandler;
282: }
283:
284: public Object getContent() throws IOException, MessagingException {
285: return m_content;
286: }
287:
288: public void setContent(Object object, String mimeType)
289: throws MessagingException {
290: m_content = object; // trivial implementation
291: }
292:
293: public void setText(String string) throws MessagingException {
294: setContent(string, "text/plain");
295: }
296:
297: public void setText(String string, String charset)
298: throws MessagingException {
299: try {
300: setContent(new String(string.getBytes(charset)),
301: "text/plain");
302: } catch (UnsupportedEncodingException e) {
303: throw new MessagingException("setting text content failed",
304: e);
305: }
306: }
307:
308: public void setContent(Multipart multipart)
309: throws MessagingException {
310: m_content = multipart;
311: }
312:
313: public Message reply(boolean b) throws MessagingException {
314: return new MockMimeMessage(this ); // trivial implementation
315: }
316:
317: public void writeTo(OutputStream outputStream) throws IOException,
318: MessagingException {
319: ; // trivial implementation
320: }
321:
322: public void writeTo(OutputStream outputStream, String[] strings)
323: throws IOException, MessagingException {
324: ; // trivial implementation
325: }
326:
327: public String[] getHeader(String name) throws MessagingException {
328: String value = (String) m_contentHeaders.get(name);
329: if (value == null)
330: return null;
331: return new String[] { value };
332: }
333:
334: public String getHeader(String name, String delimiter)
335: throws MessagingException {
336: String[] header = getHeader(name);
337: if (header == null || header.length == 0)
338: return null;
339: return header[0];
340: }
341:
342: public void setHeader(String name, String value)
343: throws MessagingException {
344: addHeader(name, value);
345: }
346:
347: public void addHeader(String name, String value)
348: throws MessagingException {
349: m_contentHeaders.put(name, value);
350: }
351:
352: public void removeHeader(String name) throws MessagingException {
353: m_contentHeaders.remove(name);
354: }
355:
356: public Enumeration getAllHeaders() throws MessagingException {
357: return Collections.enumeration(m_contentHeaders.values());
358: }
359:
360: public Enumeration getMatchingHeaders(String[] names)
361: throws MessagingException {
362: ArrayList matchingHeaders = new ArrayList();
363: for (int i = 0; i < names.length; i++) {
364: String name = names[i];
365: String value = getHeader(name, null);
366: if (value == null)
367: continue;
368: matchingHeaders.add(value);
369: }
370: return Collections.enumeration(matchingHeaders);
371: }
372:
373: public Enumeration getNonMatchingHeaders(String[] names)
374: throws MessagingException {
375: List existingHeaders = Arrays.asList(names);
376:
377: ArrayList nonMatchingHeaders = new ArrayList();
378:
379: Iterator iterator = m_contentHeaders.keySet().iterator();
380: while (iterator.hasNext()) {
381: String name = (String) iterator.next();
382: if (existingHeaders.contains(name))
383: continue;
384: String value = getHeader(name, null);
385: if (value == null)
386: continue;
387: nonMatchingHeaders.add(value);
388: }
389: return Collections.enumeration(nonMatchingHeaders);
390: }
391:
392: public void addHeaderLine(String headerLine)
393: throws MessagingException {
394: int separatorIndex = headerLine.indexOf(":");
395: if (separatorIndex < 0)
396: throw new MessagingException(
397: "header line does not conform to standard");
398:
399: addHeader(headerLine.substring(0, separatorIndex), headerLine
400: .substring(separatorIndex, headerLine.length()));
401: }
402:
403: public Enumeration getAllHeaderLines() throws MessagingException {
404: return Collections
405: .enumeration(getHeadersAsStrings(m_contentHeaders));
406: }
407:
408: private ArrayList getHeadersAsStrings(HashMap contentHeaders) {
409: ArrayList headerLines = new ArrayList();
410: Iterator iterator = contentHeaders.keySet().iterator();
411: while (iterator.hasNext()) {
412: String key = (String) iterator.next();
413: String value = (String) contentHeaders.get(key);
414: headerLines.add(key + ":" + value);
415: }
416: return headerLines;
417: }
418:
419: public Enumeration getMatchingHeaderLines(String[] names)
420: throws MessagingException {
421: ArrayList matchingHeaders = new ArrayList();
422: for (int i = 0; i < names.length; i++) {
423: String name = names[i];
424: String value = getHeader(name, null);
425: if (value == null)
426: continue;
427: matchingHeaders.add(name + ":" + value);
428: }
429: return Collections.enumeration(matchingHeaders);
430: }
431:
432: public Enumeration getNonMatchingHeaderLines(String[] names)
433: throws MessagingException {
434: List existingHeaders = names != null ? Arrays.asList(names)
435: : null;
436:
437: ArrayList nonMatchingHeaders = new ArrayList();
438:
439: Iterator iterator = m_contentHeaders.keySet().iterator();
440: while (iterator.hasNext()) {
441: String name = (String) iterator.next();
442: if (existingHeaders != null
443: && existingHeaders.contains(name))
444: continue;
445: String value = getHeader(name, null);
446: if (value == null)
447: continue;
448: nonMatchingHeaders.add(name + ":" + value);
449: }
450: return Collections.enumeration(nonMatchingHeaders);
451: }
452:
453: public synchronized Flags getFlags() throws MessagingException {
454: return new Flags(m_setFlags);
455: }
456:
457: public synchronized boolean isSet(Flags.Flag flag)
458: throws MessagingException {
459: return m_setFlags.contains(flag);
460: }
461:
462: public synchronized void setFlags(Flags flags, boolean set)
463: throws MessagingException {
464: if (set)
465: m_setFlags.add(flags);
466: else
467: m_setFlags.remove(flags);
468: }
469:
470: public void saveChanges() throws MessagingException {
471: ; // trivial implementation
472: }
473:
474: protected void updateHeaders() throws MessagingException {
475: ; // trivial implementation
476: }
477:
478: protected InternetHeaders createInternetHeaders(
479: InputStream inputStream) throws MessagingException {
480: return new InternetHeaders();
481: }
482:
483: public void setRecipient(Message.RecipientType recipientType,
484: Address address) throws MessagingException {
485: setRecipients(recipientType, new Address[] { address });
486: }
487:
488: public void addRecipient(Message.RecipientType recipientType,
489: Address address) throws MessagingException {
490: setRecipients(recipientType, new Address[] { address });
491: }
492:
493: public void setFlag(Flags.Flag flag, boolean set)
494: throws MessagingException {
495: if (set)
496: m_setFlags.add(flag);
497: else
498: m_setFlags.remove(flag);
499: }
500:
501: public int getMessageNumber() {
502: return m_iMessageNumber;
503: }
504:
505: protected void setMessageNumber(int i) {
506: m_iMessageNumber = i;
507: }
508:
509: public Folder getFolder() {
510: return null;
511: }
512:
513: public boolean isExpunged() {
514: return m_bIsExpunged;
515: }
516:
517: protected void setExpunged(boolean b) {
518: m_bIsExpunged = b;
519: }
520:
521: public void setShouldMatch(boolean doMatch) {
522: m_doMatch = doMatch;
523: }
524:
525: public boolean match(SearchTerm searchTerm)
526: throws MessagingException {
527: return m_doMatch;
528: }
529: }
|