Search in sources :

Example 6 with HttpClient

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);
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) Invocation(io.servicecomb.core.Invocation) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HttpClient(io.vertx.core.http.HttpClient) IpPort(io.servicecomb.foundation.common.net.IpPort) AsyncResponse(io.servicecomb.core.AsyncResponse) Test(org.junit.Test)

Example 7 with HttpClient

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;
}
Also used : RequestOptions(io.vertx.core.http.RequestOptions) JsonObject(io.vertx.core.json.JsonObject) CompletableFuture(java.util.concurrent.CompletableFuture) NetworkType(io.nem.sdk.model.blockchain.NetworkType) BlockInfo(io.nem.sdk.model.blockchain.BlockInfo) HttpClient(io.vertx.core.http.HttpClient) PublicAccount(io.nem.sdk.model.account.PublicAccount)

Example 8 with HttpClient

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();
}
Also used : HttpClient(io.vertx.core.http.HttpClient) ServiceDiscoveryOptions(io.vertx.servicediscovery.ServiceDiscoveryOptions) JsonObject(io.vertx.core.json.JsonObject) Record(io.vertx.servicediscovery.Record) ServiceDiscovery(io.vertx.servicediscovery.ServiceDiscovery) ServiceReference(io.vertx.servicediscovery.ServiceReference)

Example 9 with HttpClient

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"));
    });
}
Also used : HttpClient(io.vertx.core.http.HttpClient)

Example 10 with HttpClient

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();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Aggregations

HttpClient (io.vertx.core.http.HttpClient)77 Test (org.junit.Test)47 HttpClientRequest (io.vertx.core.http.HttpClientRequest)36 HttpClientOptions (io.vertx.core.http.HttpClientOptions)25 Vertx (io.vertx.core.Vertx)22 HttpMethod (io.vertx.core.http.HttpMethod)22 JsonObject (io.vertx.core.json.JsonObject)22 Handler (io.vertx.core.Handler)18 Buffer (io.vertx.core.buffer.Buffer)18 HttpClientResponse (io.vertx.core.http.HttpClientResponse)16 TimeUnit (java.util.concurrent.TimeUnit)16 HttpServer (io.vertx.core.http.HttpServer)15 Async (io.vertx.ext.unit.Async)15 Before (org.junit.Before)15 File (java.io.File)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 URL (java.net.URL)12 HttpServerOptions (io.vertx.core.http.HttpServerOptions)11 IOException (java.io.IOException)10 List (java.util.List)10