KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > test > mock > james > MockMailServer


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 package org.apache.james.test.mock.james;
21
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.container.ContainerUtil;
24 import org.apache.james.core.MailImpl;
25 import org.apache.james.services.MailRepository;
26 import org.apache.james.services.MailServer;
27 import org.apache.james.smtpserver.MessageSizeException;
28 import org.apache.james.userrepository.MockUsersRepository;
29 import org.apache.mailet.Mail;
30 import org.apache.mailet.MailAddress;
31
32 import javax.mail.Address JavaDoc;
33 import javax.mail.MessagingException JavaDoc;
34 import javax.mail.internet.InternetAddress JavaDoc;
35 import javax.mail.internet.MimeMessage JavaDoc;
36
37 import java.io.InputStream JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.HashSet JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43
44 public class MockMailServer implements MailServer, Disposable {
45
46     private final MockUsersRepository m_users = new MockUsersRepository();
47
48     private static int m_counter = 0;
49     private int m_maxMessageSizeBytes = 0;
50
51     // private final ArrayList mails = new ArrayList();
52

53     private final InMemorySpoolRepository mails = new InMemorySpoolRepository();
54     private String JavaDoc lastMailKey = null;
55
56     private HashMap JavaDoc inboxes;
57     
58     private boolean virtualHosting;
59
60     
61     public MockUsersRepository getUsersRepository() {
62         return m_users;
63     }
64
65     public void sendMail(MailAddress sender, Collection JavaDoc recipients, MimeMessage JavaDoc msg) throws MessagingException JavaDoc {
66         // Object[] mailObjects = new Object[]{sender, recipients, new MimeMessageCopyOnWriteProxy(msg)};
67
// mails.add(mailObjects);
68
//
69
String JavaDoc newId = newId();
70         MailImpl m = new MailImpl(newId, sender, recipients, msg);
71         sendMail(m);
72         m.dispose();
73     }
74
75     public void sendMail(MailAddress sender, Collection JavaDoc recipients, InputStream JavaDoc msg) throws MessagingException JavaDoc {
76 // Object[] mailObjects = new Object[]{sender, recipients, msg};
77
// mails.add(mailObjects);
78
MailImpl m = new MailImpl(newId(), sender, recipients, msg);
79         sendMail(m);
80         m.dispose();
81     }
82
83     public void sendMail(Mail mail) throws MessagingException JavaDoc {
84         int bodySize = mail.getMessage().getSize();
85         try {
86             if (m_maxMessageSizeBytes != 0 && m_maxMessageSizeBytes < bodySize) throw new MessageSizeException();
87         } catch (MessageSizeException e) {
88             throw new MessagingException JavaDoc("message size exception is nested", e);
89         }
90         
91         lastMailKey = mail.getName();
92         mails.store(mail);
93         // sendMail(mail.getSender(), mail.getRecipients(), mail.getMessage());
94
}
95
96     public void sendMail(MimeMessage JavaDoc message) throws MessagingException JavaDoc {
97         // taken from class org.apache.james.James
98
MailAddress sender = new MailAddress((InternetAddress JavaDoc)message.getFrom()[0]);
99         Collection JavaDoc recipients = new HashSet JavaDoc();
100         Address addresses[] = message.getAllRecipients();
101         if (addresses != null) {
102             for (int i = 0; i < addresses.length; i++) {
103                 // Javamail treats the "newsgroups:" header field as a
104
// recipient, so we want to filter those out.
105
if ( addresses[i] instanceof InternetAddress JavaDoc ) {
106                     recipients.add(new MailAddress((InternetAddress JavaDoc)addresses[i]));
107                 }
108             }
109         }
110         sendMail(sender, recipients, message);
111     }
112
113     public MailRepository getUserInbox(String JavaDoc userName) {
114         if (inboxes==null) {
115             return null;
116         } else {
117             if ((userName.indexOf("@") < 0) == false && supportVirtualHosting() == false) userName = userName.split("@")[0];
118             return (MailRepository) inboxes.get(userName);
119         }
120         
121     }
122     
123     public void setUserInbox(String JavaDoc userName, MailRepository inbox) {
124         if (inboxes == null) {
125             inboxes = new HashMap JavaDoc();
126         }
127         inboxes.put(userName,inbox);
128     }
129
130     public Map JavaDoc getRepositoryCounters() {
131         return null; // trivial implementation
132
}
133
134
135     public synchronized String JavaDoc getId() {
136         return newId();
137     }
138
139     public static String JavaDoc newId() {
140         m_counter++;
141         return "MockMailServer-ID-" + m_counter;
142     }
143
144     public boolean addUser(String JavaDoc userName, String JavaDoc password) {
145         m_users.addUser(userName, password);
146         return true;
147     }
148
149     public boolean isLocalServer(String JavaDoc serverName) {
150         return "localhost".equals(serverName);
151     }
152
153     public Mail getLastMail()
154     {
155         if (mails.size() == 0) return null;
156         try {
157             return mails.retrieve(lastMailKey);
158         } catch (MessagingException JavaDoc e) {
159             e.printStackTrace();
160         }
161         return null;
162     }
163
164     public void setMaxMessageSizeBytes(int maxMessageSizeBytes) {
165         m_maxMessageSizeBytes = maxMessageSizeBytes;
166     }
167
168     public void dispose() {
169 // if (mails != null) {
170
// Iterator i = mails.iterator();
171
// while (i.hasNext()) {
172
// Object[] obs = (Object[]) i.next();
173
// // this is needed to let the MimeMessageWrapper to dispose.
174
// ContainerUtil.dispose(obs[2]);
175
// }
176
// }
177
mails.dispose();
178         if (inboxes!=null) {
179             Iterator JavaDoc i = inboxes.values().iterator();
180             while (i.hasNext()) {
181                 MailRepository m = (MailRepository) i.next();
182                 ContainerUtil.dispose(m);
183             }
184         }
185     }
186     
187     public MailRepository getSentMailsRepository() {
188         return mails;
189     }
190     
191     public void setVirtualHosting(boolean virtualHosting) {
192         this.virtualHosting = virtualHosting;
193     }
194
195     public boolean supportVirtualHosting() {
196         return virtualHosting;
197     }
198
199     public String JavaDoc getDefaultDomain() {
200         return "localhost";
201     }
202
203     public String JavaDoc getHelloName() {
204         return "localhost";
205     }
206 }
207
208
209
Popular Tags