Search in sources :

Example 6 with LDAPResponse

use of com.unboundid.ldap.protocol.LDAPResponse in project ldapsdk by pingidentity.

the class CompareRequest method processSync.

/**
 * Processes this compare operation in synchronous mode, in which the same
 * thread will send the request and read the response.
 *
 * @param  connection  The connection to use to communicate with the directory
 *                     server.
 * @param  depth       The current referral depth for this request.  It should
 *                     always be one for the initial request, and should only
 *                     be incremented when following referrals.
 * @param  allowRetry   Indicates whether the request may be re-tried on a
 *                      re-established connection if the initial attempt fails
 *                      in a way that indicates the connection is no longer
 *                      valid and autoReconnect is true.
 *
 * @return  An LDAP result object that provides information about the result
 *          of the compare processing.
 *
 * @throws  LDAPException  If a problem occurs while sending the request or
 *                         reading the response.
 */
@NotNull()
private CompareResult processSync(@NotNull final LDAPConnection connection, final int depth, final boolean allowRetry) throws LDAPException {
    // Create the LDAP message.
    messageID = connection.nextMessageID();
    final LDAPMessage message = new LDAPMessage(messageID, this, getControls());
    // Send the request to the server.
    final long requestTime = System.nanoTime();
    Debug.debugLDAPRequest(Level.INFO, this, messageID, connection);
    final LDAPConnectionLogger logger = connection.getConnectionOptions().getConnectionLogger();
    if (logger != null) {
        logger.logCompareRequest(connection, messageID, this);
    }
    connection.getConnectionStatistics().incrementNumCompareRequests();
    try {
        connection.sendMessage(message, getResponseTimeoutMillis(connection));
    } catch (final LDAPException le) {
        Debug.debugException(le);
        if (allowRetry) {
            final CompareResult retryResult = reconnectAndRetry(connection, depth, le.getResultCode());
            if (retryResult != null) {
                return retryResult;
            }
        }
        throw le;
    }
    while (true) {
        final LDAPResponse response;
        try {
            response = connection.readResponse(messageID);
        } catch (final LDAPException le) {
            Debug.debugException(le);
            if ((le.getResultCode() == ResultCode.TIMEOUT) && connection.getConnectionOptions().abandonOnTimeout()) {
                connection.abandon(messageID);
            }
            if (allowRetry) {
                final CompareResult retryResult = reconnectAndRetry(connection, depth, le.getResultCode());
                if (retryResult != null) {
                    return retryResult;
                }
            }
            throw le;
        }
        if (response instanceof IntermediateResponse) {
            final IntermediateResponseListener listener = getIntermediateResponseListener();
            if (listener != null) {
                listener.intermediateResponseReturned((IntermediateResponse) response);
            }
        } else {
            return handleResponse(connection, response, requestTime, depth, allowRetry);
        }
    }
}
Also used : LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) LDAPResponse(com.unboundid.ldap.protocol.LDAPResponse) NotNull(com.unboundid.util.NotNull)

Example 7 with LDAPResponse

use of com.unboundid.ldap.protocol.LDAPResponse in project ldapsdk by pingidentity.

the class AsyncTimeoutTimerTask method run.

/**
 * Generates a timeout response for the associated operation.
 */
@Override()
public void run() {
    final long waitTimeNanos = System.nanoTime() - helper.getCreateTimeNanos();
    final long waitTimeMillis = waitTimeNanos / 1_000_000L;
    final LDAPConnection conn = helper.getConnection();
    final boolean abandon = conn.getConnectionOptions().abandonOnTimeout();
    final String message;
    if (abandon) {
        message = INFO_ASYNC_OPERATION_TIMEOUT_WITH_ABANDON.get(waitTimeMillis);
    } else {
        message = INFO_ASYNC_OPERATION_TIMEOUT_WITHOUT_ABANDON.get(waitTimeMillis);
    }
    final LDAPResponse response;
    final int messageID = helper.getAsyncRequestID().getMessageID();
    switch(helper.getOperationType()) {
        case ADD:
        case DELETE:
        case MODIFY:
        case MODIFY_DN:
            response = new LDAPResult(messageID, ResultCode.TIMEOUT, message, null, StaticUtils.NO_STRINGS, StaticUtils.NO_CONTROLS);
            break;
        case COMPARE:
            response = new CompareResult(messageID, ResultCode.TIMEOUT, message, null, StaticUtils.NO_STRINGS, StaticUtils.NO_CONTROLS);
            break;
        case SEARCH:
            final AsyncSearchHelper searchHelper = (AsyncSearchHelper) helper;
            response = new SearchResult(messageID, ResultCode.TIMEOUT, message, null, StaticUtils.NO_STRINGS, searchHelper.getNumEntries(), searchHelper.getNumReferences(), StaticUtils.NO_CONTROLS);
            break;
        default:
            // This should never happen.
            return;
    }
    try {
        try {
            final LDAPConnectionReader connectionReader = conn.getConnectionInternals(true).getConnectionReader();
            connectionReader.deregisterResponseAcceptor(messageID);
        } catch (final Exception e) {
            Debug.debugException(e);
        }
        helper.responseReceived(response);
        if (abandon) {
            conn.abandon(helper.getAsyncRequestID());
        }
    } catch (final LDAPException le) {
        Debug.debugException(le);
    }
}
Also used : LDAPResponse(com.unboundid.ldap.protocol.LDAPResponse)

