public static LDAPConnection getConnection() throws LDAPException { // host, port, username and password return new LDAPConnection("com.example.local", 389, "Administrator@com.example.local", "admin"); }
/** * Disconnects the given LDAP connection, logging any failure to do so * appropriately. * * @param ldapConnection * The LDAP connection to disconnect. */ public void disconnect(LDAPConnection ldapConnection) { // Attempt disconnect try { ldapConnection.disconnect(); } // Warn if disconnect unexpectedly fails catch (LDAPException e) { logger.warn("Unable to disconnect from LDAP server: {}", e.getMessage()); logger.debug("LDAP disconnect failed.", e); } }
public static List<SearchResultEntry> getResults(LDAPConnection connection, String baseDN, String filter) throws LDAPSearchException { SearchResult searchResult; if (connection.isConnected()) { searchResult = connection.search(baseDN, SearchScope.ONE, filter); return searchResult.getSearchEntries(); } return null; }
LDAPConstraints ldapConstraints = ldapConnection.getConstraints(); if (ldapConstraints == null) ldapConstraints = new LDAPConstraints(); ldapConstraints.setReferralFollowing(confService.getFollowReferrals()); ldapConstraints.setReferralHandler(new ReferralAuthHandler(userDN, password)); ldapConstraints.setHopLimit(confService.getMaxReferralHops()); ldapConstraints.setTimeLimit(confService.getOperationTimeout() * 1000); ldapConnection.setConstraints(ldapConstraints); ldapConnection.connect( confService.getServerHostname(), confService.getServerPort() ldapConnection.startTLS(); logger.error("Unable to connect to LDAP server: {}", e.getMessage()); logger.debug("Failed to connect to LDAP server.", e); return null; ldapConnection.bind(LDAPConnection.LDAP_V3, userDN, passwordBytes);
public Map<String, Object> get() { if (handleEndOfResults()) return null; try { Map<String, Object> entry = new LinkedHashMap<>(attributes.size() + 1); LDAPEntry en = null; en = lsr.next(); entry.put("dn", en.getDN()); if (attributes != null && attributes.size() > 0) { for (int col = 0; col < attributes.size(); col++) { Object val = readValue(en.getAttributeSet().getAttribute(attributes.get(col))); if (val != null) entry.put(attributes.get(col),val ); } } else { // make it dynamic Iterator<LDAPAttribute> iter = en.getAttributeSet().iterator(); while (iter.hasNext()) { LDAPAttribute attr = iter.next(); Object val = readValue(attr); if (val != null) entry.put(attr.getName(), readValue(attr)); } } //System.out.println("entry " + entry); return entry; } catch (LDAPException e) { e.printStackTrace(); throw new RuntimeException("Error getting next ldap entry " + e.getLDAPErrorMessage()); } }
@Test public void testLoadLDAP() throws Exception { Map<String, Object> connParms = new HashMap<>(); connParms.put("ldapHost", "ldap.forumsys.com"); connParms.put("ldapPort", 389l); connParms.put("loginDN", "cn=read-only-admin,dc=example,dc=com"); connParms.put("loginPW", "password"); LoadLdap.LDAPManager mgr = new LoadLdap.LDAPManager(LoadLdap.getConnectionMap(connParms)); Map<String, Object> searchParms = new HashMap<>(); searchParms.put("searchBase", "dc=example,dc=com"); searchParms.put("searchScope", "SCOPE_ONE"); searchParms.put("searchFilter", "(&(objectClass=*)(uid=training))"); ArrayList<String> ats = new ArrayList<>(); ats.add("uid"); searchParms.put("attributes", ats); LDAPSearchResults results = mgr.doSearch(searchParms); LDAPEntry le = results.next(); assertEquals("uid=training,dc=example,dc=com", le.getDN()); assertEquals("training", le.getAttribute("uid").getStringValue()); }
private LDAPConnection getConnection() throws LDAPException, UnsupportedEncodingException { // LDAPSocketFactory ssf; // Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); // String path ="C:\\j2sdk1.4.2_09\\jre\\lib\\security\\cacerts"; //op("the trustStore: " + System.getProperty("javax.net.ssl.trustStore")); // System.setProperty("javax.net.ssl.trustStore", path); // op(" reading the strustStore: " + System.getProperty("javax.net.ssl.trustStore")); // ssf = new LDAPJSSESecureSocketFactory(); // LDAPConnection.setSocketFactory(ssf); LDAPConnection lc = new LDAPConnection(); lc.connect(ldapHost, ldapPort); // bind to the server lc.bind(ldapVersion, loginDN, password.getBytes("UTF8")); // tbd // LDAPConnection pooling here? // return lc; }
lc = getConnection(); LDAPSearchConstraints cons = new LDAPSearchConstraints(); cons.setMaxResults(0); // no limit LDAPSearchResults searchResults = null; if (attributeList == null || attributeList.size() == 0) { searchResults = lc.search(searchBase, searchScope, searchFilter, null, false, cons); } else { searchResults = lc.search(searchBase, searchScope, searchFilter, attributeList.toArray(new String[0]), false, cons);
return new LDAPConnection(); return new LDAPConnection(new LDAPJSSESecureSocketFactory()); return new LDAPConnection(new LDAPJSSEStartTLSFactory());
private Object readValue(LDAPAttribute att) { if (att == null) return null; if (att.size() == 1) { // single value // for now everything is string return att.getStringValue(); } else { return att.getStringValueArray(); } } }
private boolean handleEndOfResults() { if (!lsr.hasMore()) { return true; } return false; } private Object readValue(LDAPAttribute att) {
public static void closeIt(LDAPConnection lc) { try { lc.disconnect(); } catch (Exception e) { // ignore e.printStackTrace(); } }
lc.connect( ldapHost, ldapPort ); lc.bind( ldapVersion, loginDN, password.getBytes("UTF8")); LDAPControl ldapCtrl = new LDAPControl("1.3.6.1.4.1.637.81.2.10.10", false, null); LDAPSearchConstraints cons = lc.getSearchConstraints(); cons.setControls( ldapCtrl ); lc.setConstraints(cons); LDAPSearchResults searchResults = lc.search("",LDAPConnection.SCOPE_BASE, "(objectclass=*)", returnedAttributes,attributeOnly , cons); LDAPControl[] controls = searchResults.getResponseControls();
/** * Creates a ReferralAuthHandler object to handle authentication when * following referrals in a LDAP connection, using the provided dn and * password. */ public ReferralAuthHandler(String dn, String password) { byte[] passwordBytes; try { // Convert password into corresponding byte array if (password != null) passwordBytes = password.getBytes("UTF-8"); else passwordBytes = null; } catch (UnsupportedEncodingException e) { logger.error("Unexpected lack of support for UTF-8: {}", e.getMessage()); logger.debug("Support for UTF-8 (as required by Java spec) not found.", e); throw new UnsupportedOperationException("Unexpected lack of UTF-8 support.", e); } ldapAuth = new LDAPAuthProvider(dn, passwordBytes); }
private LDAPSchema getSchema() throws LDAPException, UnsupportedEncodingException { LDAPSchema r = null; LDAPConnection lc = getConnection(); r = lc.fetchSchema(lc.getSchemaDN()); closeIt(lc); //op( r.toString()); return r; }
public void rename( String dn, String newRdn, String newParentDN, boolean delOld, LDAPConstraints constr) throws LDAPException { LDAPControl[] controls = constr != null ? constr.getControls() : null; LDAPModifyDNRequest msg = new LDAPModifyDNRequest(dn,newRdn,newParentDN,delOld,controls); this.sendMessage(msg); }
private LDAPEntry read(String dn) throws LDAPException, UnsupportedEncodingException { if (dn == null) return null; LDAPEntry r = null; op("read start for dn: " + dn); LDAPConnection lc = getConnection(); r = lc.read(dn); closeIt(lc); // op( r.toString()); op("read end"); return r; }
public void add(LDAPEntry entry, LDAPConstraints cont) throws LDAPException { LDAPControl[] conts = cont != null ? cont.getControls() : null; LDAPAddRequest add = new LDAPAddRequest(entry,conts); this.sendMessage(add); }
/** * Returns the message agent for this msg ID */ /* package */ final MessageAgent getMessageAgent( int msgId) throws NoSuchFieldException { Message info = messages.findMessageById( msgId); return info.getMessageAgent(); }
protected void initHandler() { //set LDAPAttribute handler. setchildelement(LDAPAttribute.getXMLHandler("attr",this)); } protected void endElement() {