Example usage for javax.naming Context OBJECT_FACTORIES

List of usage examples for javax.naming Context OBJECT_FACTORIES

Introduction

In this page you can find the example usage for javax.naming Context OBJECT_FACTORIES.

Prototype

String OBJECT_FACTORIES

To view the source code for javax.naming Context OBJECT_FACTORIES.

Click Source Link

Document

Constant that holds the name of the environment property for specifying the list of object factories to use.

Usage

From source file:iplatform.admin.ui.server.auth.ad.ActiveDirectoryLdapAuthenticationProvider.java

private DirContext bindAsUser(String username, String password) {
    // TODO. add DNS lookup based on domain
    final String bindUrl = url;

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    String bindPrincipal = createBindPrincipal(username);
    env.put(Context.SECURITY_PRINCIPAL, bindPrincipal);
    env.put(Context.PROVIDER_URL, bindUrl);
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.OBJECT_FACTORIES, DefaultDirObjectFactory.class.getName());

    try {//from w  w w  .j  a  va2s  . c o  m
        return contextFactory.createContext(env);
    } catch (NamingException e) {
        if ((e instanceof AuthenticationException) || (e instanceof OperationNotSupportedException)) {
            handleBindException(bindPrincipal, e);
            throw badCredentials(e);
        } else {
            throw LdapUtils.convertLdapException(e);
        }
    }
}

From source file:org.apache.directory.server.core.jndi.ObjStateFactoryIT.java

@Test
public void testObjectFactory() throws Exception {
    LdifEntry akarasulu = getUserAddLdif();
    getService().getAdminSession().add(new DefaultEntry(getService().getSchemaManager(), akarasulu.getEntry()));

    LdapContext sysRoot = getSystemContext(getService());
    sysRoot.addToEnvironment(Context.OBJECT_FACTORIES, PersonObjectFactory.class.getName());
    Object obj = sysRoot.lookup("uid=akarasulu, ou=users");
    Attributes attrs = sysRoot.getAttributes("uid=akarasulu, ou=users");
    assertEquals(Person.class, obj.getClass());
    Person me = (Person) obj;//w  ww.ja  v  a  2s  .c o  m
    assertEquals(attrs.get("sn").get(), me.getLastname());
    assertEquals(attrs.get("cn").get(), me.getCn());
    assertTrue(ArrayUtils.isEquals(attrs.get("userPassword").get(), Strings.getBytesUtf8("test")));
    assertEquals(attrs.get("telephonenumber").get(), me.getTelephoneNumber());
    assertNull(me.getSeealso());
    assertNull(me.getDescription());
}

From source file:org.apache.openaz.xacml.std.pip.engines.ldap.LDAPEngine.java

@Override
public void configure(String id, Properties properties) throws PIPException {
    /*//ww w  .  j  av a2 s.c  om
     * Handle the standard properties
     */
    super.configure(id, properties);
    String propertyPrefix = id + ".";

    /*
     * Configure the LDAP environment: I think the only required property is the provider_url
     */
    if (!this.configureStringProperty(propertyPrefix, Context.PROVIDER_URL, properties, null)) {
        throw new PIPException("Invalid configuration for " + this.getClass().getName() + ": No "
                + propertyPrefix + Context.PROVIDER_URL);
    }
    this.configureStringProperty(propertyPrefix, Context.AUTHORITATIVE, properties, null);
    this.configureIntegerProperty(propertyPrefix, Context.BATCHSIZE, properties, null);
    this.configureStringProperty(propertyPrefix, Context.DNS_URL, properties, null);
    this.configureStringProperty(propertyPrefix, Context.INITIAL_CONTEXT_FACTORY, properties,
            DEFAULT_CONTEXT_FACTORY);
    this.configureStringProperty(propertyPrefix, Context.LANGUAGE, properties, null);
    this.configureStringProperty(propertyPrefix, Context.OBJECT_FACTORIES, properties, null);
    this.configureStringProperty(propertyPrefix, Context.REFERRAL, properties, null);
    this.configureStringProperty(propertyPrefix, Context.SECURITY_AUTHENTICATION, properties, null);
    this.configureStringProperty(propertyPrefix, Context.SECURITY_CREDENTIALS, properties, null);
    this.configureStringProperty(propertyPrefix, Context.SECURITY_PRINCIPAL, properties, null);
    this.configureStringProperty(propertyPrefix, Context.SECURITY_PROTOCOL, properties, null);
    this.configureStringProperty(propertyPrefix, Context.STATE_FACTORIES, properties, null);
    this.configureStringProperty(propertyPrefix, Context.URL_PKG_PREFIXES, properties, null);

    String ldapScopeValue = properties.getProperty(propertyPrefix + PROP_LDAP_SCOPE, DEFAULT_SCOPE);
    if (LDAP_SCOPE_SUBTREE.equals(ldapScopeValue)) {
        this.ldapScope = SearchControls.SUBTREE_SCOPE;
    } else if (LDAP_SCOPE_OBJECT.equals(ldapScopeValue)) {
        this.ldapScope = SearchControls.OBJECT_SCOPE;
    } else if (LDAP_SCOPE_ONELEVEL.equals(ldapScopeValue)) {
        this.ldapScope = SearchControls.ONELEVEL_SCOPE;
    } else {
        this.logger.warn("Invalid LDAP Scope value '" + ldapScopeValue + "'; using " + DEFAULT_SCOPE);
        this.ldapScope = SearchControls.SUBTREE_SCOPE;
    }

    /*
     * Get list of resolvers defined for this LDAP Engine
     */
    String resolversList = properties.getProperty(propertyPrefix + PROP_RESOLVERS);
    if (resolversList == null || resolversList.isEmpty()) {
        throw new PIPException("Invalid configuration for " + this.getClass().getName() + ": No "
                + propertyPrefix + PROP_RESOLVERS);
    }

    /*
     * Iterate the resolvers
     */
    for (String resolver : Splitter.on(',').trimResults().omitEmptyStrings().split(resolversList)) {
        /*
         * Get the LDAPResolver for this LDAPEngine
         */
        String resolverClassName = properties
                .getProperty(propertyPrefix + PROP_RESOLVER + "." + resolver + ".classname");
        if (resolverClassName == null) {
            throw new PIPException("Invalid configuration for " + this.getClass().getName() + ": No "
                    + propertyPrefix + PROP_RESOLVER + "." + resolver + ".classname");
        }

        LDAPResolver ldapResolverNew = null;
        try {
            Class<?> classResolver = Class.forName(resolverClassName);
            if (!LDAPResolver.class.isAssignableFrom(classResolver)) {
                this.logger.error("LDAPResolver class " + resolverClassName + " does not implement "
                        + LDAPResolver.class.getCanonicalName());
                throw new PIPException("LDAPResolver class " + resolverClassName + " does not implement "
                        + LDAPResolver.class.getCanonicalName());
            }
            ldapResolverNew = LDAPResolver.class.cast(classResolver.newInstance());
        } catch (Exception ex) {
            this.logger.error("Exception instantiating LDAPResolver for class '" + resolverClassName + "': "
                    + ex.getMessage(), ex);
            throw new PIPException("Exception instantiating LDAPResolver for class '" + resolverClassName + "'",
                    ex);
        }
        assert ldapResolverNew != null;
        ldapResolverNew.configure(propertyPrefix + PROP_RESOLVER + "." + resolver, properties,
                this.getIssuer());

        this.ldapResolvers.add(ldapResolverNew);
    }

}

From source file:org.exoplatform.services.organization.DummyLDAPServiceImpl.java

public InitialContext getInitialContext() throws NamingException {
    Hashtable<String, Object> props = new Hashtable<String, Object>(env);
    props.put(Context.OBJECT_FACTORIES, "com.sun.jndi.ldap.obj.LdapGroupFactory");
    props.put(Context.STATE_FACTORIES, "com.sun.jndi.ldap.obj.LdapGroupFactory");
    return new DummyLdapContext(new InitialLdapContext(props, null));
}

From source file:org.springframework.ldap.core.support.AbstractContextSource.java

private Hashtable setupAnonymousEnv() {
    if (pooled) {
        baseEnv.put(SUN_LDAP_POOLING_FLAG, "true");
        log.debug("Using LDAP pooling.");
    } else {// w w w  .j  a v a2  s  . c  o  m
        baseEnv.remove(SUN_LDAP_POOLING_FLAG);
        log.debug("Not using LDAP pooling");
    }

    Hashtable env = new Hashtable(baseEnv);

    env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory.getName());
    env.put(Context.PROVIDER_URL, assembleProviderUrlString(urls));

    if (dirObjectFactory != null) {
        env.put(Context.OBJECT_FACTORIES, dirObjectFactory.getName());
    }

    if (!StringUtils.isBlank(referral)) {
        env.put(Context.REFERRAL, referral);
    }

    if (!DistinguishedName.EMPTY_PATH.equals(base)) {
        // Save the base path for use in the DefaultDirObjectFactory.
        env.put(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY, base);
    }

    log.debug("Trying provider Urls: " + assembleProviderUrlString(urls));

    return env;
}