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

The following examples show how to use com.alipay.sofa.rpc.core.response.SofaResponse#getAppResponse() . 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: CustomEchoFilter2.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    LOGGER.info("echo2 request : {}, {}", request.getInterfaceName() + "." + request.getMethodName(),
        request.getMethodArgs());

    SofaResponse response = invoker.invoke(request);

    if (response == null) {
        return response;
    } else if (response.isError()) {
        LOGGER.info("server rpc error: {}", response.getErrorMsg());
    } else {
        Object ret = response.getAppResponse();
        if (ret instanceof Throwable) {
            LOGGER.error("server biz error: {}", (Throwable) ret);
        } else {
            LOGGER.info("echo2 response : {}", response.getAppResponse());
        }
    }

    return response;
}
 
Example 2
Source File: CustomEchoFilter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    LOGGER.info("echo request : {}, {}", request.getInterfaceName() + "." + request.getMethodName(),
        request.getMethodArgs());

    SofaResponse response = invoker.invoke(request);

    if (response == null) {
        return response;
    } else if (response.isError()) {
        LOGGER.info("server rpc error: {}", response.getErrorMsg());
    } else {
        Object ret = response.getAppResponse();
        if (ret instanceof Throwable) {
            LOGGER.error("server biz error: {}", (Throwable) ret);
        } else {
            LOGGER.info("echo response : {}", response.getAppResponse());
        }
    }

    return response;
}
 
Example 3
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public <Response extends ResponseCommand> boolean serializeHeader(Response response)
    throws SerializationException {
    if (response instanceof RpcResponseCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();

        Object responseObject = ((RpcResponseCommand) response).getResponseObject();
        if (responseObject instanceof SofaResponse) {
            SofaResponse sofaResponse = (SofaResponse) responseObject;
            if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) {
                sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE);
            }
            response.setHeader(mapSerializer.encode(sofaResponse.getResponseProps()));
        }
        return true;
    }
    return false;
}
 
Example 4
Source File: ProtobufSerializer.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context)
    throws SofaRpcException {
    AbstractByteBuf byteBuf;
    if (sofaResponse.isError()) {
        // 框架异常:错误则body序列化的是错误字符串
        byteBuf = encode(sofaResponse.getErrorMsg(), context);
    } else {
        // 正确返回则解析序列化的protobuf返回对象
        Object appResponse = sofaResponse.getAppResponse();
        if (appResponse instanceof Throwable) {
            // 业务异常序列化的是错误字符串
            byteBuf = encode(((Throwable) appResponse).getMessage(), context);
        } else {
            byteBuf = encode(appResponse, context);
        }
    }
    return byteBuf;
}
 
Example 5
Source File: JacksonSerializer.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context)
    throws SofaRpcException {
    AbstractByteBuf byteBuf;
    if (sofaResponse.isError()) {
        // rpc exception:error when body is illegal string
        byteBuf = encode(sofaResponse.getErrorMsg(), context);
    } else {
        //ok: when json can be deserialize correctly.
        Object appResponse = sofaResponse.getAppResponse();
        if (appResponse instanceof Throwable) {
            // biz exception:error when body is illegal string
            byteBuf = encode(((Throwable) appResponse).getMessage(), context);
        } else {
            byteBuf = encode(appResponse, context);
        }
    }
    return byteBuf;
}
 
Example 6
Source File: MsgPackSerializer.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context)
        throws SofaRpcException {
    AbstractByteBuf byteBuf;
    if (sofaResponse.isError()) {
        // 框架异常:错误则body序列化的是错误字符串
        byteBuf = encode(sofaResponse.getErrorMsg(), context);
    } else {
        // 正确返回则解析序列化的Msgpack返回对象
        Object appResponse = sofaResponse.getAppResponse();
        if (appResponse instanceof Throwable) {
            // 业务异常序列化的是错误字符串
            byteBuf = encode(((Throwable) appResponse).getMessage(), context);
        } else {
            byteBuf = encode(appResponse, context);
        }
    }
    return byteBuf;
}
 
