Search in sources :

Example 1 with TestAsyncListener

use of com.unboundid.ldap.sdk.TestAsyncListener in project ldapsdk by pingidentity.

the class ExampleUsagesTestCase method testPersistentSearchRequestControlExample.

/**
 * Tests the example in the {@code PersistentSearchRequestControl} class.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(enabled = false)
public void testPersistentSearchRequestControlExample() throws Exception {
    // NOTE:  The in-memory directory server doesn't currently support the use
    // of the persistent search control, so this test won't actually do anything
    // except verify that the code compiles.  That's why this test is disabled.
    /* ----- BEGIN PRE-EXAMPLE SETUP ----- */
    final InMemoryDirectoryServer ds = getTestDS(true, true);
    final LDAPConnection connection = ds.getConnection();
    final TestAsyncListener asyncSearchListener = new TestAsyncListener();
    /* ----- BEGIN EXAMPLE CODE ----- */
    // Create a persistent search request that will be notified when there are
    // any writes to any entries at or below "dc=example,dc=com".  The
    // search request should have an asynchronous search result listener so that
    // this thread won't block once the search has started, but results will be
    // handled as soon as they are received.
    SearchRequest persistentSearchRequest = new SearchRequest(asyncSearchListener, "dc=example,dc=com", SearchScope.SUB, Filter.createPresenceFilter("objectClass"));
    persistentSearchRequest.addControl(new PersistentSearchRequestControl(// Notify change types.
    PersistentSearchChangeType.allChangeTypes(), // Only return new changes, don't match existing entries.
    true, // Include change notification controls in search entries.
    true));
    // Launch the persistent search as an asynchronous operation.
    AsyncRequestID persistentSearchRequestID = connection.asyncSearch(persistentSearchRequest);
    // Modify an entry that matches the persistent search criteria.  This
    // should cause the persistent search listener to be notified.
    LDAPResult modifyResult = connection.modify("uid=test.user,ou=People,dc=example,dc=com", new Modification(ModificationType.REPLACE, "description", "test"));
    // Verify that the persistent search listener was notified....
    // Since persistent search operations don't end on their own, we need to
    // abandon the search when we don't need it anymore.
    connection.abandon(persistentSearchRequestID);
    /* ----- END EXAMPLE CODE ----- */
    /* ----- BEGIN POST-EXAMPLE CLEANUP ----- */
    connection.close();
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) Modification(com.unboundid.ldap.sdk.Modification) AsyncRequestID(com.unboundid.ldap.sdk.AsyncRequestID) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) TestAsyncListener(com.unboundid.ldap.sdk.TestAsyncListener) Test(org.testng.annotations.Test)

Example 2 with TestAsyncListener

use of com.unboundid.ldap.sdk.TestAsyncListener in project ldapsdk by pingidentity.

the class ExampleUsagesTestCase method testCancelExtendedRequestExample.

/**
 * Tests the example in the {@code CancelExtendedRequest} class.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testCancelExtendedRequestExample() throws Exception {
    /* ----- BEGIN PRE-EXAMPLE SETUP ----- */
    final InMemoryDirectoryServer ds = getTestDS();
    final LDAPConnection connection = ds.getConnection();
    final TestAsyncListener myAsyncResultListener = new TestAsyncListener();
    /* ----- BEGIN EXAMPLE CODE ----- */
    Modification mod = new Modification(ModificationType.REPLACE, "description", "This is the new description.");
    ModifyRequest modifyRequest = new ModifyRequest("dc=example,dc=com", mod);
    AsyncRequestID asyncRequestID = connection.asyncModify(modifyRequest, myAsyncResultListener);
    // Assume that we've waited a reasonable amount of time but the modify
    // hasn't completed yet so we'll try to cancel it.
    ExtendedResult cancelResult;
    try {
        cancelResult = connection.processExtendedOperation(new CancelExtendedRequest(asyncRequestID));
    // This doesn't necessarily mean that the operation was successful, since
    // some kinds of extended operations (like cancel) return non-success
    // results under normal conditions.
    } catch (LDAPException le) {
        // For an extended operation, this generally means that a problem was
        // encountered while trying to send the request or read the result.
        cancelResult = new ExtendedResult(le);
    }
    switch(cancelResult.getResultCode().intValue()) {
        case ResultCode.CANCELED_INT_VALUE:
            // The modify operation was successfully canceled.
            break;
        case ResultCode.CANNOT_CANCEL_INT_VALUE:
            // modify operation, but it could happen for other kinds of operations.
            break;
        case ResultCode.TOO_LATE_INT_VALUE:
            // server is intending to process the operation.
            break;
        case ResultCode.NO_SUCH_OPERATION_INT_VALUE:
            // operation, most likely because it has already completed.
            break;
        default:
            // This suggests that the operation failed for some other reason.
            break;
    }
    /* ----- END EXAMPLE CODE ----- */
    /* ----- BEGIN POST-EXAMPLE CLEANUP ----- */
    connection.close();
    // Since the in-memory directory server doesn't support the cancel
    // operation, we expect an "unwilling to perform" result.
    assertResultCodeEquals(cancelResult, ResultCode.UNWILLING_TO_PERFORM);
}
Also used : Modification(com.unboundid.ldap.sdk.Modification) AsyncRequestID(com.unboundid.ldap.sdk.AsyncRequestID) LDAPException(com.unboundid.ldap.sdk.LDAPException) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) ModifyRequest(com.unboundid.ldap.sdk.ModifyRequest) TestAsyncListener(com.unboundid.ldap.sdk.TestAsyncListener) Test(org.testng.annotations.Test)

Aggregations

InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)2 AsyncRequestID (com.unboundid.ldap.sdk.AsyncRequestID)2 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)2 Modification (com.unboundid.ldap.sdk.Modification)2 TestAsyncListener (com.unboundid.ldap.sdk.TestAsyncListener)2 Test (org.testng.annotations.Test)2 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)1 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)1 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)1