Value Transcoders

In cases where LDAP attribute values represent concrete Java objects, a ValueTranscoder can be used to convert those types. The ValueTranscoder interface looks like:

public interface ValueTranscoder<T>
{
T decodeStringValue(String value);

T decodeBinaryValue(byte[] value);

String encodeStringValue(T value);

byte[] encodeBinaryValue(T value);

Class<T> getType();
}

Ldaptive provides the following value transcoder implementations:

CertificateValueTranscoder

Provides the ability to read a PEM encoded certificate from an LDAP attribute in order to create a java.security.cert.Certificate object. Conversely, a java.security.cert.Certificate can also be written to the directory as a PEM encoded certificate.

Connection conn = DefaultConnectionFactory.getConnection("ldap://directory.ldaptive.org");
try {
conn.open();
SearchOperation search = new SearchOperation(conn);
SearchResult result = search.execute(
new SearchRequest(
"dc=ldaptive,dc=org","(&(givenName=daniel)(sn=fisher))", "userCertificate;binary")).getResult();
LdapEntry entry = result.getEntry();
Certificate cert = entry.getAttribute("userCertificate;binary").getValue(new CertificateValueTranscoder());

} finally {
conn.close();
}

GeneralizedTimeValueTranscoder

Provides the ability to read a generalized time from an LDAP attribute in order to create a java.util.Calendar (or ZonedDateTime in v1.2) object. Conversely, a java.util.Calendar can also be written to the directory in generalized time format.

Connection conn = DefaultConnectionFactory.getConnection("ldap://directory.ldaptive.org");
try {
conn.open();
SearchOperation search = new SearchOperation(conn);
SearchResult result = search.execute(
new SearchRequest(
"dc=ldaptive,dc=org","(&(givenName=daniel)(sn=fisher))", "modifyTimestamp")).getResult();
LdapEntry entry = result.getEntry();
ZonedDateTime modifyTimestamp = entry.getAttribute("modifyTimestamp").getValue(new GeneralizedTimeValueTranscoder());

} finally {
conn.close();
}