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