Example 8 with LDAPResponse

use of com.unboundid.ldap.protocol.LDAPResponse in project ldapsdk by pingidentity.

the class SearchRequest method processSync.

/**
 * Processes this search operation in synchronous mode, in which the same
 * thread will send the request and read the response.
 *
 * @param  connection  The connection to use to communicate with the directory
 *                     server.
 * @param  depth       The current referral depth for this request.  It should
 *                     always be one for the initial request, and should only
 *                     be incremented when following referrals.
 * @param  allowRetry  Indicates whether the request may be re-tried on a
 *                     re-established connection if the initial attempt fails
 *                     in a way that indicates the connection is no longer
 *                     valid and autoReconnect is true.
 *
 * @return  An LDAP result object that provides information about the result
 *          of the search processing.
 *
 * @throws  LDAPException  If a problem occurs while sending the request or
 *                         reading the response.
 */
@NotNull()
private SearchResult processSync(@NotNull final LDAPConnection connection, final int depth, final boolean allowRetry) throws LDAPException {
    // Create the LDAP message.
    messageID = connection.nextMessageID();
    final LDAPMessage message = new LDAPMessage(messageID, this, getControls());
    // Send the request to the server.
    final long responseTimeout = getResponseTimeoutMillis(connection);
    final long requestTime = System.nanoTime();
    Debug.debugLDAPRequest(Level.INFO, this, messageID, connection);
    final LDAPConnectionLogger logger = connection.getConnectionOptions().getConnectionLogger();
    if (logger != null) {
        logger.logSearchRequest(connection, messageID, this);
    }
    connection.getConnectionStatistics().incrementNumSearchRequests();
    try {
        connection.sendMessage(message, responseTimeout);
    } catch (final LDAPException le) {
        Debug.debugException(le);
        if (allowRetry) {
            final SearchResult retryResult = reconnectAndRetry(connection, depth, le.getResultCode(), 0, 0);
            if (retryResult != null) {
                return retryResult;
            }
        }
        throw le;
    }
    final ArrayList<SearchResultEntry> entryList;
    final ArrayList<SearchResultReference> referenceList;
    if (searchResultListener == null) {
        entryList = new ArrayList<>(5);
        referenceList = new ArrayList<>(5);
    } else {
        entryList = null;
        referenceList = null;
    }
    int numEntries = 0;
    int numReferences = 0;
    ResultCode intermediateResultCode = ResultCode.SUCCESS;
    while (true) {
        final LDAPResponse response;
        try {
            response = connection.readResponse(messageID);
        } catch (final LDAPException le) {
            Debug.debugException(le);
            if ((le.getResultCode() == ResultCode.TIMEOUT) && connection.getConnectionOptions().abandonOnTimeout()) {
                connection.abandon(messageID);
            }
            if (allowRetry) {
                final SearchResult retryResult = reconnectAndRetry(connection, depth, le.getResultCode(), numEntries, numReferences);
                if (retryResult != null) {
                    return retryResult;
                }
            }
            throw le;
        }
        if (response == null) {
            if (connection.getConnectionOptions().abandonOnTimeout()) {
                connection.abandon(messageID);
            }
            throw new LDAPException(ResultCode.TIMEOUT, ERR_SEARCH_CLIENT_TIMEOUT.get(responseTimeout, messageID, baseDN, scope.getName(), filter.toString(), connection.getHostPort()));
        } else if (response instanceof ConnectionClosedResponse) {
            if (allowRetry) {
                final SearchResult retryResult = reconnectAndRetry(connection, depth, ResultCode.SERVER_DOWN, numEntries, numReferences);
                if (retryResult != null) {
                    return retryResult;
                }
            }
            final ConnectionClosedResponse ccr = (ConnectionClosedResponse) response;
            final String msg = ccr.getMessage();
            if (msg == null) {
                // The connection was closed while waiting for the response.
                final SearchResult searchResult = new SearchResult(messageID, ccr.getResultCode(), ERR_CONN_CLOSED_WAITING_FOR_SEARCH_RESPONSE.get(connection.getHostPort(), toString()), null, null, entryList, referenceList, numEntries, numReferences, null);
                throw new LDAPSearchException(searchResult);
            } else {
                // The connection was closed while waiting for the response.
                final SearchResult searchResult = new SearchResult(messageID, ccr.getResultCode(), ERR_CONN_CLOSED_WAITING_FOR_SEARCH_RESPONSE_WITH_MESSAGE.get(connection.getHostPort(), toString(), msg), null, null, entryList, referenceList, numEntries, numReferences, null);
                throw new LDAPSearchException(searchResult);
            }
        } else if (response instanceof IntermediateResponse) {
            final IntermediateResponseListener listener = getIntermediateResponseListener();
            if (listener != null) {
                listener.intermediateResponseReturned((IntermediateResponse) response);
            }
        } else if (response instanceof SearchResultEntry) {
            final SearchResultEntry searchEntry = (SearchResultEntry) response;
            numEntries++;
            if (searchResultListener == null) {
                entryList.add(searchEntry);
            } else {
                searchResultListener.searchEntryReturned(searchEntry);
            }
        } else if (response instanceof SearchResultReference) {
            final SearchResultReference searchReference = (SearchResultReference) response;
            if (followReferrals(connection)) {
                final LDAPResult result = followSearchReference(messageID, searchReference, connection, depth);
                if (!result.getResultCode().equals(ResultCode.SUCCESS)) {
                    // We couldn't follow the reference.  We don't want to fail the
                    // entire search because of this right now, so treat it as if
                    // referral following had not been enabled.  Also, set the
                    // intermediate result code to match that of the result.
                    numReferences++;
                    if (searchResultListener == null) {
                        referenceList.add(searchReference);
                    } else {
                        searchResultListener.searchReferenceReturned(searchReference);
                    }
                    if (intermediateResultCode.equals(ResultCode.SUCCESS) && (result.getResultCode() != ResultCode.REFERRAL)) {
                        intermediateResultCode = result.getResultCode();
                    }
                } else if (result instanceof SearchResult) {
                    final SearchResult searchResult = (SearchResult) result;
                    numEntries += searchResult.getEntryCount();
                    if (searchResultListener == null) {
                        entryList.addAll(searchResult.getSearchEntries());
                    }
                }
            } else {
                numReferences++;
                if (searchResultListener == null) {
                    referenceList.add(searchReference);
                } else {
                    searchResultListener.searchReferenceReturned(searchReference);
                }
            }
        } else {
            final SearchResult result = (SearchResult) response;
            if (allowRetry) {
                final SearchResult retryResult = reconnectAndRetry(connection, depth, result.getResultCode(), numEntries, numReferences);
                if (retryResult != null) {
                    return retryResult;
                }
            }
            return handleResponse(connection, response, requestTime, depth, numEntries, numReferences, entryList, referenceList, intermediateResultCode);
        }
    }
}
Also used : LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LDAPResponse(com.unboundid.ldap.protocol.LDAPResponse) NotNull(com.unboundid.util.NotNull)

