Hi,
Le 31/07/2018 à 04:36, Jon Champlin a écrit :
> I have some code that is using LDIFReader to parse an LDIF file and then I
> have a loop that loops through the list of LDIFEntry it returns. I then use
>
> entry.get(<attribute name>).getString()
>
> to get the value of the attributes I am looking for. The problem arises
> when one of the entries has an attribute I am looking for that is base64
> encoded. So the line in the LDIF file looks like:
>
> <attribute name>:: <base64 string> (notice the two colons instead of one)
>
> In this case getString() throws an LdapInvalidAttributeValueException
> because the value isn't a string. However, I didn't see a way to check
> what the value type is and if it is base64 encoded to return the decoded
> value.
The problem is that you don't use a SchemaManager associated with teh
LdifReader, so there is no way the read attribute can know if the stored
value is a String or a binary value.
You can workaround this by using :
String valueStr = entry.get( <attribute name> ).get().getValue();
(Assuming the <attribute name> is for a String attribute)
If the attribute is binary (like userPassword - but you should not be
able to read it anyway, although this is another story, I'm just using
this attributeType for the sake of the demonstration), you will do :
byte[] userPassword = entry.get( "userPassword" ).get().getBytes();
You can see that your implementation must know beforehand what is the
attribute type (String or binary).
Now, if you use a Schema aware LdifReader, that changes things:
LdifReader ldifReader = new LifReader( new DefaultSchemaManager() );
List<LdifEntry> entries = reader.parseLdif( ldif );
...
String valueStr = entry.get( <attribute name> ).getString()
will return the expected value (even if it's base64 encoded), assuming
the AttributeType is a String.
You can do better: check the attribute beforehand.
Attribute attr = entry.get( <attribute name> );
if ( attr.isHumanReadable() )
{
String value = attr.getString();
}
else
{
byte[] value = attr.getBytes();
}
That will not work for a non-schema aware LdifReader because when the
reader sees a '::', it assumes then that the Attribute is binary - even
if it's not... -
Use the SchemaAware version, it's better.
Note that you may have to extend the ShemaManager to support your own
attributes, if you have defined some (but that is another story...)
--
Emmanuel Lecharny
Symas.com
directory.apache.org