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 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   * Class Address implements the Addresss Test as defined in RFC 3028, section
31   * 5.1.
32   */
33  public class Address extends AbstractCompatatorTest {
34      /**
35       * Constructor for Address.
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          // Extract the part of the address we are matching on
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          // domain matches MUST ignore case, others should not
78          String matchKey = null;
79          if (addressPart.equals(DOMAIN_TAG)) {
80              matchKey = key.toLowerCase();
81          } else {
82              matchKey = key;
83          }
84  
85          // Match using the specified comparator
86          return ComparatorUtils.match(comparator, matchType, matchAddress,
87                  matchKey, context);
88      }
89  }