public static LDAPConnection getConnection() throws LDAPException { // host, port, username and password return new LDAPConnection("com.example.local", 389, "Administrator@com.example.local", "admin"); }
return new LDAPConnection(); return new LDAPConnection(new LDAPJSSESecureSocketFactory()); return new LDAPConnection(new LDAPJSSEStartTLSFactory());
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; }
/** * Constructs a new LDAPConnection object, which represents a connection * to an LDAP server. * * @see <a href="../../../../api/com/novell/ldap/LDAPConnection.html#LDAPConnection()"> com.novell.ldap.LDAPConnection.LDAPConnection()</a> */ public LDAPConnection() { conn = new com.novell.ldap.LDAPConnection(); return; }
LDAPConnection ldap = new LDAPConnection("ldap.example.com", 389);
int maxAttempts = 3; for(int attempts = 0; attempts < maxAttempts; attempts++) { try { connect = new LDAPConnection(...); /*if connection can be established then break from loop, but keep connection alive*/ break; } catch(LDAPException exc) { if(attempt == (maxAttempts-1)) { //throw exception message throw exc; } } Thread.sleep(1000); }
/** * Constructs a new LDAPConnection object, which will use the supplied * class factory to construct a socket connection during * LDAPConnection.connect method. * * @see <a href="../../../../api/com/novell/ldap/LDAPConnection.html#LDAPConnection(com.novell.ldap.LDAPSocketFactory)"> com.novell.ldap.LDAPConnection.LDAPConnection(LDAPSocketFactory)</a> */ public LDAPConnection(LDAPSocketFactory factory) { if( (factory != null) && (factory instanceof com.novell.ldap.LDAPSocketFactory)) { conn = new com.novell.ldap.LDAPConnection( (com.novell.ldap.LDAPSocketFactory)factory); } else { conn = new com.novell.ldap.LDAPConnection( getSocketImpl(factory)); } return; }
LDAPConnection myCon = new LDAPConnection("192.168.1.1",389); myCon.delete("cn=Alan,ou=engineers,dc=fool,dc=com");
/** * Return a new LDAPConnection with the appropriate socket factory set for the connection type. */ private LDAPConnection createConnectionWithSocketFactory() { LDAPSocketFactory factory; if (config.isSecureConnection()) { factory = config.getSecureSocketFactory(); if (factory == null) { throw new RuntimeException("You must set a 'secureSocketFactory' (in jldap-beans.xml) when using LDAPS"); } } else { factory = config.getSocketFactory(); } if (factory == null) { return new LDAPConnection(); } else { return new LDAPConnection(factory); } }
LDAPConnection ldc = new LDAPConnection() ldc.connect(...); if (ldc.isConnected()) { do good stuff } else { getLDAPConnection(...); }
SSLUtil su = new SSLUtil(new TrustAllTrustManager()); SSLSocketFactory sf = su.createSSLSocketFactory(); LDAPConnection connection = new LDAPConnection(sf,"localhost", 10636);
LDAPConnection ld = new LDAPConnection(); ld.connect(LDAP_SERVER, LDAP_PORT); LDAPSearchResults res = ld.search(BASE_DN, SEARCH_SCOPE, "(uid=" + THE_ID +")", null, false);
final String hostname = "the hostname"; final int port = PORT; // sometimes 389 try { final LDAPConnection connection = new LDAPConnection(hostname,port); try { final SimpleBindRequest request = new SimpleBindRequest(bindDN,bindPassword); final BindResult result = connection.bind(request); } finally { connection.close(); } } catch(final LDAPException ex) { handle the exception ...; }
try { // Connect to the server LDAPConnection ldapConnection = new LDAPConnection(...); try { for(String dn:listOfDns) { Entry entry = ldapConnection.getEntry(dn,"1.1"); } } catch(LDAPException ex) { // handle an exception from the search } finally { ldapConnection.close(); } } catch(LDAPException ex) { // handle an exception from the connection attempt }
public static LDAPConnection getNewSSLConnection(String address, int port, BindRequest bindRequest) throws LDAPException, GeneralSecurityException { SSLUtil sslUtil = new SSLUtil(new TrustAllTrustManager()); SSLSocketFactory sslSocketFactory = sslUtil.createSSLSocketFactory(); LDAPConnection ldc = new LDAPConnection(sslSocketFactory); ldc.connect(address, port); ldc.bind(bindRequest); return ldc; }
LDAPConnection myCon = new LDAPConnection("localhost",389); LDAPModificationSet mods = new LDAPModificationSet(); mods.add(LDAPModification.DELETE, new LDAPAttribute("notifyTo")); myCon.modify("uid=test1, ou=People, o=domain.com, o=isp", mods);
LDAPConnection conn = new LDAPConnection(); conn.connect("blah.blah.address", 389); String[] attrIDs = {"uniqueMember"}; LDAPSearchResults search = conn.search("dc=foo,dc=bar", LDAPConnection.SCOPE_ONE, "cn=testgroup", attrIDs, false); while(search.hasMore()) { LDAPEntry entry = search.next(); for (String string : entry.getAttribute("uniqueMember").getStringValueArray()) { System.out.println(string); } }
import com.unboundid.ldap.sdk.Filter; import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.SearchScope; import com.unboundid.ldap.sdk.SearchResult; public final class BSFilter { public static void main(String... args) { try { Filter searchFilter = Filter.create("cn=abc\"and'def"); LDAPConnection connection = new LDAPConnection("localhost",1389); SearchResult searchResult = connection.search("dc=example,dc=com",SearchScope.ONE, searchFilter,"1.1"); assert(searchResult.getSearchEntries().size() == 0); } catch(LDAPException lex) { lex.printStackTrace(); return; } } }
/** {@inheritDoc} */ @Override public void connect(final InetAddress address, final int port, final int timeout) throws IOException, Exception { super.connect(address, port, timeout); final LDAPConnection lc = new LDAPConnection(new TimeoutLDAPSocket(timeout)); lc.connect(InetAddressUtils.str(address), port); } }
/** {@inheritDoc} */ @Override public void connect(final InetAddress address, final int port, final int timeout) throws IOException, Exception { super.connect(address, port, timeout); final LDAPConnection lc = new LDAPConnection(new TimeoutLDAPSocket(timeout)); lc.connect(InetAddressUtils.str(address), port); } }