use of io.vertx.core.http.HttpClient in project java-chassis by ServiceComb.
the class TestVertxDeleteMethod method testCreateRequest.
@Test
public void testCreateRequest() {
HttpClient client = Mockito.mock(HttpClient.class);
IpPort ipPort = Mockito.mock(IpPort.class);
Mockito.when(ipPort.getPort()).thenReturn(23);
Mockito.when(ipPort.getHostOrIp()).thenReturn("test");
Invocation invocation = Mockito.mock(Invocation.class);
RestOperationMeta operation = Mockito.mock(RestOperationMeta.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
HttpClientRequest obj = (HttpClientRequest) VertxDeleteMethod.INSTANCE.createRequest(client, invocation, ipPort, "testCall", operation, asyncResp);
Assert.assertNull(obj);
}
use of io.vertx.core.http.HttpClient in project nem2-sdk-java by nemtech.
the class Listener method open.
/**
* @return a {@link CompletableFuture} that resolves when the websocket connection is opened
*/
public CompletableFuture<Void> open() {
HttpClient httpClient = Vertx.vertx().createHttpClient();
CompletableFuture<Void> future = new CompletableFuture<>();
if (this.webSocket != null) {
return CompletableFuture.completedFuture(null);
}
RequestOptions requestOptions = new RequestOptions();
requestOptions.setHost(this.url.getHost());
requestOptions.setPort(this.url.getPort());
requestOptions.setURI("/ws");
httpClient.websocket(requestOptions, webSocket -> {
this.webSocket = webSocket;
webSocket.handler(handler -> {
JsonObject message = handler.toJsonObject();
if (message.containsKey("uid")) {
this.UID = message.getString("uid");
future.complete(null);
} else if (message.containsKey("transaction")) {
this.messageSubject.onNext(new ListenerMessage(ListenerChannel.rawValueOf(message.getJsonObject("meta").getString("channelName")), new TransactionMapping().apply(message)));
} else if (message.containsKey("block")) {
final JsonObject meta = message.getJsonObject("meta");
final JsonObject block = message.getJsonObject("block");
int rawNetworkType = (int) Long.parseLong(Integer.toHexString(block.getInteger("version")).substring(0, 2), 16);
final NetworkType networkType;
if (rawNetworkType == NetworkType.MIJIN_TEST.getValue())
networkType = NetworkType.MIJIN_TEST;
else if (rawNetworkType == NetworkType.MIJIN.getValue())
networkType = NetworkType.MIJIN;
else if (rawNetworkType == NetworkType.MAIN_NET.getValue())
networkType = NetworkType.MAIN_NET;
else
networkType = NetworkType.TEST_NET;
final int version = (int) Long.parseLong(Integer.toHexString(block.getInteger("version")).substring(2, 4), 16);
this.messageSubject.onNext(new ListenerMessage(ListenerChannel.BLOCK, new BlockInfo(meta.getString("hash"), meta.getString("generationHash"), Optional.empty(), Optional.empty(), block.getString("signature"), new PublicAccount(block.getString("signer"), networkType), networkType, version, block.getInteger("type"), extractBigInteger(block.getJsonArray("height")), extractBigInteger(block.getJsonArray("timestamp")), extractBigInteger(block.getJsonArray("difficulty")), block.getString("previousBlockHash"), block.getString("blockTransactionsHash"))));
} else if (message.containsKey("status")) {
this.messageSubject.onNext(new ListenerMessage(ListenerChannel.STATUS, new TransactionStatusError(message.getString("hash"), message.getString("status"), new Deadline(extractBigInteger(message.getJsonArray("deadline"))))));
} else if (message.containsKey("meta")) {
this.messageSubject.onNext(new ListenerMessage(ListenerChannel.rawValueOf(message.getJsonObject("meta").getString("channelName")), message.getJsonObject("meta").getString("hash")));
} else if (message.containsKey("parentHash")) {
this.messageSubject.onNext(new ListenerMessage(ListenerChannel.COSIGNATURE, new CosignatureSignedTransaction(message.getString("parenthash"), message.getString("signature"), message.getString("signer"))));
}
});
});
return future;
}
use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.
the class ServiceDiscoveryVerticle method start.
@Override
public void start() {
ServiceDiscovery discovery = ServiceDiscovery.create(vertx, new ServiceDiscoveryOptions().setAnnounceAddress("service-announce").setName("my-name"));
// create a new custom record
Record record1 = new Record().setType("eventbus-service-proxy").setLocation(new JsonObject().put("endpoint", "the-service-address")).setName("my-service").setMetadata(new JsonObject().put("some-label", "some-value"));
// publish "my-service" service
discovery.publish(record1, ar -> {
if (ar.succeeded()) {
System.out.println("\"" + record1.getName() + "\" successfully published!");
Record publishedRecord = ar.result();
} else {
// publication failed
}
});
// create a record from type
Record record2 = HttpEndpoint.createRecord("some-rest-api", "localhost", 8080, "/api");
// publish the service
discovery.publish(record2, ar -> {
if (ar.succeeded()) {
System.out.println("\"" + record2.getName() + "\" successfully published!");
Record publishedRecord = ar.result();
} else {
// publication failed
}
});
// unpublish "my-service"
discovery.unpublish(record1.getRegistration(), ar -> {
if (ar.succeeded()) {
System.out.println("\"" + record1.getName() + "\" successfully unpublished");
} else {
// cannot un-publish the service, may have already been removed, or the record is not published
}
});
// consuming a service
discovery.getRecord(r -> r.getName().equals(record2.getName()), ar -> {
if (ar.succeeded()) {
if (ar.result() != null) {
// Retrieve the service reference
ServiceReference reference = discovery.getReference(ar.result());
// Retrieve the service object
HttpClient client = reference.get();
System.out.println("Consuming \"" + record2.getName() + "\"");
client.getNow("/api", response -> {
// release the service
reference.release();
});
}
}
});
discovery.close();
}
use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
HttpClient client = vertx.createHttpClient();
client.websocket(8080, "localhost", "/some-uri", websocket -> {
websocket.handler(data -> {
System.out.println("Received data " + data.toString("ISO-8859-1"));
client.close();
});
websocket.writeBinaryMessage(Buffer.buffer("Hello world"));
});
}
use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
// Note! in real-life you wouldn't often set trust all to true as it could leave you open to man in the middle attacks.
HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setTrustAll(true);
HttpClient client = vertx.createHttpClient(options);
HttpClientRequest request = client.get(8443, "localhost", "/", resp -> {
System.out.println("Got response " + resp.statusCode() + " with protocol " + resp.version());
resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1")));
});
// Set handler for server side push
request.pushHandler(pushedReq -> {
System.out.println("Receiving pushed content");
pushedReq.handler(pushedResp -> {
pushedResp.bodyHandler(body -> System.out.println("Got pushed data " + body.toString("ISO-8859-1")));
});
});
request.end();
}
Aggregations