Source Code Cross Referenced for UsersRepositoryAliasingForwarding.java in  » Web-Mail » james-2.3.1 » org » apache » james » transport » mailets » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Mail » james 2.3.1 » org.apache.james.transport.mailets 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /****************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one   *
003:         * or more contributor license agreements.  See the NOTICE file *
004:         * distributed with this work for additional information        *
005:         * regarding copyright ownership.  The ASF licenses this file   *
006:         * to you under the Apache License, Version 2.0 (the            *
007:         * "License"); you may not use this file except in compliance   *
008:         * with the License.  You may obtain a copy of the License at   *
009:         *                                                              *
010:         *   http://www.apache.org/licenses/LICENSE-2.0                 *
011:         *                                                              *
012:         * Unless required by applicable law or agreed to in writing,   *
013:         * software distributed under the License is distributed on an  *
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015:         * KIND, either express or implied.  See the License for the    *
016:         * specific language governing permissions and limitations      *
017:         * under the License.                                           *
018:         ****************************************************************/package org.apache.james.transport.mailets;
019:
020:        import org.apache.avalon.framework.service.ServiceException;
021:        import org.apache.avalon.framework.service.ServiceManager;
022:        import org.apache.james.Constants;
023:        import org.apache.james.services.JamesUser;
024:        import org.apache.james.services.User;
025:        import org.apache.james.services.UsersRepository;
026:        import org.apache.james.services.UsersStore;
027:        import org.apache.mailet.RFC2822Headers;
028:
029:        import org.apache.mailet.GenericMailet;
030:        import org.apache.mailet.Mail;
031:        import org.apache.mailet.MailAddress;
032:
033:        import javax.mail.MessagingException;
034:        import javax.mail.internet.MimeMessage;
035:
036:        import java.util.Collection;
037:        import java.util.HashSet;
038:        import java.util.Iterator;
039:        import java.util.LinkedList;
040:        import java.util.Vector;
041:
042:        /**
043:         * Receives a Mail from JamesSpoolManager and takes care of delivery of the
044:         * message to local inboxes.
045:         * 
046:         * Available configurations are:
047:         * 
048:         * <enableAliases>true</enableAliases>: specify wether the user aliases should
049:         * be looked up or not. Default is false.
050:         * 
051:         * <enableForwarding>true</enableForwarding>: enable the forwarding. Default to
052:         * false.
053:         * 
054:         * <usersRepository>LocalAdmins</usersRepository>: specific users repository
055:         * name. Default to empty. If empty does lookup the default userRepository.
056:         */
057:        public class UsersRepositoryAliasingForwarding extends GenericMailet {
058:
059:            /**
060:             * The user repository for this mail server. Contains all the users with
061:             * inboxes on this server.
062:             */
063:            private UsersRepository usersRepository;
064:
065:            /**
066:             * Whether to enable aliasing for users on this server
067:             */
068:            private boolean enableAliases;
069:
070:            /**
071:             * Whether to enable forwarding for users on this server
072:             */
073:            private boolean enableForwarding;
074:
075:            /**
076:             * Whether to ignore case when looking up user names on this server
077:             */
078:            private boolean ignoreCase;
079:
080:            /**
081:             * Delivers a mail to a local mailbox.
082:             * 
083:             * @param mail
084:             *            the mail being processed
085:             * 
086:             * @throws MessagingException
087:             *             if an error occurs while storing the mail
088:             */
089:            public void service(Mail mail) throws MessagingException {
090:                Collection recipients = mail.getRecipients();
091:                Collection errors = new Vector();
092:
093:                MimeMessage message = mail.getMessage();
094:
095:                // Set Return-Path and remove all other Return-Path headers from the
096:                // message
097:                // This only works because there is a placeholder inserted by
098:                // MimeMessageWrapper
099:                message.setHeader(RFC2822Headers.RETURN_PATH,
100:                        (mail.getSender() == null ? "<>" : "<"
101:                                + mail.getSender() + ">"));
102:
103:                Collection newRecipients = new LinkedList();
104:                for (Iterator i = recipients.iterator(); i.hasNext();) {
105:                    MailAddress recipient = (MailAddress) i.next();
106:                    try {
107:                        String username = processMail(mail.getSender(),
108:                                recipient, message);
109:
110:                        // if the username is null or changed we remove it from the
111:                        // remaining recipients
112:                        if (username == null) {
113:                            i.remove();
114:                        } else if (!username.equals(recipient.getUser())) {
115:                            i.remove();
116:                            // if the username has been changed we add a new recipient
117:                            // with the new name.
118:                            newRecipients.add(new MailAddress(username,
119:                                    recipient.getHost()));
120:                        }
121:
122:                    } catch (Exception ex) {
123:                        getMailetContext().log("Error while storing mail.", ex);
124:                        errors.add(recipient);
125:                    }
126:                }
127:
128:                if (newRecipients.size() > 0) {
129:                    recipients.addAll(newRecipients);
130:                }
131:
132:                if (!errors.isEmpty()) {
133:                    // If there were errors, we redirect the email to the ERROR
134:                    // processor.
135:                    // In order for this server to meet the requirements of the SMTP
136:                    // specification, mails on the ERROR processor must be returned to
137:                    // the sender. Note that this email doesn't include any details
138:                    // regarding the details of the failure(s).
139:                    // In the future we may wish to address this.
140:                    getMailetContext().sendMail(mail.getSender(), errors,
141:                            message, Mail.ERROR);
142:                }
143:
144:                if (recipients.size() == 0) {
145:                    // We always consume this message
146:                    mail.setState(Mail.GHOST);
147:                }
148:            }
149:
150:            /**
151:             * Return a string describing this mailet.
152:             * 
153:             * @return a string describing this mailet
154:             */
155:            public String getMailetInfo() {
156:                return "Local User Aliasing and Forwarding Mailet";
157:            }
158:
159:            /**
160:             * Return null when the mail should be GHOSTed, the username string when it
161:             * should be changed due to the ignoreUser configuration.
162:             * 
163:             * @param sender
164:             * @param recipient
165:             * @param message
166:             * @throws MessagingException
167:             */
168:            public String processMail(MailAddress sender,
169:                    MailAddress recipient, MimeMessage message)
170:                    throws MessagingException {
171:                String username;
172:                if (recipient == null) {
173:                    throw new IllegalArgumentException(
174:                            "Recipient for mail to be spooled cannot be null.");
175:                }
176:                if (message == null) {
177:                    throw new IllegalArgumentException(
178:                            "Mail message to be spooled cannot be null.");
179:                }
180:                if (ignoreCase) {
181:                    String originalUsername = recipient.getUser();
182:                    username = usersRepository.getRealName(originalUsername);
183:                    if (username == null) {
184:                        StringBuffer errorBuffer = new StringBuffer(128)
185:                                .append("The inbox for user ").append(
186:                                        originalUsername).append(
187:                                        " was not found on this server.");
188:                        throw new MessagingException(errorBuffer.toString());
189:                    }
190:                } else {
191:                    username = recipient.getUser();
192:                }
193:                User user;
194:                if (enableAliases || enableForwarding) {
195:                    user = usersRepository.getUserByName(username);
196:                    if (user instanceof  JamesUser) {
197:                        if (enableAliases && ((JamesUser) user).getAliasing()) {
198:                            username = ((JamesUser) user).getAlias();
199:                        }
200:                        // Forwarding takes precedence over local aliases
201:                        if (enableForwarding
202:                                && ((JamesUser) user).getForwarding()) {
203:                            MailAddress forwardTo = ((JamesUser) user)
204:                                    .getForwardingDestination();
205:                            if (forwardTo == null) {
206:                                StringBuffer errorBuffer = new StringBuffer(128)
207:                                        .append("Forwarding was enabled for ")
208:                                        .append(username)
209:                                        .append(
210:                                                " but no forwarding address was set for this account.");
211:                                throw new MessagingException(errorBuffer
212:                                        .toString());
213:                            }
214:                            Collection recipients = new HashSet();
215:                            recipients.add(forwardTo);
216:                            try {
217:                                getMailetContext().sendMail(sender, recipients,
218:                                        message);
219:                                StringBuffer logBuffer = new StringBuffer(128)
220:                                        .append("Mail for ").append(username)
221:                                        .append(" forwarded to ").append(
222:                                                forwardTo.toString());
223:                                getMailetContext().log(logBuffer.toString());
224:                                return null;
225:                            } catch (MessagingException me) {
226:                                StringBuffer logBuffer = new StringBuffer(128)
227:                                        .append("Error forwarding mail to ")
228:                                        .append(forwardTo.toString()).append(
229:                                                "attempting local delivery");
230:                                getMailetContext().log(logBuffer.toString());
231:                                throw me;
232:                            }
233:                        }
234:                    }
235:                }
236:                return username;
237:            }
238:
239:            /**
240:             * @see org.apache.mailet.GenericMailet#init()
241:             */
242:            public void init() throws MessagingException {
243:                super .init();
244:                ServiceManager compMgr = (ServiceManager) getMailetContext()
245:                        .getAttribute(Constants.AVALON_COMPONENT_MANAGER);
246:
247:                UsersStore usersStore;
248:                try {
249:                    usersStore = (UsersStore) compMgr.lookup(UsersStore.ROLE);
250:
251:                    enableAliases = new Boolean(getInitParameter(
252:                            "enableAliases", getMailetContext().getAttribute(
253:                                    Constants.DEFAULT_ENABLE_ALIASES)
254:                                    .toString())).booleanValue();
255:                    enableForwarding = new Boolean(getInitParameter(
256:                            "enableForwarding",
257:                            getMailetContext().getAttribute(
258:                                    Constants.DEFAULT_ENABLE_FORWARDING)
259:                                    .toString())).booleanValue();
260:                    ignoreCase = new Boolean(getInitParameter("ignoreCase",
261:                            getMailetContext().getAttribute(
262:                                    Constants.DEFAULT_IGNORE_USERNAME_CASE)
263:                                    .toString())).booleanValue();
264:
265:                    String userRep = getInitParameter("usersRepository");
266:                    if (userRep == null || userRep.length() == 0) {
267:                        try {
268:                            usersRepository = (UsersRepository) compMgr
269:                                    .lookup(UsersRepository.ROLE);
270:                        } catch (ServiceException e) {
271:                            log("Failed to retrieve UsersRepository component:"
272:                                    + e.getMessage());
273:                        }
274:                    } else {
275:                        usersRepository = usersStore.getRepository(userRep);
276:                    }
277:
278:                } catch (ServiceException cnfe) {
279:                    log("Failed to retrieve UsersStore component:"
280:                            + cnfe.getMessage());
281:                }
282:
283:            }
284:
285:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.