Example 9 with LDAPResponse

use of com.unboundid.ldap.protocol.LDAPResponse in project ldapsdk by pingidentity.

the class SearchRequest method process.

/**
 * Sends this search request to the directory server over the provided
 * connection and returns the associated response.  The search result entries
 * and references will either be collected and returned in the
 * {@code SearchResult} object that is returned, or will be interactively
 * returned via the {@code SearchResultListener} interface.
 *
 * @param  connection  The connection to use to communicate with the directory
 *                     server.
 * @param  depth       The current referral depth for this request.  It should
 *                     always be one for the initial request, and should only
 *                     be incremented when following referrals.
 *
 * @return  An object that provides information about the result of the
 *          search processing, potentially including the sets of matching
 *          entries and/or search references.
 *
 * @throws  LDAPException  If a problem occurs while sending the request or
 *                         reading the response.
 */
@Override()
@NotNull()
protected SearchResult process(@NotNull final LDAPConnection connection, final int depth) throws LDAPException {
    if (connection.synchronousMode()) {
        @SuppressWarnings("deprecation") final boolean autoReconnect = connection.getConnectionOptions().autoReconnect();
        return processSync(connection, depth, autoReconnect);
    }
    final long requestTime = System.nanoTime();
    processAsync(connection, null);
    try {
        // Wait for and process the response.
        final ArrayList<SearchResultEntry> entryList;
        final ArrayList<SearchResultReference> referenceList;
        if (searchResultListener == null) {
            entryList = new ArrayList<>(5);
            referenceList = new ArrayList<>(5);
        } else {
            entryList = null;
            referenceList = null;
        }
        int numEntries = 0;
        int numReferences = 0;
        ResultCode intermediateResultCode = ResultCode.SUCCESS;
        final long responseTimeout = getResponseTimeoutMillis(connection);
        while (true) {
            final LDAPResponse response;
            try {
                if (responseTimeout > 0) {
                    response = responseQueue.poll(responseTimeout, TimeUnit.MILLISECONDS);
                } else {
                    response = responseQueue.take();
                }
            } catch (final InterruptedException ie) {
                Debug.debugException(ie);
                Thread.currentThread().interrupt();
                throw new LDAPException(ResultCode.LOCAL_ERROR, ERR_SEARCH_INTERRUPTED.get(connection.getHostPort()), ie);
            }
            if (response == null) {
                if (connection.getConnectionOptions().abandonOnTimeout()) {
                    connection.abandon(messageID);
                }
                final SearchResult searchResult = new SearchResult(messageID, ResultCode.TIMEOUT, ERR_SEARCH_CLIENT_TIMEOUT.get(responseTimeout, messageID, baseDN, scope.getName(), filter.toString(), connection.getHostPort()), null, null, entryList, referenceList, numEntries, numReferences, null);
                throw new LDAPSearchException(searchResult);
            }
            if (response instanceof ConnectionClosedResponse) {
                final ConnectionClosedResponse ccr = (ConnectionClosedResponse) response;
                final String message = ccr.getMessage();
                if (message == null) {
                    // The connection was closed while waiting for the response.
                    final SearchResult searchResult = new SearchResult(messageID, ccr.getResultCode(), ERR_CONN_CLOSED_WAITING_FOR_SEARCH_RESPONSE.get(connection.getHostPort(), toString()), null, null, entryList, referenceList, numEntries, numReferences, null);
                    throw new LDAPSearchException(searchResult);
                } else {
                    // The connection was closed while waiting for the response.
                    final SearchResult searchResult = new SearchResult(messageID, ccr.getResultCode(), ERR_CONN_CLOSED_WAITING_FOR_SEARCH_RESPONSE_WITH_MESSAGE.get(connection.getHostPort(), toString(), message), null, null, entryList, referenceList, numEntries, numReferences, null);
                    throw new LDAPSearchException(searchResult);
                }
            } else if (response instanceof SearchResultEntry) {
                final SearchResultEntry searchEntry = (SearchResultEntry) response;
                numEntries++;
                if (searchResultListener == null) {
                    entryList.add(searchEntry);
                } else {
                    searchResultListener.searchEntryReturned(searchEntry);
                }
            } else if (response instanceof SearchResultReference) {
                final SearchResultReference searchReference = (SearchResultReference) response;
                if (followReferrals(connection)) {
                    final LDAPResult result = followSearchReference(messageID, searchReference, connection, depth);
                    if (!result.getResultCode().equals(ResultCode.SUCCESS)) {
                        // We couldn't follow the reference.  We don't want to fail the
                        // entire search because of this right now, so treat it as if
                        // referral following had not been enabled.  Also, set the
                        // intermediate result code to match that of the result.
                        numReferences++;
                        if (searchResultListener == null) {
                            referenceList.add(searchReference);
                        } else {
                            searchResultListener.searchReferenceReturned(searchReference);
                        }
                        if (intermediateResultCode.equals(ResultCode.SUCCESS) && (result.getResultCode() != ResultCode.REFERRAL)) {
                            intermediateResultCode = result.getResultCode();
                        }
                    } else if (result instanceof SearchResult) {
                        final SearchResult searchResult = (SearchResult) result;
                        numEntries += searchResult.getEntryCount();
                        if (searchResultListener == null) {
                            entryList.addAll(searchResult.getSearchEntries());
                        }
                    }
                } else {
                    numReferences++;
                    if (searchResultListener == null) {
                        referenceList.add(searchReference);
                    } else {
                        searchResultListener.searchReferenceReturned(searchReference);
                    }
                }
            } else {
                connection.getConnectionStatistics().incrementNumSearchResponses(numEntries, numReferences, (System.nanoTime() - requestTime));
                SearchResult result = (SearchResult) response;
                result.setCounts(numEntries, entryList, numReferences, referenceList);
                if ((result.getResultCode().equals(ResultCode.REFERRAL)) && followReferrals(connection)) {
                    if (depth >= connection.getConnectionOptions().getReferralHopLimit()) {
                        return new SearchResult(messageID, ResultCode.REFERRAL_LIMIT_EXCEEDED, ERR_TOO_MANY_REFERRALS.get(), result.getMatchedDN(), result.getReferralURLs(), entryList, referenceList, numEntries, numReferences, result.getResponseControls());
                    }
                    result = followReferral(result, connection, depth);
                }
                if ((result.getResultCode().equals(ResultCode.SUCCESS)) && (!intermediateResultCode.equals(ResultCode.SUCCESS))) {
                    return new SearchResult(messageID, intermediateResultCode, result.getDiagnosticMessage(), result.getMatchedDN(), result.getReferralURLs(), entryList, referenceList, numEntries, numReferences, result.getResponseControls());
                }
                return result;
            }
        }
    } finally {
        connection.deregisterResponseAcceptor(messageID);
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LDAPResponse(com.unboundid.ldap.protocol.LDAPResponse) NotNull(com.unboundid.util.NotNull)

Example 10 with LDAPResponse

use of com.unboundid.ldap.protocol.LDAPResponse in project ldapsdk by pingidentity.

the class SimpleBindRequest method processSync.

/**
 * Processes this bind operation in synchronous mode, in which the same
 * thread will send the request and read the response.
 *
 * @param  connection  The connection to use to communicate with the directory
 *                     server.
 * @param  allowRetry  Indicates whether the request may be re-tried on a
 *                     re-established connection if the initial attempt fails
 *                     in a way that indicates the connection is no longer
 *                     valid and autoReconnect is true.
 *
 * @return  An LDAP result object that provides information about the result
 *          of the bind processing.
 *
 * @throws  LDAPException  If a problem occurs while sending the request or
 *                         reading the response.
 */
@NotNull()
private BindResult processSync(@NotNull final LDAPConnection connection, final boolean allowRetry) throws LDAPException {
    // Create the LDAP message.
    messageID = connection.nextMessageID();
    final LDAPMessage message = new LDAPMessage(messageID, this, getControls());
    // Send the request to the server.
    final long requestTime = System.nanoTime();
    Debug.debugLDAPRequest(Level.INFO, this, messageID, connection);
    final LDAPConnectionLogger logger = connection.getConnectionOptions().getConnectionLogger();
    if (logger != null) {
        logger.logBindRequest(connection, messageID, this);
    }
    connection.getConnectionStatistics().incrementNumBindRequests();
    try {
        connection.sendMessage(message, getResponseTimeoutMillis(connection));
    } catch (final LDAPException le) {
        Debug.debugException(le);
        if (allowRetry) {
            final BindResult bindResult = reconnectAndRetry(connection, le.getResultCode());
            if (bindResult != null) {
                return bindResult;
            }
        }
        throw le;
    }
    while (true) {
        final LDAPResponse response = connection.readResponse(messageID);
        if (response instanceof IntermediateResponse) {
            final IntermediateResponseListener listener = getIntermediateResponseListener();
            if (listener != null) {
                listener.intermediateResponseReturned((IntermediateResponse) response);
            }
        } else {
            return handleResponse(connection, response, requestTime, allowRetry);
        }
    }
}
Also used : LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) LDAPResponse(com.unboundid.ldap.protocol.LDAPResponse) NotNull(com.unboundid.util.NotNull)

Aggregations

LDAPResponse (com.unboundid.ldap.protocol.LDAPResponse)23 NotNull (com.unboundid.util.NotNull)21 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)10 ASN1Exception (com.unboundid.asn1.ASN1Exception)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)2 NoticeOfDisconnectionExtendedResult (com.unboundid.ldap.sdk.extensions.NoticeOfDisconnectionExtendedResult)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 Level (java.util.logging.Level)2 ASN1StreamReader (com.unboundid.asn1.ASN1StreamReader)1 StartTLSExtendedRequest (com.unboundid.ldap.sdk.extensions.StartTLSExtendedRequest)1 BufferedInputStream (java.io.BufferedInputStream)1 Socket (java.net.Socket)1 HashSet (java.util.HashSet)1 SSLSocket (javax.net.ssl.SSLSocket)1