[Date Prev][Date Next] [Chronological] [Thread] [Top]

RE: Problem with OpenLDAP 2.0.11 and JNDI



// Assumes that you have a reference to some Attribute instance named "attr"
Object value = attr.get();
 
// cast to byte[] , then convert to String using proper encoding
if(value.getClass().isArray() && Array.getLength(value) > 0) {
    try {
        value = new String((byte[])value, "UTF-8");
    } catch (java.io.UnsupportedEncodingException e) {
        SystemLog.error("Unexpected error -- char set is a basic one", e);
    }
}
-----Original Message-----
From: Dustin Cobb [mailto:dustinc@questusgroup.com]
Sent: Friday, August 24, 2001 5:48 PM
To: openldap-software@OpenLDAP.org
Subject: Problem with OpenLDAP 2.0.11 and JNDI

Hello all,
 
I am having a very strange problem with a JNDII application I'm writing
which is connecting to an OpenLDAP 2.0.11 server.  I have a lot of code
which reads/writes other attributes, but when I try to read a
userPassword attribute, I get a ClassCastException (I'm casting from
Object to String) and when I run an Object.getClass() on the returned
object, it gives me garbage.  Any suggestions?  Is this a JNDI bug,
OpenLDAP bug, or am I just doing something wrong?  I have attached some
code with comments below (including tests I ran).  Any suggestions would
be highly appreciated.  TIA
 
//////// beginning of code
 
import javax.naming.Context;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.DirContext;
import javax.naming.directory.Attributes;
import javax.naming.NamingException;
 
import java.util.*;
 
public class test extends java.lang.Object {
 
    /** Creates new test */
    public test() {
        DirContext serverContext;
 
        /*
         * properly initialize all environment settings
         *
         */
 
        Hashtable env = new Hashtable();
 
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
 
        env.put(Context.PROVIDER_URL, "ldap://*****");
        env.put(Context.SECURITY_PRINCIPAL,
"******************************");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_CREDENTIALS, "*****");
 
        /*
         * establish connection with ldap server
         *
         */
 
        try {
            serverContext = new InitialDirContext(env);
        } catch (NamingException ne) {
            System.err.println(ne);
            return;
        }
 
        /*
         * get an arbitrary attribute, in this case cn
         *
         */
 
        Attributes attributes = null;
 
        try {
            attributes =
serverContext.getAttributes("*****************************");
            System.err.println("cn = " +
(String)attributes.get("cn").get());
        } catch (Exception e) {
            System.err.println(e);
        }
 
        /*
         * now let's try userPassword which is encrypted
         * specified in schema file as
         *
         * attributetype ( 2.5.4.35 NAME 'userPassword'
         *      EQUALITY octetStringMatch
         *      SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{128} )
         *
         */
 
        try {
            System.err.println("userPassword = " +
(String)attributes.get("userPassword").get());
        } catch (Exception e) {
            System.err.println(e);
        }
 
        /*
         * ok, got a java.lang.ClassCastException.  it should be
reasonable
         * to cast it to a string, since it is one.  let's get the
object
         * type returned
         *
         */
 
        try {
            System.err.println("userPassword class = " +
attributes.get("userPassword").get().getClass());
        } catch (Exception e) {
            System.err.println(e);
        }
 
        /*
         * strange, object type returned = "[B"  that's very strange.
         * sanity check:  let's see what object type is returned from a
normal type (like cn)
         *
         */
 
        try {
            attributes =
serverContext.getAttributes("*****************************");
            System.err.println("cn object = " +
attributes.get("cn").get().getClass());
        } catch (Exception e) {
            System.err.println(e);
        }
 
        /*
         * hmm, java.lang.String, just like I thought
         *
         */
 
        /*
         * here is my output:
         *
         * C:\TEST>java test
         * cn = Administrative User
         * java.lang.ClassCastException: [B
         * userPassword class = class [B
         * cn object = class java.lang.String
         */
 
    }