use of com.alipay.remoting.rpc.protocol.RpcRequestCommand 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;
}
Aggregations