List of usage examples for javax.naming.directory DirContext close
public void close() throws NamingException;
From source file:no.smint.anthropos.ldap.LDAP.java
public static String getDn(String uid) throws NamingException { Hashtable<String, Object> env = config(); DirContext ctx = new InitialDirContext(env); SearchControls ctls = new SearchControls(); String filter = ("uid=" + uid); NamingEnumeration answer = ctx.search(name, filter, ctls); SearchResult searchResult;/*w ww.j a va 2 s.c o m*/ if (answer.hasMoreElements()) { searchResult = (SearchResult) answer.next(); } else { System.out.println("Found no user by that name"); return null; } if (answer.hasMoreElements()) { System.err.println("Matched mutliple users for the uid" + uid); return null; } ctx.close(); return searchResult.getNameInNamespace(); }
From source file:net.grinder.util.NetworkUtils.java
public static List<String> getDnsServers() throws NamingException { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); DirContext ctx = null; List<String> dnsServers = new ArrayList<String>(); try {// w ww .j a v a 2 s .c o m ctx = new InitialDirContext(env); String dnsString = (String) ctx.getEnvironment().get("java.naming.provider.url"); for (String each : dnsString.split(" ")) { dnsServers.add(each.replace("dns://", "")); } } catch (Exception e) { NoOp.noOp(); } finally { if (ctx != null) { ctx.close(); } } return dnsServers; }
From source file:eionet.gdem.dcm.conf.LdapTest.java
protected void closeContext(DirContext ctx) throws NamingException { if (ctx != null) { ctx.close(); }/* w w w. ja va 2 s . c om*/ }
From source file:fr.aliasource.webmail.ldap.Configuration.java
public void cleanup(DirContext ctx) { if (ctx != null) { try {/*from ww w .j a va 2 s . co m*/ ctx.close(); } catch (NamingException e) { } } }
From source file:com.hs.mail.security.login.JndiLoginModule.java
protected void close(DirContext context) { if (context != null) { try {//from ww w . j ava 2s . c o m context.close(); context = null; } catch (Exception e) { } } }
From source file:edu.lafayette.metadb.model.userman.UserManDAO.java
/** * Perform LDAP authentication for a user. * @param userName The username to authenticate. * @param password The password to authenticate. * @return true if the user was successfully authenticated by LDAP, false otherwise. *///from www. j av a 2 s . c o m private static boolean authLDAP(String userName, String password) { String searchUser = Global.LDAP_BROWSE_USERNAME; String searchPassword = Global.LDAP_BROWSE_PASSWORD; String dn = null; if (searchUser.equals("") || searchPassword.equals("") || searchUser == null || searchPassword == null) dn = Global.LDAP_ID + "=" + userName + "," + Global.LDAP_CONTEXT; else { dn = getDN(searchUser, searchPassword, userName); // Check a DN was found if ((dn == null) || (dn.trim().equals(""))) { //MetaDbHelper.note("Hierarchical LDAP Authentication Error: No DN found for user "+userName); return false; } } boolean success = false; // Set up environment for creating initial context Hashtable<String, String> env = new Hashtable<String, String>(); env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(javax.naming.Context.PROVIDER_URL, Global.LDAP_URL); // Authenticate env.put(javax.naming.Context.SECURITY_AUTHENTICATION, "Simple"); env.put(javax.naming.Context.SECURITY_PRINCIPAL, dn); env.put(javax.naming.Context.SECURITY_CREDENTIALS, password); env.put(javax.naming.Context.AUTHORITATIVE, "true"); env.put(javax.naming.Context.REFERRAL, "follow"); DirContext ctx = null; try { // Try to bind ctx = new InitialDirContext(env); success = true; //MetaDbHelper.note("User "+userName+" logged in with LDAP"); //MetaDbHelper.note("Context name: "+ctx.getNameInNamespace()); //MetaDbHelper.note(ctx.toString()); } catch (NamingException e) { MetaDbHelper.note("Error: Failed to authenticate " + userName + ".\n" + e); success = false; } finally { // Close the context when we're done try { if (ctx != null) ctx.close(); //MetaDbHelper.note("LDAP connection closed"); } catch (NamingException e) { MetaDbHelper.logEvent(e); //MetaDbHelper.note("Error: Failed to establish context."); } } return success; }
From source file:org.jasig.cas.monitor.PoolingContextSourceMonitor.java
/** {@inheritDoc} */ protected StatusCode checkPool() throws Exception { final boolean success; DirContext ctxt = null; try {/*ww w .j a v a 2s . co m*/ ctxt = poolingContextSource.getReadOnlyContext(); success = poolingContextSource.getDirContextValidator().validateDirContext(DirContextType.READ_ONLY, ctxt); } finally { if (ctxt != null) { ctxt.close(); } } return success ? StatusCode.OK : StatusCode.ERROR; }
From source file:com.aurel.track.util.LdapUtil.java
public static boolean authenticate(TSiteBean siteBean, String loginName, String ppassword) throws NamingException { boolean userIsOK = false; ArrayList<String> trace = new ArrayList<String>(); trace.add("Ldap trying to authenticate user with loginname >" + loginName + "<"); if (siteBean.getLdapServerURL().startsWith("ldaps:")) { System.setProperty("javax.net.ssl.trustStore", PATH_TO_KEY_STORE); }/* w w w . j a v a 2s . c o m*/ // get the CN String keyDn = getCn(siteBean, loginName); try { if (keyDn != null) { trace.add("Using keyDn >" + keyDn + "<"); // Set up the environment for creating the initial context Hashtable<String, String> env = new Hashtable<String, String>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, siteBean.getLdapServerURL()); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, keyDn); env.put(Context.SECURITY_CREDENTIALS, ppassword); // Create initial context DirContext itest = new InitialDirContext(env); itest.close(); // user was validated userIsOK = true; } return userIsOK; } catch (NamingException e) { for (String msg : trace) { LOGGER.warn(msg); } throw e; } }
From source file:alpine.auth.LdapConnectionWrapper.java
/** * Closes a DirContext object without throwing any exceptions. * @param object the DirContext object to close * @since 1.4.0// w w w . j a v a 2 s .c o m */ public void closeQuietly(final DirContext object) { try { if (object != null) { object.close(); } } catch (final NamingException e) { // ignore } }