Source Code Cross Referenced for LdapUserHandler.java in  » Wiki-Engine » JAMWiki » org » jamwiki » ldap » 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 » Wiki Engine » JAMWiki » org.jamwiki.ldap 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003:         *
004:         * This program is free software; you can redistribute it and/or modify
005:         * it under the terms of the latest version of the GNU Lesser General
006:         * Public License as published by the Free Software Foundation;
007:         *
008:         * This program is distributed in the hope that it will be useful,
009:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
010:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011:         * GNU Lesser General Public License for more details.
012:         *
013:         * You should have received a copy of the GNU Lesser General Public License
014:         * along with this program (LICENSE.txt); if not, write to the Free Software
015:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
016:         */package org.jamwiki.ldap;
017:
018:        import java.util.Hashtable;
019:        import javax.naming.Context;
020:        import javax.naming.NamingEnumeration;
021:        import javax.naming.directory.Attributes;
022:        import javax.naming.directory.BasicAttribute;
023:        import javax.naming.directory.BasicAttributes;
024:        import javax.naming.directory.InitialDirContext;
025:        import javax.naming.directory.SearchResult;
026:        import org.apache.commons.lang.StringUtils;
027:        import org.jamwiki.Environment;
028:        import org.jamwiki.UserHandler;
029:        import org.jamwiki.model.WikiUserInfo;
030:        import org.jamwiki.utils.Encryption;
031:        import org.jamwiki.utils.WikiLogger;
032:
033:        /**
034:         * Implementation of the {@link org.jamwiki.UserHandler} interface that uses
035:         * an LDAP database for storing user login, password and other basic user
036:         * information.
037:         */
038:        public class LdapUserHandler implements  UserHandler {
039:
040:            private static final WikiLogger logger = WikiLogger
041:                    .getLogger(LdapUserHandler.class.getName());
042:
043:            private static String[] SEARCH_ATTRIBUTES = new String[4];
044:
045:            static {
046:                SEARCH_ATTRIBUTES[0] = Environment
047:                        .getValue(Environment.PROP_LDAP_FIELD_EMAIL);
048:                SEARCH_ATTRIBUTES[1] = Environment
049:                        .getValue(Environment.PROP_LDAP_FIELD_FIRST_NAME);
050:                SEARCH_ATTRIBUTES[2] = Environment
051:                        .getValue(Environment.PROP_LDAP_FIELD_LAST_NAME);
052:                SEARCH_ATTRIBUTES[3] = Environment
053:                        .getValue(Environment.PROP_LDAP_FIELD_USERID);
054:            }
055:
056:            /**
057:             *
058:             */
059:            public void addWikiUserInfo(WikiUserInfo userInfo,
060:                    Object transactionObject) throws Exception {
061:                throw new UnsupportedOperationException();
062:            }
063:
064:            /**
065:             *
066:             */
067:            public boolean authenticate(String username, String password)
068:                    throws Exception {
069:                InitialDirContext ctx = null;
070:                try {
071:                    username = this .fullDirectoryPath(username);
072:                    ctx = getContext(username, password);
073:                    return true;
074:                } catch (Exception e) {
075:                    // could not authenticate, return false
076:                    return false;
077:                } finally {
078:                    try {
079:                        ctx.close();
080:                    } catch (Exception e) {
081:                    }
082:                }
083:            }
084:
085:            /**
086:             *
087:             */
088:            private String fullDirectoryPath(String value) {
089:                // convert single user value to full path
090:                value = Environment
091:                        .getValue(Environment.PROP_LDAP_FIELD_USERID)
092:                        + "=" + value;
093:                String context = Environment
094:                        .getValue(Environment.PROP_LDAP_CONTEXT);
095:                if (!StringUtils.isBlank(context)) {
096:                    // add context
097:                    value += "," + context;
098:                }
099:                return value;
100:            }
101:
102:            /**
103:             * Connect to the LDAP server and return a context.
104:             *
105:             * @return The LDAP context to use when retrieving user information.
106:             */
107:            private InitialDirContext getContext(String username,
108:                    String password) throws Exception {
109:                // Set up the environment for creating the initial context
110:                Hashtable env = new Hashtable();
111:                env.put(Context.INITIAL_CONTEXT_FACTORY, Environment
112:                        .getValue(Environment.PROP_LDAP_FACTORY_CLASS));
113:                env.put(Context.PROVIDER_URL, Environment
114:                        .getValue(Environment.PROP_LDAP_URL));
115:                if (!StringUtils.isBlank(username)) {
116:                    // "simple" "DIGEST-MD5"
117:                    env
118:                            .put(
119:                                    Context.SECURITY_AUTHENTICATION,
120:                                    Environment
121:                                            .getValue(Environment.PROP_LDAP_SECURITY_AUTHENTICATION));
122:                    // cn=login, ou=NewHires, o=JNDITutorial
123:                    env.put(Context.SECURITY_PRINCIPAL, username);
124:                    env.put(Context.SECURITY_CREDENTIALS, password);
125:                }
126:                InitialDirContext ctx = new InitialDirContext(env);
127:                return ctx;
128:            }
129:
130:            /**
131:             *
132:             */
133:            private WikiUserInfo initWikiUserInfo(NamingEnumeration answer)
134:                    throws Exception {
135:                WikiUserInfo userInfo = new WikiUserInfo();
136:                SearchResult sr = (SearchResult) answer.next();
137:                Attributes attributes = sr.getAttributes();
138:                userInfo
139:                        .setEmail((String) attributes
140:                                .get(
141:                                        Environment
142:                                                .getValue(Environment.PROP_LDAP_FIELD_EMAIL))
143:                                .get());
144:                userInfo
145:                        .setFirstName((String) attributes
146:                                .get(
147:                                        Environment
148:                                                .getValue(Environment.PROP_LDAP_FIELD_FIRST_NAME))
149:                                .get());
150:                userInfo
151:                        .setLastName((String) attributes
152:                                .get(
153:                                        Environment
154:                                                .getValue(Environment.PROP_LDAP_FIELD_LAST_NAME))
155:                                .get());
156:                return userInfo;
157:            }
158:
159:            /**
160:             *
161:             */
162:            public boolean isWriteable() {
163:                return false;
164:            }
165:
166:            /**
167:             *
168:             */
169:            public WikiUserInfo lookupWikiUserInfo(String username)
170:                    throws Exception {
171:                InitialDirContext ctx = null;
172:                try {
173:                    ctx = getContext(Environment
174:                            .getValue(Environment.PROP_LDAP_LOGIN), Encryption
175:                            .getEncryptedProperty(
176:                                    Environment.PROP_LDAP_PASSWORD, null));
177:                    BasicAttributes matchAttrs = new BasicAttributes(true);
178:                    matchAttrs.put(new BasicAttribute(Environment
179:                            .getValue(Environment.PROP_LDAP_FIELD_USERID),
180:                            username));
181:                    NamingEnumeration answer = ctx.search(Environment
182:                            .getValue(Environment.PROP_LDAP_CONTEXT),
183:                            matchAttrs, SEARCH_ATTRIBUTES);
184:                    return (!answer.hasMore()) ? null : this 
185:                            .initWikiUserInfo(answer);
186:                } finally {
187:                    try {
188:                        ctx.close();
189:                    } catch (Exception e) {
190:                    }
191:                }
192:            }
193:
194:            /**
195:             *
196:             */
197:            public void updateWikiUserInfo(WikiUserInfo userInfo,
198:                    Object transactionObject) throws Exception {
199:                throw new UnsupportedOperationException();
200:            }
201:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.