You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@directory.apache.org by Sudheer Kumar <su...@gmail.com> on 2009/02/28 07:06:07 UTC

How to bind and search binary data in Apache DS?

Hi, I am new to LDAP and Apache DS.  Kindly help me solve my problem.

I have an attribute with binary (m-syntax: 1.3.6.1.4.1.1466.115.121.1.5) as
its type.  This attribute is  added as an attribute to an Object class.  I
tried to bind a String value (xml) to it.  Using the following line of code.


       String xml = getXml();


       LdapContext.modifyAttributes(dn,
                                new ModificationItem[] { new
ModificationItem(

DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(

"myAttributeName", xml)) })




It throws an exception saying: "ERROR Ldap server exception: [LDAP: error
code 80 - OTHER: failed for     Modify Request
        Object :
'cn=sudheer,ou=Preferences,ou=My_Org_Name,ou=Applications,dc=myorg,dc=com'
            Modification[0]
                Operation :  replace
                Modification
    myattributeName: '0x3C 0x3F 0x78 0x6D 0x6C 0x20 0x76 0x65 0x72 0x73 0x69
0x6F 0x6E 0x3D 0x22 0x31 ...'
            Modification[1]
                Operation :  replace
                Modification
    myattributeNametimestamp: 1233046896765: java.lang.String]
ERROR Failed to save home pages in LDAP."

Converting the String to byte array ( byte[] contentInBytes =
xml.getBytes()) doesn't solve the problem.  Then just for verification I
read a jpeg file using BufferedInputStream and extracted the content to a
byte array. This time it successfully stored the byte array data.  However,
when retrieving the data back, what I am getting is a Char array. Why it
doesn't return a byte array?  How do I store String values in binary format?
Please help.


-- 
Sudheer Kumar Arimbra

Re: How to bind and search binary data in Apache DS?

Posted by Stefan Seelmann <se...@apache.org>.
Emmanuel Lecharny wrote:
> Sudheer Kumar wrote:
>> Hi, I am new to LDAP and Apache DS.  Kindly help me solve my problem.
>>
>> I have an attribute with binary (m-syntax:
>> 1.3.6.1.4.1.1466.115.121.1.5) as
>> its type.  This attribute is  added as an attribute to an Object
>> class.  I
>> tried to bind a String value (xml) to it.
> It can't work. Your attribute is binary, there are no conversion done on
> the client side.

As you are using JNDI you need to specify your attribute as binary.
Either use the ;binary option or put your attribute to the
"java.naming.ldap.attributes.binary" environment property. See [1] for
more information.

Kind Regards,
Stefan

[1] http://java.sun.com/products/jndi/tutorial/ldap/misc/attrs.html

Re: How to bind and search binary data in Apache DS?

Posted by Emmanuel Lecharny <el...@apache.org>.
Sudheer Kumar wrote:
> Hi, I am new to LDAP and Apache DS.  Kindly help me solve my problem.
>
> I have an attribute with binary (m-syntax: 1.3.6.1.4.1.1466.115.121.1.5) as
> its type.  This attribute is  added as an attribute to an Object class.  I
> tried to bind a String value (xml) to it.
It can't work. Your attribute is binary, there are no conversion done on 
the client side.

> Converting the String to byte array ( byte[] contentInBytes =
> xml.getBytes()) doesn't solve the problem. 
Can you post the code where you do this transformation ? Here is a test 
we use to check that we can inject binary attributes :

    /**
     * Add a new attribute to a person entry.
     */
    @Test
    public void testAddNewBinaryAttributeValue() throws Exception
    {
        DirContext ctx = ( DirContext ) getWiredContext( ldapService 
).lookup( BASE );
       
        // Add a binary attribute
        byte[] newValue = new byte[]{0x00, 0x01, 0x02, 0x03};
        Attributes attrs = new BasicAttributes( 
"userCertificate;binary", newValue, true );
        ctx.modifyAttributes( RDN_TORI_AMOS, DirContext.ADD_ATTRIBUTE, 
attrs );

        // Verify, that attribute value is added
        attrs = ctx.getAttributes( RDN_TORI_AMOS );
        Attribute attr = attrs.get( "userCertificate" );
        assertNotNull( attr );
        assertTrue( attr.contains( newValue ) );
        byte[] certificate = (byte[])attr.get();
        assertTrue( Arrays.equals( newValue, certificate ) );
        assertEquals( 1, attr.size() );
    }
}



-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org