Example usage for io.vertx.core.http HttpClientResponse handler

List of usage examples for io.vertx.core.http HttpClientResponse handler

Introduction

In this page you can find the example usage for io.vertx.core.http HttpClientResponse handler.

Prototype

@Override
    HttpClientResponse handler(Handler<Buffer> handler);

Source Link

Usage

From source file:io.apiman.gateway.platforms.vertx3.connector.HttpConnector.java

License:Apache License

private void doConnection() {
    String endpoint = ApimanPathUtils.join(apiPath, destination + queryParams(apiRequest.getQueryParams()));
    logger.debug(String.format("Connecting to %s | port: %d verb: %s path: %s", apiHost, apiPort,
            HttpMethod.valueOf(apiRequest.getType()), endpoint));

    clientRequest = client.request(HttpMethod.valueOf(apiRequest.getType()), apiPort, apiHost, endpoint,
            (HttpClientResponse vxClientResponse) -> {
                clientResponse = vxClientResponse;

                // Pause until we're given permission to xfer the response.
                vxClientResponse.pause();

                apiResponse = HttpApiFactory.buildResponse(vxClientResponse, SUPPRESSED_HEADERS);

                vxClientResponse.handler((Handler<Buffer>) chunk -> {
                    bodyHandler.handle(new VertxApimanBuffer(chunk));
                });/*from   www  . j av  a2 s .co  m*/

                vxClientResponse.endHandler((Handler<Void>) v -> {
                    endHandler.handle((Void) null);
                });

                vxClientResponse.exceptionHandler(exceptionHandler);

                // The response is only ever returned when vxClientResponse is valid.
                resultHandler.handle(AsyncResultImpl.create((IApiConnectionResponse) HttpConnector.this));
            });

    clientRequest.exceptionHandler(exceptionHandler);

    if (options.hasDataPolicy()) {
        clientRequest.headers().remove("Content-Length");
        clientRequest.setChunked(true);
    }

    apiRequest.getHeaders().forEach(e -> clientRequest.headers().add(e.getKey(), e.getValue()));

    addMandatoryRequestHeaders(clientRequest.headers());

    if (options.getRequiredAuthType() == RequiredAuthType.BASIC) {
        clientRequest.putHeader("Authorization",
                Basic.encode(basicOptions.getUsername(), basicOptions.getPassword()));
    }
}

From source file:io.gravitee.gateway.http.connector.VertxHttpClient.java

License:Apache License

private void handleClientResponse(final VertxProxyConnection proxyConnection,
        final HttpClientResponse clientResponse, final HttpClientRequest clientRequest) {
    VertxProxyResponse proxyClientResponse = new VertxProxyResponse(clientResponse);
    proxyConnection.setProxyResponse(proxyClientResponse);

    // Copy HTTP headers
    clientResponse.headers().names().forEach(headerName -> proxyClientResponse.headers().put(headerName,
            clientResponse.headers().getAll(headerName)));

    proxyClientResponse.pause();//from  w w  w .java2s . c  o  m

    // Copy body content
    clientResponse.handler(event -> proxyClientResponse.bodyHandler().handle(Buffer.buffer(event.getBytes())));

    // Signal end of the response
    clientResponse.endHandler(v -> proxyClientResponse.endHandler().handle(null));

    clientResponse.exceptionHandler(throwable -> {
        LOGGER.error("Unexpected error while handling backend response for request {} {} - {}",
                clientRequest.method(), clientRequest.absoluteURI(), throwable.getMessage());
        ProxyResponse clientResponse1 = new EmptyProxyResponse(HttpStatusCode.BAD_GATEWAY_502);

        proxyConnection.handleResponse(clientResponse1);
    });

    proxyConnection.handleResponse(proxyClientResponse);
}

From source file:io.gravitee.gateway.http.vertx.VertxHttpClient.java

License:Apache License

private void handleClientResponse(HttpClientResponse clientResponse,
        Handler<ClientResponse> clientResponseHandler) {
    VertxClientResponse proxyClientResponse = new VertxClientResponse(clientResponse.statusCode());

    // Copy HTTP headers
    clientResponse.headers().names().forEach(headerName -> proxyClientResponse.headers().put(headerName,
            clientResponse.headers().getAll(headerName)));

    // Copy body content
    clientResponse.handler(event -> proxyClientResponse.bodyHandler().handle(Buffer.buffer(event.getBytes())));

    // Signal end of the response
    clientResponse.endHandler(v -> proxyClientResponse.endHandler().handle(null));

    clientResponseHandler.handle(proxyClientResponse);
}