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

org.asynchttpclient.shaded.netty.handler.StreamedResponsePublisher Maven / Gradle / Ivy

/*
 * Copyright (c) 2015 AsyncHttpClient Project. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package org.asynchttpclient.shaded.netty.handler;

import org.asynchttpclient.shaded.com.typesafe.netty.HandlerPublisher;
import org.asynchttpclient.shaded.io.netty.channel.Channel;
import org.asynchttpclient.shaded.io.netty.channel.ChannelHandlerContext;
import org.asynchttpclient.shaded.io.netty.util.concurrent.EventExecutor;
import org.asynchttpclient.shaded.HttpResponseBodyPart;
import org.asynchttpclient.shaded.netty.NettyResponseFuture;
import org.asynchttpclient.shaded.netty.channel.ChannelManager;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StreamedResponsePublisher extends HandlerPublisher {

  protected final Logger logger = LoggerFactory.getLogger(getClass());

  private final ChannelManager channelManager;
  private final NettyResponseFuture future;
  private final Channel channel;
  private volatile boolean hasOutstandingRequest = false;
  private Throwable error;

  StreamedResponsePublisher(EventExecutor executor, ChannelManager channelManager, NettyResponseFuture future, Channel channel) {
    super(executor, HttpResponseBodyPart.class);
    this.channelManager = channelManager;
    this.future = future;
    this.channel = channel;
  }

  @Override
  protected void cancelled() {
    logger.debug("Subscriber cancelled, ignoring the rest of the body");

    try {
      future.done();
    } catch (Exception t) {
      // Never propagate exception once we know we are done.
      logger.debug(t.getMessage(), t);
    }

    // The subscriber cancelled early - this channel is dead and should be closed.
    channelManager.closeChannel(channel);
  }

  @Override
  protected void requestDemand() {
    hasOutstandingRequest = true;
    super.requestDemand();
  }

  @Override
  public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    hasOutstandingRequest = false;
    super.channelReadComplete(ctx);
  }

  @Override
  public void subscribe(Subscriber subscriber) {
    super.subscribe(new ErrorReplacingSubscriber(subscriber));
  }

  public boolean hasOutstandingRequest() {
    return hasOutstandingRequest;
  }

  NettyResponseFuture future() {
    return future;
  }

  public void setError(Throwable t) {
    this.error = t;
  }

  private class ErrorReplacingSubscriber implements Subscriber {

    private final Subscriber subscriber;

    ErrorReplacingSubscriber(Subscriber subscriber) {
      this.subscriber = subscriber;
    }

    @Override
    public void onSubscribe(Subscription s) {
      subscriber.onSubscribe(s);
    }

    @Override
    public void onNext(HttpResponseBodyPart httpResponseBodyPart) {
      subscriber.onNext(httpResponseBodyPart);
    }

    @Override
    public void onError(Throwable t) {
      subscriber.onError(t);
    }

    @Override
    public void onComplete() {
      Throwable replacementError = error;
      if (replacementError == null) {
        subscriber.onComplete();
      } else {
        subscriber.onError(replacementError);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy