use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class SofaRpcSerialization method serializeContent.
@Override
public <Request extends RequestCommand> boolean serializeContent(Request request, InvokeContext invokeContext) throws SerializationException {
if (request instanceof RpcRequestCommand) {
RpcRequestCommand requestCommand = (RpcRequestCommand) request;
Object requestObject = requestCommand.getRequestObject();
byte serializerCode = requestCommand.getSerializer();
long serializeStartTime = System.nanoTime();
try {
Map<String, String> header = (Map<String, String>) requestCommand.getRequestHeader();
if (header == null) {
header = new HashMap<String, String>();
}
putKV(header, RemotingConstants.HEAD_GENERIC_TYPE, (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));
Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
AbstractByteBuf byteBuf = rpcSerializer.encode(requestObject, header);
request.setContent(byteBuf.array());
return true;
} catch (Exception ex) {
throw new SerializationException(ex.getMessage(), ex);
} finally {
// R5:record request serialization time
recordSerializeRequest(requestCommand, invokeContext, serializeStartTime);
}
}
return false;
}
use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class SofaRpcSerialization method serializeContent.
@Override
public <Response extends ResponseCommand> boolean serializeContent(Response response) throws SerializationException {
if (response instanceof RpcResponseCommand) {
RpcResponseCommand responseCommand = (RpcResponseCommand) response;
byte serializerCode = response.getSerializer();
long serializeStartTime = System.nanoTime();
try {
Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
AbstractByteBuf byteBuf = rpcSerializer.encode(responseCommand.getResponseObject(), null);
responseCommand.setContent(byteBuf.array());
return true;
} catch (Exception ex) {
String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
LOGGER.error("traceId={}, rpcId={}, Response serializeContent exception, msg = {}", traceId, rpcId, ex.getMessage(), ex);
throw new SerializationException(ex.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, ex);
} finally {
// R6:Record response serialization time
recordSerializeResponse(responseCommand, serializeStartTime);
}
}
return false;
}
use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class AbstractHttpClientHandler method decode.
protected void decode(SofaResponse response) {
AbstractByteBuf byteBuffer = response.getData();
if (byteBuffer != null) {
try {
Map<String, String> context = new HashMap<String, String>(4);
if (response.isError()) {
context.put(RemotingConstants.HEAD_RESPONSE_ERROR, response.isError() + "");
String errorMsg = StringSerializer.decode(byteBuffer.array());
response.setAppResponse(new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, errorMsg));
} else {
context.put(RemotingConstants.HEAD_TARGET_SERVICE, request.getTargetServiceUniqueName());
context.put(RemotingConstants.HEAD_METHOD_NAME, request.getMethodName());
Serializer serializer = SerializerFactory.getSerializer(response.getSerializeType());
serializer.decode(byteBuffer, response, context);
}
} finally {
byteBuffer.release();
response.setData(null);
}
}
}
use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class ProtostuffSerializerTest 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, ProtostuffService.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();
final ExampleObj exampleObj = new ExampleObj();
exampleObj.setName("result");
response.setAppResponse(exampleObj);
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", ((ExampleObj) newResponse.getAppResponse()).getName());
// null response
head = new HashMap<String, String>();
head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.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.assertNull(((ExampleObj) newResponse.getAppResponse()).getName());
// error response
head = new HashMap<String, String>();
head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.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());
}
use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class ProtostuffSerializerTest method testSofaRequest.
@Test
public void testSofaRequest() throws Exception {
SofaRequest request = buildRequest();
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);
Map<String, String> head = new HashMap<String, String>();
head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.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");
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.assertArrayEquals(newRequest.getMethodArgSigs(), request.getMethodArgSigs());
Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
Assert.assertEquals("xxxx", ((ExampleObj) 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));
// null request
head = new HashMap<String, String>();
head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.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");
newRequest = new SofaRequest();
serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newRequest, head);
Assert.assertNull(((ExampleObj) newRequest.getMethodArgs()[0]).getName());
}
Aggregations