Exemplo n.º 1
0
 private void prepareResponseChannel(ClientResponse response, ClientExchange exchange) {
   String encoding = response.getResponseHeaders().getLast(TRANSFER_ENCODING);
   boolean chunked = encoding != null && Headers.CHUNKED.equals(new HttpString(encoding));
   String length = response.getResponseHeaders().getFirst(CONTENT_LENGTH);
   if (exchange.getRequest().getMethod().equals(Methods.HEAD)) {
     connection
         .getSourceChannel()
         .setConduit(
             new FixedLengthStreamSourceConduit(
                 connection.getSourceChannel().getConduit(), 0, responseFinishedListener));
   } else if (chunked) {
     connection
         .getSourceChannel()
         .setConduit(
             new ChunkedStreamSourceConduit(
                 connection.getSourceChannel().getConduit(),
                 pushBackStreamSourceConduit,
                 bufferPool,
                 responseFinishedListener,
                 exchange));
   } else if (length != null) {
     try {
       long contentLength = Long.parseLong(length);
       connection
           .getSourceChannel()
           .setConduit(
               new FixedLengthStreamSourceConduit(
                   connection.getSourceChannel().getConduit(),
                   contentLength,
                   responseFinishedListener));
     } catch (NumberFormatException e) {
       handleError(new IOException(e));
       throw e;
     }
   } else if (response.getProtocol().equals(Protocols.HTTP_1_1)) {
     connection
         .getSourceChannel()
         .setConduit(
             new FixedLengthStreamSourceConduit(
                 connection.getSourceChannel().getConduit(), 0, responseFinishedListener));
   } else {
     state |= CLOSE_REQ;
   }
 }
Exemplo n.º 2
0
    @Override
    public void completed(final ClientExchange result) {

      final ClientResponse response = result.getResponse();

      if (log.isDebugEnabled()) {
        log.debugf(
            "Received response %s for request %s for exchange %s",
            response, result.getRequest(), exchange);
      }
      final HeaderMap inboundResponseHeaders = response.getResponseHeaders();
      final HeaderMap outboundResponseHeaders = exchange.getResponseHeaders();
      exchange.setStatusCode(response.getResponseCode());
      copyHeaders(outboundResponseHeaders, inboundResponseHeaders);

      if (exchange.isUpgrade()) {

        exchange.upgradeChannel(
            new HttpUpgradeListener() {
              @Override
              public void handleUpgrade(
                  StreamConnection streamConnection, HttpServerExchange exchange) {

                if (log.isDebugEnabled()) {
                  log.debugf(
                      "Upgraded request %s to for exchange %s", result.getRequest(), exchange);
                }
                StreamConnection clientChannel = null;
                try {
                  clientChannel = result.getConnection().performUpgrade();

                  final ClosingExceptionHandler handler =
                      new ClosingExceptionHandler(streamConnection, clientChannel);
                  Transfer.initiateTransfer(
                      clientChannel.getSourceChannel(),
                      streamConnection.getSinkChannel(),
                      ChannelListeners.closingChannelListener(),
                      ChannelListeners.writeShutdownChannelListener(
                          ChannelListeners.<StreamSinkChannel>flushingChannelListener(
                              ChannelListeners.closingChannelListener(),
                              ChannelListeners.closingChannelExceptionHandler()),
                          ChannelListeners.closingChannelExceptionHandler()),
                      handler,
                      handler,
                      result.getConnection().getBufferPool());
                  Transfer.initiateTransfer(
                      streamConnection.getSourceChannel(),
                      clientChannel.getSinkChannel(),
                      ChannelListeners.closingChannelListener(),
                      ChannelListeners.writeShutdownChannelListener(
                          ChannelListeners.<StreamSinkChannel>flushingChannelListener(
                              ChannelListeners.closingChannelListener(),
                              ChannelListeners.closingChannelExceptionHandler()),
                          ChannelListeners.closingChannelExceptionHandler()),
                      handler,
                      handler,
                      result.getConnection().getBufferPool());

                } catch (IOException e) {
                  IoUtils.safeClose(streamConnection, clientChannel);
                }
              }
            });
      }
      final IoExceptionHandler handler = new IoExceptionHandler(exchange, result.getConnection());
      Transfer.initiateTransfer(
          result.getResponseChannel(),
          exchange.getResponseChannel(),
          ChannelListeners.closingChannelListener(),
          new HTTPTrailerChannelListener(result, exchange),
          handler,
          handler,
          exchange.getConnection().getByteBufferPool());
    }