Example usage for javax.naming.directory InitialDirContext close

List of usage examples for javax.naming.directory InitialDirContext close

Introduction

In this page you can find the example usage for javax.naming.directory InitialDirContext close.

Prototype

public void close() throws NamingException 

Source Link

Usage

From source file:org.apache.syncope.fit.AbstractITCase.java

protected Object getLdapRemoteObject(final String bindDn, final String bindPwd, final String objectDn) {
    InitialDirContext ctx = null;
    try {/*  www . j a  v a 2s. c o  m*/
        ctx = getLdapResourceDirContext(bindDn, bindPwd);
        return ctx.lookup(objectDn);
    } catch (Exception e) {
        return null;
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                // ignore
            }
        }
    }
}

From source file:org.apache.syncope.fit.AbstractITCase.java

protected void updateLdapRemoteObject(final String bindDn, final String bindPwd, final String objectDn,
        final Pair<String, String> attribute) {

    InitialDirContext ctx = null;
    try {/*ww w  .j  a  v a 2  s.  c  om*/
        ctx = getLdapResourceDirContext(bindDn, bindPwd);

        Attribute ldapAttribute = new BasicAttribute(attribute.getKey(), attribute.getValue());
        ModificationItem[] item = new ModificationItem[1];
        item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ldapAttribute);
        ctx.modifyAttributes(objectDn, item);
    } catch (Exception e) {
        // ignore
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                // ignore
            }
        }
    }
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapSecurityService.java

@Override
public boolean authenticate(String id, char[] password) {
    String cachedPassword = credentialCache.get(id);
    String encodedPassword = null;

    try {//from   ww  w.  j a va  2  s .  c  om
        encodedPassword = codec.encode(new String(password));
    } catch (EncoderException e1) {
    }

    if (cachedPassword != null && encodedPassword != null && cachedPassword.equals(encodedPassword))
        return true;

    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, url);
    environment.put(Context.SECURITY_AUTHENTICATION, "simple");
    environment.put(Context.SECURITY_PRINCIPAL, id);
    environment.put(Context.SECURITY_CREDENTIALS, new String(password));

    try {
        InitialDirContext context = new InitialDirContext(environment);
        context.close();

        if (encodedPassword != null)
            credentialCache.put(id, encodedPassword);

        return true;
    } catch (NamingException e) {
        return false;
    }
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapService.java

@Override
public boolean authenticate(String id, char[] password) {
    if (id == null || id.isEmpty())
        return false;

    if (idSuffix != null)
        id = id + idSuffix;/*from   w ww  .ja v  a  2  s .  c  om*/

    String cachedPassword = credentialCache.get(id);
    String encodedPassword = null;

    try {
        encodedPassword = codec.encode(new String(password));
    } catch (EncoderException e1) {
    }

    if (cachedPassword != null && encodedPassword != null && cachedPassword.equals(encodedPassword))
        return true;

    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, url);
    environment.put(Context.SECURITY_AUTHENTICATION, "simple");
    environment.put(Context.SECURITY_PRINCIPAL, id);
    environment.put(Context.SECURITY_CREDENTIALS, new String(password));

    try {
        InitialDirContext context = new InitialDirContext(environment);
        context.close();

        if (encodedPassword != null)
            credentialCache.put(id, encodedPassword);

        return true;
    } catch (NamingException e) {
        return false;
    }
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapService.java

@Override
public NamingEnumeration<SearchResult> find(int scope, String path, String filter) throws NamingException {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(scope);
    String searchPath = path != null && !path.isEmpty() ? path + "," + baseDN : baseDN;

    InitialDirContext searchContext = new InitialDirContext(searchEnvironment);
    NamingEnumeration<SearchResult> searchResults = searchContext.search(searchPath, filter, searchControls);
    searchContext.close();
    return searchResults;
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapService.java

@Override
public Attributes getAttributes(String dn) throws NamingException {
    InitialDirContext searchContext = new InitialDirContext(searchEnvironment);
    Attributes attributes = searchContext.getAttributes(dn);
    searchContext.close();
    return attributes;
}

From source file:org.jamwiki.ldap.LdapUserHandler.java

/**
 *
 *///from  w  w w .j  a  v  a 2 s.co  m
public boolean authenticate(String username, String password) throws Exception {
    InitialDirContext ctx = null;
    try {
        username = this.fullDirectoryPath(username);
        ctx = getContext(username, password);
        return true;
    } catch (Exception e) {
        // could not authenticate, return false
        return false;
    } finally {
        try {
            ctx.close();
        } catch (Exception e) {
        }
    }
}

From source file:org.jamwiki.ldap.LdapUserHandler.java

/**
 *
 *///  w ww .  j  a v  a  2 s.  c o m
public WikiUserInfo lookupWikiUserInfo(String username) throws Exception {
    InitialDirContext ctx = null;
    try {
        ctx = getContext(Environment.getValue(Environment.PROP_LDAP_LOGIN),
                Encryption.getEncryptedProperty(Environment.PROP_LDAP_PASSWORD, null));
        BasicAttributes matchAttrs = new BasicAttributes(true);
        matchAttrs.put(new BasicAttribute(Environment.getValue(Environment.PROP_LDAP_FIELD_USERID), username));
        NamingEnumeration answer = ctx.search(Environment.getValue(Environment.PROP_LDAP_CONTEXT), matchAttrs,
                SEARCH_ATTRIBUTES);
        return (!answer.hasMore()) ? null : this.initWikiUserInfo(answer);
    } finally {
        try {
            ctx.close();
        } catch (Exception e) {
        }
    }
}