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