Search in sources :

Example 1 with CancelExtendedRequest

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

the class IdentifyUniqueAttributeConflicts method checkForConflictsInCombination.

/**
 * Performs the processing necessary to check for conflicts between a
 * combination of attribute values obtained from the provided entry.
 *
 * @param  entry  The entry to examine.
 */
private void checkForConflictsInCombination(@NotNull final SearchResultEntry entry) {
    // Construct a filter used to identify conflicting entries as an AND for
    // each attribute.  Handle the possibility of multivalued attributes by
    // creating an OR of all values for each attribute.  And if an additional
    // filter was also specified, include it in the AND as well.
    final ArrayList<Filter> andComponents = new ArrayList<>(attributes.length + 1);
    for (final String attrName : attributes) {
        final LinkedHashSet<Filter> values = new LinkedHashSet<>(StaticUtils.computeMapCapacity(5));
        for (final Attribute a : entry.getAttributesWithOptions(attrName, null)) {
            for (final byte[] value : a.getValueByteArrays()) {
                final Filter equalityFilter = Filter.createEqualityFilter(attrName, value);
                values.add(Filter.createEqualityFilter(attrName, value));
            }
        }
        switch(values.size()) {
            case 0:
                // this entry for conflicts, so just assume there aren't any.
                return;
            case 1:
                andComponents.add(values.iterator().next());
                break;
            default:
                andComponents.add(Filter.createORFilter(values));
                break;
        }
    }
    if (filterArgument.isPresent()) {
        andComponents.add(filterArgument.getValue());
    }
    final Filter filter = Filter.createANDFilter(andComponents);
    // Search below each of the configured base DNs.
    baseDNLoop: for (final DN baseDN : baseDNArgument.getValues()) {
        SearchResult searchResult;
        final SearchRequest searchRequest = new SearchRequest(baseDN.toString(), SearchScope.SUB, DereferencePolicy.NEVER, 2, timeLimitArgument.getValue(), false, filter, "1.1");
        try {
            searchResult = findConflictsPool.search(searchRequest);
        } catch (final LDAPSearchException lse) {
            Debug.debugException(lse);
            if (lse.getResultCode() == ResultCode.TIME_LIMIT_EXCEEDED) {
                // The server spent more time than the configured time limit to
                // process the search.  This almost certainly means that the search is
                // unindexed, and we don't want to continue. Indicate that the time
                // limit has been exceeded, cancel the outer search, and display an
                // error message to the user.
                timeLimitExceeded.set(true);
                try {
                    findConflictsPool.processExtendedOperation(new CancelExtendedRequest(entry.getMessageID()));
                } catch (final Exception e) {
                    Debug.debugException(e);
                }
                err("A server-side time limit was exceeded when searching below " + "base DN '" + baseDN + "' with filter '" + filter + "', which likely means that the search request is not indexed " + "in the server.  Check the server configuration to ensure " + "that any appropriate indexes are in place.  To indicate that " + "searches should not request any time limit, use the " + timeLimitArgument.getIdentifierString() + " to indicate a time limit of zero seconds.");
                return;
            } else if (lse.getResultCode().isConnectionUsable()) {
                searchResult = lse.getSearchResult();
            } else {
                try {
                    searchResult = findConflictsPool.search(searchRequest);
                } catch (final LDAPSearchException lse2) {
                    Debug.debugException(lse2);
                    searchResult = lse2.getSearchResult();
                }
            }
        }
        for (final SearchResultEntry e : searchResult.getSearchEntries()) {
            try {
                if (DN.equals(entry.getDN(), e.getDN())) {
                    continue;
                }
            } catch (final Exception ex) {
                Debug.debugException(ex);
            }
            err("Entry '" + entry.getDN() + " has a combination of values that " + "are also present in entry '" + e.getDN() + "'.");
            combinationConflictCounts.incrementAndGet();
            break baseDNLoop;
        }
        if (searchResult.getResultCode() != ResultCode.SUCCESS) {
            err("An error occurred while attempting to search for conflicts " + " with entry '" + entry.getDN() + "' below '" + baseDN + "':  " + searchResult.getDiagnosticMessage());
            combinationConflictCounts.incrementAndGet();
            break baseDNLoop;
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SearchRequest(com.unboundid.ldap.sdk.SearchRequest) Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) DN(com.unboundid.ldap.sdk.DN) SearchResult(com.unboundid.ldap.sdk.SearchResult) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) Filter(com.unboundid.ldap.sdk.Filter) CancelExtendedRequest(com.unboundid.ldap.sdk.extensions.CancelExtendedRequest) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 2 with CancelExtendedRequest

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

the class LDAPConnectionTestCase method testExtendedCancel.

/**
 * Tests the {@code processExtendedOperation} method variant that takes an
 * extended request object, using the cancel extended request object.
 * <BR><BR>
 * Access to a Directory Server instance is required for complete processing.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testExtendedCancel() throws Exception {
    if (!isDirectoryInstanceAvailable()) {
        return;
    }
    LDAPConnection conn = getAdminConnection();
    try {
        assertTrue(conn.isConnected());
        assertNotNull(conn.getConnectedAddress());
        assertTrue((conn.getConnectedPort() >= 1) && (conn.getConnectedPort() <= 65535));
        assertNotNull(conn.toString());
        CancelExtendedRequest extendedRequest = new CancelExtendedRequest(999);
        ExtendedResult extendedResult = conn.processExtendedOperation(extendedRequest);
        assertNotNull(extendedResult);
        assertEquals(extendedResult.getResultCode(), ResultCode.NO_SUCH_OPERATION);
        assertNull(extendedResult.getOID());
        assertNull(extendedResult.getValue());
    } finally {
        conn.close();
        assertFalse(conn.isConnected());
        assertNull(conn.getConnectedAddress());
        assertTrue(conn.getConnectedPort() < 0);
        assertNotNull(conn.toString());
    }
}
Also used : CancelExtendedRequest(com.unboundid.ldap.sdk.extensions.CancelExtendedRequest) WhoAmIExtendedResult(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedResult) Test(org.testng.annotations.Test)

Example 3 with CancelExtendedRequest

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

the class OperationTypeTestCase method testRequestGetOperationType.

/**
 * Provides test coverage for the {@code Request.getOperationType} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testRequestGetOperationType() throws Exception {
    final AddRequest addRequest = new AddRequest("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
    assertEquals(addRequest.getOperationType(), OperationType.ADD);
    final SimpleBindRequest simpleBindRequest = new SimpleBindRequest("cn=Directory Manager", "password");
    assertEquals(simpleBindRequest.getOperationType(), OperationType.BIND);
    final PLAINBindRequest plainBindRequest = new PLAINBindRequest("u:test.user", "password");
    assertEquals(plainBindRequest.getOperationType(), OperationType.BIND);
    final CompareRequest compareRequest = new CompareRequest("dc=example,dc=com", "foo", "bar");
    assertEquals(compareRequest.getOperationType(), OperationType.COMPARE);
    final DeleteRequest deleteRequest = new DeleteRequest("dc=example,dc=com");
    assertEquals(deleteRequest.getOperationType(), OperationType.DELETE);
    final CancelExtendedRequest cancelRequest = new CancelExtendedRequest(1);
    assertEquals(cancelRequest.getOperationType(), OperationType.EXTENDED);
    final WhoAmIExtendedRequest whoAmIRequest = new WhoAmIExtendedRequest();
    assertEquals(whoAmIRequest.getOperationType(), OperationType.EXTENDED);
    final ModifyRequest modifyRequest = new ModifyRequest("dn: dc=example,dc=com", "changetype: modify", "replace: description", "description: foo");
    assertEquals(modifyRequest.getOperationType(), OperationType.MODIFY);
    final ModifyDNRequest modifyDNRequest = new ModifyDNRequest("ou=People,dc=example,dc=com", "ou=Users", true);
    assertEquals(modifyDNRequest.getOperationType(), OperationType.MODIFY_DN);
    final SearchRequest searchRequest = new SearchRequest("dc=example,dc=com", SearchScope.BASE, "(objectClass=*)");
    assertEquals(searchRequest.getOperationType(), OperationType.SEARCH);
}
Also used : WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) CancelExtendedRequest(com.unboundid.ldap.sdk.extensions.CancelExtendedRequest) Test(org.testng.annotations.Test)

Example 4 with CancelExtendedRequest

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

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

the class SynchronousModeTestCase method testProhibitedSynchronousOperations.

/**
 * Tests the behavior of the LDAP SDK when attempting to perform operations
 * that are not allowed in synchronous mode.
 * <BR><BR>
 * Access to a Directory Server instance is required for complete processing.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testProhibitedSynchronousOperations() throws Exception {
    if (!isDirectoryInstanceAvailable()) {
        return;
    }
    LDAPConnectionOptions options = new LDAPConnectionOptions();
    options.setUseSynchronousMode(true);
    LDAPConnection conn = new LDAPConnection(options);
    conn.connect(getTestHost(), getTestPort());
    try {
        assertTrue(conn.synchronousMode());
        BindResult bindResult = conn.bind(getTestBindDN(), getTestBindPassword());
        assertEquals(bindResult.getResultCode(), ResultCode.SUCCESS);
        try {
            conn.abandon(new AsyncRequestID(1, conn));
            fail("Expected an exception when attempting an abandon on a " + "synchronous connection");
        } catch (LDAPException le) {
            // This was expected.
            assertEquals(le.getResultCode(), ResultCode.NOT_SUPPORTED);
        }
        try {
            conn.processExtendedOperation(new CancelExtendedRequest(1));
            fail("Expected an exception when attempting a cancel on a " + "synchronous connection");
        } catch (LDAPException le) {
            // This was expected.
            assertEquals(le.getResultCode(), ResultCode.NOT_SUPPORTED);
        }
        try {
            conn.asyncAdd(new AddRequest(getTestBaseDN(), getBaseEntryAttributes()), new TestAsyncListener());
            fail("Expected an exception when attempting an async add on a " + "synchronous connection");
        } catch (LDAPException le) {
            // This was expected.
            assertEquals(le.getResultCode(), ResultCode.NOT_SUPPORTED);
        }
        try {
            conn.asyncCompare(new CompareRequest("", "objectClass", "top"), new TestAsyncListener());
            fail("Expected an exception when attempting an async compare on a " + "synchronous connection");
        } catch (LDAPException le) {
            // This was expected.
            assertEquals(le.getResultCode(), ResultCode.NOT_SUPPORTED);
        }
        try {
            conn.asyncDelete(new DeleteRequest(getTestBaseDN()), new TestAsyncListener());
            fail("Expected an exception when attempting an async delete on a " + "synchronous connection");
        } catch (LDAPException le) {
            // This was expected.
            assertEquals(le.getResultCode(), ResultCode.NOT_SUPPORTED);
        }
        try {
            conn.asyncModify(new ModifyRequest(getTestBaseDN(), new Modification(ModificationType.REPLACE, "description", "foo")), new TestAsyncListener());
            fail("Expected an exception when attempting an async modify on a " + "synchronous connection");
        } catch (LDAPException le) {
            // This was expected.
            assertEquals(le.getResultCode(), ResultCode.NOT_SUPPORTED);
        }
        try {
            conn.asyncModifyDN(new ModifyDNRequest("ou=People," + getTestBaseDN(), "ou=Users", true), new TestAsyncListener());
            fail("Expected an exception when attempting an async modify DN on a " + "synchronous connection");
        } catch (LDAPException le) {
            // This was expected.
            assertEquals(le.getResultCode(), ResultCode.NOT_SUPPORTED);
        }
        try {
            conn.asyncSearch(new SearchRequest(new TestAsyncListener(), "", SearchScope.BASE, "(objectClass=*)"));
            fail("Expected an exception when attempting an async search on a " + "synchronous connection");
        } catch (LDAPException le) {
            // This was expected.
            assertEquals(le.getResultCode(), ResultCode.NOT_SUPPORTED);
        }
    } finally {
        conn.close();
    }
}
Also used : CancelExtendedRequest(com.unboundid.ldap.sdk.extensions.CancelExtendedRequest) Test(org.testng.annotations.Test)

Aggregations

CancelExtendedRequest (com.unboundid.ldap.sdk.extensions.CancelExtendedRequest)7 Test (org.testng.annotations.Test)4 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)3 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)2 Attribute (com.unboundid.ldap.sdk.Attribute)2 Filter (com.unboundid.ldap.sdk.Filter)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)2 SearchResult (com.unboundid.ldap.sdk.SearchResult)2 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)2 ArgumentException (com.unboundid.util.args.ArgumentException)2 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)1 AsyncRequestID (com.unboundid.ldap.sdk.AsyncRequestID)1 DN (com.unboundid.ldap.sdk.DN)1 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)1 WhoAmIExtendedRequest (com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest)1 WhoAmIExtendedResult (com.unboundid.ldap.sdk.extensions.WhoAmIExtendedResult)1 InternalUseOnly (com.unboundid.util.InternalUseOnly)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1