Java Code Examples for com.alipay.sofa.rpc.core.response.SofaResponse#setAppResponse()

The following examples show how to use com.alipay.sofa.rpc.core.response.SofaResponse#setAppResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JacksonSerializerTest.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Test
public void testListResponse() {
    // 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, "say3");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    SofaResponse response = new SofaResponse();
    final DemoResponse response1 = new DemoResponse();
    response1.setWord("result");

    List<DemoResponse> listResponse = new ArrayList<DemoResponse>();
    listResponse.add(response1);

    response.setAppResponse(listResponse);

    AbstractByteBuf 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", ((List<DemoResponse>) newResponse.getAppResponse()).get(0).getWord());

}
 
Example 2
Source File: ComplexDataGenerator.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
public SofaResponse generateSofaResponse() {
    SofaResponse response = new SofaResponse();

    response.setErrorMsg("just_error");
    response.setAppResponse(generatePerson_6());

    return response;
}
 
Example 3
Source File: TestChainFilter5.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 4
Source File: TestChainFilter1.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q1";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s1");
    }
    return response;
}
 
Example 5
Source File: TestChainFilter4.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q4";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s4");
    }
    return response;
}
 
Example 6
Source File: TestChainFilter3.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 7
Source File: TestChainFilter7.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q7";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s7");
    }
    return response;
}
 
Example 8
Source File: SofaResponseHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse decodeObject(AbstractByteBuf data, Map<String, String> context) throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        // 根据SerializeType信息决定序列化器
        Object object;
        boolean genericSerialize = context != null && isGenericResponse(
            context.get(RemotingConstants.HEAD_GENERIC_TYPE));
        if (genericSerialize) {
            input.setSerializerFactory(genericSerializerFactory);
            GenericObject genericObject = (GenericObject) input.readObject();
            SofaResponse sofaResponse = new SofaResponse();
            sofaResponse.setErrorMsg((String) genericObject.getField("errorMsg"));
            sofaResponse.setAppResponse(genericObject.getField("appResponse"));
            sofaResponse.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
            object = sofaResponse;
        } else {
            input.setSerializerFactory(serializerFactory);
            object = input.readObject();
        }
        input.close();
        return (SofaResponse) object;
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
 
Example 9
Source File: TestChainFilter6.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q6";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s6");
    }
    return response;
}
 
Example 10
Source File: MsgPackSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaResponse(AbstractByteBuf data, SofaResponse sofaResponse, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService == null) {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName == null) {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }

    boolean isError = false;
    if (StringUtils.TRUE.equals(head.remove(RemotingConstants.HEAD_RESPONSE_ERROR))) {
        isError = true;
    }
    if (!head.isEmpty()) {
        sofaResponse.setResponseProps(head);
    }
    if (isError) {
        String errorMessage = (String) decode(data, String.class, head);
        sofaResponse.setErrorMsg(errorMessage);
    } else {
        // according interface and method name to find paramter types
        Class responseClass = helper.getResClass(targetService, methodName);
        Object pbRes = decode(data, responseClass, head);
        sofaResponse.setAppResponse(pbRes);
    }
}
 
Example 11
Source File: TestChainFilter8.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q8";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s8");
    }
    return response;
}
 
Example 12
Source File: AbstractCluster.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private SofaResponse buildEmptyResponse(SofaRequest request) {
    SofaResponse response = new SofaResponse();
    Method method = request.getMethod();
    if (method != null) {
        response.setAppResponse(ClassUtils.getDefaultPrimitiveValue(method.getReturnType()));
    }
    return response;
}
 
Example 13
Source File: TestProviderFilterInvoker.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public SofaResponse invoke(SofaRequest sofaRequest) throws SofaRpcException {
    SofaResponse response = new SofaResponse();
    response.setAppResponse(sofaRequest.getMethodArgs()[0]);
    return response;
}
 
Example 14
Source File: TestChainFilter2.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onAsyncResponse(ConsumerConfig config, SofaRequest request, SofaResponse response, Throwable throwable)
    throws SofaRpcException {
    response.setAppResponse(response.getAppResponse() + "_a2");
}
 
Example 15
Source File: TestChainFilter7.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onAsyncResponse(ConsumerConfig config, SofaRequest request, SofaResponse response, Throwable throwable)
    throws SofaRpcException {
    response.setAppResponse(response.getAppResponse() + "_a7");
}
 
Example 16
Source File: TestChainFilter6.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onAsyncResponse(ConsumerConfig config, SofaRequest request, SofaResponse response, Throwable throwable)
    throws SofaRpcException {
    response.setAppResponse(response.getAppResponse() + "_a6");
}
 
Example 17
Source File: TestChainFilter0.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onAsyncResponse(ConsumerConfig config, SofaRequest request, SofaResponse response, Throwable throwable)
    throws SofaRpcException {
    response.setAppResponse(response.getAppResponse() + "_a0");
}
 
Example 18
Source File: TestChainFilter8.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onAsyncResponse(ConsumerConfig config, SofaRequest request, SofaResponse response, Throwable throwable)
    throws SofaRpcException {
    response.setAppResponse(response.getAppResponse() + "_a8");
}
 
Example 19
Source File: SofaRpcMetricsTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
private SofaResponse buildResponse() {
    SofaResponse response = new SofaResponse();
    response.setAppResponse("123");
    return response;
}
 
Example 20
Source File: RestTracerAdapter.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
/**
 * 适配服务端serverSend
 */
public static void serverSend(NettyHttpResponse response, Throwable throwable) {
    try {
        SofaRequest sofaRequest = new SofaRequest();
        SofaResponse sofaResponse = new SofaResponse();

        if (response == null) {
            sofaResponse.setErrorMsg("rest path ends with /favicon.ico");
        } else if (throwable != null) {
            if (response.getStatus() == 500) {
                sofaResponse.setAppResponse(throwable);
            } else {
                sofaResponse.setErrorMsg(throwable.getMessage());
            }

            Object method = RpcInternalContext.getContext().getAttachment(METHOD_TYPE_STRING);
            if (method != null) {
                Class[] parameterTypes = ((Method) method).getParameterTypes();
                String[] methodTypeString = new String[parameterTypes.length];
                for (int i = 0; i < methodTypeString.length; i++) {
                    methodTypeString[i] = (parameterTypes[i].getName());
                }
                sofaRequest.setMethodArgSigs(methodTypeString);
            }
        }

        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();

        RpcInternalContext context = RpcInternalContext.getContext();

        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME,
                (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }

        RestBaggageItemsHandler.encodeBaggageItemsToResponse(response, sofaResponse);
        Tracers.serverSend(sofaRequest, sofaResponse, null);
    } catch (Throwable t) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("the process of rest tracer server send occur error ", t);
        }
    }
}