Results 1 to 10 of 10

Thread: retrive jpg image from ldap?

  1. #1
    Join Date
    Nov 2008
    Posts
    6

    Default retrive jpg image from ldap?

    Hi all!

    I need retrive a jpg image from ldap, but when I cast the object to byte[] an CastExeption is throw. Please, could anyone help me?

    Code:
    public Object mapFromAttributes(Attributes attributes) throws NamingException 
    {
    		
    	Attribute ldapAtributo = attributes.get("photo");
    	if(ldapAtributo != null){
    		
    		byte[] photo = (byte[])ldapAtributo.get();
    	}
    }

    * Sorry for my poor English
    Thanks

  2. #2
    Join Date
    Mar 2005
    Location
    Landskrona, Sweden
    Posts
    505

    Default

    Try debugging and checking the class of the object before casting.
    Mattias Arthursson
    Jayway AB (www.jayway.se)
    Spring-LDAP project member

  3. #3
    Join Date
    Nov 2008
    Posts
    6

    Default

    The type is String (photo instanceof String return true). But in ldap these photos was inserted in the binary mode.

    How I inform to LdapTemplate the type of the attribute? In this case byte[] (I think)...

  4. #4
    Join Date
    Oct 2008
    Location
    India
    Posts
    20

    Default

    Why don't you convert the String to byte[] ?

  5. #5
    Join Date
    Nov 2008
    Posts
    6

    Default

    Because a ClassCastException is throw.
    The attributes.get("photo").get() return an Object in default. But in really it's a String! I dont understant why the binary attribute in LDAP return in this type. I need do any configuration in spring ldap? Do you have a example for me?
    The ldap is Oracle Internet Directory.

    *Sorry, the getBytes() String's method work ok, but the byte[] is not a valid jpg image.
    The
    Code:
    ImageIcon icone = new ImageIcon(foto);
    not show any image
    Last edited by fausto; Nov 12th, 2008 at 05:18 AM.

  6. #6
    Join Date
    Oct 2008
    Location
    India
    Posts
    20

    Default

    Sorry I dont have access to Oracle LDAP directory but if it is an Octet String type then I cant fathom the problem. I have worked on fetching X509certificates from Active Directory and I was able to get bytes in correct format. I dont think it is Spring-LDAP issue.

  7. #7
    Join Date
    Nov 2008
    Posts
    6

    Default

    Ok, I will talk to ldap production team, asking if exist this Octet String type in Oracle ldap. Later I report the result here

    Thanks

  8. #8
    Join Date
    Nov 2008
    Posts
    6

    Default

    Apparently is necessary config the InitialDirContext environment putting this value ("java.naming.ldap.attributes.binary", "workphoto"). Them the cast for Object attribute type to byte[] will work fine.

    Anyone could help me for this configuration? where I do this? In the applicationContext.xml?

  9. #9
    Join Date
    Mar 2005
    Location
    Landskrona, Sweden
    Posts
    505

    Default

    There is a "baseEnvironment" property on AbstractContextSource (LdapContextSource) which can be used for supplying custom properties to be used when creating DirContext instances.
    Mattias Arthursson
    Jayway AB (www.jayway.se)
    Spring-LDAP project member

  10. #10
    Join Date
    Nov 2008
    Posts
    6

    Default

    Thanks! Now it's working!!!

    I add these lines in applicationContext.xml (LdapContextSource bean)

    Code:
    <property name="baseEnvironmentProperties">
    	<map><entry>
    		<key><value>java.naming.ldap.attributes.binary</value></key>
    			<value>workphoto</value>
    	</entry></map>
    </property>
    The casting from Object to byte[] works fine!

    Last edited by fausto; Nov 12th, 2008 at 10:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •