use of io.vertx.core.http.HttpClientResponse in project vert.x by eclipse.
the class HttpClientRequestImpl method handleNextRequest.
private void handleNextRequest(HttpClientResponse resp, HttpClientRequestImpl next, long timeoutMs) {
next.handler(respHandler);
next.exceptionHandler(exceptionHandler());
exceptionHandler(null);
next.endHandler(endHandler);
next.pushHandler = pushHandler;
next.followRedirects = followRedirects - 1;
next.written = written;
if (next.hostHeader == null) {
next.hostHeader = hostHeader;
}
if (headers != null && next.headers == null) {
next.headers().addAll(headers);
}
ByteBuf body;
switch(next.method) {
case GET:
body = null;
break;
case OTHER:
next.rawMethod = rawMethod;
body = null;
break;
default:
if (cachedChunks != null) {
body = cachedChunks;
} else {
body = null;
}
break;
}
cachedChunks = null;
Future<Void> fut = Future.future();
fut.setHandler(ar -> {
if (ar.succeeded()) {
if (timeoutMs > 0) {
next.setTimeout(timeoutMs);
}
next.write(body, true);
} else {
next.handleException(ar.cause());
}
});
if (exceptionOccurred != null) {
fut.fail(exceptionOccurred);
} else if (completed) {
fut.complete();
} else {
exceptionHandler(err -> {
if (!fut.isComplete()) {
fut.fail(err);
}
});
completionHandler = v -> {
if (!fut.isComplete()) {
fut.complete();
}
};
}
}
use of io.vertx.core.http.HttpClientResponse in project java-chassis by ServiceComb.
the class GrpcTransport method send.
@Override
public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
HttpClientWithContext httpClientWithContext = clientMgr.findThreadBindClientPool();
OperationMeta operationMeta = invocation.getOperationMeta();
OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
String cseContext = JsonUtils.writeValueAsString(invocation.getContext());
// 在verticle之外的线程调用
IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
Buffer requestBuf = GrpcCodec.encodeRequest(invocation, operationProtobuf);
String url = "/" + invocation.getSchemaId() + "/" + operationMeta.getOperationId();
Handler<HttpClientResponse> responseHandler = httpResponse -> {
httpResponse.bodyHandler(responseBuf -> {
invocation.getResponseExecutor().execute(() -> {
try {
Response response = GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, responseBuf);
ResponseMeta responseMeta = operationMeta.findResponseMeta(response.getStatusCode());
for (String headerName : responseMeta.getHeaders().keySet()) {
for (String value : httpResponse.headers().getAll(headerName)) {
response.getHeaders().addHeader(headerName, value);
}
}
asyncResp.complete(response);
} catch (Exception e) {
asyncResp.fail(invocation.getInvocationType(), e);
}
});
});
};
// 从业务线程转移到网络线程中去发送
httpClientWithContext.runOnContext(httpClient -> {
HttpClientRequest httpClientRequest = httpClient.post(ipPort.getPort(), ipPort.getHostOrIp(), url, responseHandler);
httpClientRequest.exceptionHandler(e -> {
asyncResp.fail(invocation.getInvocationType(), e);
});
httpClientRequest.setTimeout(AbstractTransport.getRequestTimeout());
httpClientRequest.putHeader(HEADER_CONTENT_TYPE, "application/grpc").putHeader(HEADER_TE, "trailers").putHeader(HEADER_USER_AGENT, "cse-client/1.0.0").putHeader(Const.CSE_CONTEXT, cseContext).putHeader(Const.DEST_MICROSERVICE, invocation.getMicroserviceName()).end(requestBuf);
});
}
use of io.vertx.core.http.HttpClientResponse in project java-chassis by ServiceComb.
the class TestGrpcCodec method testDecodeResponse.
@Test
public void testDecodeResponse() {
boolean status = false;
try {
RoutingContext routingContext = Mockito.mock(RoutingContext.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
WrapSchema schema = ProtobufSchemaUtils.getOrCreateSchema(int.class);
Mockito.when(operationProtobuf.getRequestSchema()).thenReturn(schema);
Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
Buffer bodyBuffer = Mockito.mock(Buffer.class);
Mockito.when(routingContext.getBody()).thenReturn(bodyBuffer);
HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
Mockito.when(routingContext.request()).thenReturn(request);
Mockito.when(request.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"name\":\"test\"}");
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(operationProtobuf.getResponseSchema()).thenReturn(Mockito.mock(WrapSchema.class));
HttpClientResponse httpResponse = Mockito.mock(HttpClientResponse.class);
Buffer buffer = Mockito.mock(Buffer.class);
GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, buffer);
} catch (Exception e) {
status = true;
}
Assert.assertFalse(status);
}
use of io.vertx.core.http.HttpClientResponse in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method unregisterMicroserviceInstance.
@Override
public boolean unregisterMicroserviceInstance(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);
CountDownLatch countDownLatch = new CountDownLatch(1);
RestUtils.delete(ipPort, url.toString(), new RequestParam(), syncHandler(countDownLatch, HttpClientResponse.class, holder));
try {
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("unregister microservice instance {}/{} failed", microserviceId, microserviceInstanceId, e);
}
return false;
}
use of io.vertx.core.http.HttpClientResponse in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method updateMicroserviceProperties.
@Override
public boolean updateMicroserviceProperties(String microserviceId, Map<String, String> serviceProperties) {
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.PROPERTIES_PATH);
try {
UpdatePropertiesRequest request = new UpdatePropertiesRequest();
request.setProperties(serviceProperties);
byte[] body = JsonUtils.writeValueAsBytes(request);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("update properties of microservice: {}", 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 {} failed", microserviceId, e);
}
return false;
}
Aggregations