Beispiel #1
0
  /**
   * Asynchronous send of HTTP content.
   *
   * @param httpContent The HTTP content to send
   * @param callback The callback to use to notify success or failure
   */
  public void sendContent(HttpContent httpContent, Callback callback) {
    if (LOG.isDebugEnabled()) LOG.debug("sendContent(http={},{})", httpContent, callback);

    if (BufferUtil.hasContent(_aggregate)) {
      callback.failed(new IOException("cannot sendContent() after write()"));
      return;
    }
    if (_channel.isCommitted()) {
      callback.failed(new IOException("committed"));
      return;
    }

    while (true) {
      switch (_state.get()) {
        case OPEN:
          if (!_state.compareAndSet(OutputState.OPEN, OutputState.PENDING)) continue;
          break;

        case ERROR:
          callback.failed(new EofException(_onError));
          return;

        case CLOSED:
          callback.failed(new EofException("Closed"));
          return;

        default:
          throw new IllegalStateException();
      }
      break;
    }

    ByteBuffer buffer = _channel.useDirectBuffers() ? httpContent.getDirectBuffer() : null;
    if (buffer == null) buffer = httpContent.getIndirectBuffer();

    if (buffer != null) {
      sendContent(buffer, callback);
      return;
    }

    try {
      ReadableByteChannel rbc = httpContent.getReadableByteChannel();
      if (rbc != null) {
        // Close of the rbc is done by the async sendContent
        sendContent(rbc, callback);
        return;
      }

      InputStream in = httpContent.getInputStream();
      if (in != null) {
        sendContent(in, callback);
        return;
      }

      throw new IllegalArgumentException("unknown content for " + httpContent);
    } catch (Throwable th) {
      abort(th);
      callback.failed(th);
    }
  }
Beispiel #2
0
 /**
  * Blocking send of HTTP content.
  *
  * @param content The HTTP content to send
  * @throws IOException if the send fails
  */
 public void sendContent(HttpContent content) throws IOException {
   try (Blocker blocker = _writeBlock.acquire()) {
     sendContent(content, blocker);
     blocker.block();
   } catch (Throwable failure) {
     if (LOG.isDebugEnabled()) LOG.debug(failure);
     abort(failure);
     throw failure;
   }
 }
Beispiel #3
0
  /**
   * Asynchronous send of content.
   *
   * @param httpContent The content to send
   * @param callback The callback to use to notify success or failure
   */
  public void sendContent(HttpContent httpContent, Callback callback) throws IOException {
    if (BufferUtil.hasContent(_aggregate)) throw new IOException("written");
    if (_channel.isCommitted()) throw new IOException("committed");

    while (true) {
      switch (_state.get()) {
        case OPEN:
          if (!_state.compareAndSet(State.OPEN, State.PENDING)) continue;
          break;
        case CLOSED:
          throw new EofException("Closed");
        default:
          throw new IllegalStateException();
      }
      break;
    }
    ByteBuffer buffer = _channel.useDirectBuffers() ? httpContent.getDirectBuffer() : null;
    if (buffer == null) buffer = httpContent.getIndirectBuffer();

    if (buffer != null) {
      sendContent(buffer, callback);
      return;
    }

    ReadableByteChannel rbc = httpContent.getReadableByteChannel();
    if (rbc != null) {
      // Close of the rbc is done by the async sendContent
      sendContent(rbc, callback);
      return;
    }

    InputStream in = httpContent.getInputStream();
    if (in != null) {
      sendContent(in, callback);
      return;
    }

    callback.failed(new IllegalArgumentException("unknown content for " + httpContent));
  }
Beispiel #4
0
 /**
  * Blocking send of content.
  *
  * @param content The content to send
  * @throws IOException
  */
 public void sendContent(HttpContent content) throws IOException {
   final BlockingCallback callback = _channel.getWriteBlockingCallback();
   sendContent(content, callback);
   callback.block();
 }