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 java.util.ArrayList;
25  import java.util.Collection;
26  
27  import javax.mail.MessagingException;
28  import javax.mail.internet.MimeMessage;
29  
30  import org.apache.avalon.framework.service.ServiceException;
31  import org.apache.avalon.framework.service.ServiceManager;
32  import org.apache.james.Constants;
33  import org.apache.james.api.user.UsersRepository;
34  import org.apache.james.api.user.UsersStore;
35  import org.apache.james.api.vut.ErrorMappingException;
36  import org.apache.james.api.vut.VirtualUserTable;
37  import org.apache.mailet.MailAddress;
38  
39  /**
40   * Receives a Mail from JamesSpoolManager and takes care of delivery of the
41   * message to local inboxes.
42   * 
43   * Available configurations are:
44   * 
45   * <enableAliases>true</enableAliases>: specify wether the user aliases should
46   * be looked up or not. Default is false.
47   * 
48   * <enableForwarding>true</enableForwarding>: enable the forwarding. Default to
49   * false.
50   * 
51   * <usersRepository>LocalAdmins</usersRepository>: specific users repository
52   * name. Default to empty. If empty does lookup the default userRepository.
53   */
54  public class UsersRepositoryAliasingForwarding extends AbstractVirtualUserTableMailet {
55  
56      /**
57       * The user repository for this mail server. Contains all the users with
58       * inboxes on this server.
59       */
60      private UsersRepository usersRepository;
61  
62      /**
63       * Return a string describing this mailet.
64       * 
65       * @return a string describing this mailet
66       */
67      public String getMailetInfo() {
68          return "Local User Aliasing and Forwarding Mailet";
69      }
70  
71      /**
72       * Return null when the mail should be GHOSTed, the username string when it
73       * should be changed due to the ignoreUser configuration.
74       * 
75       * @param sender
76       * @param recipient
77       * @param message
78       * @throws MessagingException
79       */
80      public Collection processMail(MailAddress sender, MailAddress recipient,
81              MimeMessage message) throws MessagingException {
82          if (recipient == null) {
83              throw new IllegalArgumentException(
84                      "Recipient for mail to be spooled cannot be null.");
85          }
86          if (message == null) {
87              throw new IllegalArgumentException(
88                      "Mail message to be spooled cannot be null.");
89          }
90  
91          if (usersRepository instanceof VirtualUserTable) {
92              Collection mappings;
93              try {
94                  mappings = ((VirtualUserTable) usersRepository).getMappings(recipient.getUser(), recipient.getHost());
95              } catch (ErrorMappingException e) {
96                  StringBuffer errorBuffer = new StringBuffer(128)
97                      .append("A problem as occoured trying to alias and forward user ")
98                      .append(recipient)
99                      .append(": ")
100                     .append(e.getMessage());
101                     throw new MessagingException(errorBuffer.toString());
102             }
103             
104             if (mappings != null) {
105                 return handleMappings(mappings, sender, recipient, message);
106             }
107         } else {
108             StringBuffer errorBuffer = new StringBuffer(128)
109                 .append("Warning: the repository ")
110                 .append(usersRepository.getClass().getName())
111                 .append(" does not implement VirtualUserTable interface).");
112             getMailetContext().log(errorBuffer.toString());
113         }
114         String realName = usersRepository.getRealName(recipient.getUser());
115         if (realName != null) {
116             ArrayList ret = new ArrayList();
117             ret.add(new MailAddress(realName, recipient.getHost()));
118             return ret;
119         } else {
120             ArrayList ret = new ArrayList();
121             ret.add(recipient);
122             return ret;
123         }
124     }
125 
126     /**
127      * @see org.apache.mailet.GenericMailet#init()
128      */
129     public void init() throws MessagingException {
130         super.init();
131         ServiceManager compMgr = (ServiceManager) getMailetContext()
132                 .getAttribute(Constants.AVALON_COMPONENT_MANAGER);
133 
134         try {
135             String userRep = getInitParameter("usersRepository");
136             if (userRep == null || userRep.length() == 0) {
137                 try {
138                     usersRepository = (UsersRepository) compMgr
139                             .lookup(UsersRepository.ROLE);
140                 } catch (ServiceException e) {
141                     log("Failed to retrieve UsersRepository component:"
142                             + e.getMessage());
143                 }
144             } else {
145                 UsersStore usersStore = (UsersStore) compMgr.lookup(UsersStore.ROLE);
146                 usersRepository = usersStore.getRepository(userRep);
147             }
148 
149         } catch (ServiceException cnfe) {
150             log("Failed to retrieve UsersStore component:" + cnfe.getMessage());
151         }
152 
153     }
154 
155 }