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());
}
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();
}
}
}
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));
}
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));
}
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;
}
Aggregations