Java Code Examples for com.alipay.remoting.rpc.protocol.RpcResponseCommand#setResponseObject()

The following examples show how to use com.alipay.remoting.rpc.protocol.RpcResponseCommand#setResponseObject() . 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: ProtobufSerializerTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecodeResponseContent() throws Exception {
    final PingRequest reqObject = TestUtils.createPingRequest();
    final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
    final ErrorResponse respObject = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null,
        new Status(-1, "test"));
    final RpcResponseCommand response = cmdFactory.createResponse(respObject, request);
    response.setResponseClass(ErrorResponse.class.getName());
    assertTrue(serializer.serializeContent(response));

    response.setResponseObject(null);
    assertTrue(serializer.deserializeContent(response, null));
    assertNotNull(response.getResponseObject());
    assertEquals(respObject, response.getResponseObject());
    assertNotSame(respObject, response.getResponseObject());
}
 
Example 2
Source File: NormalStringCustomSerializer_InvokeContext.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * @see CustomSerializer#deserializeContent(ResponseCommand, InvokeContext)
 */
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response,
                                                              InvokeContext invokeContext)
                                                                                          throws DeserializationException {
    deserialFlag.set(true);
    RpcResponseCommand rpcResp = (RpcResponseCommand) response;

    if (StringUtils.equals(SERIALTYPE1_value, (String) invokeContext.get(SERIALTYPE_KEY))) {
        try {
            rpcResp.setResponseObject(new String(rpcResp.getContent(), "UTF-8") + "RANDOM");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    } else {
        rpcResp.setResponseObject(UNIVERSAL_RESP);
    }
    return true;
}
 
Example 3
Source File: ProtobufSerializer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response, InvokeContext invokeContext)
                                                                                                      throws DeserializationException {
    final RpcResponseCommand cmd = (RpcResponseCommand) response;
    final String className = cmd.getResponseClass();

    cmd.setResponseObject(ProtobufMsgFactory.newMessageByJavaClassName(className, response.getContent()));
    return true;
}
 
Example 4
Source File: RpcCommandFactory.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public RpcResponseCommand createExceptionResponse(int id, ResponseStatus status, Throwable t) {
    RpcResponseCommand responseCommand = this.createExceptionResponse(id, status);
    responseCommand.setResponseObject(createServerException(t, null));
    responseCommand.setResponseClass(RpcServerException.class.getName());
    return responseCommand;
}
 
Example 5
Source File: NormalStringCustomSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see CustomSerializer#deserializeContent(ResponseCommand, InvokeContext)
 */
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response,
                                                              InvokeContext invokeContext)
                                                                                          throws DeserializationException {
    deserialFlag.set(true);
    RpcResponseCommand rpcResp = (RpcResponseCommand) response;
    try {
        rpcResp.setResponseObject(new String(rpcResp.getContent(), "UTF-8") + "RANDOM");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    contentDeserialier = response.getSerializer();
    return true;
}
 
Example 6
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public <Response extends ResponseCommand> boolean deserializeContent(Response response, InvokeContext invokeContext)
    throws DeserializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte serializer = response.getSerializer();
        byte[] content = responseCommand.getContent();
        if (content == null || content.length == 0) {
            return false;
        }
        try {
            Object sofaResponse = ClassUtils.forName(responseCommand.getResponseClass()).newInstance();

            Map<String, String> header = (Map<String, String>) responseCommand.getResponseHeader();
            if (header == null) {
                header = new HashMap<String, String>();
            }
            putKV(header, RemotingConstants.HEAD_TARGET_SERVICE,
                (String) invokeContext.get(RemotingConstants.HEAD_TARGET_SERVICE));
            putKV(header, RemotingConstants.HEAD_METHOD_NAME,
                (String) invokeContext.get(RemotingConstants.HEAD_METHOD_NAME));
            putKV(header, RemotingConstants.HEAD_GENERIC_TYPE,
                (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));

            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializer);
            rpcSerializer.decode(new ByteArrayWrapperByteBuf(responseCommand.getContent()), sofaResponse, header);

            responseCommand.setResponseObject(sofaResponse);
            return true;
        } catch (Exception ex) {
            throw new DeserializationException(ex.getMessage(), ex);
        } finally {
            recordDeserializeResponse(responseCommand, invokeContext);
        }
    }

    return false;
}