Example 7
Source File: ProtostuffSerializer.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context)
    throws SofaRpcException {
    AbstractByteBuf byteBuf;
    if (sofaResponse.isError()) {
        // 框架异常:错误则body序列化的是错误字符串
        byteBuf = encode(sofaResponse.getErrorMsg(), context);
    } else {
        // 正确返回则解析序列化的protobuf返回对象
        Object appResponse = sofaResponse.getAppResponse();
        if (appResponse instanceof Throwable) {
            // 业务异常序列化的是错误字符串
            byteBuf = encode(((Throwable) appResponse).getMessage(), context);
        } else {
            byteBuf = encode(appResponse, context);
        }
    }
    return byteBuf;
}
 
Example 8
Source File: BytebuddyInvocationHandler.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@RuntimeType
public Object byteBuddyInvoke(@This Object proxy, @Origin Method method, @AllArguments @RuntimeType Object[] args)
    throws Throwable {
    String name = method.getName();
    if ("equals".equals(name)) {
        Object another = args[0];
        return proxy == another ||
            (proxy.getClass().isInstance(another) && proxyInvoker.equals(BytebuddyProxy.parseInvoker(another)));
    } else if ("hashCode".equals(name)) {
        return proxyInvoker.hashCode();
    } else if ("toString".equals(name)) {
        return proxyInvoker.toString();
    }

    SofaRequest request = MessageBuilder.buildSofaRequest(method.getDeclaringClass(), method,
        method.getParameterTypes(), args);
    SofaResponse response = proxyInvoker.invoke(request);

    return response.getAppResponse();
}
 
Example 9
Source File: AbstractSofaRpcFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
protected void traceResponseException(SofaResponse response, Entry interfaceEntry, Entry methodEntry) {
    if (response.isError()) {
        SofaRpcException rpcException = new SofaRpcException(RpcErrorType.SERVER_FILTER, response.getErrorMsg());
        Tracer.traceEntry(rpcException, interfaceEntry);
        Tracer.traceEntry(rpcException, methodEntry);
    } else {
        Object appResponse = response.getAppResponse();
        if (appResponse instanceof Throwable) {
            Tracer.traceEntry((Throwable) appResponse, interfaceEntry);
            Tracer.traceEntry((Throwable) appResponse, methodEntry);
        }
    }
}
 
Example 10
Source File: LookoutSubscriber.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * create RpcClientLookoutModel
 * @param request
 * @param response
 * @return
 */
private RpcClientLookoutModel createClientMetricsModel(SofaRequest request, SofaResponse response) {

    RpcClientLookoutModel clientMetricsModel = new RpcClientLookoutModel();

    RpcInternalContext context = RpcInternalContext.getContext();

    String app = getStringAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_APP_NAME));
    String service = request.getTargetServiceUniqueName();
    String method = request.getMethodName();
    String protocol = getStringAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_PROTOCOL_NAME));
    String invokeType = request.getInvokeType();
    String targetApp = request.getTargetAppName();
    Long requestSize = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE));
    Long responseSize = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE));
    Long elapsedTime = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE));
    Boolean success = response != null && !response.isError() && response.getErrorMsg() == null &&
        (!(response.getAppResponse() instanceof Throwable));

    clientMetricsModel.setApp(app);
    clientMetricsModel.setService(service);
    clientMetricsModel.setMethod(method);
    clientMetricsModel.setProtocol(protocol);
    clientMetricsModel.setInvokeType(invokeType);
    clientMetricsModel.setTargetApp(targetApp);
    clientMetricsModel.setRequestSize(requestSize);
    clientMetricsModel.setResponseSize(responseSize);
    clientMetricsModel.setElapsedTime(elapsedTime);
    clientMetricsModel.setSuccess(success);

    return clientMetricsModel;
}
 
Example 11
Source File: LookoutSubscriber.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * create RpcServerLookoutModel
 * @param request
 * @param response
 * @return
 */
