Search in sources :

Example 1 with ResponseMeta

use of io.servicecomb.swagger.invocation.response.ResponseMeta 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);
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonUtils(io.servicecomb.foundation.common.utils.JsonUtils) OperationMeta(io.servicecomb.core.definition.OperationMeta) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) IpPort(io.servicecomb.foundation.common.net.IpPort) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) ProtobufManager(io.servicecomb.codec.protobuf.definition.ProtobufManager) JksOptions(io.vertx.core.net.JksOptions) HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) AsyncResponse(io.servicecomb.core.AsyncResponse) Const(io.servicecomb.core.Const) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Component(org.springframework.stereotype.Component) VertxUtils(io.servicecomb.foundation.vertx.VertxUtils) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpVersion(io.vertx.core.http.HttpVersion) AbstractTransport(io.servicecomb.core.transport.AbstractTransport) Response(io.servicecomb.core.Response) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Handler(io.vertx.core.Handler) ClientPoolManager(io.servicecomb.foundation.vertx.client.ClientPoolManager) Invocation(io.servicecomb.core.Invocation) IpPort(io.servicecomb.foundation.common.net.IpPort) AsyncResponse(io.servicecomb.core.AsyncResponse) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Response(io.servicecomb.core.Response) HttpClientRequest(io.vertx.core.http.HttpClientRequest) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) OperationMeta(io.servicecomb.core.definition.OperationMeta)

Example 2 with ResponseMeta

use of io.servicecomb.swagger.invocation.response.ResponseMeta in project java-chassis by ServiceComb.

the class VertxHttpMethod method handleResponse.

protected void handleResponse(Invocation invocation, HttpClientResponse httpResponse, RestOperationMeta restOperation, AsyncResponse asyncResp) {
    ProduceProcessor produceProcessor = null;
    String contentType = httpResponse.getHeader("Content-Type");
    if (contentType != null) {
        String contentTypeForFind = contentType;
        int idx = contentType.indexOf(';');
        if (idx != -1) {
            contentTypeForFind = contentType.substring(0, idx);
        }
        produceProcessor = restOperation.findProduceProcessor(contentTypeForFind);
    }
    if (produceProcessor == null) {
        String msg = String.format("path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", restOperation.getAbsolutePath(), httpResponse.statusCode(), httpResponse.statusMessage(), contentType);
        Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
        asyncResp.fail(invocation.getInvocationType(), exception);
        return;
    }
    ProduceProcessor finalProduceProcessor = produceProcessor;
    httpResponse.bodyHandler(responseBuf -> {
        invocation.getResponseExecutor().execute(() -> {
            try {
                ResponseMeta responseMeta = restOperation.getOperationMeta().findResponseMeta(httpResponse.statusCode());
                Object result = finalProduceProcessor.decodeResponse(responseBuf, responseMeta.getJavaType());
                Response response = Response.create(httpResponse.statusCode(), httpResponse.statusMessage(), result);
                for (String headerName : responseMeta.getHeaders().keySet()) {
                    List<String> headerValues = httpResponse.headers().getAll(headerName);
                    for (String headerValue : headerValues) {
                        response.getHeaders().addHeader(headerName, headerValue);
                    }
                }
                asyncResp.complete(response);
            } catch (Throwable e) {
                asyncResp.fail(invocation.getInvocationType(), e);
            }
        });
    });
}
Also used : AsyncResponse(io.servicecomb.core.AsyncResponse) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Response(io.servicecomb.core.Response) ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData)

Example 3 with ResponseMeta

use of io.servicecomb.swagger.invocation.response.ResponseMeta in project java-chassis by ServiceComb.

the class TestOperationMeta method testOperationMeta.

@Test
public void testOperationMeta() {
    UnitTestMeta meta = new UnitTestMeta();
    SchemaMeta schemaMeta = meta.getOrCreateSchemaMeta(Impl.class);
    OperationMeta operationMeta = schemaMeta.findOperation("test");
    Assert.assertEquals("POST", operationMeta.getHttpMethod());
    Assert.assertEquals("/test", operationMeta.getOperationPath());
    Assert.assertEquals(schemaMeta, operationMeta.getSchemaMeta());
    Assert.assertEquals("io.servicecomb.core.definition.TestOperationMeta$Impl.test", operationMeta.getSchemaQualifiedName());
    Assert.assertEquals("app:test.io.servicecomb.core.definition.TestOperationMeta$Impl.test", operationMeta.getMicroserviceQualifiedName());
    Assert.assertEquals("app:test", operationMeta.getMicroserviceName());
    Assert.assertEquals("test", operationMeta.getOperationId());
    Assert.assertEquals("x", operationMeta.getParamName(0));
    Assert.assertEquals(true, operationMeta.isSync());
    operationMeta.putExtData("ext", 1);
    Assert.assertEquals(1, (int) operationMeta.getExtData("ext"));
    ResponseMeta responseMeta = operationMeta.findResponseMeta(200);
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getJavaType().getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h1").getGenericSignature());
    Assert.assertEquals("Ljava/util/List<Ljava/lang/String;>;", responseMeta.getHeaders().get("h2").getGenericSignature());
    Assert.assertEquals(null, responseMeta.getHeaders().get("h3"));
    responseMeta = operationMeta.findResponseMeta(300);
    Assert.assertEquals("Ljava/lang/String;", responseMeta.getJavaType().getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h1").getGenericSignature());
    Assert.assertEquals("Ljava/util/List<Ljava/lang/String;>;", responseMeta.getHeaders().get("h2").getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h3").getGenericSignature());
}
Also used : UnitTestMeta(io.servicecomb.core.unittest.UnitTestMeta) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) Test(org.junit.Test)

Aggregations

ResponseMeta (io.servicecomb.swagger.invocation.response.ResponseMeta)3 AsyncResponse (io.servicecomb.core.AsyncResponse)2 Response (io.servicecomb.core.Response)2 HttpClientResponse (io.vertx.core.http.HttpClientResponse)2 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)1 ProtobufManager (io.servicecomb.codec.protobuf.definition.ProtobufManager)1 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)1 Const (io.servicecomb.core.Const)1 Invocation (io.servicecomb.core.Invocation)1 OperationMeta (io.servicecomb.core.definition.OperationMeta)1 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)1 AbstractTransport (io.servicecomb.core.transport.AbstractTransport)1 UnitTestMeta (io.servicecomb.core.unittest.UnitTestMeta)1 IpPort (io.servicecomb.foundation.common.net.IpPort)1 JsonUtils (io.servicecomb.foundation.common.utils.JsonUtils)1 VertxUtils (io.servicecomb.foundation.vertx.VertxUtils)1 ClientPoolManager (io.servicecomb.foundation.vertx.client.ClientPoolManager)1 HttpClientWithContext (io.servicecomb.foundation.vertx.client.http.HttpClientWithContext)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 Handler (io.vertx.core.Handler)1