View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.transport.mailets;
19  
20  import org.apache.commons.collections.iterators.IteratorChain;
21  import org.apache.mailet.GenericMailet;
22  import org.apache.mailet.Mail;
23  import org.apache.mailet.MailetConfig;
24  import org.apache.mailet.MailetContext;
25  
26  import javax.mail.MessagingException;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Iterator;
31  
32  /***
33   * Receives a Mail from JamesSpoolManager and takes care of delivery of the
34   * message to local inboxes.
35   * 
36   * Since James 2.3.0 this mailet is a composition of
37   * UserRepositoryAliasingForwarding and ToMultiRepository
38   * configurated to mimic the old "LocalDelivery" behaviour.
39   */
40  public class LocalDelivery extends GenericMailet {
41  
42      /***
43       * Mailet that apply aliasing and forwarding
44       */
45      private UsersRepositoryAliasingForwarding aliasingMailet;
46  
47      /***
48       * Mailet that actually store the message
49       */
50      private ToMultiRepository deliveryMailet;
51  
52      /***
53       * Delivers a mail to a local mailbox.
54       * 
55       * @param mail
56       *            the mail being processed
57       * 
58       * @throws MessagingException
59       *             if an error occurs while storing the mail
60       */
61      public void service(Mail mail) throws MessagingException {
62          aliasingMailet.service(mail);
63          if (mail.getState() != Mail.GHOST) {
64              deliveryMailet.service(mail);
65          }
66      }
67  
68      /***
69       * Return a string describing this mailet.
70       * 
71       * @return a string describing this mailet
72       */
73      public String getMailetInfo() {
74          return "Local Delivery Mailet";
75      }
76  
77      /***
78       * @see org.apache.mailet.GenericMailet#init()
79       */
80      public void init() throws MessagingException {
81          super.init();
82          aliasingMailet = new UsersRepositoryAliasingForwarding();
83          aliasingMailet.init(getMailetConfig());
84          deliveryMailet = new ToMultiRepository();
85          MailetConfig m = new MailetConfig() {
86  
87              /***
88               * @see org.apache.mailet.MailetConfig#getInitParameter(java.lang.String)
89               */
90              public String getInitParameter(String name) {
91                  if ("addDeliveryHeader".equals(name)) {
92                      return "Delivered-To";
93                  } else if ("resetReturnPath".equals(name)) {
94                      return "true";
95                  } else {
96                      return getMailetConfig().getInitParameter(name);
97                  }
98              }
99  
100             /***
101              * @see org.apache.mailet.MailetConfig#getInitParameterNames()
102              */
103             public Iterator getInitParameterNames() {
104                 IteratorChain c = new IteratorChain();
105                 Collection h = new ArrayList();
106                 h.add("addDeliveryHeader");
107                 h.add("resetReturnPath");
108                 c.addIterator(getMailetConfig().getInitParameterNames());
109                 c.addIterator(h.iterator());
110                 return c;
111             }
112 
113             /***
114              * @see org.apache.mailet.MailetConfig#getMailetContext()
115              */
116             public MailetContext getMailetContext() {
117                 return getMailetConfig().getMailetContext();
118             }
119 
120             /***
121              * @see org.apache.mailet.MailetConfig#getMailetName()
122              */
123             public String getMailetName() {
124                 return getMailetConfig().getMailetName();
125             }
126 
127         };
128         deliveryMailet.init(m);
129     }
130 
131 }