001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009-2010 Sun Microsystems, Inc.
015 * Portions copyright 2011-2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.ldap.spi;
018
019import org.forgerock.opendj.ldap.IntermediateResponseHandler;
020import org.forgerock.opendj.ldap.LdapException;
021import org.forgerock.opendj.ldap.ResultCode;
022import org.forgerock.opendj.ldap.SearchResultHandler;
023import org.forgerock.opendj.ldap.controls.ADNotificationRequestControl;
024import org.forgerock.opendj.ldap.controls.PersistentSearchRequestControl;
025import org.forgerock.opendj.ldap.requests.SearchRequest;
026import org.forgerock.opendj.ldap.responses.Responses;
027import org.forgerock.opendj.ldap.responses.Result;
028import org.forgerock.opendj.ldap.responses.SearchResultEntry;
029import org.forgerock.opendj.ldap.responses.SearchResultReference;
030import org.forgerock.util.promise.PromiseImpl;
031
032/** Search result promise implementation. */
033public final class SearchResultLdapPromiseImpl extends ResultLdapPromiseImpl<SearchRequest, Result> implements
034        SearchResultHandler {
035    private SearchResultHandler searchResultHandler;
036    private final boolean isPersistentSearch;
037
038    SearchResultLdapPromiseImpl(
039            final PromiseImpl<Result, LdapException> impl,
040            final int requestID,
041            final SearchRequest request,
042            final SearchResultHandler resultHandler,
043            final IntermediateResponseHandler intermediateResponseHandler) {
044        super(impl, requestID, request, intermediateResponseHandler);
045        this.searchResultHandler = resultHandler;
046        this.isPersistentSearch = request.containsControl(PersistentSearchRequestControl.OID)
047                || request.containsControl(ADNotificationRequestControl.OID);
048    }
049
050    @Override
051    public boolean handleEntry(final SearchResultEntry entry) {
052        // FIXME: there's a potential race condition here - the promise could
053        // get cancelled between the isDone() call and the handler
054        // invocation. We'd need to add support for intermediate handlers in
055        // the synchronizer.
056        if (!isDone()) {
057            updateTimestamp();
058            if (searchResultHandler != null && !searchResultHandler.handleEntry(entry)) {
059                searchResultHandler = null;
060            }
061        }
062        return true;
063    }
064
065    @Override
066    public boolean handleReference(final SearchResultReference reference) {
067        // FIXME: there's a potential race condition here - the promise could
068        // get cancelled between the isDone() call and the handler
069        // invocation. We'd need to add support for intermediate handlers in
070        // the synchronizer.
071        if (!isDone()) {
072            updateTimestamp();
073            if (searchResultHandler != null && !searchResultHandler.handleReference(reference)) {
074                searchResultHandler = null;
075            }
076        }
077        return true;
078    }
079
080    @Override
081    Result newErrorResult(final ResultCode resultCode, final String diagnosticMessage, final Throwable cause) {
082        return Responses.newResult(resultCode).setDiagnosticMessage(diagnosticMessage).setCause(cause);
083    }
084
085    @Override
086    public boolean checkForTimeout() {
087        // Persistent searches should not time out.
088        return !isPersistentSearch;
089    }
090}