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.io.IOException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.UUID;
33
34 import javax.mail.FetchProfile;
35 import javax.mail.FolderClosedException;
36 import javax.mail.Message;
37 import javax.mail.MessagingException;
38 import javax.mail.MethodNotSupportedException;
39 import javax.mail.event.ConnectionEvent;
40 import javax.mail.event.MessageChangedEvent;
41
42 import com.sun.mail.pop3.POP3MockFolder0;
43
44 import de.saly.javamail.mock2.MailboxFolder.MailboxEventListener;
45
46 public class POP3MockFolder extends POP3MockFolder0 implements MailboxEventListener {
47 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
48 private final MailboxFolder mailboxFolder;
49 private final UUID objectId = UUID.randomUUID();
50 private volatile boolean opened;
51
52 {
53 logger.warn("POP3 Mock Store in use");
54 System.out.println("POP3 Mock Store in use");
55 }
56
57 protected POP3MockFolder(final POP3MockStore store, final MailboxFolder mailboxFolder) {
58 super(store);
59 this.mailboxFolder = mailboxFolder;
60 this.mailboxFolder.addMailboxEventListener(this);
61 }
62
63
64
65
66
67
68
69
70 protected synchronized void checkClosed() {
71 if (opened) {
72 throw new IllegalStateException("This operation is not allowed on an open folder " + objectId);
73 }
74 }
75
76 protected synchronized void checkOpened() throws FolderClosedException {
77
78 if (!opened) {
79
80 throw new IllegalStateException("This operation is not allowed on a closed folder " + objectId);
81
82 }
83 }
84
85 @Override
86 public synchronized void close(final boolean expunge) throws MessagingException {
87 checkOpened();
88
89 if (expunge) {
90 mailboxFolder.expunge();
91
92 }
93
94 opened = false;
95
96 logger.debug("Folder closed " + objectId);
97 notifyConnectionListeners(ConnectionEvent.CLOSED);
98 }
99
100 @Override
101 public void fetch(final Message[] msgs, final FetchProfile fp) throws MessagingException {
102
103 }
104
105 @Override
106 public void folderCreated(final MailboxFolder mf) {
107
108
109 }
110
111 @Override
112 public void folderDeleted(final MailboxFolder mf) {
113
114
115 }
116
117 @Override
118 public void folderRenamed(final String from, final MailboxFolder to) {
119
120
121 }
122
123 @Override
124 public synchronized Message getMessage(final int msgnum) throws MessagingException {
125 checkOpened();
126 return new MockMessage(mailboxFolder.getByMsgNum(msgnum), this);
127 }
128
129 @Override
130 public synchronized int getMessageCount() throws MessagingException {
131 return mailboxFolder.getMessageCount();
132 }
133
134 @Override
135 public synchronized Message[] getMessages() throws MessagingException {
136 checkOpened();
137 final List<Message> messages = new ArrayList<Message>();
138 for (int i = 1; i <= mailboxFolder.getMessageCount(); i++) {
139 final Message m = mailboxFolder.getByMsgNum(i);
140 messages.add(new MockMessage(m, this));
141 }
142 return messages.toArray(new Message[messages.size()]);
143 }
144
145 @Override
146 public synchronized Message[] getMessages(final int low, final int high) throws MessagingException {
147
148 checkOpened();
149 final List<Message> messages = new ArrayList<Message>();
150 for (int i = low; i <= high; i++) {
151 final Message m = mailboxFolder.getByMsgNum(i);
152 messages.add(new MockMessage(m, this));
153 }
154 return messages.toArray(new Message[messages.size()]);
155 }
156
157 @Override
158 public synchronized Message[] getMessages(final int[] msgnums) throws MessagingException {
159 checkOpened();
160
161 final List<Integer> idlist = new ArrayList<Integer>();
162 for (final int value : msgnums) {
163 idlist.add(value);
164 }
165
166 final List<Message> messages = new ArrayList<Message>();
167
168 for (int i = 1; i <= mailboxFolder.getMessageCount(); i++) {
169
170 if (!idlist.contains(new Integer(i))) {
171 continue;
172 }
173
174 final Message m = mailboxFolder.getByMsgNum(i);
175 messages.add(new MockMessage(m, this));
176 }
177 return messages.toArray(new Message[messages.size()]);
178 }
179
180 @Override
181 public synchronized int getSize() throws MessagingException {
182 checkOpened();
183 return mailboxFolder.getSizeInBytes();
184 }
185
186 @Override
187 public synchronized int[] getSizes() throws MessagingException {
188 checkOpened();
189 final int count = getMessageCount();
190 final int[] sizes = new int[count];
191
192 for (int i = 1; i <= count; i++) {
193 sizes[i - 1] = getMessage(i).getSize();
194 }
195
196 return sizes;
197
198 }
199
200 @Override
201 public synchronized String getUID(final Message msg) throws MessagingException {
202 checkOpened();
203 return String.valueOf(((MockMessage) msg).getMockid());
204 }
205
206 @Override
207 public boolean isOpen() {
208 return opened;
209 }
210
211 @Override
212 public InputStream listCommand() throws MessagingException, IOException {
213 throw new MethodNotSupportedException();
214 }
215
216 @Override
217 public void messageAdded(final MailboxFolder mf, final MockMessage msg) {
218
219
220
221
222 }
223
224 @Override
225 public void messageChanged(final MailboxFolder mf, final MockMessage msg, final boolean headerChanged, final boolean flagsChanged) {
226 notifyMessageChangedListeners(MessageChangedEvent.FLAGS_CHANGED, msg);
227
228 }
229
230 @Override
231 public void messageExpunged(final MailboxFolder mf, final MockMessage msg, final boolean removed) {
232
233
234 }
235
236 @Override
237 public synchronized void open(final int mode) throws MessagingException {
238 checkClosed();
239 opened = true;
240 logger.debug("Open " + objectId);
241 notifyConnectionListeners(ConnectionEvent.OPENED);
242 }
243
244 @Override
245 public void uidInvalidated() {
246
247
248 }
249
250 }