Search in sources :

Example 1 with SearchResultListener

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

the class InMemoryDirectoryServer method search.

/**
 * {@inheritDoc}
 * <BR><BR>
 * This method may be used regardless of whether the server is listening for
 * client connections, and regardless of whether search operations are allowed
 * in the server.
 */
@Override()
@NotNull()
public SearchResult search(@NotNull final SearchRequest searchRequest) throws LDAPSearchException {
    final ArrayList<Control> requestControlList = new ArrayList<>(searchRequest.getControlList());
    requestControlList.add(new Control(InMemoryRequestHandler.OID_INTERNAL_OPERATION_REQUEST_CONTROL, false));
    final List<SearchResultEntry> entryList = new ArrayList<>(10);
    final List<SearchResultReference> referenceList = new ArrayList<>(10);
    final LDAPMessage responseMessage = inMemoryHandler.processSearchRequest(1, new SearchRequestProtocolOp(searchRequest.getBaseDN(), searchRequest.getScope(), searchRequest.getDereferencePolicy(), searchRequest.getSizeLimit(), searchRequest.getTimeLimitSeconds(), searchRequest.typesOnly(), searchRequest.getFilter(), searchRequest.getAttributeList()), requestControlList, entryList, referenceList);
    final List<SearchResultEntry> returnEntryList;
    final List<SearchResultReference> returnReferenceList;
    final SearchResultListener searchListener = searchRequest.getSearchResultListener();
    if (searchListener == null) {
        returnEntryList = Collections.unmodifiableList(entryList);
        returnReferenceList = Collections.unmodifiableList(referenceList);
    } else {
        returnEntryList = null;
        returnReferenceList = null;
        for (final SearchResultEntry e : entryList) {
            searchListener.searchEntryReturned(e);
        }
        for (final SearchResultReference r : referenceList) {
            searchListener.searchReferenceReturned(r);
        }
    }
    final SearchResultDoneProtocolOp searchDone = responseMessage.getSearchResultDoneProtocolOp();
    final ResultCode rc = ResultCode.valueOf(searchDone.getResultCode());
    final String[] referralURLs;
    final List<String> referralURLList = searchDone.getReferralURLs();
    if ((referralURLList == null) || referralURLList.isEmpty()) {
        referralURLs = StaticUtils.NO_STRINGS;
    } else {
        referralURLs = new String[referralURLList.size()];
        referralURLList.toArray(referralURLs);
    }
    final Control[] responseControls;
    final List<Control> controlList = responseMessage.getControls();
    if ((controlList == null) || controlList.isEmpty()) {
        responseControls = StaticUtils.NO_CONTROLS;
    } else {
        responseControls = new Control[controlList.size()];
        controlList.toArray(responseControls);
    }
    final SearchResult searchResult = new SearchResult(responseMessage.getMessageID(), rc, searchDone.getDiagnosticMessage(), searchDone.getMatchedDN(), referralURLs, returnEntryList, returnReferenceList, entryList.size(), referenceList.size(), responseControls);
    if (rc == ResultCode.SUCCESS) {
        return searchResult;
    } else {
        throw new LDAPSearchException(searchResult);
    }
}
Also used : SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) ArrayList(java.util.ArrayList) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SearchResultReference(com.unboundid.ldap.sdk.SearchResultReference) SearchResult(com.unboundid.ldap.sdk.SearchResult) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Control(com.unboundid.ldap.sdk.Control) SearchResultListener(com.unboundid.ldap.sdk.SearchResultListener) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) ResultCode(com.unboundid.ldap.sdk.ResultCode) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) NotNull(com.unboundid.util.NotNull)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)1 SearchRequestProtocolOp (com.unboundid.ldap.protocol.SearchRequestProtocolOp)1 SearchResultDoneProtocolOp (com.unboundid.ldap.protocol.SearchResultDoneProtocolOp)1 Control (com.unboundid.ldap.sdk.Control)1 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)1 ResultCode (com.unboundid.ldap.sdk.ResultCode)1 SearchResult (com.unboundid.ldap.sdk.SearchResult)1 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)1 SearchResultListener (com.unboundid.ldap.sdk.SearchResultListener)1 SearchResultReference (com.unboundid.ldap.sdk.SearchResultReference)1 NotNull (com.unboundid.util.NotNull)1 ArrayList (java.util.ArrayList)1