public static LdapJndiProperties getLdapJndiProperties()

in core-jndi/src/main/java/org/apache/directory/server/core/jndi/LdapJndiProperties.java [101:260]


    public static LdapJndiProperties getLdapJndiProperties( Hashtable env ) throws NamingException
    {
        if ( env == null )
        {
            throw new ConfigurationException( "environment cannot be null" );
        }

        LdapJndiProperties props = new LdapJndiProperties();
        Object principal = env.get( Context.SECURITY_PRINCIPAL );
        Object credobj = env.get( Context.SECURITY_CREDENTIALS );
        Object authentication = env.get( Context.SECURITY_AUTHENTICATION );

        // -------------------------------------------------------------------
        // check for the provider URL property 
        // -------------------------------------------------------------------

        if ( !env.containsKey( Context.PROVIDER_URL ) )
        {
            String msg = I18n.err( I18n.ERR_06003_EXPECTED_PROPERTY, Context.PROVIDER_URL );
            throw new ConfigurationException( msg );
        }

        String url = ( String ) env.get( Context.PROVIDER_URL );

        if ( url == null )
        {
            String msg = I18n.err( I18n.ERR_06004_PROPERTY_SET_TO_NULL, Context.PROVIDER_URL );
            throw new ConfigurationException( msg );
        }

        if ( url.trim().equals( "" ) )
        {
            props.providerDn = Dn.ROOT_DSE;
        }
        else
        {
            try
            {
                props.providerDn = new Dn( url );
            }
            catch ( LdapInvalidDnException lide )
            {
                String msg = I18n.err( I18n.ERR_06016_PRINCIPAL_NOT_VALID, url );
                throw new ConfigurationException( msg );
            }
        }

        // -------------------------------------------------------------------
        // Figure out and set the authentication level and mechanisms
        // -------------------------------------------------------------------

        if ( authentication == null )
        {
            // if the property is not set but Context.SECURITY_CREDENTIALS is then SIMPLE
            if ( credobj == null )
            {
                props.level = AuthenticationLevel.NONE;
            }
            else
            {
                props.level = AuthenticationLevel.SIMPLE;
            }
        }
        else if ( !( authentication instanceof String ) )
        {
            throw new ConfigurationException( I18n.err( I18n.ERR_06002_DONT_KNOW_HOW_TO_INTERPRET, authentication.getClass(),
                Context.SECURITY_AUTHENTICATION ) );
        }
        else
        {
            if ( AuthenticationLevel.NONE.toString().equals( authentication ) )
            {
                props.level = AuthenticationLevel.NONE;
            }
            else if ( AuthenticationLevel.SIMPLE.toString().equals( authentication ) )
            {
                props.level = AuthenticationLevel.SIMPLE;
            }
            else
            {
                props.level = AuthenticationLevel.STRONG;
                props.saslMechanism = ( String ) authentication;
            }
        }

        // -------------------------------------------------------------------
        // Figure out and set the security principal bindDn and saslAuthId
        // -------------------------------------------------------------------

        if ( principal == null && props.level == AuthenticationLevel.SIMPLE )
        {
            throw new ConfigurationException( I18n.err( I18n.ERR_06005_CANNOT_BE_NULL, Context.SECURITY_PRINCIPAL ) );
        }
        else if ( principal == null && props.level == AuthenticationLevel.NONE )
        {
            props.bindDn = Dn.EMPTY_DN;
        }
        else if ( !( principal instanceof String ) )
        {
            throw new ConfigurationException( I18n.err( I18n.ERR_06002_DONT_KNOW_HOW_TO_INTERPRET, principal.getClass(), Context.SECURITY_PRINCIPAL ) );
        }
        else if ( ( ( String ) principal ).trim().equals( "" ) )
        {
            props.bindDn = Dn.EMPTY_DN;
        }
        else
        {
            try
            {
                props.providerDn = new Dn( ( String ) principal );
            }
            catch ( LdapInvalidDnException lide )
            {
                String msg = I18n.err( I18n.ERR_06016_PRINCIPAL_NOT_VALID, principal );
                throw new ConfigurationException( msg );
            }

        }

        if ( env.get( SASL_AUTHID ) != null && props.level == AuthenticationLevel.STRONG )
        {
            Object obj = env.get( SASL_AUTHID );
            if ( obj instanceof String )
            {
                props.saslAuthId = ( String ) obj;
            }
            else
            {
                throw new ConfigurationException( I18n.err( I18n.ERR_06002_DONT_KNOW_HOW_TO_INTERPRET, obj.getClass(), SASL_AUTHID ) );
            }
            props.saslAuthId = ( String ) principal;
        }

        // -------------------------------------------------------------------
        // Figure out the credentials
        // -------------------------------------------------------------------

        if ( props.level == AuthenticationLevel.SIMPLE && credobj == null )
        {
            throw new ConfigurationException( I18n.err( I18n.ERR_06006_CANT_SPECIFY_SIMPLE_AUTHENTICATION ) );
        }
        else if ( credobj != null )
        {
            if ( credobj instanceof String )
            {
                props.credentials = Strings.getBytesUtf8( ( String ) credobj );
            }
            else if ( credobj instanceof byte[] )
            {
                props.credentials = ( byte[] ) credobj;
            }
            else
            {
                throw new ConfigurationException( I18n.err( I18n.ERR_06002_DONT_KNOW_HOW_TO_INTERPRET, credobj.getClass(),
                    Context.SECURITY_CREDENTIALS ) );
            }
        }

        return props;
    }