Search in sources :

Example 16 with ByteArrayWrapperByteBuf

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

the class SofaRpcSerialization method deserializeContent.

@Override
public <Response extends ResponseCommand> boolean deserializeContent(Response response, InvokeContext invokeContext) throws DeserializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte serializer = response.getSerializer();
        byte[] content = responseCommand.getContent();
        if (content == null || content.length == 0) {
            return false;
        }
        long deserializeStartTime = System.nanoTime();
        try {
            Object sofaResponse = ClassUtils.forName(responseCommand.getResponseClass()).newInstance();
            Map<String, String> header = (Map<String, String>) responseCommand.getResponseHeader();
            if (header == null) {
                header = new HashMap<String, String>();
            }
            putKV(header, RemotingConstants.HEAD_TARGET_SERVICE, (String) invokeContext.get(RemotingConstants.HEAD_TARGET_SERVICE));
            putKV(header, RemotingConstants.HEAD_METHOD_NAME, (String) invokeContext.get(RemotingConstants.HEAD_METHOD_NAME));
            putKV(header, RemotingConstants.HEAD_GENERIC_TYPE, (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));
            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializer);
            rpcSerializer.decode(new ByteArrayWrapperByteBuf(responseCommand.getContent()), sofaResponse, header);
            if (sofaResponse instanceof SofaResponse) {
                parseResponseHeader(header, (SofaResponse) sofaResponse);
            }
            responseCommand.setResponseObject(sofaResponse);
            return true;
        } catch (Exception ex) {
            throw new DeserializationException(ex.getMessage(), ex);
        } finally {
            // R5:Record response deserialization time
            recordDeserializeResponse(responseCommand, invokeContext, deserializeStartTime);
        }
    }
    return false;
}
Also used : ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException) DeserializationException(com.alipay.remoting.exception.DeserializationException) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) HashMap(java.util.HashMap) Map(java.util.Map) 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