List of usage examples for io.vertx.core.http HttpClient request
HttpClientRequest request(HttpMethod method, int port, String host, String requestURI,
Handler<AsyncResult<HttpClientResponse>> responseHandler);
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 -> {/*w ww . j ava 2 s. 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:io.apiman.gateway.platforms.vertx3.components.HttpClientComponentImpl.java
License:Apache License
@Override public IHttpClientRequest request(String endpoint, HttpMethod method, IAsyncResultHandler<IHttpClientResponse> responseHandler) { URL pEndpoint = parseEndpoint(endpoint); int port = pEndpoint.getPort(); String proto = pEndpoint.getProtocol(); HttpClient client; // If protocol provided if (port != -1 || proto != null) { if (port == 443 || "https".equals(proto)) { //$NON-NLS-1$ client = sslClient;/*from ww w . ja v a2s.co m*/ port = (port == -1) ? 443 : port; } else { client = plainClient; port = (port == -1) ? 80 : port; } } else { client = plainClient; port = 80; } HttpClientRequest request = client.request(convertMethod(method), pEndpoint.getPort(), pEndpoint.getHost(), pEndpoint.getFile(), new HttpClientResponseImpl(responseHandler)); request.setChunked(true); request.exceptionHandler(exception -> { logger.error("Exception in HttpClientRequestImpl: {0}", exception.getMessage()); //$NON-NLS-1$ responseHandler.handle(AsyncResultImpl.create(exception)); }); return new HttpClientRequestImpl(request); }
From source file:io.gravitee.gateway.http.vertx.VertxHttpClient.java
License:Apache License
@Override public ClientRequest request(io.gravitee.common.http.HttpMethod method, URI uri, HttpHeaders headers, Handler<ClientResponse> responseHandler) { HttpClient httpClient = httpClients.computeIfAbsent(Vertx.currentContext(), createHttpClient()); final int port = uri.getPort() != -1 ? uri.getPort() : (HTTPS_SCHEME.equals(uri.getScheme()) ? 443 : 80); String relativeUri = (uri.getRawQuery() == null) ? uri.getRawPath() : uri.getRawPath() + '?' + uri.getRawQuery(); HttpClientRequest clientRequest = httpClient.request(convert(method), port, uri.getHost(), relativeUri, clientResponse -> handleClientResponse(clientResponse, responseHandler)); clientRequest.setTimeout(endpoint.getHttpClientOptions().getReadTimeout()); VertxClientRequest invokerRequest = new VertxClientRequest(clientRequest); clientRequest.exceptionHandler(event -> { LOGGER.error("Server proxying failed: {}", event.getMessage()); if (invokerRequest.connectTimeoutHandler() != null && event instanceof ConnectTimeoutException) { invokerRequest.connectTimeoutHandler().handle(event); } else {// w ww .j av a 2 s . c om VertxClientResponse clientResponse = new VertxClientResponse( ((event instanceof ConnectTimeoutException) || (event instanceof TimeoutException)) ? HttpStatusCode.GATEWAY_TIMEOUT_504 : HttpStatusCode.BAD_GATEWAY_502); clientResponse.headers().set(HttpHeaders.CONNECTION, HttpHeadersValues.CONNECTION_CLOSE); Buffer buffer = null; if (event.getMessage() != null) { // Create body content with error message buffer = Buffer.buffer(event.getMessage()); clientResponse.headers().set(HttpHeaders.CONTENT_LENGTH, Integer.toString(buffer.length())); } responseHandler.handle(clientResponse); if (buffer != null) { clientResponse.bodyHandler().handle(buffer); } clientResponse.endHandler().handle(null); } }); // Copy headers to final API copyRequestHeaders(headers, clientRequest, (port == 80 || port == 443) ? uri.getHost() : uri.getHost() + ':' + port); // Check chunk flag on the request if there are some content to push and if transfer_encoding is set // with chunk value if (hasContent(headers)) { String transferEncoding = headers.getFirst(HttpHeaders.TRANSFER_ENCODING); if (HttpHeadersValues.TRANSFER_ENCODING_CHUNKED.equalsIgnoreCase(transferEncoding)) { clientRequest.setChunked(true); } } // Send HTTP head as soon as possible clientRequest.sendHead(); return invokerRequest; }
From source file:io.servicecomb.transport.rest.client.http.VertxHttpMethod.java
License:Apache License
HttpClientRequest createRequest(HttpClient client, Invocation invocation, IpPort ipPort, String path, AsyncResponse asyncResp) {/* w w w. j a v a 2s . co m*/ HttpMethod method = getMethod(invocation); LOGGER.debug("Calling method {} of {} by rest", method, invocation.getMicroserviceName()); HttpClientRequest request = client.request(method, ipPort.getPort(), ipPort.getHostOrIp(), path, response -> { handleResponse(invocation, response, asyncResp); }); return request; }
From source file:otocloud.webserver.verticle.WebTestBase.java
License:Open Source License
protected void testRequestBuffer(HttpClient client, HttpMethod method, int port, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction, int statusCode, String statusMessage, Buffer responseBodyBuffer) throws Exception { CountDownLatch latch = new CountDownLatch(1); HttpClientRequest req = client.request(method, port, "localhost", path, resp -> { assertEquals(statusCode, resp.statusCode()); assertEquals(statusMessage, resp.statusMessage()); if (responseAction != null) { responseAction.accept(resp); }/*from ww w .j ava2 s. c o m*/ if (responseBodyBuffer == null) { latch.countDown(); } else { resp.bodyHandler(buff -> { assertEquals(responseBodyBuffer, buff); latch.countDown(); }); } }); if (requestAction != null) { requestAction.accept(req); } req.end(); awaitLatch(latch); }