1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package de.saly.javamail.mock2;
27
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.UUID;
32
33 import javax.mail.Folder;
34 import javax.mail.MessagingException;
35 import javax.mail.Quota;
36 import javax.mail.Session;
37 import javax.mail.URLName;
38 import javax.mail.event.ConnectionEvent;
39
40 import com.sun.mail.imap.IMAPStore;
41
42 public class IMAPMockStore extends IMAPStore {
43 private boolean connected;
44 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
45
46 private MockMailbox mailbox;
47 private final UUID objectId = UUID.randomUUID();
48
49 {
50 logger.warn("IMAP Mock Store in use");
51 System.out.println("IMAP Mock Store in use");
52 }
53
54 public IMAPMockStore(final Session session, final URLName urlname) {
55 this(session, urlname, "imap", false);
56
57 }
58
59 public IMAPMockStore(final Session session, final URLName url, final String name, final boolean isSSL) {
60 super(session, url, name, isSSL);
61
62 logger.debug("Created " + objectId);
63 }
64
65 protected void checkConnected() throws MessagingException {
66 if (!isConnected()) {
67 throw new MessagingException("Not connected");
68 }
69 }
70
71 @Override
72 public synchronized void close() throws MessagingException {
73 this.connected = false;
74 notifyConnectionListeners(ConnectionEvent.CLOSED);
75 logger.debug("Closed " + objectId);
76 }
77
78 @Override
79 public void connect() throws MessagingException {
80 if (isConnected()) {
81 throw new IllegalStateException("already connected");
82 }
83 super.connect(url.getHost(), url.getPort(), url.getUsername(), url.getPassword());
84 }
85
86 @Override
87 public void connect(final String user, final String password) throws MessagingException {
88 if (isConnected()) {
89 throw new IllegalStateException("already connected");
90 }
91 super.connect(url.getHost(), url.getPort(), user, password);
92 }
93
94 @Override
95 public void connect(final String host, final String user, final String password) throws MessagingException {
96 if (isConnected()) {
97 throw new IllegalStateException("already connected");
98 }
99 super.connect(host, url.getPort(), user, password);
100 }
101
102 @Override
103 public Folder getDefaultFolder() throws MessagingException {
104 checkConnected();
105
106 return new IMAPDefaultMockFolder(this, mailbox);
107 }
108
109 @Override
110 public Folder getFolder(final String name) throws MessagingException {
111 checkConnected();
112 logger.debug("getFolder(" + name + ")");
113 if ("inbox".equalsIgnoreCase(name)) {
114 return new IMAPMockFolder(this, mailbox.getInbox());
115 } else {
116
117 return new IMAPMockFolder(this, mailbox.getRoot().getOrAddSubFolder(name));
118 }
119 }
120
121 @Override
122 public Folder getFolder(final URLName url) throws MessagingException {
123 checkConnected();
124 return getFolder(url.getFile());
125 }
126
127 @Override
128 public Folder[] getPersonalNamespaces() throws MessagingException {
129 return new Folder[] { getDefaultFolder() };
130 }
131
132 @Override
133 public synchronized Quota[] getQuota(final String root) throws MessagingException {
134 throw new MessagingException("QUOTA not supported");
135 }
136
137
138
139 Session getSession() {
140 return session;
141 }
142
143 @Override
144 public Folder[] getSharedNamespaces() throws MessagingException {
145 return new Folder[0];
146 }
147
148 @Override
149 public Folder[] getUserNamespaces(final String user) {
150 return new Folder[0];
151 }
152
153
154
155
156 @Override
157 public synchronized boolean hasCapability(final String capability) throws MessagingException {
158 return capability != null
159 && (capability.toLowerCase().startsWith("IMAP4") || capability.toLowerCase().startsWith("IDLE") || capability.toLowerCase()
160 .startsWith("ID"));
161 }
162
163 @Override
164 public synchronized Map<String, String> id(final Map<String, String> clientParams) throws MessagingException {
165 checkConnected();
166 final Map<String, String> id = new HashMap<String, String>();
167
168 id.put("name", "JavaMail Mock2");
169 id.put("vendor", "Hendrik Saly");
170 id.put("support-url", "https://github.com/salyh/javamail-mock2/issues");
171
172 return Collections.unmodifiableMap(id);
173
174 }
175
176 @Override
177 public void idle() throws MessagingException {
178 checkConnected();
179
180 try {
181 Thread.sleep(1000);
182 } catch (final InterruptedException e) {
183 Thread.currentThread().interrupt();
184 return;
185 }
186
187 }
188
189 @Override
190 public boolean isConnected() {
191 return this.connected;
192 }
193
194 @Override
195 protected boolean protocolConnect(final String host, final int port, final String user, final String password)
196 throws MessagingException {
197 logger.debug("Connect to " + user + " (" + objectId + ")");
198 mailbox = MockMailbox.get(user);
199
200 if (mailbox.getInbox().isSimulateError()) {
201 throw new MessagingException("Simulated error connecting to mailbox of " + user);
202 }
203
204 this.connected = true;
205
206 return true;
207 }
208
209 @Override
210 public synchronized void setQuota(final Quota quota) throws MessagingException {
211
212 throw new MessagingException("QUOTA not supported");
213
214 }
215
216 }