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
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
49
50
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
74
75
76 public void process() throws MessagingException, NotificationException {
77
78
79
80 Session session = Session.getDefaultInstance(new Properties(), null);
81
82
83
84 Store store = session.getStore(protocol);
85 store.connect(mailHost, -1, user, password);
86
87
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
95
96 src_folder.open(Folder.READ_WRITE);
97
98
99
100
101
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
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
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