Search in sources :

Example 1 with AsyncRequestID

use of com.unboundid.ldap.sdk.AsyncRequestID 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 AsyncRequestID

use of com.unboundid.ldap.sdk.AsyncRequestID 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)

Example 3 with AsyncRequestID

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

the class LDAPDebuggerTestCase method testAbandon.

/**
 * Provides test coverage for the abandon operation.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testAbandon() throws Exception {
    final AsyncRequestID requestID = InternalSDKHelper.createAsyncRequestID(1, null);
    conn.abandon(requestID);
}
Also used : AsyncRequestID(com.unboundid.ldap.sdk.AsyncRequestID) Test(org.testng.annotations.Test)

Example 4 with AsyncRequestID

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

the class PersistentSearchRequestControlTestCase method testSendRequestWithPersistentSearchControl.

/**
 * Sends a search request to the server with an assertion control that
 * contains a non-matching filter.
 * <BR><BR>
 * Access to a Directory Server instance is required for complete processing.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSendRequestWithPersistentSearchControl() throws Exception {
    if (!isDirectoryInstanceAvailable()) {
        return;
    }
    // This is necessary if we invoke this method with (invocationCount > 1).
    persistentSearchEntries.clear();
    final LDAPConnection conn = getAdminConnection();
    conn.add(getTestBaseDN(), getBaseEntryAttributes());
    // Start an asynchronous persistent search.  Include "changesOnly=false" so
    // that the existing entry will be returned.  This will be used as a flag to
    // indicate that the search has started.
    final SearchRequest searchRequest = new SearchRequest(this, getTestBaseDN(), SearchScope.BASE, Filter.createPresenceFilter("objectClass"), "1.1");
    searchRequest.addControl(new PersistentSearchRequestControl(PersistentSearchChangeType.allChangeTypes(), false, true, true));
    final AsyncRequestID asyncRequestID = conn.asyncSearch(searchRequest);
    // Wait for a search result entry to appear.  This will signal that the
    // search has started.
    long stopWaitingTime = System.currentTimeMillis() + 30000L;
    while (System.currentTimeMillis() < stopWaitingTime) {
        if (!persistentSearchEntries.isEmpty()) {
            break;
        }
        Thread.sleep(1L);
    }
    assertFalse(persistentSearchEntries.isEmpty());
    assertEquals(persistentSearchEntries.size(), 1);
    // Apply a change to the base entry.
    conn.modify("dn: " + getTestBaseDN(), "changetype: modify", "replace: description", "description: foo");
    // Wait for the change to be returned by the persistent search.
    stopWaitingTime = System.currentTimeMillis() + 30000L;
    while (System.currentTimeMillis() < stopWaitingTime) {
        if (persistentSearchEntries.size() == 2) {
            break;
        }
        Thread.sleep(1L);
    }
    assertEquals(persistentSearchEntries.size(), 2);
    // Cancel the asynchronous search.
    assertResultCodeEquals(conn, new CancelExtendedRequest(asyncRequestID), ResultCode.CANCELED);
    // NOTE:  The following lines are commented out because some versions of the
    // UnboundID Directory Server suffer from a bug that prevented it
    // from returning a result to a canceled persistent search.
    /*
    // Get the search result.
    final LDAPResult genericResult =
         asyncRequestID.get(30L, TimeUnit.SECONDS);
    assertNotNull(genericResult);
    assertTrue(genericResult instanceof SearchResult);

    final SearchResult searchResult = (SearchResult) genericResult;
    assertEquals(searchResult.getResultCode(), ResultCode.CANCELED);
    assertEquals(searchResult.getEntryCount(), 2);
 */
    conn.delete(getTestBaseDN());
    conn.close();
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) AsyncRequestID(com.unboundid.ldap.sdk.AsyncRequestID) CancelExtendedRequest(com.unboundid.ldap.sdk.extensions.CancelExtendedRequest) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.testng.annotations.Test)

Example 5 with AsyncRequestID

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

the class JSONAccessLogRequestHandlerTestCase method testAbandon.

/**
 * Provides test coverage for an abandon.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testAbandon() throws Exception {
    logHandler.clear();
    final LDAPConnection conn = new LDAPConnection("127.0.0.1", successPort);
    // The connect message.
    waitForCount(1);
    List<JSONObject> logMessages = getLogMessageObjects();
    assertEquals(logMessages.size(), 1);
    assertEquals(logMessages.get(0).getFieldAsString("message-type"), "connect");
    logHandler.clear();
    final AsyncRequestID requestID = InternalSDKHelper.createAsyncRequestID(1, conn);
    conn.abandon(requestID);
    waitForCount(1);
    logMessages = getLogMessageObjects();
    assertEquals(logMessages.size(), 1);
    assertEquals(logMessages.get(0).getFieldAsString("message-type"), "request");
    assertEquals(logMessages.get(0).getFieldAsString("operation-type"), "abandon");
    assertEquals(logMessages.get(0).getFieldAsInteger("id-to-abandon").intValue(), 1);
    conn.close();
    // The unbind and disconnect messages.
    waitForCount(2);
    logMessages = getLogMessageObjects();
    assertEquals(logMessages.size(), 2);
    assertEquals(logMessages.get(0).getFieldAsString("message-type"), "request");
    assertEquals(logMessages.get(0).getFieldAsString("operation-type"), "unbind");
    assertEquals(logMessages.get(1).getFieldAsString("message-type"), "disconnect");
}
Also used : AsyncRequestID(com.unboundid.ldap.sdk.AsyncRequestID) JSONObject(com.unboundid.util.json.JSONObject) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.testng.annotations.Test)

Aggregations

AsyncRequestID (com.unboundid.ldap.sdk.AsyncRequestID)7 Test (org.testng.annotations.Test)6 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)5 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)2 Modification (com.unboundid.ldap.sdk.Modification)2 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)2 TestAsyncListener (com.unboundid.ldap.sdk.TestAsyncListener)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 CancelExtendedRequest (com.unboundid.ldap.sdk.extensions.CancelExtendedRequest)1 JSONObject (com.unboundid.util.json.JSONObject)1