Example usage for javax.naming.ldap LdapContext getEnvironment

List of usage examples for javax.naming.ldap LdapContext getEnvironment

Introduction

In this page you can find the example usage for javax.naming.ldap LdapContext getEnvironment.

Prototype

public Hashtable<?, ?> getEnvironment() throws NamingException;

Source Link

Document

Retrieves the environment in effect for this context.

Usage

From source file:org.pepstock.jem.gwt.server.security.ExtendedJndiLdapRealm.java

/**
 * Performs the authorization by LDAP.//from  ww w.  j a v  a  2s .  c o  m
 */
@SuppressWarnings("unchecked")
@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token, Object ldapPrincipal,
        Object ldapCredentials, LdapContext ldapContext) throws NamingException {
    if (token instanceof FirstInstallationToken) {
        FirstInstallationToken upToken = (FirstInstallationToken) token;
        // Creates a user object
        User user = new User(upToken.getUsername());
        // creates account
        return new SimpleAccount(user, ldapCredentials, getName());
    }
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    Collection<PrincipalAttribute> principals = null;
    try {
        // if environment null, uses the ldap context already prepared
        // this part is necessary to load attribtues from LDAP
        if (principalEnvironment == null) {
            LdapContext context = super.getContextFactory().getSystemLdapContext();
            Hashtable<String, String> currentEnvironment = (Hashtable<String, String>) context.getEnvironment();
            principalEnvironment = (Hashtable<String, String>) currentEnvironment.clone();
            // no authentication
            principalEnvironment.put(InitialDirContext.SECURITY_AUTHENTICATION, "none");
            // searchs attributes
        }
        principals = search(upToken.getUsername(), principalEnvironment);
    } catch (NamingException e) {
        LogAppl.getInstance().emit(UserInterfaceMessage.JEMG031E, e, upToken.getUsername());
    }
    // Creates a user object
    User user = new User(upToken.getUsername());
    // sets attribtues
    user.setAttributes(principals);
    if (principals != null) {
        for (PrincipalAttribute pa : principals) {
            if (orgUnitIdAttribute != null && pa.getName().equalsIgnoreCase(orgUnitIdAttribute)) {
                user.setOrgUnitId(pa.getValue().toString());
            }
            if (orgUnitNameAttribute != null && pa.getName().equalsIgnoreCase(orgUnitNameAttribute)) {
                user.setOrgUnitName(pa.getValue().toString());
            }
            if (userNameAttribute != null && pa.getName().equalsIgnoreCase(userNameAttribute)) {
                user.setName(pa.getValue().toString());
            }
        }
    }
    // creates account
    return new SimpleAccount(user, token.getCredentials(), getName());
}