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