1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.mail.monitor;
18
19 import javax.mail.FetchProfile;
20 import javax.mail.Flags;
21 import javax.mail.Folder;
22 import javax.mail.Message;
23 import javax.mail.MessagingException;
24 import javax.mail.search.AndTerm;
25 import javax.mail.search.FlagTerm;
26 import javax.mail.search.SearchTerm;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32
33
34
35
36
37
38 public abstract class AbstractMonitoringStrategy implements MonitoringStrategy {
39
40
41 protected final Log logger = LogFactory.getLog(getClass());
42
43 private boolean deleteMessages = true;
44
45
46
47
48
49 public void setDeleteMessages(boolean deleteMessages) {
50 this.deleteMessages = deleteMessages;
51 }
52
53 public int getFolderOpenMode() {
54 return deleteMessages ? Folder.READ_WRITE : Folder.READ_ONLY;
55 }
56
57
58
59
60
61
62
63
64
65
66
67
68 public final Message[] monitor(Folder folder) throws MessagingException, InterruptedException {
69 waitForNewMessages(folder);
70 Message[] messages = searchForNewMessages(folder);
71 if (logger.isDebugEnabled()) {
72 logger.debug("Found " + messages.length + " new messages");
73 }
74 if (messages.length > 0) {
75 fetchMessages(folder, messages);
76 }
77 if (deleteMessages) {
78 deleteMessages(folder, messages);
79 }
80 return messages;
81 }
82
83
84
85
86
87
88
89
90
91 protected abstract void waitForNewMessages(Folder folder) throws MessagingException, InterruptedException;
92
93
94
95
96
97
98
99
100
101
102
103 protected Message[] searchForNewMessages(Folder folder) throws MessagingException {
104 if (!folder.isOpen()) {
105 return new Message[0];
106 }
107 Flags supportedFlags = folder.getPermanentFlags();
108 SearchTerm searchTerm = null;
109 if (supportedFlags != null) {
110 if (supportedFlags.contains(Flags.Flag.RECENT)) {
111 searchTerm = new FlagTerm(new Flags(Flags.Flag.RECENT), true);
112 }
113 if (supportedFlags.contains(Flags.Flag.ANSWERED)) {
114 FlagTerm answeredTerm = new FlagTerm(new Flags(Flags.Flag.ANSWERED), false);
115 if (searchTerm == null) {
116 searchTerm = answeredTerm;
117 }
118 else {
119 searchTerm = new AndTerm(searchTerm, answeredTerm);
120 }
121 }
122 if (supportedFlags.contains(Flags.Flag.DELETED)) {
123 FlagTerm deletedTerm = new FlagTerm(new Flags(Flags.Flag.DELETED), false);
124 if (searchTerm == null) {
125 searchTerm = deletedTerm;
126 }
127 else {
128 searchTerm = new AndTerm(searchTerm, deletedTerm);
129 }
130 }
131 }
132 return searchTerm != null ? folder.search(searchTerm) : folder.getMessages();
133 }
134
135
136
137
138
139
140
141
142
143 protected void fetchMessages(Folder folder, Message[] messages) throws MessagingException {
144 FetchProfile contentsProfile = new FetchProfile();
145 contentsProfile.add(FetchProfile.Item.ENVELOPE);
146 contentsProfile.add(FetchProfile.Item.CONTENT_INFO);
147 contentsProfile.add(FetchProfile.Item.FLAGS);
148 folder.fetch(messages, contentsProfile);
149 }
150
151
152
153
154
155
156
157
158
159 protected void deleteMessages(Folder folder, Message[] messages) throws MessagingException {
160 for (int i = 0; i < messages.length; i++) {
161 messages[i].setFlag(Flags.Flag.DELETED, true);
162 }
163 }
164 }