private RpcServerLookoutModel createServerMetricsModel(SofaRequest request, SofaResponse response) {

    RpcServerLookoutModel rpcServerMetricsModel = new RpcServerLookoutModel();

    RpcInternalContext context = RpcInternalContext.getContext();

    String app = request.getTargetAppName();
    String service = request.getTargetServiceUniqueName();
    String method = request.getMethodName();
    String protocol = getStringAvoidNull(request.getRequestProp(RemotingConstants.HEAD_PROTOCOL));
    String invokeType = request.getInvokeType();
    String callerApp = getStringAvoidNull(request.getRequestProp(RemotingConstants.HEAD_APP_NAME));
    Long elapsedTime = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
    boolean success = response != null && !response.isError() && response.getErrorMsg() == null &&
        (!(response.getAppResponse() instanceof Throwable));

    rpcServerMetricsModel.setApp(app);
    rpcServerMetricsModel.setService(service);
    rpcServerMetricsModel.setMethod(method);
    rpcServerMetricsModel.setProtocol(protocol);
    rpcServerMetricsModel.setInvokeType(invokeType);
    rpcServerMetricsModel.setCallerApp(callerApp);
    rpcServerMetricsModel.setElapsedTime(elapsedTime);
    rpcServerMetricsModel.setSuccess(success);

    return rpcServerMetricsModel;
}
 
Example 12
Source File: JDKInvocationHandler.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] paramValues)
    throws Throwable {
    String methodName = method.getName();
    Class[] paramTypes = method.getParameterTypes();
    if ("toString".equals(methodName) && paramTypes.length == 0) {
        return proxyInvoker.toString();
    } else if ("hashCode".equals(methodName) && paramTypes.length == 0) {
        return proxyInvoker.hashCode();
    } else if ("equals".equals(methodName) && paramTypes.length == 1) {
        Object another = paramValues[0];
        return proxy == another ||
            (proxy.getClass().isInstance(another) && proxyInvoker.equals(JDKProxy.parseInvoker(another)));
    }
    SofaRequest sofaRequest = MessageBuilder.buildSofaRequest(method.getDeclaringClass(),
        method, paramTypes, paramValues);
    SofaResponse response = proxyInvoker.invoke(sofaRequest);
    if (response.isError()) {
        throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
    }
    Object ret = response.getAppResponse();
    if (ret instanceof Throwable) {
        throw (Throwable) ret;
    } else {
        if (ret == null) {
            return ClassUtils.getDefaultPrimitiveValue(method.getReturnType());
        }
        return ret;
    }
}
 
Example 13
Source File: CallbackInvokeClientHandler.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void doOnResponse(Object result) {
    if (callback == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    Throwable throwable = null;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, response, null));
        }

        pickupBaggage(response);

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }

        decode(response);

        Object appResp = response.getAppResponse();
        if (response.isError()) { // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(
                RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            callback.onSofaException(sofaRpcException, request.getMethodName(), request);
        } else if (appResp instanceof Throwable) { // 业务层异常
            throwable = (Throwable) appResp;
            callback.onAppException(throwable, request.getMethodName(), request);
        } else {
            callback.onAppResponse(appResp, request.getMethodName(), request);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 14
Source File: BoltFutureInvokeCallback.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onResponse(Object result) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    Throwable throwable = null;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, response, null));
        }

        pickupBaggage(response);

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }

        Object appResp = response.getAppResponse();
        if (response.isError()) { // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(
                RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            rpcFuture.setFailure(sofaRpcException);
        } else if (appResp instanceof Throwable) { // 业务层异常
            throwable = (Throwable) appResp;
            rpcFuture.setFailure(throwable);
        } else {
            rpcFuture.setSuccess(appResp);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 15
Source File: BoltInvokerCallback.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onResponse(Object result) {
    if (callback == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    Throwable throwable = null;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, response, null));
        }

        pickupBaggage(response);

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }

        Object appResp = response.getAppResponse();
        if (response.isError()) { // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(
                RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            callback.onSofaException(sofaRpcException, request.getMethodName(), request);
        } else if (appResp instanceof Throwable) { // 业务层异常
            throwable = (Throwable) appResp;
            callback.onAppException(throwable, request.getMethodName(), request);
        } else {
            callback.onAppResponse(appResp, request.getMethodName(), request);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}