Search in sources :

Example 1 with HttpRequestImpl

use of io.vertx.ext.web.client.impl.HttpRequestImpl in project vertx-web by vert-x3.

the class PredicateInterceptor method handle.

@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()) {
                        predicateResult.setHttpResponse(responseCopy(resp, httpContext, null));
                        failOnPredicate(httpContext, errorConverter, predicateResult);
                    } else {
                        resp.bodyHandler(buffer -> {
                            predicateResult.setHttpResponse(responseCopy(resp, httpContext, buffer));
                            failOnPredicate(httpContext, errorConverter, predicateResult);
                        });
                        resp.resume();
                    }
                    return;
                }
            }
        }
    }
    httpContext.next();
}
Also used : ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpRequestImpl(io.vertx.ext.web.client.impl.HttpRequestImpl) ErrorConverter(io.vertx.ext.web.client.predicate.ErrorConverter)

Aggregations

HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 HttpRequestImpl (io.vertx.ext.web.client.impl.HttpRequestImpl)1 ErrorConverter (io.vertx.ext.web.client.predicate.ErrorConverter)1 ResponsePredicate (io.vertx.ext.web.client.predicate.ResponsePredicate)1