Search in sources :

Example 11 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class ProtobufSerializerTest method testSofaResponse.

@Test
public void testSofaResponse() throws Exception {
    SofaResponse response = new SofaResponse();
    response.setAppResponse("1233");
    AbstractByteBuf data = serializer.encode(response, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaResponse.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, null, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, new SofaResponse(), null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // success response
    Map<String, String> head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtoService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    response = new SofaResponse();
    response.setAppResponse(EchoStrRes.newBuilder().setS("result").build());
    data = serializer.encode(response, null);
    SofaResponse newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertEquals(response.getAppResponse(), newResponse.getAppResponse());
    Assert.assertEquals("result", ((EchoStrRes) newResponse.getAppResponse()).getS());
    // null response
    head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtoService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    newResponse = new SofaResponse();
    serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertNotNull(newResponse.getAppResponse());
    Assert.assertEquals("", ((EchoStrRes) newResponse.getAppResponse()).getS());
    // error response
    head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtoService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    head.put(RemotingConstants.HEAD_RESPONSE_ERROR, "true");
    response = new SofaResponse();
    response.setErrorMsg("1233");
    data = serializer.encode(response, null);
    newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertTrue(newResponse.isError());
    Assert.assertEquals(response.getErrorMsg(), newResponse.getErrorMsg());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) HashMap(java.util.HashMap) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 12 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class ProtostuffSerializer method encode.

@Override
public AbstractByteBuf encode(Object object, Map<String, String> context) throws SofaRpcException {
    if (object == null) {
        throw buildSerializeError("Unsupported null message!");
    } else if (object instanceof SofaRequest) {
        return encodeSofaRequest((SofaRequest) object, context);
    } else if (object instanceof SofaResponse) {
        return encodeSofaResponse((SofaResponse) object, context);
    } else {
        Class clazz = object.getClass();
        Schema schema = RuntimeSchema.getSchema(clazz);
        // Re-use (manage) this buffer to avoid allocating on every serialization
        LinkedBuffer buffer = LinkedBuffer.allocate(512);
        // ser
        try {
            return new ByteArrayWrapperByteBuf(ProtostuffIOUtil.toByteArray(object, schema, buffer));
        } finally {
            buffer.clear();
        }
    }
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) Schema(io.protostuff.Schema) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)

Example 13 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class JacksonSerializerTest method testListParameter.

@Test
public void testListParameter() throws NoSuchMethodException {
    SofaRequest request = buildSay3Request();
    AbstractByteBuf data = serializer.encode(request, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaRequest.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, new SofaRequest(), null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    // parameters size error
    Map<String, String> errorHead1 = new HashMap<String, String>();
    errorHead1.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    errorHead1.put(RemotingConstants.HEAD_METHOD_NAME, "say3");
    try {
        ObjectMapper mapper = new ObjectMapper();
        DemoRequest req = new DemoRequest();
        req.setName("123");
        serializer.decode(new ByteArrayWrapperByteBuf(mapper.writeValueAsBytes(req)), new SofaRequest(), errorHead1);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // parameters size error
    Map<String, String> errorHead2 = new HashMap<String, String>();
    errorHead2.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    errorHead2.put(RemotingConstants.HEAD_METHOD_NAME, "say3");
    try {
        ObjectMapper mapper = new ObjectMapper();
        DemoRequest req = new DemoRequest();
        req.setName("123");
        serializer.decode(new ByteArrayWrapperByteBuf(mapper.writeValueAsBytes(new Object[] { req, "123" })), new SofaRequest(), errorHead2);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    Map<String, String> head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "say3");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    head.put("unkown", "yes");
    SofaRequest newRequest = new SofaRequest();
    serializer.decode(data, newRequest, head);
    Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName());
    Assert.assertEquals(newRequest.getMethodName(), request.getMethodName());
    Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
    Assert.assertEquals("name", ((List<DemoRequest>) newRequest.getMethodArgs()[0]).get(0).getName());
    Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName());
    Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName());
    Assert.assertEquals(newRequest.getRequestProp(RemotingConstants.RPC_TRACE_NAME), request.getRequestProp(RemotingConstants.RPC_TRACE_NAME));
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) DemoRequest(com.alipay.sofa.rpc.codec.jackson.model.DemoRequest) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 14 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class JacksonSerializerTest method testMoreParameters.

