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
21
22 package org.apache.james.transport.matchers;
23
24 import org.apache.mailet.base.GenericMatcher;
25 import org.apache.mailet.Mail;
26
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.LinkedList;
30 import java.util.StringTokenizer;
31
32 import javax.mail.MessagingException;
33 import javax.mail.internet.MimeMessage;
34
35 /**
36 * use: <pre><code><mailet match="HasHeader={<header>[=value]}+" class="..." /></code></pre>
37 *
38 * <p>This matcher checks if the header named is present. If complements the
39 * AddHeader mailet.</p>
40 */
41 public class HasHeader extends GenericMatcher {
42
43 private LinkedList conditionline_ = new LinkedList();
44
45 // set headernames and values
46 public void init() throws MessagingException {
47 StringTokenizer st = new StringTokenizer(getCondition(), "+");
48 conditionline_ = new LinkedList();
49
50 // separates the headernames from the matchline
51 while (st.hasMoreTokens()) {
52 String condition = st.nextToken().trim();
53 conditionline_.add(condition);
54 }
55 }
56
57 public Collection match(Mail mail) throws javax.mail.MessagingException {
58 boolean match = false;
59 MimeMessage message = mail.getMessage();
60
61
62 header:
63 // goes through the headernames one by one
64 for (Iterator it=conditionline_.iterator(); it.hasNext(); ) {
65 String element = (String)it.next();
66 StringTokenizer st = new StringTokenizer(element, "=", false);
67 String header = new String();
68
69 // read the headername
70 if (st.hasMoreTokens()) {
71 header = st.nextToken().trim();
72 } else {
73 throw new MessagingException("Missing headerName");
74 }
75
76 // try to read headervalue
77 String headerValue = new String();
78 if (st.hasMoreTokens()) {
79 headerValue = st.nextToken().trim();
80 } else {
81 headerValue = null;
82 }
83
84 // find headername in Mailheaders
85 String [] headerArray = message.getHeader(header);
86 if (headerArray != null && headerArray.length > 0) {
87 // if there is the headername specified without the headervalue
88 // only the existence of the headername ist performed
89 if (headerValue != null) {
90 //
91 if (headerArray[0].trim().equalsIgnoreCase(headerValue)) {
92 // headername and value found and match to the condition
93 match = true;
94 } else {
95 // headername and value found but the value does not match the condition
96 match = false;
97 // if one condition fails the matcher returns false
98 break header;
99 };
100 } else {
101 // just the headername is specified
102 match = true;
103 }
104 } else {
105 // no headername is found
106 match = false;
107 // if one condition fails the matcher returns false
108 break header;
109 }
110 }
111
112 return (match) ? mail.getRecipients() : null;
113
114 }
115 }
116