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