View Javadoc
1   package org.itracker.web.scheduler.tasks;
2   
3   import org.apache.log4j.Logger;
4   import org.itracker.services.NotificationService;
5   
6   import javax.mail.*;
7   import javax.mail.search.FlagTerm;
8   import java.util.Properties;
9   
10  /**
11   * @author rui (rui.silva@emation.pt)
12   */
13  public class MailNotification extends BaseJob {
14  
15      private static final Logger logger = Logger.getLogger(MailNotification.class);
16      private String projectId;
17      private String mailHost;
18      private String user;
19      private String password;
20      private String folderName;
21      private String protocol;
22      private NotificationService notificationService;
23  
24      public MailNotification() {
25      }
26  
27      public void setNotificationService(NotificationService notificationService) {
28          this.notificationService = notificationService;
29      }
30  
31      public NotificationService getNotificationService() {
32          return notificationService;
33      }
34  
35      /**
36       *
37       */
38      @SuppressWarnings("unused")
39      private String getProjectId() {
40          return projectId;
41      }
42  
43      private void setProjectId(String id) {
44          projectId = id;
45      }
46  
47      /*
48       * (non-Javadoc)
49       * 
50       * @see org.itracker.web.scheduler.SchedulableTask#performTask(java.lang.String[])
51       */
52      public void performTask(String[] args) {
53  
54          mailHost = args[0];
55          user = args[1];
56          password = args[2];
57          folderName = args[3];
58  
59          setProjectId(args[4]);
60  
61          protocol = args[5];
62  
63          try {
64              process();
65          } catch (MessagingException ex) {
66              logger.error("performTask: failed with messaging exception", ex);
67          } catch (NotificationException ex) {
68              logger.error("performTask: failed with notification exception", ex);
69          }
70      }
71  
72      /**
73       * process() checks for new messages and calls processMsg() for every new
74       * message
75       */
76      public void process() throws MessagingException, NotificationException {
77  
78          // Get a Session object
79          //
80          Session session = Session.getDefaultInstance(new Properties(), null);
81  
82          // Connect to host
83          //
84          Store store = session.getStore(protocol);
85          store.connect(mailHost, -1, user, password);
86  
87          // Open the default folder
88          //
89          Folder src_folder = store.getFolder(folderName);
90  
91          if (src_folder == null) {
92              throw new NotificationException("Unable to get folder: null");
93          }
94          // Get message count
95          //
96          src_folder.open(Folder.READ_WRITE);
97  
98          // TODO: never used, commented, task added:
99          // int totalMessages = src_folder.getMessageCount();
100 
101         // Get attributes & flags for all messages
102         //
103         Message[] messages = src_folder.getMessages();
104         FetchProfile fp = new FetchProfile();
105         fp.add(FetchProfile.Item.ENVELOPE);
106         fp.add(FetchProfile.Item.FLAGS);
107         fp.add("From");
108         src_folder.fetch(messages, fp);
109 
110         // Process each message
111         //
112 
113         FlagTerm search = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
114 
115         for (int i = 0; i < messages.length; i++) {
116             Message message = messages[i];
117             if (search.match(message)) {
118                 try {
119 
120                     // TODO: process message
121 
122                     message.setFlag(Flags.Flag.SEEN, true);
123                     logger.info("Processed Message: " + message.getSubject() + " From: " + message.getFrom()[0]);
124                 } catch (Exception e) {
125                     logger.error("Couldn't process Message: " + message.getSubject() + " From: "
126                             + message.getFrom()[0], e);
127                     try {
128                         message.setFlag(Flags.Flag.SEEN, false);
129                     } catch (Exception exception) {
130                         logger.error(exception.getMessage(), exception);
131                     }
132                 }
133             } else {
134                 logger.info("Didn't process Message: " + message.getSubject() + " From: " + message.getFrom()[0]
135                         + ". Message already read.");
136             }
137 
138         }
139         src_folder.close(true);
140         store.close();
141     }
142 
143 }
144