1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve.mailet;
21
22 import javax.mail.MessagingException;
23
24 import org.apache.mailet.Mail;
25 import org.apache.mailet.MailAddress;
26
27
28
29
30 public class ActionUtils
31 {
32
33 private final static String ATTRIBUTE_PREFIX = ActionUtils.class.getPackage().getName() + ".";
34
35
36
37
38
39
40
41
42 public static MailAddress getSoleRecipient(Mail aMail) throws MessagingException
43 {
44 if (aMail.getRecipients() == null) {
45 throw new MessagingException("Invalid number of recipients - 0"
46 + ". Exactly 1 recipient is expected.");
47 } else if (1 != aMail.getRecipients().size())
48 throw new MessagingException("Invalid number of recipients - "
49 + new Integer(aMail.getRecipients().size()).toString()
50 + ". Exactly 1 recipient is expected.");
51 return (MailAddress) aMail.getRecipients().iterator().next();
52 }
53
54
55
56
57
58
59
60
61
62
63 public static void detectAndHandleLocalLooping(Mail aMail, ActionContext context, String anAttributeSuffix)
64 throws MessagingException
65 {
66 MailAddress thisRecipient = getSoleRecipient(aMail);
67 MailAddress lastRecipient = (MailAddress) aMail
68 .getAttribute(ATTRIBUTE_PREFIX + anAttributeSuffix);
69 if (null != lastRecipient && lastRecipient.equals(thisRecipient))
70 {
71 MessagingException ex = new MessagingException(
72 "This message is looping! Message ID: "
73 + aMail.getMessage().getMessageID());
74 context.getLog().warn(ex.getMessage(), ex);
75 throw ex;
76 }
77 aMail.setAttribute(ATTRIBUTE_PREFIX + anAttributeSuffix,
78 thisRecipient);
79 }
80 }