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.james.transport.mailets;
21  
22  import org.apache.commons.collections.iterators.IteratorChain;
23  import org.apache.mailet.GenericMailet;
24  import org.apache.mailet.Mail;
25  import org.apache.mailet.MailetConfig;
26  import org.apache.mailet.MailetContext;
27  
28  import javax.mail.MessagingException;
29  
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.Iterator;
33  
34  /***
35   * Receives a Mail from JamesSpoolManager and takes care of delivery of the
36   * message to local inboxes.
37   * 
38   * Since James 2.3.0 this mailet is a composition of
39   * UserRepositoryAliasingForwarding and ToMultiRepository
40   * configurated to mimic the old "LocalDelivery" behaviour.
41   */
42  public class LocalDelivery extends GenericMailet {
43  
44      /***
45       * Mailet that apply aliasing and forwarding
46       */
47      private UsersRepositoryAliasingForwarding aliasingMailet;
48  
49      /***
50       * Mailet that actually store the message
51       */
52      private ToMultiRepository deliveryMailet;
53  
54      /***
55       * Delivers a mail to a local mailbox.
56       * 
57       * @param mail
58       *            the mail being processed
59       * 
60       * @throws MessagingException
61       *             if an error occurs while storing the mail
62       */
63      public void service(Mail mail) throws MessagingException {
64          aliasingMailet.service(mail);
65          if (mail.getState() != Mail.GHOST) {
66              deliveryMailet.service(mail);
67          }
68      }
69  
70      /***
71       * Return a string describing this mailet.
72       * 
73       * @return a string describing this mailet
74       */
75      public String getMailetInfo() {
76          return "Local Delivery Mailet";
77      }
78  
79      /***
80       * @see org.apache.mailet.GenericMailet#init()
81       */
82      public void init() throws MessagingException {
83          super.init();
84          aliasingMailet = new UsersRepositoryAliasingForwarding();
85          aliasingMailet.init(getMailetConfig());
86          deliveryMailet = new ToMultiRepository();
87          MailetConfig m = new MailetConfig() {
88  
89              /***
90               * @see org.apache.mailet.MailetConfig#getInitParameter(java.lang.String)
91               */
92              public String getInitParameter(String name) {
93                  if ("addDeliveryHeader".equals(name)) {
94                      return "Delivered-To";
95                  } else if ("resetReturnPath".equals(name)) {
96                      return "true";
97                  } else {
98                      return getMailetConfig().getInitParameter(name);
99                  }
100             }
101 
102             /***
103              * @see org.apache.mailet.MailetConfig#getInitParameterNames()
104              */
105             public Iterator getInitParameterNames() {
106                 IteratorChain c = new IteratorChain();
107                 Collection h = new ArrayList();
108                 h.add("addDeliveryHeader");
109                 h.add("resetReturnPath");
110                 c.addIterator(getMailetConfig().getInitParameterNames());
111                 c.addIterator(h.iterator());
112                 return c;
113             }
114 
115             /***
116              * @see org.apache.mailet.MailetConfig#getMailetContext()
117              */
118             public MailetContext getMailetContext() {
119                 return getMailetConfig().getMailetContext();
120             }
121 
122             /***
123              * @see org.apache.mailet.MailetConfig#getMailetName()
124              */
125             public String getMailetName() {
126                 return getMailetConfig().getMailetName();
127             }
128 
129         };
130         deliveryMailet.init(m);
131     }
132 
133 }