All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opensearch.client.ReactiveResponseConsumer Maven / Gradle / Ivy

There is a newer version: 2.18.0
Show newest version
/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * The OpenSearch Contributors require contributions made to
 * this file be licensed under the Apache-2.0 license or a
 * compatible open source license.
 */

package org.opensearch.client;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.concurrent.BasicFuture;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.client.methods.AsyncByteConsumer;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.Args;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.reactivestreams.Publisher;

class ReactiveResponseConsumer extends AsyncByteConsumer {
    private final BasicFuture>> responseFuture;
    private final ReactiveDataConsumer reactiveDataConsumer = new ReactiveDataConsumer();

    ReactiveResponseConsumer(final FutureCallback>> responseCallback) {
        this.responseFuture = new BasicFuture<>(Args.notNull(responseCallback, "responseCallback"));
    }

    @Override
    protected void onByteReceived(ByteBuffer buf, IOControl ioctrl) throws IOException {
        reactiveDataConsumer.consume(buf);
        ioctrl.requestInput();
    }

    @Override
    public void onResponseReceived(HttpResponse response) throws HttpException, IOException {
        responseFuture.completed(new Message<>(response, reactiveDataConsumer));
    }

    @Override
    public HttpResponse buildResult(HttpContext context) throws Exception {
        reactiveDataConsumer.complete();
        return null;
    }

    @Override
    protected void releaseResources() {
        if (getException() != null) {
            reactiveDataConsumer.failed(getException());
            responseFuture.failed(getException());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy