Example usage for io.vertx.core.http HttpClientRequest write

List of usage examples for io.vertx.core.http HttpClientRequest write

Introduction

In this page you can find the example usage for io.vertx.core.http HttpClientRequest write.

Prototype

Future<Void> write(String chunk);

Source Link

Document

Write a String to the request body, encoded as UTF-8.

Usage

From source file:de.braintags.netrelay.util.MultipartUtil.java

License:Open Source License

/**
 * Completes the request and writes the buffer to the connection
 * //from   ww w .j  a  va  2  s  . c om
 */
public void finish(HttpClientRequest httpConn) {
    httpConn.headers().set("Content-Type", contentType + "; boundary=" + boundary);
    httpConn.headers().set("User-Agent", "CodeJava Agent");
    httpConn.headers().set("Test", "Bonjour");

    // writer.appendString(LINE_FEED);
    writer.appendString("--" + boundary + "--").appendString(LINE_FEED);
    httpConn.headers().set("content-length", String.valueOf(writer.length()));
    httpConn.write(writer);
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example34(Vertx vertx, String body) {
    HttpClient client = vertx.createHttpClient();

    HttpClientRequest request = client.post("some-uri", response -> {
        System.out.println("Received response with status code " + response.statusCode());
    });/*from   www  . j  av  a2 s.  c  o m*/

    // Now do stuff with the request
    request.putHeader("content-length", "1000");
    request.putHeader("content-type", "text/plain");
    request.write(body);

    // Make sure the request is ended when you're done with it
    request.end();

    // Or fluently:

    client.post("some-uri", response -> {
        System.out.println("Received response with status code " + response.statusCode());
    }).putHeader("content-length", "1000").putHeader("content-type", "text/plain").write(body).end();

    // Or event more simply:

    client.post("some-uri", response -> {
        System.out.println("Received response with status code " + response.statusCode());
    }).putHeader("content-type", "text/plain").end(body);

}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example35(HttpClientRequest request) {

    // Write string encoded in UTF-8
    request.write("some data");

    // Write string encoded in specific encoding
    request.write("some other data", "UTF-16");

    // Write a buffer
    Buffer buffer = Buffer.buffer();
    buffer.appendInt(123).appendLong(245l);
    request.write(buffer);//w w  w .  java 2 s .com

}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example41(HttpClientRequest request) {

    request.setChunked(true);//from   w  ww. j  a  v  a2s  .c  om

    // Write some chunks
    for (int i = 0; i < 10; i++) {
        request.write("this-is-chunk-" + i);
    }

    request.end();
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example50(HttpClient client) {

    HttpClientRequest request = client.put("some-uri", response -> {
        System.out.println("Received response with status code " + response.statusCode());
    });/*from  www .  ja v a2s .c  om*/

    request.putHeader("Expect", "100-Continue");

    request.continueHandler(v -> {
        // OK to send rest of body
        request.write("Some data");
        request.write("Some more data");
        request.end();
    });
}

From source file:io.flowly.core.security.OAuth2.java

License:Open Source License

/**
 * Include an Authorization header to the request with the value of Basic and bearer token credentials.
 * Add request body for OAuth2.//w w  w  .  j a v a2s .c om
 *
 * @param request the https client request to which the authorization header is to be added.
 * @param bearerTokenCredentials Base64 encoded bearer token credentials for OAuth2.
 */
public static void prepareOAuthBearerTokenRequest(HttpClientRequest request, String bearerTokenCredentials) {
    String body = "grant_type=client_credentials";
    request.putHeader("Authorization", "Basic " + bearerTokenCredentials);
    request.putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    request.putHeader("Content-Length", "" + body.length());
    request.write(body);
}

From source file:io.helixservice.feature.restclient.RestRequest.java

License:Open Source License

private HttpClientResponse getHttpClientResponse(HttpClientRequest request, Buffer body)
        throws SuspendExecution {
    return awaitEvent(h -> {
        request.handler(h);// w ww.j  a v  a 2 s .co  m
        request.write(body);
        request.end();
    });
}

From source file:org.hawkular.metrics.clients.ptrans.backend.MetricsSender.java

License:Apache License

private void send(List<SingleMetric> metrics) {
    connectionsUsed++;/*from   w  ww.j  a  v  a2  s.c  om*/
    String json = Batcher.metricListToJson(metrics);
    Buffer data = Buffer.buffer(json);
    HttpClientRequest req = httpClient.post(postUri, response -> {
        connectionsUsed--;
        if (response.statusCode() != 200) {
            if (log.isTraceEnabled()) {
                response.bodyHandler(msg -> {
                    log.trace("Could not send metrics: " + response.statusCode() + " : " + msg.toString());
                });
            }
            buffer.reInsert(metrics);
            metricInserted();
        } else {
            scheduleFlush();
        }
    });
    req.putHeader(HttpHeaders.HOST, hostHeader);
    req.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length()));
    req.putHeader(HttpHeaders.CONTENT_TYPE, Constants.APPLICATION_JSON);
    req.putHeader(Constants.TENANT_HEADER_NAME, tenant);
    req.exceptionHandler(err -> {
        connectionsUsed--;
        log.trace("Could not send metrics", err);
        buffer.reInsert(metrics);
        metricInserted();
    });
    req.write(data);
    req.end();
}

From source file:org.sfs.integration.java.func.ExecuteBufferRequest.java

License:Apache License

@Override
public Observable<Void> writeEntity(HttpClientRequest httpClientRequest) {
    return just((Void) null).doOnNext(aVoid -> httpClientRequest.write(buffer));
}

From source file:org.workspace7.k8s.auth.proxy.K8sWebProxyVerticle.java

License:Apache License

/**
 *
 *
 * @param routingContext// w  ww  .j a va  2s . c  om
 */
protected void handleApiPath(RoutingContext routingContext) {

    HttpServerRequest request = routingContext.request();
    HttpServerResponse response = routingContext.response();

    JsonObject userPrincipal = routingContext.user().principal();
    _logger.trace("User Principal:{}" + userPrincipal);

    final String accessToken = userPrincipal.getString("id_token");
    final String authHeader = String.format(BEARER_FORMAT, accessToken);

    _logger.debug("API: Proxying Request to K8s Master :{} with method {}", request.uri(), request.method());

    //Proxying request to Kubernetes Master
    HttpClientRequest k8sClientRequest = k8HttpClient.request(request.method(), request.uri());

    k8sClientRequest.handler(k8sApiResp -> {

        k8sApiResp.exceptionHandler(event -> {
            _logger.error("Error while calling Kubernetes :", event.getCause());
            //TODO clear headers for security
            response.setStatusCode(503).end();
            k8sClientRequest.end();
        });

        response.setChunked(true);
        response.setStatusCode(k8sApiResp.statusCode());
        //TODO clear headers for security
        response.headers().setAll(k8sApiResp.headers());

        k8sApiResp.handler(data -> {
            _logger.debug("Proxying Resp Body:{}", data.toString());
            response.write(data);
        });

        k8sApiResp.endHandler((v) -> {
            response.end();
            k8sClientRequest.end();
        });
    });

    k8sClientRequest.setChunked(true);
    //Add Required Headers to k8s
    k8sClientRequest.headers().set(AUTHORIZATION_HEADER, authHeader);

    request.handler(data -> k8sClientRequest.write(data));

    k8sClientRequest.exceptionHandler(ex -> {
        _logger.error("Error while calling Kubernetes API", ex);
        //TODO clear headers for security
        response.setStatusCode(503).end();
    });

}