Search in sources :

Example 31 with SofaResponse

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));
        }
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) StatusException(io.grpc.StatusException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 32 with SofaResponse

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());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) HashMap(java.util.HashMap) DemoResponse(com.alipay.sofa.rpc.codec.msgpack.model.DemoResponse) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 33 with SofaResponse

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());
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 34 with SofaResponse

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;
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 35 with SofaResponse

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;
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Aggregations

SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)85 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)25 Test (org.junit.Test)21 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)19 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)12 Method (java.lang.reflect.Method)10 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)9 HashMap (java.util.HashMap)9 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)6 ClientEndInvokeEvent (com.alipay.sofa.rpc.event.ClientEndInvokeEvent)6 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 ClientAsyncReceiveEvent (com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent)5 ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)5 Serializer (com.alipay.sofa.rpc.codec.Serializer)4 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 FilterChain (com.alipay.sofa.rpc.filter.FilterChain)4 Hessian2Input (com.caucho.hessian.io.Hessian2Input)4 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)3 DeserializationException (com.alipay.remoting.exception.DeserializationException)3