package examples;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.FileSystem;
import io.vertx.core.file.OpenOptions;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.parsetools.JsonParser;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
import io.vertx.ext.auth.authentication.TokenCredentials;
import io.vertx.ext.auth.authentication.UsernamePasswordCredentials;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.ext.web.client.predicate.ErrorConverter;
import io.vertx.ext.web.client.predicate.ResponsePredicate;
import io.vertx.ext.web.client.predicate.ResponsePredicateResult;
import io.vertx.ext.web.codec.BodyCodec;
import io.vertx.ext.web.multipart.MultipartForm;
import java.util.function.Function;
public class WebClientExamples {
public void create(Vertx vertx) {
WebClient client = WebClient.create(vertx);
}
public void createFromOptions(Vertx vertx) {
WebClientOptions options = new WebClientOptions()
.setUserAgent("My-App/1.2.3");
options.setKeepAlive(false);
WebClient client = WebClient.create(vertx, options);
}
public void wrap(HttpClient httpClient) {
WebClient client = WebClient.wrap(httpClient);
}
public void simpleGetAndHead(Vertx vertx) {
WebClient client = WebClient.create(vertx);
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.send()
.onSuccess(response -> System.out
.println("Received response with status code" + response.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
client
.head(8080, "myserver.mycompany.com", "/some-uri")
.send()
.onSuccess(response -> System.out
.println("Received response with status code" + response.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void simpleGetWithParams(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.addQueryParam("param", "param_value")
.send()
.onSuccess(response -> System.out
.println("Received response with status code" + response.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void simpleGetWithInitialParams(WebClient client) {
HttpRequest<Buffer> request = client
.get(
8080,
"myserver.mycompany.com",
"/some-uri?param1=param1_value¶m2=param2_value");
request.addQueryParam("param3", "param3_value");
request.setQueryParam("param2", "another_param2_value");
}
public void simpleGetOverwritePreviousParams(WebClient client) {
HttpRequest<Buffer> request = client
.get(8080, "myserver.mycompany.com", "/some-uri");
request.addQueryParam("param1", "param1_value");
request.uri("/some-uri?param1=param1_value¶m2=param2_value");
}
public void multiGet(WebClient client) {
HttpRequest<Buffer> get = client
.get(8080, "myserver.mycompany.com", "/some-uri");
get
.send()
.onSuccess(res -> {
});
get
.send()
.onSuccess(res -> {
});
}
public void multiGetCopy(WebClient client) {
HttpRequest<Buffer> get = client
.get(8080, "myserver.mycompany.com", "/some-uri");
get
.send()
.onSuccess(res -> {
});
get
.copy()
.putHeader("a-header", "with-some-value")
.send()
.onSuccess(res -> {
});
}
public void timeout(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.timeout(5000)
.send()
.onSuccess(res -> {
})
.onFailure(err -> {
});
}
public void sendBuffer(WebClient client, Buffer buffer) {
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendBuffer(buffer)
.onSuccess(res -> {
});
}
public void sendStream(WebClient client, FileSystem fs) {
fs.open("content.txt", new OpenOptions(), fileRes -> {
if (fileRes.succeeded()) {
ReadStream<Buffer> fileStream = fileRes.result();
String fileLen = "1024";
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.putHeader("content-length", fileLen)
.sendStream(fileStream)
.onSuccess(res -> {
})
;
}
});
}
public void sendStreamChunked(WebClient client, ReadStream<Buffer> stream) {
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendStream(stream)
.onSuccess(res -> {
});
}
public void sendJsonObject(WebClient client) {
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendJsonObject(
new JsonObject()
.put("firstName", "Dale")
.put("lastName", "Cooper"))
.onSuccess(res -> {
});
}
static class User {
String firstName;
String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
public void sendJsonPOJO(WebClient client) {
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendJson(new User("Dale", "Cooper"))
.onSuccess(res -> {
});
}
public void sendForm(WebClient client) {
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.set("firstName", "Dale");
form.set("lastName", "Cooper");
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendForm(form)
.onSuccess(res -> {
});
}
public void sendMultipart(WebClient client) {
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.set("firstName", "Dale");
form.set("lastName", "Cooper");
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.putHeader("content-type", "multipart/form-data")
.sendForm(form)
.onSuccess(res -> {
});
}
public void sendMultipartWithFileUpload(WebClient client) {
MultipartForm form = MultipartForm.create()
.attribute("imageDescription", "a very nice image")
.binaryFileUpload(
"imageFile",
"image.jpg",
"/path/to/image",
"image/jpeg");
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendMultipartForm(form)
.onSuccess(res -> {
});
}
public void (WebClient client) {
HttpRequest<Buffer> request = client
.get(8080, "myserver.mycompany.com", "/some-uri");
MultiMap headers = request.headers();
headers.set("content-type", "application/json");
headers.set("other-header", "foo");
}
public void (WebClient client) {
HttpRequest<Buffer> request = client
.get(8080, "myserver.mycompany.com", "/some-uri");
request.putHeader("content-type", "application/json");
request.putHeader("other-header", "foo");
}
public void addBasicAccessAuthentication(WebClient client) {
HttpRequest<Buffer> request = client
.get(8080, "myserver.mycompany.com", "/some-uri")
.authentication(new UsernamePasswordCredentials("myid", "mypassword"));
}
public void addBearerTokenAuthentication(WebClient client) {
HttpRequest<Buffer> request = client
.get(8080, "myserver.mycompany.com", "/some-uri")
.authentication(new TokenCredentials("myBearerToken"));
}
public void receiveResponse(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.send()
.onSuccess(res ->
System.out.println("Received response with status code" + res.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void receiveResponseAsJsonObject(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.jsonObject())
.send()
.onSuccess(res -> {
JsonObject body = res.body();
System.out.println(
"Received response with status code" +
res.statusCode() +
" with body " +
body);
})
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void receiveResponseAsJsonPOJO(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.json(User.class))
.send()
.onSuccess(res -> {
User user = res.body();
System.out.println(
"Received response with status code" +
res.statusCode() +
" with body " +
user.getFirstName() +
" " +
user.getLastName());
})
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void receiveResponseAsWriteStream(WebClient client, WriteStream<Buffer> writeStream) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.pipe(writeStream))
.send()
.onSuccess(res ->
System.out.println("Received response with status code" + res.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void receiveResponseAsJsonStream(WebClient client) {
JsonParser parser = JsonParser.newParser().objectValueMode();
parser.handler(event -> {
JsonObject object = event.objectValue();
System.out.println("Got " + object.encode());
});
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.jsonStream(parser))
.send()
.onSuccess(res ->
System.out.println("Received response with status code" + res.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void receiveResponseAndDiscard(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.none())
.send()
.onSuccess(res ->
System.out.println("Received response with status code" + res.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void receiveResponseAsBufferDecodeAsJsonObject(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.send()
.onSuccess(res -> {
JsonObject body = res.bodyAsJsonObject();
System.out.println(
"Received response with status code" +
res.statusCode() +
" with body " +
body);
})
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void manualSanityChecks(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.send()
.onSuccess(res -> {
if (
res.statusCode() == 200 &&
res.getHeader("content-type").equals("application/json")) {
JsonObject body = res.bodyAsJsonObject();
System.out.println(
"Received response with status code" +
res.statusCode() +
" with body " +
body);
} else {
System.out.println("Something went wrong " + res.statusCode());
}
})
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void usingPredicates(WebClient client) {
Function<HttpResponse<Void>, ResponsePredicateResult> methodsPredicate =
resp -> {
String methods = resp.getHeader("Access-Control-Allow-Methods");
if (methods != null) {
if (methods.contains("POST")) {
return ResponsePredicateResult.success();
}
}
return ResponsePredicateResult.failure("Does not work");
};
client
.request(
HttpMethod.OPTIONS,
8080,
"myserver.mycompany.com",
"/some-uri")
.putHeader("Origin", "Server-b.com")
.putHeader("Access-Control-Request-Method", "POST")
.expect(methodsPredicate)
.send()
.onSuccess(res -> {
})
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void usingPredefinedPredicates(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.expect(ResponsePredicate.SC_SUCCESS)
.expect(ResponsePredicate.JSON)
.send()
.onSuccess(res -> {
JsonObject body = res.bodyAsJsonObject();
System.out.println(
"Received response with status code" +
res.statusCode() +
" with body " +
body);
})
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void usingSpecificStatus(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.expect(ResponsePredicate.status(200, 202))
.send()
.onSuccess(res -> {
});
}
public void usingSpecificContentType(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.expect(ResponsePredicate.contentType("some/content-type"))
.send()
.onSuccess(res -> {
});
}
private static class MyCustomException extends Exception {
private final String code;
public MyCustomException(String message) {
super(message);
code = null;
}
public MyCustomException(String code, String message) {
super(message);
this.code = code;
}
}
public void predicateCustomError() {
ResponsePredicate predicate = ResponsePredicate.create(
ResponsePredicate.SC_SUCCESS,
result -> new MyCustomException(result.message()));
}
public void predicateCustomErrorWithBody() {
ErrorConverter converter = ErrorConverter.createFullBody(result -> {
HttpResponse<Buffer> response = result.response();
if (response
.getHeader("content-type")
.equals("application/json")) {
JsonObject body = response.bodyAsJsonObject();
return new MyCustomException(
body.getString("code"),
body.getString("message"));
}
return new MyCustomException(result.message());
});
ResponsePredicate predicate = ResponsePredicate
.create(ResponsePredicate.SC_SUCCESS, converter);
}
public void testClientDisableFollowRedirects(Vertx vertx) {
WebClient client = WebClient
.create(vertx, new WebClientOptions().setFollowRedirects(false));
}
public void testClientChangeMaxRedirects(Vertx vertx) {
WebClient client = WebClient
.create(vertx, new WebClientOptions().setMaxRedirects(5));
}
public void testClientChangeMaxRedirects(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.followRedirects(false)
.send()
.onSuccess(res -> {
System.out.println("Received response with status code" + res.statusCode());
})
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void testOverrideRequestSSL(WebClient client) {
client
.get(443, "myserver.mycompany.com", "/some-uri")
.ssl(true)
.send()
.onSuccess(res ->
System.out.println("Received response with status code" + res.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void testAbsRequestSSL(WebClient client) {
client
.getAbs("https://myserver.mycompany.com:4043/some-uri")
.send()
.onSuccess(res ->
System.out.println("Received response with status code" + res.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
public void testSocketAddress(WebClient client) {
SocketAddress serverAddress = SocketAddress
.domainSocketAddress("/var/run/docker.sock");
client
.request(
HttpMethod.GET,
serverAddress,
8080,
"localhost",
"/images/json")
.expect(ResponsePredicate.SC_ACCEPTED)
.as(BodyCodec.jsonObject())
.send()
.onSuccess(res ->
System.out.println("Current Docker images" + res.body()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
}
}