Content Synchronization (Sync Repl)

Request that the server send the client updates in order for the client to stay in sync with the server. See RFC 4533.

Note that these examples use the DefaultCookieManager which stores cookie data in memory. Most implementers will want to provide a custom implementation to persist cookie data. The interface for CookieManager looks like:

public interface CookieManager
{
  byte[] readCookie(); // invoked when the operation begins

  void writeCookie(byte[] cookie); // invoked whenever a cookie is seen in a control or message
}

Refresh Only

Creates a SyncReplClient to use for a content synchronization without an existing cookie. By setting the persist property to false we expect the server will send the client all content followed by a response. Which means we do not expect to block indefinitely.

SingleConnectionFactory cf = new SingleConnectionFactory("ldap://directory.ldaptive.org");
cf.initialize();
SyncReplClient client = new SyncReplClient(cf, false); // false indicates do not persist
SearchRequest request = SearchRequest.objectScopeSearchRequest("dc=ldaptive,dc=org");
client.setOnEntry(e -> {
  // process an entry
  SyncStateControl ssc = (SyncStateControl) e.getControl(SyncStateControl.OID);
});
client.setOnMessage(m -> {
  // process a message
});
client.setOnResult(r -> {
  // synchronization complete
  SyncDoneControl syncDoneControl = (SyncDoneControl) r.getControl(SyncDoneControl.OID);
});
client.setOnException(e -> {
  // handle exception
});
SearchOperationHandle handle = client.send(request, new DefaultCookieManager());
// wait until result is received
handle.await();
client.close();

Refresh and Persist

Creates a SyncReplClient to use for a content synchronization without an existing cookie. By setting the persist property to true we expect the server will continue sending us content updates until the operation is cancelled.

SingleConnectionFactory cf = new SingleConnectionFactory("ldap://directory.ldaptive.org");
cf.initialize();
SyncReplClient client = new SyncReplClient(cf, true); // true indicates persist
SearchRequest request = SearchRequest.objectScopeSearchRequest("dc=ldaptive,dc=org");
client.setOnEntry(e -> {
  // process this entry with the sync state control data
  SyncStateControl ssc = (SyncStateControl) e.getControl(SyncStateControl.OID);
  if (e.size() > 0) { // arbitrary condition
    // stop receiving updates
    client.cancel();
  }
});
client.setOnMessage(m -> {
  // process a message
});
client.setOnResult(r -> {
  // synchronization complete
  SyncDoneControl syncDoneControl = (SyncDoneControl) r.getControl(SyncDoneControl.OID);
});
client.setOnException(e -> {
  // handle exception
});
SearchOperationHandle handle = client.send(request, new DefaultCookieManager());
// wait until result is received (or forever)
handle.await();
client.close();