Esempio n. 1
0
  @Override
  protected ByteBuf extractResponse(Object message) throws XioException {
    if (!(message instanceof HttpObject)) {
      return null;
    }

    FullHttpResponse httpResponse = null;
    //    HttpContent httpContent = null;
    //    ByteBuf content = null;

    //    XioClientChannel xioClientChannel;

    if (message instanceof FullHttpResponse) {
      httpResponse = (FullHttpResponse) message;
      //    content = httpResponse.content();

      //    if (message instanceof LastHttpContent) {
      //      LastHttpContent lastHttpContent = (LastHttpContent) message;
      //      if (lastHttpContent.toString().equals("EmptyLastHttpContent")) {
      //        ((LastHttpContent) message).release();
      //      }
      //    }

      //    if (message instanceof HttpContent) {
      //      httpContent = (HttpContent) message;
      //
      //      if (httpContent.getDecoderResult() == DecoderResult.SUCCESS) {
      //        content = httpContent.content();
      //      }
      //
      //      if (content != null) {
      //        if (!content.isReadable()) {
      //          log.error("Message Delivered with Unreadable Content");
      //        }
      //      }
      //    }

      // TODO(JR): Leave out for testing, ADD BACK BEFORE DEPLOYMENT!!!!!
      //      switch (httpResponse.getStatus().reasonPhrase()) {
      //        case("Unknown Status"):
      //          throw wrapException(new XioTransportException("HTTP response had non-OK status: "
      // + httpResponse
      //            .getStatus().toString()));
      //        case("Informational"):
      //          break;
      //        case("Successful"):
      //          break;
      //        case("Redirection"):
      //          break;
      //        case("Client Error"):
      //          throw wrapException(new XioTransportException("HTTP response had non-OK status: "
      // + httpResponse
      //              .getStatus().toString()));
      //      }

      //      HttpContent httpContent = (HttpContent) httpResponse;

      String CRLF = "\r\n";
      StringBuilder responseHeader = new StringBuilder();
      responseHeader
          .append(httpResponse.getProtocolVersion())
          .append(' ')
          .append(httpResponse.getStatus())
          .append(CRLF);

      httpResponse
          .headers()
          .entries()
          .forEach(
              xs -> {
                responseHeader.append(xs.getKey()).append(": ").append(xs.getValue()).append(CRLF);
              });

      responseHeader.append(CRLF);

      ByteBuf headerAndBody = getCtx().alloc().buffer();
      headerAndBody.writeBytes(responseHeader.toString().getBytes(Charset.defaultCharset()));

      if (httpResponse.content() != null) {
        headerAndBody.writeBytes(httpResponse.content());
        headerAndBody.writeBytes("\r\n".getBytes());
      }

      return headerAndBody;
    } else {
      return null;
    }
  }