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

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

Introduction

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

Prototype

@Override
Future<Void> end();

Source Link

Document

Ends the request.

Usage

From source file:de.braintags.netrelay.controller.api.MailController.java

License:Open Source License

private static void readData(MailPreferences prefs, UriMailAttachment attachment,
        Handler<AsyncResult<Void>> handler) {
    URI uri = attachment.getUri();
    HttpClient client = prefs.httpClient;
    int port = uri.getPort() > 0 ? uri.getPort() : 80;
    HttpClientRequest req = client.request(HttpMethod.GET, port, uri.getHost(), uri.getPath(), resp -> {
        resp.bodyHandler(buff -> {//from ww w  . j  a  va2s .c om
            try {
                attachment.setData(buff);
                handler.handle(Future.succeededFuture());
            } catch (Exception e) {
                LOGGER.error("", e);
                handler.handle(Future.failedFuture(e));
            }
        });
    });
    req.end();
}

From source file:examples.HTTP2Examples.java

License:Open Source License

public void example13(HttpClient client) {

    HttpClientRequest request = client.get("/index.html", response -> {
        // Process index.html response
    });//  ww w.  j  ava  2 s  .  com

    // Set a push handler to be aware of any resource pushed by the server
    request.pushHandler(pushedRequest -> {

        // A resource is pushed for this request
        System.out.println("Server pushed " + pushedRequest.path());

        // Set an handler for the response
        pushedRequest.handler(pushedResponse -> {
            System.out.println("The response for the pushed request");
        });
    });

    // End the request
    request.end();
}

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  . ja va2 s .co 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 example39(HttpClientRequest request) {
    request.end();
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example41(HttpClientRequest request) {

    request.setChunked(true);// ww w .ja  v a 2 s. com

    // 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 statusCodeHandling(HttpClient client) {
    HttpClientRequest request = client.post("some-uri", response -> {
        if (response.statusCode() == 200) {
            System.out.println("Everything fine");
            return;
        }//from   w  w w .  java2s .c om
        if (response.statusCode() == 500) {
            System.out.println("Unexpected behavior on the server side");
            return;
        }
    });
    request.end();
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example44(HttpClientRequest request, AsyncFile file) {

    request.setChunked(true);/*from w  ww  .jav a 2 s .  co  m*/
    Pump pump = Pump.pump(file, request);
    file.endHandler(v -> request.end());
    pump.start();

}

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());
    });//  w w w .  j a  va  2 s .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.apiman.gateway.engine.vertx.polling.fetchers.HttpResourceFetcher.java

License:Apache License

@Override
public void fetch(Handler<Buffer> resultHandler) {
    int port = uri.getPort();
    if (port == -1) {
        if (isHttps) {
            port = 443;//from w  w  w  . j a v  a2s  .c  om
        } else {
            port = 80;
        }
    }

    HttpClientRequest httpClientRequest = vertx.createHttpClient(new HttpClientOptions().setSsl(isHttps))
            .get(port, uri.getHost(), uri.getPath(), clientResponse -> {
                if (clientResponse.statusCode() / 100 == 2) {
                    clientResponse.handler(data -> {
                        rawData.appendBuffer(data);
                    }).endHandler(end -> resultHandler.handle(rawData)).exceptionHandler(exceptionHandler);
                } else {
                    exceptionHandler.handle(
                            new BadResponseCodeError("Unexpected response code when trying to retrieve config: " //$NON-NLS-1$
                                    + clientResponse.statusCode()));
                }
            }).exceptionHandler(exceptionHandler);

    authenticator.authenticate(vertx, config, httpClientRequest.headers(), authResult -> {
        if (authResult.succeeded()) {
            // The client request is executed when HttpClientRequest#end is invoked.
            httpClientRequest.end();
        } else {
            exceptionHandler.handle(authResult.cause());
        }
    });
}

From source file:io.apiman.gateway.platforms.vertx3.engine.VertxPluginRegistry.java

License:Apache License

/**
 * @see io.apiman.gateway.engine.impl.DefaultPluginRegistry#downloadArtifactTo(java.net.URL, java.io.File,
 *      io.apiman.gateway.engine.async.IAsyncResultHandler)
 *///from www . j  a v  a  2 s .c  o m
@Override
protected void downloadArtifactTo(final URL artifactUrl, final File pluginFile,
        final IAsyncResultHandler<File> handler) {
    int port = artifactUrl.getPort();
    if (port == -1) {
        port = 80;
    }

    final HttpClientRequest request = client.get(port, artifactUrl.getHost(), artifactUrl.getPath(),
            (Handler<HttpClientResponse>) response -> {

                response.exceptionHandler((Handler<Throwable>) error -> {
                    handler.handle(AsyncResultImpl.create(error, File.class));
                });

                // Body Handler
                response.handler((Handler<Buffer>) buffer -> {
                    try {
                        Files.write(pluginFile.toPath(), buffer.getBytes(), StandardOpenOption.APPEND,
                                StandardOpenOption.CREATE, StandardOpenOption.WRITE);
                    } catch (IOException e) {
                        handler.handle(AsyncResultImpl.create(e, File.class));
                    }
                });

                response.endHandler((Handler<Void>) event -> {
                    handler.handle(AsyncResultImpl.create(pluginFile));
                });
            });

    request.exceptionHandler((Handler<Throwable>) error -> {
        handler.handle(AsyncResultImpl.create(error, File.class));
    });

    request.end();
}