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-2014 ForgeRock AS.
026 */
027
028package org.forgerock.opendj.ldap.spi;
029
030import org.forgerock.opendj.ldap.Connection;
031import org.forgerock.opendj.ldap.IntermediateResponseHandler;
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;
041
042/**
043 * Search result promise implementation.
044 */
045public final class SearchResultLdapPromiseImpl extends ResultLdapPromiseImpl<SearchRequest, Result> implements
046        SearchResultHandler {
047    private SearchResultHandler searchResultHandler;
048    private final boolean isPersistentSearch;
049
050    SearchResultLdapPromiseImpl(final int requestID, final SearchRequest request,
051        final SearchResultHandler resultHandler, final IntermediateResponseHandler intermediateResponseHandler,
052        final Connection connection) {
053        super(requestID, request, intermediateResponseHandler, connection);
054        this.searchResultHandler = resultHandler;
055        this.isPersistentSearch =
056            request.containsControl(PersistentSearchRequestControl.OID)
057                || request.containsControl(ADNotificationRequestControl.OID);
058    }
059
060    /** {@inheritDoc} */
061    @Override
062    public boolean handleEntry(final SearchResultEntry entry) {
063        // FIXME: there's a potential race condition here - the promise could
064        // get cancelled between the isDone() call and the handler
065        // invocation. We'd need to add support for intermediate handlers in
066        // the synchronizer.
067        if (!isDone()) {
068            updateTimestamp();
069            if (searchResultHandler != null && !searchResultHandler.handleEntry(entry)) {
070                searchResultHandler = null;
071            }
072        }
073        return true;
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    public boolean handleReference(final SearchResultReference reference) {
079        // FIXME: there's a potential race condition here - the promise could
080        // get cancelled between the isDone() call and the handler
081        // invocation. We'd need to add support for intermediate handlers in
082        // the synchronizer.
083        if (!isDone()) {
084            updateTimestamp();
085            if (searchResultHandler != null && !searchResultHandler.handleReference(reference)) {
086                searchResultHandler = null;
087            }
088        }
089        return true;
090    }
091
092    @Override
093    Result newErrorResult(final ResultCode resultCode, final String diagnosticMessage,
094            final Throwable cause) {
095        return Responses.newResult(resultCode).setDiagnosticMessage(diagnosticMessage).setCause(cause);
096    }
097
098    @Override
099    public boolean checkForTimeout() {
100        // Persistent searches should not time out.
101        return !isPersistentSearch;
102    }
103
104}