@Test
public void testMoreParameters() throws NoSuchMethodException {
    SofaRequest request = buildSay2Request();
    AbstractByteBuf data = serializer.encode(request, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaRequest.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, new SofaRequest(), null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    // parameters size error
    Map<String, String> errorHead1 = new HashMap<String, String>();
    errorHead1.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    errorHead1.put(RemotingConstants.HEAD_METHOD_NAME, "say2");
    try {
        ObjectMapper mapper = new ObjectMapper();
        DemoRequest req = new DemoRequest();
        req.setName("123");
        serializer.decode(new ByteArrayWrapperByteBuf(mapper.writeValueAsBytes(req)), new SofaRequest(), errorHead1);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // parameters size error
    Map<String, String> errorHead2 = new HashMap<String, String>();
    errorHead2.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    errorHead2.put(RemotingConstants.HEAD_METHOD_NAME, "say2");
    try {
        ObjectMapper mapper = new ObjectMapper();
        DemoRequest req = new DemoRequest();
        req.setName("123");
        serializer.decode(new ByteArrayWrapperByteBuf(mapper.writeValueAsBytes(new Object[] { req, "123" })), new SofaRequest(), errorHead2);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    Map<String, String> head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "say2");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    head.put("unkown", "yes");
    SofaRequest newRequest = new SofaRequest();
    serializer.decode(data, newRequest, head);
    Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName());
    Assert.assertEquals(newRequest.getMethodName(), request.getMethodName());
    Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
    Assert.assertEquals("name", ((DemoRequest) newRequest.getMethodArgs()[0]).getName());
    Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName());
    Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName());
    Assert.assertEquals(newRequest.getRequestProp(RemotingConstants.RPC_TRACE_NAME), request.getRequestProp(RemotingConstants.RPC_TRACE_NAME));
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) DemoRequest(com.alipay.sofa.rpc.codec.jackson.model.DemoRequest) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 15 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class SofaRpcSerialization method deserializeContent.

@Override
public <Request extends RequestCommand> boolean deserializeContent(Request request) throws DeserializationException {
    if (request instanceof RpcRequestCommand) {
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        Object header = requestCommand.getRequestHeader();
        if (!(header instanceof Map)) {
            throw new DeserializationException("Head of request is null or is not map");
        }
        Map<String, String> headerMap = (Map<String, String>) header;
        String traceId = headerMap.get("rpc_trace_context.sofaTraceId");
        String rpcId = headerMap.get("rpc_trace_context.sofaRpcId");
        long deserializeStartTime = System.nanoTime();
        try {
            byte[] content = requestCommand.getContent();
            if (content == null || content.length == 0) {
                throw new DeserializationException("Content of request is null");
            }
            String service = headerMap.get(RemotingConstants.HEAD_SERVICE);
            ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
            ClassLoader serviceClassLoader = ReflectCache.getServiceClassLoader(service);
            try {
                Thread.currentThread().setContextClassLoader(serviceClassLoader);
                Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(requestCommand.getSerializer());
                Object sofaRequest = ClassUtils.forName(requestCommand.getRequestClass()).newInstance();
                rpcSerializer.decode(new ByteArrayWrapperByteBuf(requestCommand.getContent()), sofaRequest, headerMap);
                // for service mesh or other scene, we need to add more info from header
                if (sofaRequest instanceof SofaRequest) {
                    setRequestPropertiesWithHeaderInfo(headerMap, (SofaRequest) sofaRequest);
                    parseRequestHeader(headerMap, (SofaRequest) sofaRequest);
                }
                requestCommand.setRequestObject(sofaRequest);
            } finally {
                Thread.currentThread().setContextClassLoader(oldClassLoader);
            }
            return true;
        } catch (Exception ex) {
            LOGGER.error("traceId={}, rpcId={}, Request deserializeContent exception, msg={}", traceId, rpcId, ex.getMessage(), ex);
            throw new DeserializationException(ex.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, ex);
        } finally {
            // R6:Record request deserialization time
            recordDeserializeRequest(requestCommand, deserializeStartTime);
        }
    }
    return false;
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) DeserializationException(com.alipay.remoting.exception.DeserializationException) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException) HashMap(java.util.HashMap) Map(java.util.Map) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) Serializer(com.alipay.sofa.rpc.codec.Serializer) DefaultCustomSerializer(com.alipay.remoting.DefaultCustomSerializer)

Aggregations

ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)16 HashMap (java.util.HashMap)11 Test (org.junit.Test)11 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)10 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)8 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)8 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)6 Serializer (com.alipay.sofa.rpc.codec.Serializer)4 DefaultCustomSerializer (com.alipay.remoting.DefaultCustomSerializer)2 DeserializationException (com.alipay.remoting.exception.DeserializationException)2 SerializationException (com.alipay.remoting.exception.SerializationException)2 DemoRequest (com.alipay.sofa.rpc.codec.jackson.model.DemoRequest)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Map (java.util.Map)2 Response (triple.Response)2 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)1 RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)1 DemoRequest2 (com.alipay.sofa.rpc.codec.jackson.model.DemoRequest2)1 DemoResponse (com.alipay.sofa.rpc.codec.msgpack.model.DemoResponse)1