View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.mail.monitor;
18  
19  import javax.mail.Folder;
20  import javax.mail.Message;
21  import javax.mail.MessagingException;
22  import javax.mail.internet.MimeMessage;
23  
24  import org.springframework.ws.transport.mail.support.MailTransportUtils;
25  
26  /**
27   * Implementation of the {@link MonitoringStrategy} interface that uses a simple polling mechanism suitable for POP3
28   * servers. Since POP3 does not have a native mechanism to determine which messages are "new", this implementation
29   * simply retrieves all messages in the {@link Folder}, and delete them afterwards. All messages in the POP3 mailbox are
30   * therefore, by definition, new.
31   * <p/>
32   * Setting the {@link #setDeleteMessages(boolean) deleteMessages} property is therefore ignored: messages are always
33   * deleted.
34   *
35   * @author Arjen Poutsma
36   * @since 1.5.0
37   */
38  public class Pop3PollingMonitoringStrategy extends PollingMonitoringStrategy {
39  
40      public Pop3PollingMonitoringStrategy() {
41          super.setDeleteMessages(true);
42      }
43  
44      public void setDeleteMessages(boolean deleteMessages) {
45      }
46  
47      /**
48       * Re-opens the folder, if it closed.
49       */
50      protected void afterSleep(Folder folder) throws MessagingException {
51          if (!folder.isOpen()) {
52              folder.open(Folder.READ_WRITE);
53          }
54      }
55  
56      /**
57       * Simply returns {@link Folder#getMessages()}.
58       */
59      protected Message[] searchForNewMessages(Folder folder) throws MessagingException {
60          return folder.getMessages();
61      }
62  
63      /**
64       * Deletes the given messages from the given folder, and closes it to expunge deleted messages.
65       *
66       * @param folder   the folder to delete messages from
67       * @param messages the messages to delete
68       * @throws MessagingException in case of JavaMail errors
69       */
70      protected void deleteMessages(Folder folder, Message[] messages) throws MessagingException {
71          super.deleteMessages(folder, messages);
72          // expunge deleted mails, and make sure we've retrieved them before closing the folder
73          for (int i = 0; i < messages.length; i++) {
74              new MimeMessage((MimeMessage) messages[i]);
75          }
76          MailTransportUtils.closeFolder(folder, true);
77      }
78  }