/** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request <code>body</code> object encoded as json and the content type * set to <code>application/json</code>. * @param body the body * @param handler */ public void sendJsonObject(JsonObject body, Handler<AsyncResult<io.vertx.rxjava.ext.web.client.HttpResponse<T>>> handler) { delegate.sendJsonObject(body, new Handler<AsyncResult<io.vertx.ext.web.client.HttpResponse<T>>>() { public void handle(AsyncResult<io.vertx.ext.web.client.HttpResponse<T>> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance(ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); }
/** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request <code>body</code> object encoded as json and the content type * set to <code>application/json</code>. * @param body the body * @param handler */ public void sendJsonObject(JsonObject body, Handler<AsyncResult<io.vertx.rxjava.ext.web.client.HttpResponse<T>>> handler) { delegate.sendJsonObject(body, new Handler<AsyncResult<io.vertx.ext.web.client.HttpResponse<T>>>() { public void handle(AsyncResult<io.vertx.ext.web.client.HttpResponse<T>> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance(ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); }
public static void sendJsonObject(io.vertx.ext.web.client.HttpRequest<Object> j_receiver, java.util.Map<String, Object> body, io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.ext.web.client.HttpResponse<java.lang.Object>>> handler) { j_receiver.sendJsonObject(body != null ? io.vertx.core.impl.ConversionHelper.toJsonObject(body) : null, handler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.ext.web.client.HttpResponse<java.lang.Object>>>() { public void handle(io.vertx.core.AsyncResult<io.vertx.ext.web.client.HttpResponse<java.lang.Object>> ar) { handler.handle(ar.map(event -> io.vertx.core.impl.ConversionHelper.fromObject(event))); } } : null); } public static void sendJson(io.vertx.ext.web.client.HttpRequest<Object> j_receiver, java.lang.Object body, io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.ext.web.client.HttpResponse<java.lang.Object>>> handler) {
/** * Logs in against the `userpass` backend. * * @param username the username * @param password the password * @param resultHandler the callback invoked with the result */ public void loginWithUserCredentials(String username, String password, Handler<AsyncResult<Auth>> resultHandler) { JsonObject payload = new JsonObject() .put("password", Objects.requireNonNull(password, "The password must not be null")); client.post("/v1/auth/userpass/login/" + Objects.requireNonNull(username, "The username must not be null")) .sendJsonObject(payload, ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } manageAuthResult(resultHandler, ar.result()); }); }
/** * Logs in against the `AppRole` backend. * * @param roleId the role id * @param secretId the secret id * @param resultHandler the callback invoked with the result */ public void loginWithAppRole(String roleId, String secretId, Handler<AsyncResult<Auth>> resultHandler) { JsonObject payload = new JsonObject() .put("role_id", Objects.requireNonNull(roleId, "The role must not be null")) .put("secret_id", Objects.requireNonNull(secretId, "The secret must not be null")); client.post("/v1/auth/approle/login") .sendJsonObject(payload, ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } manageAuthResult(resultHandler, ar.result()); }); }
/** * Renews the current token. * * @param leaseDurationInSecond the extension in second * @param resultHandler the callback invoked with the result */ public void renewSelf(long leaseDurationInSecond, Handler<AsyncResult<Auth>> resultHandler) { JsonObject payload = null; if (leaseDurationInSecond > 0) { payload = new JsonObject().put("increment", leaseDurationInSecond); } HttpRequest<Buffer> request = client.post("/v1/auth/token/renew-self") .putHeader(TOKEN_HEADER, Objects.requireNonNull(getToken(), "The token must not be null")); Handler<AsyncResult<HttpResponse<Buffer>>> handler = ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } manageAuthResult(resultHandler, ar.result()); }; if (payload != null) { request.sendJsonObject(payload, handler); } else { request.send(handler); } }
/** * Creates a new token. * * @param tokenRequest the token request * @param resultHandler the callback invoked with the result. */ public void createToken(TokenRequest tokenRequest, Handler<AsyncResult<Auth>> resultHandler) { client.post("/v1/auth/token/create" + ((tokenRequest.getRole() == null) ? "" : "/" + tokenRequest.getRole())) .putHeader(TOKEN_HEADER, Objects.requireNonNull(getToken(), "The token must be set")) .sendJsonObject(tokenRequest.toPayload(), ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } manageAuthResult(resultHandler, ar.result()); }); }
/** * Write a secret to `path`. * * @param path the path * @param resultHandler the callback invoked with the result */ public void write(String path, JsonObject secrets, Handler<AsyncResult<Secret>> resultHandler) { Objects.requireNonNull(resultHandler); client.post("/v1/" + Objects.requireNonNull(path)) .putHeader(TOKEN_HEADER, Objects.requireNonNull(getToken(), "The token must be set")) .sendJsonObject(Objects.requireNonNull(secrets, "The secret must be set"), ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } HttpResponse<Buffer> response = ar.result(); if (response.statusCode() == 200 || response.statusCode() == 204) { resultHandler.handle(Future.succeededFuture(response.bodyAsJson(Secret.class))); } else { resultHandler.handle(VaultException.toFailure(response.statusMessage(), response.statusCode(), response.bodyAsString())); } }); }
private void testSendBody(Object body, BiConsumer<String, Buffer> checker) throws Exception { waitFor(2); server.requestHandler(req -> req.bodyHandler(buff -> { checker.accept(req.getHeader("content-type"), buff); complete(); req.response().end(); })); startServer(); HttpRequest<Buffer> post = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); if (body instanceof Buffer) { post.sendBuffer((Buffer) body, onSuccess(resp -> complete())); } else if (body instanceof JsonObject) { post.sendJsonObject((JsonObject) body, onSuccess(resp -> complete())); } else { post.sendJson(body, onSuccess(resp -> complete())); } await(); }
req.sendForm(new CaseInsensitiveHeaders().add("a", "b"), handler); req.sendJson("", handler); req.sendJsonObject(new JsonObject(), handler); req.sendMultipartForm(MultipartForm.create().attribute("a", "b"), handler);