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