View Javadoc

1   /*
2    * Copyright 2005-2010 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 [email protected] 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 [email protected] Folder}, and delete them afterwards. All messages in the POP3 mailbox are
30   * therefore, by definition, new.
31   * <p/>
32   * Setting the [email protected] #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      @Override
45      public void setDeleteMessages(boolean deleteMessages) {
46      }
47  
48      /**
49       * Re-opens the folder, if it closed.
50       */
51      @Override
52      protected void afterSleep(Folder folder) throws MessagingException {
53          if (!folder.isOpen()) {
54              folder.open(Folder.READ_WRITE);
55          }
56      }
57  
58      /**
59       * Simply returns [email protected] Folder#getMessages()}.
60       */
61      @Override
62      protected Message[] searchForNewMessages(Folder folder) throws MessagingException {
63          return folder.getMessages();
64      }
65  
66      /**
67       * Deletes the given messages from the given folder, and closes it to expunge deleted messages.
68       *
69       * @param folder   the folder to delete messages from
70       * @param messages the messages to delete
71       * @throws MessagingException in case of JavaMail errors
72       */
73      @Override
74      protected void deleteMessages(Folder folder, Message[] messages) throws MessagingException {
75          super.deleteMessages(folder, messages);
76          // expunge deleted mails, and make sure we've retrieved them before closing the folder
77          for (Message message : messages) {
78              new MimeMessage((MimeMessage) message);
79          }
80          MailTransportUtils.closeFolder(folder, true);
81      }
82  }