Rev 1310 | Rev 1808 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | fvelez | 1 | package openldap.impl; |
2 | |||
3 | |||
1807 | jmachado | 4 | import jomm.utils.DiacriticFilter; |
1 | fvelez | 5 | import netscape.ldap.LDAPConnection; |
6 | import netscape.ldap.LDAPException; |
||
7 | import openldap.ILdapManager; |
||
8 | import openldap.LdapProperties; |
||
9 | import org.apache.log4j.Logger; |
||
10 | |||
11 | import java.sql.*; |
||
12 | import java.util.HashMap; |
||
13 | import java.util.List; |
||
14 | import java.util.Iterator; |
||
15 | |||
16 | /** |
||
17 | * Created by IntelliJ IDEA. |
||
18 | * User: User |
||
19 | * Date: 9/Abr/2005 |
||
20 | * Time: 12:04:08 |
||
21 | * To change this template use File | Settings | File Templates. |
||
22 | */ |
||
23 | public class LdapManager implements ILdapManager{ |
||
24 | |||
25 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(LdapManager.class); |
||
26 | |||
27 | private static final 1.5.0/docs/api/java/lang/String.html">String host =LdapProperties.getProperty("ldap.host"); |
||
28 | private static final int port =LdapProperties.getIntProperty("ldap.port"); |
||
29 | private static final 1.5.0/docs/api/java/lang/String.html">String baseQueries =LdapProperties.getProperty("ldap.base.queries"); |
||
30 | private static final 1.5.0/docs/api/java/lang/String.html">String login =LdapProperties.getProperty("ldap.queries.login"); |
||
31 | private static final 1.5.0/docs/api/java/lang/String.html">String password =LdapProperties.getProperty("ldap.queries.password"); |
||
32 | private static final 1.5.0/docs/api/java/lang/String.html">String userField =LdapProperties.getProperty("ldap.username.field"); |
||
33 | private static final 1.5.0/docs/api/java/lang/String.html">String dnField =LdapProperties.getProperty("ldap.dn.field"); |
||
34 | |||
35 | |||
36 | public boolean login(1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String password) { |
||
37 | return authenticate(user,password); |
||
38 | } |
||
39 | |||
40 | public 1.5.0/docs/api/java/util/HashMap.html">HashMap getUserInfo(1.5.0/docs/api/java/lang/String.html">String username) { |
||
41 | 1.5.0/docs/api/java/sql/Connection.html">Connection con = null; |
||
42 | 1.5.0/docs/api/java/util/HashMap.html">HashMap result = null; |
||
43 | try { |
||
44 | con = getConnection(); |
||
45 | 5+0%2Fdocs%2Fapi+Statement">Statement stmt = con.createStatement(); |
||
46 | 1.5.0/docs/api/java/sql/ResultSet.html">ResultSet rs = stmt.executeQuery("SELECT * FROM " + baseQueries + " where " + userField + "=" + username); |
||
47 | if(rs.next()) { |
||
48 | result = new 1.5.0/docs/api/java/util/HashMap.html">HashMap(); |
||
49 | 5+0%2Fdocs%2Fapi+List">List propertyValues = LdapProperties.getListValuesProperties("ldap.map"); |
||
50 | 1.5.0/docs/api/java/util/Iterator.html">Iterator iter = propertyValues.iterator(); |
||
51 | while (iter.hasNext()) { |
||
52 | 1.5.0/docs/api/java/lang/String.html">String propValue = (1.5.0/docs/api/java/lang/String.html">String) iter.next(); |
||
53 | result.put(propValue,rs.getString(propValue)); |
||
54 | } |
||
55 | } |
||
56 | stmt.close(); |
||
57 | con.close(); |
||
58 | |||
59 | } catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e) { |
||
60 | e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
||
61 | } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) { |
||
62 | e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
||
63 | } |
||
64 | |||
65 | return result; |
||
66 | } |
||
67 | |||
68 | public 1.5.0/docs/api/java/sql/Connection.html">Connection getConnection() throws 1.5.0/docs/api/java/sql/SQLException.html">SQLException, 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException { |
||
69 | 1.5.0/docs/api/java/lang/Class.html">Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver"); |
||
70 | 1.5.0/docs/api/java/lang/String.html">String ldapConnectString = "jdbc:ldap://" + host + ":" + port + "/" + baseQueries + "?SEARCH_SCOPE:=subTreeScope"; |
||
71 | java.sql.1.5.0/docs/api/java/sql/Connection.html">Connection con; |
||
72 | con = 1.5.0/docs/api/java/sql/DriverManager.html">DriverManager.getConnection(ldapConnectString,login,password); |
||
73 | return con; |
||
74 | } |
||
75 | public 1.5.0/docs/api/java/lang/String.html">String getDN(1.5.0/docs/api/java/lang/String.html">String username) throws 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException, 1.5.0/docs/api/java/sql/SQLException.html">SQLException { |
||
76 | |||
77 | 1.5.0/docs/api/java/sql/Connection.html">Connection con = getConnection(); |
||
78 | 1.5.0/docs/api/java/lang/String.html">String DN= null; |
||
79 | 5+0%2Fdocs%2Fapi+Statement">Statement stmt = con.createStatement(); |
||
175 | jmachado | 80 | //" + dnField + " |
1807 | jmachado | 81 | 1.5.0/docs/api/java/sql/ResultSet.html">ResultSet rs = stmt.executeQuery("SELECT * FROM " + baseQueries + " where " + userField + "=" + username); |
82 | //ResultSet rs = stmt.executeQuery("SELECT " + dnField + " FROM " + baseQueries + " where cn=" + username); |
||
1 | fvelez | 83 | if(rs.next()) { |
1807 | jmachado | 84 | DN = rs.getString("DN"); |
1 | fvelez | 85 | } |
86 | stmt.close(); |
||
87 | con.close(); |
||
88 | return DN; |
||
89 | } |
||
90 | |||
91 | public boolean authenticate(1.5.0/docs/api/java/lang/String.html">String username,1.5.0/docs/api/java/lang/String.html">String password){ |
||
92 | try { |
||
93 | 1.5.0/docs/api/java/lang/String.html">String dn = getDN(username); |
||
94 | if(dn == null) |
||
95 | return false; |
||
96 | |||
97 | LDAPConnection ld = new LDAPConnection(); |
||
98 | ld.connect( host, 389 ); |
||
99 | |||
100 | if (! ld.isConnected() ){ |
||
101 | logger.error("Cant connect to LDAP"); |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | try { |
||
1807 | jmachado | 106 | ld.authenticate(DiacriticFilter.clean(dn),password); |
1 | fvelez | 107 | } catch ( LDAPException e ) { |
108 | 1.5.0/docs/api/java/lang/System.html">System.out.println(e.toString()); |
||
109 | ld.disconnect(); |
||
110 | return false; |
||
111 | } //catch |
||
112 | |||
113 | if (ld.isAuthenticated()) { |
||
114 | ld.disconnect(); |
||
115 | return true; |
||
116 | } else { |
||
117 | ld.disconnect(); |
||
118 | return false; |
||
119 | } |
||
120 | } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) { |
||
121 | logger.error("Logging LDAP",e); |
||
122 | } catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e) { |
||
123 | logger.error("Logging LDAP",e); |
||
124 | } catch (LDAPException e) { |
||
125 | logger.error("Logging LDAP",e); |
||
126 | } |
||
127 | return false; |
||
128 | } |
||
129 | |||
130 | |||
131 | |||
132 | } |