Java Code Examples for io.vertx.core.http.HttpClient#request()
The following examples show how to use
io.vertx.core.http.HttpClient#request() .
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: TranslatorExampleTest.java From weld-vertx with Apache License 2.0 | 6 votes |
@Test(timeout = DEFAULT_TIMEOUT) public void testTranslator(TestContext context) throws InterruptedException { Async async = context.async(); HttpClient client = vertx.createHttpClient(); HttpClientRequest request = client.request(HttpMethod.POST, 8080, "localhost", "/translate"); request.putHeader("Content-type", "application/x-www-form-urlencoded"); request.handler((response) -> { if (response.statusCode() == 200) { response.bodyHandler((buffer) -> { context.assertEquals("[{\"word\":\"Hello\",\"translations\":[\"ahoj\",\"dobry den\"]},{\"word\":\"world\",\"translations\":[\"svet\"]}]", buffer.toString()); client.close(); async.complete(); }); } }); request.end("sentence=Hello world"); }
Example 2
Source File: HttpClientLegacy.java From okapi with Apache License 2.0 | 5 votes |
/** * Send HTTP request with style ala Vert.x 3. * @param client HTTP client * @param method HTTP method * @param port server port * @param host server host * @param response response handler * @return handler for HTTP request */ public static HttpClientRequest request(HttpClient client, HttpMethod method, int port, String host, String uri, Handler<HttpClientResponse> response) { return client.request(method, port, host, uri, hndlr -> { if (hndlr.succeeded()) { response.handle(hndlr.result()); } }); }
Example 3
Source File: APIGatewayVerticle.java From vertx-blueprint-microservice with Apache License 2.0 | 5 votes |
/** * Dispatch the request to the downstream REST layers. * * @param context routing context instance * @param path relative path * @param client relevant HTTP client */ private void doDispatch(RoutingContext context, String path, HttpClient client, Future<Object> cbFuture) { HttpClientRequest toReq = client .request(context.request().method(), path, response -> { response.bodyHandler(body -> { if (response.statusCode() >= 500) { // api endpoint server error, circuit breaker should fail cbFuture.fail(response.statusCode() + ": " + body.toString()); } else { HttpServerResponse toRsp = context.response() .setStatusCode(response.statusCode()); response.headers().forEach(header -> { toRsp.putHeader(header.getKey(), header.getValue()); }); // send response toRsp.end(body); cbFuture.complete(); } ServiceDiscovery.releaseServiceObject(discovery, client); }); }); // set headers context.request().headers().forEach(header -> { toReq.putHeader(header.getKey(), header.getValue()); }); if (context.user() != null) { toReq.putHeader("user-principal", context.user().principal().encode()); } // send request if (context.getBody() == null) { toReq.end(); } else { toReq.end(context.getBody()); } }
Example 4
Source File: DefaultRestClientRequest.java From vertx-rest-client with Apache License 2.0 | 5 votes |
DefaultRestClientRequest(Vertx vertx, DefaultRestClient restClient, HttpClient httpClient, List<HttpMessageConverter> httpMessageConverters, HttpMethod method, String uri, Class<T> responseClass, Handler<RestClientResponse<T>> responseHandler, Long timeoutInMillis, RequestCacheOptions requestCacheOptions, MultiMap globalHeaders, @Nullable Handler<Throwable> exceptionHandler) { checkNotNull(vertx, "vertx must not be null"); checkNotNull(restClient, "restClient must not be null"); checkNotNull(httpClient, "httpClient must not be null"); checkNotNull(httpMessageConverters, "dataMappers must not be null"); checkArgument(!httpMessageConverters.isEmpty(), "dataMappers must not be empty"); checkNotNull(globalHeaders, "globalHeaders must not be null"); this.vertx = vertx; this.restClient = restClient; this.httpClient = httpClient; this.method = method; this.uri = uri; this.httpMessageConverters = httpMessageConverters; this.responseHandler = responseHandler; this.globalHeaders = globalHeaders; httpClientRequest = httpClient.request(method, uri, (HttpClientResponse httpClientResponse) -> { handleResponse(httpClientResponse, responseClass); }); this.requestCacheOptions = requestCacheOptions; this.timeoutInMillis = timeoutInMillis; if (exceptionHandler != null) { exceptionHandler(exceptionHandler); } }
Example 5
Source File: WebTestBase.java From vertx-web with Apache License 2.0 | 5 votes |
protected void testRequestBuffer(HttpClient client, HttpMethod method, int port, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction, int statusCode, String statusMessage, Buffer responseBodyBuffer, boolean normalizeLineEndings) throws Exception { CountDownLatch latch = new CountDownLatch(1); HttpClientRequest req = client.request(method, port, "localhost", path); req.onComplete(onSuccess(resp -> { assertEquals(statusCode, resp.statusCode()); assertEquals(statusMessage, resp.statusMessage()); if (responseAction != null) { responseAction.accept(resp); } if (responseBodyBuffer == null) { latch.countDown(); } else { resp.bodyHandler(buff -> { if (normalizeLineEndings) { buff = normalizeLineEndingsFor(buff); } assertEquals(responseBodyBuffer, buff); latch.countDown(); }); } })); if (requestAction != null) { requestAction.accept(req); } req.end(); awaitLatch(latch); }
Example 6
Source File: HttpProvider.java From gravitee-management-rest-api with Apache License 2.0 | 4 votes |
@Override public CompletableFuture<Collection<DynamicProperty>> get() { CompletableFuture<Buffer> future = new VertxCompletableFuture<>(vertx); URI requestUri = URI.create(configuration.getUrl()); boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme()); final HttpClientOptions options = new HttpClientOptions() .setSsl(ssl) .setTrustAll(true) .setMaxPoolSize(1) .setKeepAlive(false) .setTcpKeepAlive(false) .setConnectTimeout(2000); final HttpClient httpClient = vertx.createHttpClient(options); final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80); try { HttpClientRequest request = httpClient.request( HttpMethod.GET, port, requestUri.getHost(), requestUri.toString() ); request.putHeader(HttpHeaders.USER_AGENT, NodeUtils.userAgent(node)); request.putHeader("X-Gravitee-Request-Id", RandomString.generate()); if (configuration.getHeaders() != null) { configuration.getHeaders().forEach(httpHeader -> request.putHeader(httpHeader.getName(), httpHeader.getValue())); } request.handler(response -> { if (response.statusCode() == HttpStatusCode.OK_200) { response.bodyHandler(buffer -> { future.complete(buffer); // Close client httpClient.close(); }); } else { future.complete(null); } }); request.exceptionHandler(event -> { try { future.completeExceptionally(event); // Close client httpClient.close(); } catch (IllegalStateException ise) { // Do not take care about exception when closing client } }); request.end(); } catch (Exception ex) { logger.error("Unable to look for dynamic properties", ex); future.completeExceptionally(ex); } return future.thenApply(buffer -> { if (buffer == null) { return null; } return mapper.map(buffer.toString()); }); }
Example 7
Source File: HttpProvider.java From gravitee-management-rest-api with Apache License 2.0 | 4 votes |
@Override public CompletableFuture<Collection<DynamicProperty>> get() { CompletableFuture<Buffer> future = new VertxCompletableFuture<>(vertx); URI requestUri = URI.create(dpConfiguration.getUrl()); boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme()); final HttpClientOptions options = new HttpClientOptions() .setSsl(ssl) .setTrustAll(true) .setMaxPoolSize(1) .setKeepAlive(false) .setTcpKeepAlive(false) .setConnectTimeout(2000); final HttpClient httpClient = vertx.createHttpClient(options); final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80); try { HttpClientRequest request = httpClient.request( HttpMethod.GET, port, requestUri.getHost(), requestUri.toString() ); request.putHeader(HttpHeaders.USER_AGENT, NodeUtils.userAgent(node)); request.putHeader("X-Gravitee-Request-Id", RandomString.generate()); request.handler(response -> { if (response.statusCode() == HttpStatusCode.OK_200) { response.bodyHandler(buffer -> { future.complete(buffer); // Close client httpClient.close(); }); } else { future.complete(null); } }); request.exceptionHandler(event -> { try { future.completeExceptionally(event); // Close client httpClient.close(); } catch (IllegalStateException ise) { // Do not take care about exception when closing client } }); request.end(); } catch (Exception ex) { logger.error("Unable to look for dynamic properties", ex); future.completeExceptionally(ex); } return future.thenApply(buffer -> { if (buffer == null) { return null; } return mapper.map(buffer.toString()); }); }