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.optional;
21  
22  
23  import static org.apache.jsieve.tests.AddressPartTags.DOMAIN_TAG;
24  import static org.apache.jsieve.tests.AddressPartTags.LOCALPART_TAG;
25  
26  import java.util.List;
27  
28  import org.apache.jsieve.SieveContext;
29  import org.apache.jsieve.comparators.ComparatorUtils;
30  import org.apache.jsieve.exception.SieveException;
31  import org.apache.jsieve.mail.MailAdapter;
32  import org.apache.jsieve.mail.SieveMailException;
33  import org.apache.jsieve.mail.optional.EnvelopeAccessors;
34  import org.apache.jsieve.tests.AbstractCompatatorTest;
35  
36  /**
37   * Class Envelope implements the optional Envelope Test as defined in RFC 3028,
38   * section 5.4.
39   */
40  public class Envelope extends AbstractCompatatorTest {
41  
42      /**
43       * Constructor for EnvelopeAccessors.
44       */
45      public Envelope() {
46          super();
47      }
48  
49      protected List<String> getMatchingValues(MailAdapter mail, String valueName)
50              throws SieveMailException {
51          return ((EnvelopeAccessors) mail).getMatchingEnvelope(valueName);
52      }
53  
54      /**
55       * Method match.
56       * 
57       * @param addressPart
58       * @param comparator
59       * @param matchType
60       * @param headerValue
61       * @param key
62       * @param context not null
63       * @return boolean
64       * @throws SieveMailException
65       */
66      protected boolean match(String addressPart, String comparator,
67              String matchType, String headerValue, String key,
68              SieveContext context) throws SieveException {
69  
70          // Extract the part of the address we are matching on
71          String matchAddress = null;
72          if (addressPart.equals(":all"))
73              matchAddress = headerValue;
74          else {
75              int localStart = 0;
76              int localEnd = 0;
77              int domainStart = 0;
78              int domainEnd = headerValue.length();
79              int splitIndex = headerValue.indexOf('@');
80              // If there is no domain part (-1), treat it as an empty String
81              if (splitIndex == -1) {
82                  localEnd = domainEnd;
83                  domainStart = domainEnd;
84              } else {
85                  localEnd = splitIndex;
86                  domainStart = splitIndex + 1;
87              }
88              matchAddress = (addressPart.equals(LOCALPART_TAG) ? headerValue
89                      .substring(localStart, localEnd) : headerValue.substring(
90                      domainStart, domainEnd));
91          }
92  
93          // domain matches MUST ignore case, others should not
94          String matchKey = null;
95          if (addressPart.equals(DOMAIN_TAG)) {
96              matchKey = key.toLowerCase();
97              matchAddress = matchAddress.toLowerCase();
98          } else
99              matchKey = key;
100 
101         // Match using the specified comparator
102         return ComparatorUtils.match(comparator, matchType, matchAddress,
103                 matchKey, context);
104     }
105 
106     protected boolean match(MailAdapter mail, String addressPart,
107             String comparator, String matchType, String headerName, String key,
108             SieveContext context) throws SieveException {
109         final List<String> headerValues = getMatchingValues(mail, headerName);
110         boolean isMatched = false;
111         for (final String value:headerValues) {
112             isMatched = match(addressPart, comparator, matchType, value, key, context);
113             if (isMatched) {
114                 break;
115             }
116         }
117         return isMatched;
118     }
119 
120 }