use of io.vertx.core.http.HttpClient in project vertx-web by vert-x3.
the class RoutingContextNullCurrentRouteTest method test.
@Test
public void test(TestContext testContext) {
HttpClient client = vertx.createHttpClient(new HttpClientOptions().setConnectTimeout(10000));
Async async = testContext.async();
HttpClientRequest httpClientRequest = client.get(PORT, "127.0.0.1", "/test", httpClientResponse -> {
testContext.assertEquals(HttpURLConnection.HTTP_NO_CONTENT, httpClientResponse.statusCode());
async.complete();
}).exceptionHandler(testContext::fail);
httpClientRequest.end();
}
use of io.vertx.core.http.HttpClient in project gravitee-management-rest-api by gravitee-io.
the class WebhookNotifierServiceImpl method trigger.
@Override
public void trigger(final Hook hook, GenericNotificationConfig genericNotificationConfig, final Map<String, Object> params) {
if (genericNotificationConfig.getConfig() == null || genericNotificationConfig.getConfig().isEmpty()) {
LOGGER.error("Webhook Notifier configuration is empty");
return;
}
URI requestUri = URI.create(genericNotificationConfig.getConfig());
boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());
final HttpClientOptions options = new HttpClientOptions().setSsl(ssl).setTrustAll(true).setMaxPoolSize(1).setKeepAlive(false).setTcpKeepAlive(false).setConnectTimeout(GLOBAL_TIMEOUT);
final HttpClient httpClient = vertx.createHttpClient(options);
final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
HttpClientRequest request = httpClient.post(port, requestUri.getHost(), requestUri.toString(), response -> LOGGER.debug("Webhook response status code : {}", response.statusCode()));
request.setTimeout(GLOBAL_TIMEOUT);
// body
String body = toJson(hook, params);
// headers
request.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
request.putHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(body.length()));
request.putHeader("X-Gravitee-Event", hook.name());
request.putHeader("X-Gravitee-Event-Scope", hook.getScope().name());
request.putHeader("X-Gravitee-Request-Id", UUID.toString(UUID.random()));
request.write(body);
request.end();
}
use of io.vertx.core.http.HttpClient in project raml-module-builder by folio-org.
the class HttpModuleClient method request.
private void request(HttpMethod method, String endpoint, Map<String, String> headers, boolean cache, Handler<HttpClientResponse> responseHandler, CompletableFuture<Response> cf2) {
if (responseHandler == null) {
CompletableFuture<Response> cf = new CompletableFuture<>();
responseHandler = new HTTPJsonResponseHandler(endpoint, cf);
}
httpClient = vertx.createHttpClient(options);
HttpClientRequest request = null;
if (absoluteHostAddr) {
request = httpClient.requestAbs(method, options.getDefaultHost() + endpoint);
} else {
request = httpClient.request(method, endpoint);
}
request.exceptionHandler(error -> {
Response r = new Response();
r.populateError(endpoint, -1, error.getMessage());
cf2.complete(r);
}).handler(responseHandler);
if (headers != null) {
this.headers.putAll(headers);
}
request.headers().setAll(this.headers);
request.end();
}
use of io.vertx.core.http.HttpClient in project raml-module-builder by folio-org.
the class DemoRamlRestTest method postData.
/**
* for POST
*/
private void postData(TestContext context, String url, Buffer buffer, int errorCode, int mode, String contenttype, String tenant, boolean userIdHeader) {
// save stacktrace for async handler
Exception stacktrace = new RuntimeException();
Async async = context.async();
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = null;
if (mode == 0) {
request = client.putAbs(url);
} else if (mode == 1) {
request = client.postAbs(url);
} else {
request = client.deleteAbs(url);
}
request.exceptionHandler(error -> {
async.complete();
context.fail(new RuntimeException(error.getMessage(), stacktrace));
}).handler(response -> {
int statusCode = response.statusCode();
// is it 2XX
log.info(statusCode + ", " + errorCode + " expected status at " + System.currentTimeMillis() + " mode " + mode + " for " + url);
if (statusCode == errorCode) {
if (statusCode == 422) {
String str = response.getHeader("Content-type");
if (str != null && str.contains("application/json")) {
context.assertTrue(true);
} else {
context.fail(new RuntimeException("422 response code should contain a content type header of application/json", stacktrace));
}
} else if (statusCode == 201) {
response.bodyHandler(responseData -> {
String date = (String) new JsonPathParser(responseData.toJsonObject()).getValueAt("metadata.createdDate");
if (date == null && userIdHeader) {
context.fail(new RuntimeException("metaData schema createdDate missing from returned json", stacktrace));
}
});
}
context.assertTrue(true);
} else {
response.bodyHandler(responseData -> {
context.fail(new RuntimeException("got unexpected response code, expected: " + errorCode + ", received code: " + statusCode + " mode " + mode + " for url " + url + "\ndata:" + responseData.toString(), stacktrace));
});
}
if (!async.isCompleted()) {
async.complete();
}
});
request.setChunked(true);
request.putHeader("X-Okapi-Request-Id", "999999999999");
if (tenant != null) {
request.putHeader("x-okapi-tenant", tenant);
}
request.putHeader("Accept", "application/json,text/plain");
if (userIdHeader) {
request.putHeader("X-Okapi-User-Id", "af23adf0-61ba-4887-bf82-956c4aae2260");
}
if (contenttype != null) {
request.putHeader("Content-type", contenttype);
} else {
if (mode == 0 || mode == 2) {
request.putHeader("Content-type", "application/json");
} else {
request.putHeader("Content-type", "multipart/form-data; boundary=MyBoundary");
}
}
if (buffer != null) {
request.write(buffer);
}
request.end();
}
use of io.vertx.core.http.HttpClient in project raml-module-builder by folio-org.
the class DemoRamlRestTest method jobsTest.
/**
* @param context
*/
private void jobsTest(TestContext context) {
String url = "/jobs/jobconfs";
Async async = context.async();
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = null;
request = client.postAbs("http://localhost:" + port + url);
request.exceptionHandler(error -> {
async.complete();
context.fail(error.getMessage());
}).handler(response -> {
int statusCode = response.statusCode();
String location = response.getHeader("Location");
// is it 2XX
log.info(statusCode + " status at " + System.currentTimeMillis() + " for " + "http://localhost:" + port + url);
if (statusCode == 201) {
checkURLs(context, "http://localhost:" + port + url, 200);
try {
postData(context, "http://localhost:" + port + url + "/" + location + "/jobs", Buffer.buffer(getFile("job.json")), 201, 1, "application/json", TENANT, false);
postData(context, "http://localhost:" + port + url + "/" + location, Buffer.buffer(getFile("job_conf_post.json")), 204, 0, null, TENANT, false);
postData(context, "http://localhost:" + port + url + "/12345", Buffer.buffer(getFile("job_conf_post.json")), 404, 2, null, TENANT, false);
postData(context, "http://localhost:" + port + url + "/" + location + "/jobs/12345", Buffer.buffer(getFile("job_conf_post.json")), 404, 2, null, TENANT, false);
} catch (Exception e) {
log.error(e.getMessage(), e);
context.fail();
}
} else {
context.fail("got incorrect response code");
}
if (!async.isCompleted()) {
async.complete();
}
});
request.setChunked(true);
request.putHeader("Accept", "application/json,text/plain");
request.putHeader("Content-type", "application/json");
try {
request.write(getFile("job_conf_post.json"));
} catch (IOException e) {
log.error(e.getMessage(), e);
context.fail("unable to read file");
}
request.end();
}
Aggregations