1 /*******************************************************************************
2 * Imixs Workflow
3 * Copyright (C) 2001, 2011 Imixs Software Solutions GmbH,
4 * http://www.imixs.com
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You can receive a copy of the GNU General Public
17 * License at http://www.gnu.org/licenses/gpl.html
18 *
19 * Project:
20 * http://www.imixs.org
21 * http://java.net/projects/imixs-workflow
22 *
23 * Contributors:
24 * Imixs Software Solutions GmbH - initial API and implementation
25 * Ralph Soika - Software Developer
26 *******************************************************************************/
27
28 package org.imixs.workflow.plugins.jee.extended;
29
30 import javax.mail.internet.AddressException;
31 import javax.mail.internet.InternetAddress;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingEnumeration;
35 import javax.naming.NamingException;
36 import javax.naming.directory.Attribute;
37 import javax.naming.directory.Attributes;
38 import javax.naming.directory.DirContext;
39 import javax.naming.directory.SearchControls;
40 import javax.naming.directory.SearchResult;
41
42 import org.imixs.workflow.plugins.jee.MailPlugin;
43
44 /**
45 * This Plugin extends the standard JEE MailPlugin to lookup smtp mail addresses from an OpenLDAP
46 * directory by a given username. Therefore the Plugin expects an JNDI Ressource to the
47 * corresponding directory identified by the jndi name:
48 * 'org.imixs.mail.directory'
49 * <p>
50 * The search phrase is <code>(uid=%u)</code> where %u is the username to be
51 * translated into a smtp address.
52 *
53 *
54 *
55 * @author rsoika
56 *
57 */
58 public class OpenLDAPMailPlugin extends MailPlugin {
59
60 public static final String LDAP_JNDI_NAME = "org.imixs.mail.directory";
61
62 /**
63 * this method tries to lookup a users smtp address from the ldap directory
64 * if no smtp name is provided. The method overwrite the standard behaivor
65 * form the MailPlugin Class.
66 *
67 * @param aAddr
68 * @return
69 * @throws AddressException
70 */
71 public InternetAddress getInternetAddress(String aAddr)
72 throws AddressException {
73
74 // is smtp address provided?
75 if (aAddr.indexOf('@') > -1)
76 // yes - return imitate
77 return super.getInternetAddress(aAddr);
78
79 // try to get email from ldap directory....
80 try {
81 aAddr = fetchEmail(aAddr);
82 } catch (NamingException e) {
83 // no valid email was found!
84 System.out.println("OpenLDAPMailPlugin: mail for '" + aAddr
85 + "' not found");
86 //e.printStackTrace();
87 }
88 return new InternetAddress(aAddr);
89 }
90
91 /**
92 * This method searches a mail address for a specific username.
93 *
94 * @param aUsername
95 * @return
96 * @throws NamingException
97 */
98 private String fetchEmail(String aUsername) throws NamingException {
99 Context initCtx = new InitialContext();
100 DirContext ldapCtx = (DirContext) initCtx.lookup(LDAP_JNDI_NAME);
101
102
103 SearchControls ctls = new SearchControls();
104 ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
105
106 String searchfilter = "";
107 searchfilter = "(uid=" + aUsername + ")";
108
109 NamingEnumeration answer = ldapCtx.search("", searchfilter, ctls);
110
111 if (answer.hasMore()) {
112 SearchResult entry = (SearchResult) answer.next();
113 Attributes attrs = entry.getAttributes();
114 // read mail Attribute
115 Attribute attr = attrs.get("mail");
116 if (attr != null)
117 return (String) attr.get(0);
118 }
119
120 return aUsername;
121 }
122 }