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.utils;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.mail.internet.MimeMessage;
29  
30  import org.apache.jsieve.mail.SieveMailException;
31  import org.apache.jsieve.mail.optional.EnvelopeAccessors;
32  
33  /**
34   * <p>
35   * Class SieveEnvelopeMailAdapter extends class SieveMailAdapter, a mock
36   * implementation of a MailAdapter, to add support for EnvelopeAccessors.
37   * <p>
38   * 
39   * <p>
40   * As the Envelope Test is an optional Sieve test, MailAdapter support for the
41   * interface is optional too.
42   * </p>
43   */
44  public class SieveEnvelopeMailAdapter extends SieveMailAdapter implements
45          EnvelopeAccessors {
46      /**
47       * The FROM address used in the SMTP MAIL command.
48       */
49      private String fieldEnvelopeFrom;
50  
51      /**
52       * The TO address used in the SMTP RCPT command that resulted in this
53       * message getting delivered to this user.
54       */
55      private String fieldEnvelopeTo;
56  
57      /**
58       * Constructor for SieveEnvelopeMailAdapter.
59       * 
60       * @param message
61       */
62      public SieveEnvelopeMailAdapter(MimeMessage message) {
63          super(message);
64      }
65  
66      /**
67       * Method getEnvelopes.
68       * 
69       * @return Map
70       */
71      protected Map<String, String> getEnvelopes() {
72          Map<String, String> envelopes = new HashMap<String, String>(2);
73          if (null != getEnvelopeFrom())
74              envelopes.put("From", getEnvelopeFrom());
75          if (null != getEnvelopeTo())
76              envelopes.put("To", getEnvelopeTo());
77          return envelopes;
78      }
79  
80      /**
81       * @see org.apache.jsieve.mail.optional.EnvelopeAccessors#getEnvelope(String)
82       */
83      public List<String> getEnvelope(String name) throws SieveMailException {
84          List<String> values = new ArrayList<String>(1);
85          String value = getEnvelopes().get(name);
86          if (null != value)
87              values.add(value);
88          return values;
89      }
90  
91      /**
92       * @see org.apache.jsieve.mail.optional.EnvelopeAccessors#getEnvelopeNames()
93       */
94      public List<String> getEnvelopeNames() throws SieveMailException {
95          return new ArrayList<String>(getEnvelopes().keySet());
96      }
97  
98      /**
99       * @see org.apache.jsieve.mail.optional.EnvelopeAccessors#getMatchingEnvelope(String)
100      */
101     public List<String> getMatchingEnvelope(String name) throws SieveMailException {
102         Iterator envelopeNamesIter = getEnvelopeNames().iterator();
103         List<String> matchedEnvelopeValues = new ArrayList<String>(32);
104         while (envelopeNamesIter.hasNext()) {
105             String envelopeName = (String) envelopeNamesIter.next();
106             if (envelopeName.trim().equalsIgnoreCase(name))
107                 matchedEnvelopeValues.addAll(getEnvelope(envelopeName));
108         }
109 
110         return matchedEnvelopeValues;
111     }
112 
113     /**
114      * Returns the from.
115      * 
116      * @return String
117      */
118     public String getEnvelopeFrom() {
119         return fieldEnvelopeFrom;
120     }
121 
122     /**
123      * Returns the recipient.
124      * 
125      * @return String
126      */
127     public String getEnvelopeTo() {
128         return fieldEnvelopeTo;
129     }
130 
131     /**
132      * Sets the from.
133      * 
134      * @param from
135      *            The from to set
136      */
137     public void setEnvelopeFrom(String from) {
138         fieldEnvelopeFrom = from;
139     }
140 
141     /**
142      * Sets the recipient.
143      * 
144      * @param recipient
145      *            The recipient to set
146      */
147     public void setEnvelopeTo(String recipient) {
148         fieldEnvelopeTo = recipient;
149     }
150 
151 }