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

The following examples show how to use com.alipay.sofa.rpc.core.response.SofaResponse#setResponseProps() . 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: ProtobufSerializer.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 {
        // 根据接口+方法名找到参数类型
        Class responseClass = protobufHelper.getResClass(targetService, methodName);
        Object pbRes = decode(data, responseClass, head);
        sofaResponse.setAppResponse(pbRes);
    }
}
 
Example 2
Source File: SofaResponseHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template)
    throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        // 根据SerializeType信息决定序列化器
        boolean genericSerialize = context != null && isGenericResponse(
            context.get(RemotingConstants.HEAD_GENERIC_TYPE));
        if (genericSerialize) {
            input.setSerializerFactory(genericSerializerFactory);
            GenericObject genericObject = (GenericObject) input.readObject();
            template.setErrorMsg((String) genericObject.getField("errorMsg"));
            template.setAppResponse(genericObject.getField("appResponse"));
            template.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
        } else {
            input.setSerializerFactory(serializerFactory);
            SofaResponse tmp = (SofaResponse) input.readObject();
            // copy values to template
            template.setErrorMsg(tmp.getErrorMsg());
            template.setAppResponse(tmp.getAppResponse());
            template.setResponseProps(tmp.getResponseProps());
        }
        input.close();
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
 
Example 3
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 4
Source File: JacksonSerializer.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
        JavaType respType = jacksonHelper.getResClass(targetService, methodName);
        Object result;
        try {
            result = mapper.readValue(data.array(), respType);
        } catch (IOException e) {
            throw buildDeserializeError(e.getMessage());
        }
        sofaResponse.setAppResponse(result);
    }
}
 
Example 5
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 6
Source File: ProtostuffSerializer.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 {
        // 根据接口+方法名找到参数类型
        Class responseClass = protostuffHelper.getResClass(targetService, methodName);
        Object pbRes = decode(data, responseClass, head);
        sofaResponse.setAppResponse(pbRes);
    }
}