View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
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   * Class Address implements the Addresss Test as defined in RFC 3028, section
34   * 5.1.
35   */
36  public class Address extends AbstractCompatatorTest {
37      /**
38       * Constructor for Address.
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          // Extract the part of the address we are matching on
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          // domain matches MUST ignore case, others should not
81          String matchKey = null;
82          if (addressPart.equals(DOMAIN_TAG)) {
83              matchKey = key.toLowerCase();
84          } else {
85              matchKey = key;
86          }
87  
88          // Match using the specified comparator
89          return ComparatorUtils.match(comparator, matchType, matchAddress,
90                  matchKey, context);
91      }
92  }