private NettyBody body(Request request, boolean connect) throws IOException {
    NettyBody nettyBody = null;
    if (!connect) {

      Charset bodyCharset =
          request.getBodyCharset() == null ? DEFAULT_CHARSET : request.getBodyCharset();

      if (request.getByteData() != null) nettyBody = new NettyByteArrayBody(request.getByteData());
      else if (request.getCompositeByteData() != null)
        nettyBody = new NettyCompositeByteArrayBody(request.getCompositeByteData());
      else if (request.getStringData() != null)
        nettyBody =
            new NettyByteBufferBody(
                StringUtils.charSequence2ByteBuffer(request.getStringData(), bodyCharset));
      else if (request.getByteBufferData() != null)
        nettyBody = new NettyByteBufferBody(request.getByteBufferData());
      else if (request.getStreamData() != null)
        nettyBody = new NettyInputStreamBody(request.getStreamData(), config);
      else if (isNonEmpty(request.getFormParams())) {

        String contentType = null;
        if (!request.getHeaders().containsKey(CONTENT_TYPE))
          contentType = HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED;

        nettyBody =
            new NettyByteBufferBody(
                urlEncodeFormParams(request.getFormParams(), bodyCharset), contentType);

      } else if (isNonEmpty(request.getParts()))
        nettyBody = new NettyMultipartBody(request.getParts(), request.getHeaders(), config);
      else if (request.getFile() != null) nettyBody = new NettyFileBody(request.getFile(), config);
      else if (request.getBodyGenerator() instanceof FileBodyGenerator) {
        FileBodyGenerator fileBodyGenerator = (FileBodyGenerator) request.getBodyGenerator();
        nettyBody =
            new NettyFileBody(
                fileBodyGenerator.getFile(),
                fileBodyGenerator.getRegionSeek(),
                fileBodyGenerator.getRegionLength(),
                config);

      } else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator)
        nettyBody =
            new NettyInputStreamBody(
                InputStreamBodyGenerator.class.cast(request.getBodyGenerator()).getInputStream(),
                config);
      else if (request.getBodyGenerator() != null)
        nettyBody = new NettyBodyBody(request.getBodyGenerator().createBody(), config);
    }

    return nettyBody;
  }
Example #2
0
 private byte[] md5FromRecycledStringBuilder(StringBuilder sb, MessageDigest md) {
   md.update(StringUtils.charSequence2ByteBuffer(sb, ISO_8859_1));
   sb.setLength(0);
   return md.digest();
 }