use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class TripleClientTransport method syncSend.
@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
SofaResponse sofaResponse = null;
SofaRpcException throwable = null;
try {
RpcInternalContext context = RpcInternalContext.getContext();
beforeSend(context, request);
RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
invokeContext.put(TripleContants.SOFA_REQUEST_KEY, request);
invokeContext.put(TripleContants.SOFA_CONSUMER_CONFIG_KEY, transportConfig.getConsumerConfig());
sofaResponse = tripleClientInvoker.invoke(request, timeout);
return sofaResponse;
} catch (Exception e) {
throwable = convertToRpcException(e);
throw throwable;
} finally {
if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, sofaResponse, throwable));
}
}
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class MsgPackSerializerTest 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, DemoService.class.getCanonicalName() + ":1.0");
head.put(RemotingConstants.HEAD_METHOD_NAME, "say");
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 DemoResponse response1 = new DemoResponse();
response1.setWord("result");
response.setAppResponse(response1);
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", ((DemoResponse) newResponse.getAppResponse()).getWord());
// null response
head = new HashMap<String, String>();
head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
head.put(RemotingConstants.HEAD_METHOD_NAME, "say");
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(null, ((DemoResponse) newResponse.getAppResponse()).getWord());
// error response
head = new HashMap<String, String>();
head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
head.put(RemotingConstants.HEAD_METHOD_NAME, "say");
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.core.response.SofaResponse in project sofa-rpc by sofastack.
the class MessageBuilderTest method buildSofaErrorResponse.
@Test
public void buildSofaErrorResponse() throws Exception {
SofaResponse response = MessageBuilder.buildSofaErrorResponse("xxx");
Assert.assertTrue(response.isError());
Assert.assertEquals("xxx", response.getErrorMsg());
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class TestChainFilter3 method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q3";
SofaResponse response = invoker.invoke(request);
if (!request.isAsync()) {
response.setAppResponse(response.getAppResponse() + "_s3");
}
return response;
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class TestChainFilter5 method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q5";
SofaResponse response = invoker.invoke(request);
if (!request.isAsync()) {
response.setAppResponse(response.getAppResponse() + "_s5");
}
return response;
}
Aggregations