Search in sources :

Example 6 with ResponseRootSerializer

use of org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer in project java-chassis by ServiceComb.

the class HighwayServerInvoke method sendResponse.

private void sendResponse(Map<String, String> context, Response response) {
    invocation.getInvocationStageTrace().finishHandlersResponse();
    ResponseHeader header = new ResponseHeader();
    header.setStatusCode(response.getStatusCode());
    header.setReasonPhrase(response.getReasonPhrase());
    header.setContext(context);
    header.fromMultiMap(response.getHeaders());
    ResponseRootSerializer bodySchema = operationProtobuf.findResponseRootSerializer(response.getStatusCode());
    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }
    try {
        Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body);
        invocation.getInvocationStageTrace().finishServerFiltersResponse();
        connection.write(respBuffer.getByteBuf());
    } catch (Exception e) {
        // keep highway performance and simple, this encoding/decoding error not need handle by client
        String msg = String.format("encode response failed, %s, msgId=%d", invocation.getOperationMeta().getMicroserviceQualifiedName(), msgId);
        LOGGER.error(msg, e);
    } finally {
        if (invocation != null) {
            invocation.onFinish(response);
        }
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ResponseHeader(org.apache.servicecomb.transport.highway.message.ResponseHeader) ResponseRootSerializer(org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 7 with ResponseRootSerializer

use of org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer in project java-chassis by ServiceComb.

the class TestSchemaMetaCodec method testProtoSchemaOperationBaseImpl.

private void testProtoSchemaOperationBaseImpl(boolean isPojo) throws IOException {
    Invocation consumerInvocation = mockInvocation("base", InvocationType.CONSUMER);
    Invocation providerInvocation = mockInvocation("base", InvocationType.PRODUCER);
    OperationProtobuf providerOperationProtobuf = ProtobufManager.getOrCreateOperation(providerInvocation);
    OperationProtobuf consumerOperationProtobuf = ProtobufManager.getOrCreateOperation(consumerInvocation);
    byte[] values;
    // request message
    RequestRootSerializer requestSerializer = consumerOperationProtobuf.getRequestRootSerializer();
    boolean boolValue = true;
    int iValue = 20;
    long lValue = 30L;
    float fValue = 40f;
    double dValue = 50D;
    String sValue = "hello";
    int[] iArray = new int[] { 60, 70 };
    Color color = Color.BLUE;
    LocalDate localDate = LocalDate.of(2019, 10, 1);
    Date date = new Date();
    Empty empty = new Empty();
    Map<String, Object> args = new HashMap<>();
    args.put("boolValue", boolValue);
    args.put("iValue", iValue);
    args.put("lValue", lValue);
    args.put("fValue", fValue);
    args.put("dValue", dValue);
    args.put("sValue", sValue);
    args.put("iArray", iArray);
    args.put("color", color);
    args.put("localDate", localDate);
    args.put("date", date);
    args.put("empty", empty);
    if (isPojo) {
        Map<String, Object> swaggerArgs = new HashMap<>();
        swaggerArgs.put("baseBody", args);
        values = requestSerializer.serialize(swaggerArgs);
    } else {
        values = requestSerializer.serialize(args);
    }
    RequestRootDeserializer<Object> requestDeserializer = providerOperationProtobuf.getRequestRootDeserializer();
    Map<String, Object> decodedSwaggerArgs = requestDeserializer.deserialize(values);
    Map<String, Object> decodedArgs;
    if (isPojo) {
        Assert.assertEquals(1, decodedSwaggerArgs.size());
        decodedArgs = (Map<String, Object>) decodedSwaggerArgs.get("baseBody");
    } else {
        decodedArgs = decodedSwaggerArgs;
    }
    Assert.assertEquals(boolValue, decodedArgs.get("boolValue"));
    Assert.assertEquals(iValue, decodedArgs.get("iValue"));
    Assert.assertEquals(lValue, decodedArgs.get("lValue"));
    Assert.assertEquals(fValue, decodedArgs.get("fValue"));
    Assert.assertEquals(dValue, decodedArgs.get("dValue"));
    if (isPojo) {
        Assert.assertEquals(2, ((List<Integer>) decodedArgs.get("iArray")).size());
        Assert.assertEquals(60, (((List<Integer>) decodedArgs.get("iArray")).get(0).intValue()));
        Assert.assertEquals(70, (((List<Integer>) decodedArgs.get("iArray")).get(1).intValue()));
        Assert.assertEquals(color.ordinal(), decodedArgs.get("color"));
        Assert.assertEquals(date.getTime(), decodedArgs.get("date"));
        Assert.assertEquals(localDate.getLong(ChronoField.EPOCH_DAY), decodedArgs.get("localDate"));
        Assert.assertEquals(true, ((Map) decodedArgs.get("empty")).isEmpty());
    } else {
        Assert.assertArrayEquals(iArray, (int[]) decodedArgs.get("iArray"));
        Assert.assertEquals(color, decodedArgs.get("color"));
        Assert.assertEquals(date, decodedArgs.get("date"));
        Assert.assertTrue(decodedArgs.get("localDate") instanceof LocalDate);
        Assert.assertEquals(localDate, decodedArgs.get("localDate"));
        Assert.assertTrue(decodedArgs.get("empty") instanceof Empty);
    }
    // default value testing
    args.put("boolValue", false);
    args.put("iValue", 0);
    args.put("lValue", 0L);
    args.put("fValue", 0F);
    args.put("dValue", 0D);
    args.put("sValue", null);
    args.put("iArray", new int[0]);
    args.put("color", null);
    args.put("localDate", null);
    args.put("date", null);
    args.put("empty", null);
    values = requestSerializer.serialize(args);
    decodedArgs = requestDeserializer.deserialize(values);
    Assert.assertEquals(null, decodedArgs.get("boolValue"));
    Assert.assertEquals(null, decodedArgs.get("iValue"));
    Assert.assertEquals(null, decodedArgs.get("lValue"));
    Assert.assertEquals(null, decodedArgs.get("fValue"));
    Assert.assertEquals(null, decodedArgs.get("dValue"));
    Assert.assertEquals(null, decodedArgs.get("iArray"));
    Assert.assertEquals(null, decodedArgs.get("color"));
    Assert.assertEquals(null, decodedArgs.get("localDate"));
    Assert.assertEquals(null, decodedArgs.get("date"));
    Assert.assertEquals(null, decodedArgs.get("empty"));
    // response message
    ResponseRootSerializer responseSerializer = providerOperationProtobuf.findResponseRootSerializer(200);
    values = responseSerializer.serialize(30);
    ResponseRootDeserializer<Object> responseDeserializer = consumerOperationProtobuf.findResponseRootDeserializer(200);
    Object decodedValue = responseDeserializer.deserialize(values, TypeFactory.defaultInstance().constructType(int.class));
    Assert.assertEquals(30, (int) decodedValue);
}
Also used : RequestRootSerializer(org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) Color(org.apache.servicecomb.foundation.test.scaffolding.model.Color) ResponseRootSerializer(org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) Empty(org.apache.servicecomb.foundation.test.scaffolding.model.Empty) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)

Example 8 with ResponseRootSerializer

use of org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer in project java-chassis by ServiceComb.

the class TestSchemaMetaCodec method testProtoSchemaOperationmapUserImpl.

private void testProtoSchemaOperationmapUserImpl(boolean isPojo) throws IOException {
    Invocation consumerInvocation = mockInvocation("mapUser", InvocationType.CONSUMER);
    Invocation providerInvocation = mockInvocation("mapUser", InvocationType.PRODUCER);
    OperationProtobuf providerOperationProtobuf = ProtobufManager.getOrCreateOperation(providerInvocation);
    OperationProtobuf consumerOperationProtobuf = ProtobufManager.getOrCreateOperation(consumerInvocation);
    User user = new User();
    user.name = "user";
    User friend = new User();
    friend.name = "friend";
    List<User> friends = new ArrayList<>();
    friends.add(friend);
    user.friends = friends;
    byte[] values;
    Map<String, User> userMap = new HashMap<>();
    userMap.put("test", user);
    // request message
    Map<String, Object> args = new HashMap<>();
    RequestRootSerializer requestSerializer = consumerOperationProtobuf.getRequestRootSerializer();
    user.friends = friends;
    args.put("users", userMap);
    if (isPojo) {
        Map<String, Object> swaggerArgs = new HashMap<>(1);
        swaggerArgs.put("users", args);
        values = requestSerializer.serialize(swaggerArgs);
    } else {
        values = requestSerializer.serialize(args);
    }
    RequestRootDeserializer<Object> requestDeserializer = providerOperationProtobuf.getRequestRootDeserializer();
    Map<String, Object> decodedUserArgs = requestDeserializer.deserialize(values);
    if (isPojo) {
        decodedUserArgs = (Map<String, Object>) decodedUserArgs.get("users");
        Assert.assertEquals(user.name, ((Map<String, Map<String, Object>>) decodedUserArgs.get("users")).get("test").get("name"));
        Assert.assertEquals(user.friends.get(0).name, ((List<Map<String, Object>>) ((Map<String, Map<String, Object>>) decodedUserArgs.get("users")).get("test").get("friends")).get(0).get("name"));
    } else {
        Assert.assertEquals(user.name, ((Map<String, User>) decodedUserArgs.get("users")).get("test").name);
        Assert.assertEquals(user.friends.get(0).name, ((Map<String, User>) decodedUserArgs.get("users")).get("test").friends.get(0).name);
    }
    // response message
    ResponseRootSerializer responseSerializer = providerOperationProtobuf.findResponseRootSerializer(200);
    values = responseSerializer.serialize(userMap);
    ResponseRootDeserializer<Object> responseDeserializer = consumerOperationProtobuf.findResponseRootDeserializer(200);
    Map<String, User> decodedUser = (Map<String, User>) responseDeserializer.deserialize(values, ProtoConst.MAP_TYPE);
    Assert.assertEquals(user.name, decodedUser.get("test").name);
    Assert.assertEquals(user.friends.get(0).name, decodedUser.get("test").friends.get(0).name);
    user.friends = new ArrayList<>();
    values = responseSerializer.serialize(userMap);
    decodedUser = (Map<String, User>) responseDeserializer.deserialize(values, ProtoConst.MAP_TYPE);
    Assert.assertEquals(user.name, decodedUser.get("test").name);
    // proto buffer encode and decode empty list to be null
    Assert.assertEquals(null, decodedUser.get("test").friends);
}
Also used : User(org.apache.servicecomb.foundation.test.scaffolding.model.User) RequestRootSerializer(org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) ResponseRootSerializer(org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer) ArrayList(java.util.ArrayList) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ResponseRootSerializer (org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer)8 OperationProtobuf (org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)6 HashMap (java.util.HashMap)5 RequestRootSerializer (org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer)5 Invocation (org.apache.servicecomb.core.Invocation)5 ArrayList (java.util.ArrayList)3 User (org.apache.servicecomb.foundation.test.scaffolding.model.User)3 Test (org.junit.Test)3 Buffer (io.vertx.core.buffer.Buffer)2 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 Map (java.util.Map)2 Color (org.apache.servicecomb.foundation.test.scaffolding.model.Color)2 Empty (org.apache.servicecomb.foundation.test.scaffolding.model.Empty)2 ResponseHeader (org.apache.servicecomb.transport.highway.message.ResponseHeader)2 List (java.util.List)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)1