Search in sources :

Example 6 with HttpClientResponse

use of io.vertx.core.http.HttpClientResponse in project java-chassis by ServiceComb.

the class ServiceRegistryClientImpl method registerSchema.

@Override
public boolean registerSchema(String microserviceId, String schemaId, String schemaContent) {
    Holder<HttpClientResponse> holder = new Holder<>();
    IpPort ipPort = IpPortManager.INSTANCE.get();
    try {
        CreateSchemaRequest request = new CreateSchemaRequest();
        request.setSchema(schemaContent);
        byte[] body = JsonUtils.writeValueAsBytes(request);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        RestUtils.put(ipPort, Const.MS_API_PATH + Const.MICROSERVICE_PATH + "/" + microserviceId + Const.SCHEMA_PATH + "/" + schemaId, new RequestParam().setBody(body), syncHandler(countDownLatch, HttpClientResponse.class, holder));
        countDownLatch.await();
        boolean result = false;
        if (holder.value != null) {
            result = holder.value.statusCode() == Status.OK.getStatusCode();
        }
        LOGGER.info("register schema {}/{}, result {}", microserviceId, schemaId, result);
        return result;
    } catch (Exception e) {
        LOGGER.error("query schema exist {}/{} fail", microserviceId, schemaId, e);
    }
    return false;
}
Also used : CreateSchemaRequest(io.servicecomb.serviceregistry.api.request.CreateSchemaRequest) Holder(javax.xml.ws.Holder) HttpClientResponse(io.vertx.core.http.HttpClientResponse) IpPort(io.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 7 with HttpClientResponse

use of io.vertx.core.http.HttpClientResponse in project java-chassis by ServiceComb.

the class ServiceRegistryClientImpl method updateInstanceProperties.

@Override
public boolean updateInstanceProperties(String microserviceId, String microserviceInstanceId, Map<String, String> instanceProperties) {
    Holder<HttpClientResponse> holder = new Holder<>();
    IpPort ipPort = IpPortManager.INSTANCE.get();
    StringBuilder url = new StringBuilder(Const.MS_API_PATH);
    url.append(Const.MICROSERVICE_PATH).append("/").append(microserviceId).append(Const.INSTANCES_PATH).append("/").append(microserviceInstanceId).append(Const.PROPERTIES_PATH);
    try {
        UpdatePropertiesRequest request = new UpdatePropertiesRequest();
        request.setProperties(instanceProperties);
        byte[] body = JsonUtils.writeValueAsBytes(request);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("update properties of microservice instance: {}", new String(body, Charset.defaultCharset()));
        }
        CountDownLatch countDownLatch = new CountDownLatch(1);
        RestUtils.put(ipPort, url.toString(), new RequestParam().setBody(body), syncHandler(countDownLatch, HttpClientResponse.class, holder));
        countDownLatch.await();
        if (holder.value != null) {
            if (holder.value.statusCode() == Status.OK.getStatusCode()) {
                return true;
            }
            LOGGER.warn(holder.value.statusMessage());
        }
    } catch (Exception e) {
        LOGGER.error("update properties of microservice instance {}/{} failed", microserviceId, microserviceInstanceId, e);
    }
    return false;
}
Also used : Holder(javax.xml.ws.Holder) HttpClientResponse(io.vertx.core.http.HttpClientResponse) IpPort(io.servicecomb.foundation.common.net.IpPort) UpdatePropertiesRequest(io.servicecomb.serviceregistry.api.request.UpdatePropertiesRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 8 with HttpClientResponse

use of io.vertx.core.http.HttpClientResponse in project vert.x by eclipse.

the class ProxyErrorTest method proxyTest.

private void proxyTest(int error, String username, String url, Handler<HttpClientResponse> assertResponse, boolean completeOnException) throws Exception {
    startProxy(error, username);
    final HttpClientOptions options = new HttpClientOptions().setSsl(url.startsWith("https")).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()));
    HttpClient client = vertx.createHttpClient(options);
    client.getAbs(url, assertResponse).exceptionHandler(e -> {
        if (completeOnException) {
            testComplete();
        } else {
            fail(e);
        }
    }).end();
    await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientResponse(io.vertx.core.http.HttpClientResponse) ProxyOptions(io.vertx.core.net.ProxyOptions) ProxyType(io.vertx.core.net.ProxyType) HttpProxy(io.vertx.test.core.HttpProxy) Test(org.junit.Test) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) VertxTestBase(io.vertx.test.core.VertxTestBase) LoggerFactory(io.vertx.core.logging.LoggerFactory) HttpClient(io.vertx.core.http.HttpClient) ProxyOptions(io.vertx.core.net.ProxyOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 9 with HttpClientResponse

use of io.vertx.core.http.HttpClientResponse in project vert.x by eclipse.

the class HttpTest method testFoo.

private void testFoo(String location, String expected) throws Exception {
    int status = 301;
    Map<String, String> headers = Collections.singletonMap("Location", location);
    HttpMethod method = HttpMethod.GET;
    String baseURI = "https://localhost:8080";
    class MockReq implements HttpClientRequest {

        public HttpClientRequest exceptionHandler(Handler<Throwable> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest write(Buffer data) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setWriteQueueMaxSize(int maxSize) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest drainHandler(Handler<Void> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest handler(Handler<HttpClientResponse> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest pause() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest resume() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest endHandler(Handler<Void> endHandler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setFollowRedirects(boolean followRedirects) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setChunked(boolean chunked) {
            throw new UnsupportedOperationException();
        }

        public boolean isChunked() {
            return false;
        }

        public HttpMethod method() {
            return method;
        }

        public String getRawMethod() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setRawMethod(String method) {
            throw new UnsupportedOperationException();
        }

        public String absoluteURI() {
            return baseURI;
        }

        public String uri() {
            throw new UnsupportedOperationException();
        }

        public String path() {
            throw new UnsupportedOperationException();
        }

        public String query() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setHost(String host) {
            throw new UnsupportedOperationException();
        }

        public String getHost() {
            throw new UnsupportedOperationException();
        }

        public MultiMap headers() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest putHeader(String name, String value) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest putHeader(CharSequence name, CharSequence value) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest putHeader(String name, Iterable<String> values) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest putHeader(CharSequence name, Iterable<CharSequence> values) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest write(String chunk) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest write(String chunk, String enc) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest continueHandler(@Nullable Handler<Void> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest sendHead() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest sendHead(Handler<HttpVersion> completionHandler) {
            throw new UnsupportedOperationException();
        }

        public void end(String chunk) {
            throw new UnsupportedOperationException();
        }

        public void end(String chunk, String enc) {
            throw new UnsupportedOperationException();
        }

        public void end(Buffer chunk) {
            throw new UnsupportedOperationException();
        }

        public void end() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setTimeout(long timeoutMs) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest pushHandler(Handler<HttpClientRequest> handler) {
            throw new UnsupportedOperationException();
        }

        public boolean reset(long code) {
            return false;
        }

        public HttpConnection connection() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest connectionHandler(@Nullable Handler<HttpConnection> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest writeCustomFrame(int type, int flags, Buffer payload) {
            throw new UnsupportedOperationException();
        }

        public boolean writeQueueFull() {
            throw new UnsupportedOperationException();
        }
    }
    HttpClientRequest req = new MockReq();
    class MockResp implements HttpClientResponse {

        public HttpClientResponse resume() {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse exceptionHandler(Handler<Throwable> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse handler(Handler<Buffer> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse pause() {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse endHandler(Handler<Void> endHandler) {
            throw new UnsupportedOperationException();
        }

        public HttpVersion version() {
            throw new UnsupportedOperationException();
        }

        public int statusCode() {
            return status;
        }

        public String statusMessage() {
            throw new UnsupportedOperationException();
        }

        public MultiMap headers() {
            throw new UnsupportedOperationException();
        }

        public String getHeader(String headerName) {
            return headers.get(headerName);
        }

        public String getHeader(CharSequence headerName) {
            return getHeader(headerName.toString());
        }

        public String getTrailer(String trailerName) {
            throw new UnsupportedOperationException();
        }

        public MultiMap trailers() {
            throw new UnsupportedOperationException();
        }

        public List<String> cookies() {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse bodyHandler(Handler<Buffer> bodyHandler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse customFrameHandler(Handler<HttpFrame> handler) {
            throw new UnsupportedOperationException();
        }

        public NetSocket netSocket() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest request() {
            return req;
        }
    }
    MockResp resp = new MockResp();
    Function<HttpClientResponse, Future<HttpClientRequest>> handler = client.redirectHandler();
    Future<HttpClientRequest> redirection = handler.apply(resp);
    if (expected != null) {
        assertEquals(location, redirection.result().absoluteURI());
    } else {
        assertTrue(redirection == null || redirection.failed());
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Handler(io.vertx.core.Handler) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Future(io.vertx.core.Future) CompletableFuture(java.util.concurrent.CompletableFuture) HttpMethod(io.vertx.core.http.HttpMethod) Nullable(io.vertx.codegen.annotations.Nullable)

Example 10 with HttpClientResponse

use of io.vertx.core.http.HttpClientResponse in project java-chassis by ServiceComb.

the class ServiceRegistryClientImpl method heartbeat.

@Override
public HeartbeatResponse heartbeat(String microserviceId, String microserviceInstanceId) {
    Holder<HttpClientResponse> holder = new Holder<>();
    IpPort ipPort = IpPortManager.INSTANCE.get();
    StringBuilder url = new StringBuilder(Const.MS_API_PATH);
    url.append(Const.MICROSERVICE_PATH).append("/").append(microserviceId).append(Const.INSTANCES_PATH).append("/").append(microserviceInstanceId).append(Const.HEARTBEAT_PATH);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.put(ipPort, url.toString(), new RequestParam(), syncHandler(countDownLatch, HttpClientResponse.class, holder));
    try {
        countDownLatch.await();
        if (holder.value != null) {
            HeartbeatResponse response = new HeartbeatResponse();
            response.setMessage(holder.value.statusMessage());
            if (holder.value.statusCode() == Status.OK.getStatusCode()) {
                response.setOk(true);
                return response;
            }
            LOGGER.warn(holder.value.statusMessage());
            return response;
        }
    } catch (Exception e) {
        LOGGER.error("update microservice instance {}/{} heartbeat failed", microserviceId, microserviceInstanceId, e);
    }
    return null;
}
Also used : HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) Holder(javax.xml.ws.Holder) HttpClientResponse(io.vertx.core.http.HttpClientResponse) IpPort(io.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Aggregations

HttpClientResponse (io.vertx.core.http.HttpClientResponse)14 IpPort (io.servicecomb.foundation.common.net.IpPort)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Invocation (io.servicecomb.core.Invocation)5 OperationMeta (io.servicecomb.core.definition.OperationMeta)5 ClientException (io.servicecomb.serviceregistry.client.ClientException)5 Buffer (io.vertx.core.buffer.Buffer)5 Holder (javax.xml.ws.Holder)5 Handler (io.vertx.core.Handler)4 HttpClientRequest (io.vertx.core.http.HttpClientRequest)4 Test (org.junit.Test)4 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)3 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)3 AsyncResponse (io.servicecomb.core.AsyncResponse)3 Response (io.servicecomb.core.Response)3 WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)2 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)2 URLPathBuilder (io.servicecomb.common.rest.definition.path.URLPathBuilder)2 Endpoint (io.servicecomb.core.Endpoint)2 UpdatePropertiesRequest (io.servicecomb.serviceregistry.api.request.UpdatePropertiesRequest)2