Reading and Writing LDAP Results
Ldaptive provides implementations for formatting LDAP results in LDIF, DSML version 1, and JSON.
LDIF
LDIF can be written to any java.io.Writer using an LdifWriter.
StringWriter writer = new StringWriter ();
LdifWriter ldifWriter = new LdifWriter ( writer );
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" , new SearchFilter ( "(uid=dfisher)" ), new String [] { "mail" })). getResult ();
ldifWriter . write ( result );
System . out . println ( writer . toString ());
} finally {
conn . close ();
}
produces:
dn: uid=dfisher,ou=people,dc=ldaptive,dc=org
mail: dfisher@ldaptive.org
LDIF can be read using any java.io.Reader using an LdifReader.
FileReader reader = new FileReader ( "entry.ldif" );
LdifReader ldifReader = new LdifReader ( reader );
SearchResult result = ldifReader . read ();
DSML
DSML can be written to any java.io.Writer using a Dsmlv1Writer.
StringWriter writer = new StringWriter ();
Dsmlv1Writer dsmlWriter = new Dsmlv1Writer ( writer );
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" , new SearchFilter ( "(uid=dfisher)" ), new String [] { "mail" })). getResult ();
dsmlWriter . write ( result );
System . out . println ( writer . toString ());
} finally {
conn . close ();
}
produces:
<?xml version="1.0" encoding="UTF-8"?>
<dsml:dsml xmlns:dsml= "http://www.dsml.org/DSML" >
<dsml:directory-entries>
<dsml:entry dn= "uid=dfisher,ou=people,dc=ldaptive,dc=org" >
<dsml:attr name= "mail" >
<dsml:value> dfisher@ldaptive.org</dsml:value>
</dsml:attr>
</dsml:entry>
</dsml:directory-entries>
</dsml:dsml>
DSML can be read using any java.io.Reader using a Dsmlv1Reader.
FileReader reader = new FileReader ( "entry.dsml" );
Dsmlv1Reader dsmlReader = new Dsmlv1Reader ( reader );
SearchResult result = dsmlReader . read ();
JSON
JSON support is provided in a separate library that uses GSON . This support is provided in a separate library that is available in the jars directory of the latest download .
Or included as a maven dependency:
<dependencies>
<dependency>
<groupId> org.ldaptive</groupId>
<artifactId> ldaptive-json</artifactId>
<version> 1.2.4</version>
</dependency>
</dependencies>
JSON can be written to any java.io.Writer using a JsonWriter.
StringWriter writer = new StringWriter ();
JsonWriter jsonWriter = new JsonWriter ( writer );
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" , new SearchFilter ( "(uid=dfisher)" ), new String [] { "mail" })). getResult ();
jsonWriter . write ( result );
System . out . println ( writer . toString ());
} finally {
conn . close ();
}
produces:
[{ "dn" : "uid=dfisher,ou=people,dc=ldaptive,dc=org" , "mail" :[ "dfisher@ldaptive.org" ]}]
JSON can be read using any java.io.Reader using a JsonReader.
FileReader reader = new FileReader ( "entry.json" );
JsonReader jsonReader = new JsonReader ( reader );
SearchResult result = jsonReader . read ();
Sorting
To control sorting when reading an LDAP result the sort behavior can be supplied to the reader:
FileReader reader = new FileReader ( "entry.ldif" );
LdifReader ldifReader = new LdifReader ( reader , SortBehavior . SORTED );
SearchResult result = ldifReader . read (); // data will be sorted accordingly
Sort behavior can also be controlled by setting a JVM System property:
-Dorg.ldaptive.sortBehavior=ORDERED