Java Code Examples for io.vertx.core.http.HttpClientResponse#bodyHandler()

The following examples show how to use io.vertx.core.http.HttpClientResponse#bodyHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertxRequestTransmitter.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
public void handleResponse(final RoutingContext context, final HttpClientResponse response) {
  logResponse(response);

  response.bodyHandler(
      body ->
          context
              .vertx()
              .executeBlocking(
                  future -> {
                    logResponseBody(body);
                    bodyHandler.handleResponseBody(context, response, body);
                    future.complete();
                  },
                  false,
                  res -> {
                    if (res.failed()) {
                      LOG.error(
                          "An unhandled error occurred while processing "
                              + context.getBodyAsString(),
                          res.cause());
                      context.fail(res.cause());
                    }
                  }));
}
 
Example 2
Source File: S3Client.java    From vertx-s3-client with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(HttpClientResponse response) {
    if (response.statusCode() / 100 != 2) {
        response.bodyHandler(buffer -> {
            try {
                log.warn("Error occurred. Status: {}, Message: {}", response.statusCode(), response.statusMessage());
                logInfoResponse(buffer);

                exceptionHandler.handle(
                        new HttpErrorException(
                                response.statusCode(),
                                response.statusMessage(),
                                (ErrorResponse) jaxbUnmarshaller.unmarshal(convertToSaxSource(buffer.getBytes())),
                                "Error occurred during on '" + action + "'"
                        )
                );
            } catch (Exception e) {
                exceptionHandler.handle(e);
            }
        });
    } else {
        log.info("Request successful. Status: {}, Message: {}", response.statusCode(), response.statusMessage());
        successHandler.handle(new ResponseWithBody<>(responseHeaderMapper.map(response.headers()), response));
    }
}
 
Example 3
Source File: VertxRequestTransmitter.java    From besu with Apache License 2.0 5 votes vote down vote up
private <T> void handleResponse(
    final HttpClientResponse response,
    final ResponseBodyHandler<T> responseHandler,
    final CompletableFuture<T> future) {
  response.bodyHandler(
      responseBody -> {
        try {
          future.complete(
              responseHandler.convertResponse(response.statusCode(), responseBody.getBytes()));
        } catch (final Exception e) {
          future.completeExceptionally(e);
        }
      });
}
 
Example 4
Source File: ServiceRegistryClientImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private Handler<RestResponse> syncHandlerEx(CountDownLatch countDownLatch, Holder<ResponseWrapper> holder) {
  return restResponse -> {
    RequestContext requestContext = restResponse.getRequestContext();
    HttpClientResponse response = restResponse.getResponse();
    if (response == null) {
      // 请求失败,触发请求SC的其他实例
      if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) {
        retry(requestContext, syncHandlerEx(countDownLatch, holder));
      } else {
        countDownLatch.countDown();
      }

      return;
    }
    response.exceptionHandler(e -> {
      LOGGER.error("error in processing response.", e);
      countDownLatch.countDown();
    });
    response.bodyHandler(bodyBuffer -> {
      ResponseWrapper responseWrapper = new ResponseWrapper();
      responseWrapper.response = response;
      responseWrapper.bodyBuffer = bodyBuffer;
      holder.value = responseWrapper;
      countDownLatch.countDown();
    });
  };
}
 
Example 5
Source File: S3Client.java    From vertx-s3-client with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(HttpClientResponse event) {
    event.bodyHandler(buffer -> {
        try {
            if (event.statusCode() / 100 != 2) {
                log.warn("Error occurred. Status: {}, Message: {}", event.statusCode(), event.statusMessage());
                logInfoResponse(buffer);

                final ErrorResponse errorResponse;
                if (headOnly) {
                    errorResponse = null;
                } else {
                    errorResponse = (ErrorResponse) jaxbUnmarshaller.unmarshal(convertToSaxSource(buffer.getBytes()));
                }

                exceptionHandler.handle(
                        new HttpErrorException(
                                event.statusCode(),
                                event.statusMessage(),
                                errorResponse,
                                "Error occurred on '" + action + "'"
                        )
                );
            } else {
                log.info("Request successful. Status: {}, Message: {}", event.statusCode(), event.statusMessage());
                if (log.isDebugEnabled()) {
                    log.debug("Response headers: {}", event.headers());
                }
                successHandler.handle(new HeaderOnlyResponse<>(responseHeaderMapper.map(event.headers())));
            }
        } catch (Exception e) {
            exceptionHandler.handle(e);
        }
    });
}
 
Example 6
Source File: PredicateInterceptor.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(HttpContext<?> httpContext) {

  if (httpContext.phase() == ClientPhase.RECEIVE_RESPONSE) {

    // Run expectations
    HttpRequestImpl request = (HttpRequestImpl) httpContext.request();
    HttpClientResponse resp = httpContext.clientResponse();
    List<ResponsePredicate> expectations = request.expectations;
    if (expectations != null) {
      for (ResponsePredicate expectation : expectations) {
        ResponsePredicateResultImpl predicateResult;
        try {
          predicateResult = (ResponsePredicateResultImpl) expectation.apply(responseCopy(resp, httpContext,null));
        } catch (Exception e) {
          httpContext.fail(e);
          return;
        }
        if (!predicateResult.succeeded()) {
          ErrorConverter errorConverter = expectation.errorConverter();
          if (!errorConverter.requiresBody()) {
            failOnPredicate(httpContext, errorConverter, predicateResult);
          } else {
            resp.bodyHandler(buffer -> {
              predicateResult.setHttpResponse(responseCopy(resp, httpContext, buffer));
              failOnPredicate(httpContext, errorConverter, predicateResult);
            });
            resp.resume();
          }
          return;
        }
      }
    }
  }

  httpContext.next();
}
 
Example 7
Source File: VxmsGateway.java    From vxms with Apache License 2.0 4 votes vote down vote up
public void writeResponse(Future<String> future, HttpClientResponse resp) {
  resp.bodyHandler(body -> future.complete(body.getString(0, body.length())));
}