- Timestamp:
- 10/28/09 20:58:21 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
contrib/psync/src/main/java/br/com/prognus/psync/items/dao/PIMEntityDAO.java
r1103 r1545 23 23 import java.sql.ResultSet; 24 24 import java.sql.Timestamp; 25 import java.util.Enumeration; 26 import java.util.Iterator; 25 import java.util.Hashtable; 27 26 import java.util.List; 28 27 28 import javax.naming.Context; 29 import javax.naming.NamingEnumeration; 29 30 import javax.naming.NamingException; 31 import javax.naming.directory.DirContext; 32 import javax.naming.directory.InitialDirContext; 33 import javax.naming.directory.SearchControls; 34 import javax.naming.directory.SearchResult; 30 35 import javax.sql.DataSource; 31 36 … … 38 43 import com.funambol.framework.tools.DBTools; 39 44 import com.funambol.framework.tools.DataSourceTools; 40 import com.novell.ldap.LDAPAttribute;41 import com.novell.ldap.LDAPAttributeSet;42 import com.novell.ldap.LDAPConnection;43 import com.novell.ldap.LDAPEntry;44 import com.novell.ldap.LDAPException;45 import com.novell.ldap.LDAPSearchResults;46 import com.novell.ldap.util.Base64;47 45 48 46 public abstract class PIMEntityDAO { … … 123 121 // -----------------------------------------------------------Public methods 124 122 123 @SuppressWarnings("unchecked") 125 124 protected String LdapUID(String server, String dc, String user) 126 125 throws Exception { 127 126 128 LDAPConnection ldap = new LDAPConnection(); 129 String searchAttrs[] = { "uidNumber" }; 130 String id_owner = null; 131 132 try { 133 134 ldap.connect(server, 389); 135 LDAPSearchResults searchResults = ldap.search(dc, 136 LDAPConnection.SCOPE_SUB, "(uid=" + user + ")", 137 searchAttrs, false); 138 139 while (searchResults.hasMore()) { 140 141 LDAPEntry nextEntry = null; 142 try { 143 nextEntry = searchResults.next(); 144 } catch (LDAPException e) { 145 if (e.getResultCode() == LDAPException.LDAP_TIMEOUT 146 || e.getResultCode() == LDAPException.CONNECT_ERROR) 147 break; 148 else 149 continue; 150 } 151 LDAPAttributeSet attributeSet = nextEntry.getAttributeSet(); 152 Iterator allAttributes = attributeSet.iterator(); 153 154 while (allAttributes.hasNext()) { 155 156 LDAPAttribute attribute = (LDAPAttribute) allAttributes 157 .next(); 158 String attributeName = attribute.getName(); 159 Enumeration allValues = attribute.getStringValues(); 160 161 if (allValues != null) { 162 163 while (allValues.hasMoreElements()) { 164 165 String Value = (String) allValues.nextElement(); 166 167 if (Base64.isLDIFSafe(Value)) { 168 } else { 169 Value = Base64.encode(Value.getBytes()); 170 } 171 172 if (attributeName.equalsIgnoreCase("uidNumber")) { 173 id_owner = Value; 174 break; 175 } 176 177 } 178 } 179 } 180 } 181 182 ldap.disconnect(); 183 184 if (id_owner == null) { 185 throw new LDAPException(); 186 } 187 188 } catch (LDAPException e) { 189 log.info("Error while connecting and binding to LDAP: " 190 + e.toString()); 191 } 192 193 return id_owner; 127 String uidNumber=null; 128 129 Hashtable env = new Hashtable(); 130 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 131 env.put(Context.PROVIDER_URL, "ldap://" + server + ":" + 389); 132 133 DirContext ctx = new InitialDirContext(env); 134 // Searching User 135 SearchControls ctls = new SearchControls(); 136 ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 137 ctls.setCountLimit(1); 138 ctls.setTimeLimit(10000); // Espera no max 10 segundos 139 String filter = "(uid=" + user + ")"; 140 141 NamingEnumeration answer = ctx.search(dc, filter, ctls); 142 if (answer.hasMore()) { 143 SearchResult sr = (SearchResult) answer.next(); 144 try { 145 // Getting User attributes 146 uidNumber = sr.getAttributes().get("uidNumber").get(0).toString(); 147 } catch (Exception e) { 148 e.printStackTrace(); 149 } 150 } 151 ctx.close(); 152 if (uidNumber == null) { 153 log.info("Error while connecting and binding to LDAP"); 154 } 155 156 return uidNumber; 157 158 159 // LDAPConnection ldap = new LDAPConnection(); 160 // String searchAttrs[] = { "uidNumber" }; 161 // String id_owner = null; 162 // 163 // try { 164 // 165 // ldap.connect(server, 389); 166 // LDAPSearchResults searchResults = ldap.search(dc, 167 // LDAPConnection.SCOPE_SUB, "(uid=" + user + ")", 168 // searchAttrs, false); 169 // 170 // while (searchResults.hasMore()) { 171 // 172 // LDAPEntry nextEntry = null; 173 // try { 174 // nextEntry = searchResults.next(); 175 // } catch (LDAPException e) { 176 // if (e.getResultCode() == LDAPException.LDAP_TIMEOUT 177 // || e.getResultCode() == LDAPException.CONNECT_ERROR) 178 // break; 179 // else 180 // continue; 181 // } 182 // LDAPAttributeSet attributeSet = nextEntry.getAttributeSet(); 183 // Iterator allAttributes = attributeSet.iterator(); 184 // 185 // while (allAttributes.hasNext()) { 186 // 187 // LDAPAttribute attribute = (LDAPAttribute) allAttributes 188 // .next(); 189 // String attributeName = attribute.getName(); 190 // Enumeration allValues = attribute.getStringValues(); 191 // 192 // if (allValues != null) { 193 // 194 // while (allValues.hasMoreElements()) { 195 // 196 // String Value = (String) allValues.nextElement(); 197 // 198 // if (Base64.isLDIFSafe(Value)) { 199 // } else { 200 // Value = Base64.encode(Value.getBytes()); 201 // } 202 // 203 // if (attributeName.equalsIgnoreCase("uidNumber")) { 204 // id_owner = Value; 205 // break; 206 // } 207 // 208 // } 209 // } 210 // } 211 // } 212 // 213 // ldap.disconnect(); 214 // 215 // if (id_owner == null) { 216 // throw new LDAPException(); 217 // } 218 // 219 // } catch (LDAPException e) { 220 // log.info("Error while connecting and binding to LDAP: " 221 // + e.toString()); 222 // } 223 // return id_owner; 194 224 } 195 225
Note: See TracChangeset
for help on using the changeset viewer.