1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve.tests;
21
22 import static org.apache.jsieve.tests.AddressPartTags.DOMAIN_TAG;
23 import static org.apache.jsieve.tests.AddressPartTags.LOCALPART_TAG;
24
25 import org.apache.jsieve.SieveContext;
26 import org.apache.jsieve.comparators.ComparatorUtils;
27 import org.apache.jsieve.exception.InternetAddressException;
28 import org.apache.jsieve.exception.SieveException;
29 import org.apache.jsieve.mail.MailAdapter;
30 import org.apache.jsieve.mail.SieveMailException;
31
32
33
34
35
36 public class Address extends AbstractCompatatorTest {
37
38
39
40 public Address() {
41 super();
42 }
43
44 protected boolean match(MailAdapter mail, String addressPart,
45 String comparator, String matchType, String headerName, String key,
46 SieveContext context) throws SieveException {
47 final MailAdapter.Address[] addresses = getMatchingValues(mail,
48 headerName);
49 final int length = addresses.length;
50 int i = 0;
51 boolean isMatched = false;
52 while (!isMatched && i < length) {
53 isMatched = match(addressPart, comparator, matchType,
54 addresses[i++], key, context);
55 }
56 return isMatched;
57 }
58
59 private MailAdapter.Address[] getMatchingValues(MailAdapter mail,
60 String valueName) throws SieveMailException,
61 InternetAddressException {
62 return mail.parseAddresses(valueName);
63 }
64
65 protected boolean match(String addressPart, String comparator,
66 String matchType, MailAdapter.Address address, String key,
67 SieveContext context) throws SieveException {
68 final String localPart = address.getLocalPart();
69 final String domain = address.getDomain();
70
71
72 final String matchAddress;
73 if (addressPart.equals(":all"))
74 matchAddress = localPart + "@" + domain;
75 else {
76 matchAddress = (addressPart.equals(LOCALPART_TAG) ? localPart
77 : domain.toLowerCase());
78 }
79
80
81 String matchKey = null;
82 if (addressPart.equals(DOMAIN_TAG)) {
83 matchKey = key.toLowerCase();
84 } else {
85 matchKey = key;
86 }
87
88
89 return ComparatorUtils.match(comparator, matchType, matchAddress,
90 matchKey, context);
91 }
92 }