Java Code Examples for org.apache.thrift.protocol.TMessageType#REPLY

The following examples show how to use org.apache.thrift.protocol.TMessageType#REPLY . 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: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 6 votes vote down vote up
protected RpcResult doDeserializeResponse(DefaultResponse response, TMessage message,
                                          TProtocol protocol) throws Exception {
    RpcResult rpcResult = new RpcResult();
    if (message.type == TMessageType.REPLY) {
        Object realResult = deserializeResult(protocol, response.getServiceInterface().getName(), message.name);
        if (realResult instanceof Exception) {
            // 服务端自定义异常
            response.setException((Exception) realResult);
        }
        rpcResult.setReturnVal(realResult);
    } else if (message.type == TMessageType.EXCEPTION) {
        TApplicationException exception = TApplicationException.read(protocol);
        MetaUtil.wrapException(exception, response);
    }
    if (!response.isOctoProtocol() && hasOldRequestHeader(protocol)) {
        // 解析老协议的Header
        RequestHeader requestHeader = new RequestHeader();
        protocol.readFieldBegin();
        requestHeader.read(protocol);
        protocol.readFieldEnd();
    }
    return rpcResult;
}
 
Example 2
Source File: TestDriftNettyServerTransport.java    From drift with Apache License 2.0 6 votes vote down vote up
private static ResultCode readLogResponse(int expectedSequenceId, TProtocol protocol)
        throws TException
{
    TMessage message = protocol.readMessageBegin();
    if (message.type == TMessageType.EXCEPTION) {
        throw TApplicationException.readFrom(protocol);
    }
    if (message.type != TMessageType.REPLY) {
        throw new TApplicationException(MISSING_RESULT, "request failed");
    }
    if (message.seqid != expectedSequenceId) {
        throw new TApplicationException(BAD_SEQUENCE_ID, format("expected sequenceId %s, but received %s", expectedSequenceId, message.seqid));
    }

    Log_result result = new Log_result();
    result.read(protocol);
    protocol.readMessageEnd();
    return result.success;
}
 
Example 3
Source File: TypedParser.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
void writeValue(JsonGenerator jw, Byte val) throws IOException {
    final String serialized;
    switch (val.byteValue()) {
    case TMessageType.CALL:
        serialized = "CALL";
        break;
    case TMessageType.REPLY:
        serialized = "REPLY";
        break;
    case TMessageType.EXCEPTION:
        serialized = "EXCEPTION";
        break;
    case TMessageType.ONEWAY:
        serialized = "ONEWAY";
        break;
    default:
        throw new IllegalArgumentException("Unsupported message type: " + val);
    }
    jw.writeString(serialized);
}
 
Example 4
Source File: ThriftJacksonTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void serializeThriftReply() throws IOException {
    final ThriftReply reply = new ThriftReply(
            new TMessage(THRIFT_METHOD_NAME, TMessageType.REPLY, 0),
            new hello_result().setSuccess("Hello kawamuray"));
    final String actualJson = defaultMapper.writeValueAsString(reply);
    final String actualJson2 = customMapper.writeValueAsString(reply);

    assertThatJson(actualJson).isEqualTo(
            '{' +
            "    \"header\": {" +
            "        \"name\": \"hello\"," +
            "        \"type\": 2," +
            "        \"seqid\": 0" +
            "    }," +
            "    \"result\": {" +
            "        \"success\": \"Hello kawamuray\"" +
            "    }," +
            "    \"exception\": null" +
            '}');
    assertThatJson(actualJson2).isEqualTo(actualJson);
}
 
Example 5
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected Object deserialize4Thrift(byte[] buff, Class<?> iface, Map<String, Object> attachments) throws Exception {
    TraceTimeline timeline = TraceTimeline.newRecord(CommonUtil.objectToBool(attachments.get(Constants.TRACE_IS_RECORD_TIMELINE), false),
            TraceTimeline.DECODE_START_TS);

    Object obj = null;
    TMemoryInputTransport transport = new TMemoryInputTransport(buff);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    TMessage message = protocol.readMessageBegin();
    if (message.type == TMessageType.CALL) {
        DefaultRequest request = new DefaultRequest(Long.valueOf(message.seqid));
        request.setServiceInterface(iface);
        RpcInvocation rpcInvocation = doDeserializeRequest(request, message, protocol);
        request.setData(rpcInvocation);
        obj = request;

        rpcInvocation.putAttachment(Constants.TRACE_TIMELINE, timeline);
    } else if (message.type == TMessageType.REPLY || message.type == TMessageType.EXCEPTION) {
        DefaultResponse response = new DefaultResponse(Long.valueOf(message.seqid));
        response.setServiceInterface(iface);
        try {
            RpcResult rpcResult = doDeserializeResponse(response, message, protocol);
            response.setResult(rpcResult);
        } catch (Exception e) {
            response.setException(e);
        }

        obj = response;
        if (response.getRequest() != null && response.getRequest().getData() != null) {
            TraceTimeline.copyRecord(timeline, response.getRequest().getData());
        }
    } else {
        throw new ProtocolException("Thrift deserialize message type is invalid");
    }
    protocol.readMessageEnd();

    MetaUtil.recordTraceInfoInDecode(buff, obj);
    return obj;
}
 
Example 6
Source File: THttpService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static String typeString(byte typeValue) {
    switch (typeValue) {
        case TMessageType.CALL:
            return "CALL";
        case TMessageType.REPLY:
            return "REPLY";
        case TMessageType.EXCEPTION:
            return "EXCEPTION";
        case TMessageType.ONEWAY:
            return "ONEWAY";
        default:
            return "UNKNOWN(" + (typeValue & 0xFF) + ')';
    }
}
 
Example 7
Source File: THttpService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static HttpData encodeSuccess(ServiceRequestContext ctx,
                                      RpcResponse reply,
                                      SerializationFormat serializationFormat,
                                      String methodName, int seqId,
                                      TBase<?, ?> result) {

    final ByteBuf buf = ctx.alloc().buffer(128);
    boolean success = false;
    try {
        final TTransport transport = new TByteBufTransport(buf);
        final TProtocol outProto = ThriftProtocolFactories.get(serializationFormat).getProtocol(transport);
        final TMessage header = new TMessage(methodName, TMessageType.REPLY, seqId);
        outProto.writeMessageBegin(header);
        result.write(outProto);
        outProto.writeMessageEnd();

        ctx.logBuilder().responseContent(reply, new ThriftReply(header, result));

        final HttpData encoded = PooledHttpData.wrap(buf);
        success = true;
        return encoded;
    } catch (TException e) {
        throw new Error(e); // Should never reach here.
    } finally {
        if (!success) {
            buf.release();
        }
    }
}
 
Example 8
Source File: ThriftMessage.java    From armeria with Apache License 2.0 5 votes vote down vote up
static String typeStr(byte type) {
    switch (type) {
        case TMessageType.CALL:
            return "CALL";
        case TMessageType.ONEWAY:
            return "ONEWAY";
        case TMessageType.REPLY:
            return "REPLY";
        case TMessageType.EXCEPTION:
            return "EXCEPTION";
        default:
            return "UNKNOWN(" + (type & 0xFF) + ')';
    }
}
 
Example 9
Source File: TypedParser.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
Byte readFromString(String s) {
    switch (s) {
    case "CALL":
        return TMessageType.CALL;
    case "REPLY":
        return TMessageType.REPLY;
    case "EXCEPTION":
        return TMessageType.EXCEPTION;
    case "ONEWAY":
        return TMessageType.ONEWAY;
    default:
        throw new IllegalArgumentException("Unsupported message type: " + s);
    }
}
 
Example 10
Source File: ThriftReply.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance that contains a Thrift {@link TMessageType#REPLY} message.
 */
public ThriftReply(TMessage header, TBase<?, ?> result) {
    super(header);
    if (header.type != TMessageType.REPLY) {
        throw new IllegalArgumentException(
                "header.type: " + typeStr(header.type) + " (expected: REPLY)");
    }

    this.result = requireNonNull(result, "result");
    exception = null;
}
 
Example 11
Source File: ThriftCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 2 votes vote down vote up
@Test
public void testDecodeReplyResponse() throws Exception {

    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName());

    Channel channel = new MockedChannel(url);

    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);

    Request request = createRequest();

    DefaultFuture future = new DefaultFuture(channel, request, 10);

    TMessage message = new TMessage("echoString", TMessageType.REPLY, ThriftCodec.getSeqId());

    Demo.echoString_result methodResult = new Demo.echoString_result();

    methodResult.success = "Hello, World!";

    TTransport transport = new TIOStreamTransport(bos);

    TBinaryProtocol protocol = new TBinaryProtocol(transport);

    int messageLength, headerLength;
    // prepare
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(Demo.Iface.class.getName());
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin(message);
    methodResult.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    // prepare

    byte[] buf = new byte[4 + bos.size()];
    System.arraycopy(bos.toByteArray(), 0, buf, 4, bos.size());

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf);

    Object obj = codec.decode((Channel) null, bis);

    Assert.assertNotNull(obj);

    Assert.assertEquals(true, obj instanceof Response);

    Response response = (Response) obj;

    Assert.assertEquals(request.getId(), response.getId());

    Assert.assertTrue(response.getResult() instanceof RpcResult);

    RpcResult result = (RpcResult) response.getResult();

    Assert.assertTrue(result.getResult() instanceof String);

    Assert.assertEquals(methodResult.success, result.getResult());

}
 
Example 12
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testDecodeReplyResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 128 );

    Request request = createRequest();

    DefaultFuture future = new DefaultFuture( channel, request, 10 );

    TMessage message = new TMessage( "echoString", TMessageType.REPLY, ThriftCodec.getSeqId() );

    Demo.echoString_result methodResult = new Demo.echoString_result();

    methodResult.success = "Hello, World!";

    TTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    int messageLength, headerLength;
    // prepare
    protocol.writeI16( ThriftCodec.MAGIC );
    protocol.writeI32( Integer.MAX_VALUE );
    protocol.writeI16( Short.MAX_VALUE );
    protocol.writeByte( ThriftCodec.VERSION );
    protocol.writeString( Demo.Iface.class.getName() );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin( message );
    methodResult.write( protocol );
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try {
        bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX );
        protocol.writeI32( messageLength );
        bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX );
        protocol.writeI16( ( short ) ( 0xffff & headerLength ) );
    } finally {
        bos.setWriteIndex( oldIndex );
    }
    // prepare

    byte[] buf = new byte[ 4 + bos.size()];
    System.arraycopy( bos.toByteArray(), 0, buf, 4, bos.size() );

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf);

    Object obj = codec.decode( ( Channel ) null, bis );

    Assert.assertNotNull( obj );

    Assert.assertEquals( true, obj instanceof Response );

    Response response = ( Response ) obj;

    Assert.assertEquals( request.getId(), response.getId() );

    Assert.assertTrue( response.getResult() instanceof RpcResult );

    RpcResult result = ( RpcResult ) response.getResult();

    Assert.assertTrue( result.getResult() instanceof String );

    Assert.assertEquals( methodResult.success, result.getResult() );

}
 
Example 13
Source File: ThriftCodecTest.java    From dubbox-hystrix with Apache License 2.0 2 votes vote down vote up
@Test
public void testDecodeReplyResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 128 );

    Request request = createRequest();

    DefaultFuture future = new DefaultFuture( channel, request, 10 );

    TMessage message = new TMessage( "echoString", TMessageType.REPLY, ThriftCodec.getSeqId() );

    Demo.echoString_result methodResult = new Demo.echoString_result();

    methodResult.success = "Hello, World!";

    TTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    int messageLength, headerLength;
    // prepare
    protocol.writeI16( ThriftCodec.MAGIC );
    protocol.writeI32( Integer.MAX_VALUE );
    protocol.writeI16( Short.MAX_VALUE );
    protocol.writeByte( ThriftCodec.VERSION );
    protocol.writeString( Demo.Iface.class.getName() );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin( message );
    methodResult.write( protocol );
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try {
        bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX );
        protocol.writeI32( messageLength );
        bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX );
        protocol.writeI16( ( short ) ( 0xffff & headerLength ) );
    } finally {
        bos.setWriteIndex( oldIndex );
    }
    // prepare

    byte[] buf = new byte[ 4 + bos.size()];
    System.arraycopy( bos.toByteArray(), 0, buf, 4, bos.size() );

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf);

    Object obj = codec.decode( ( Channel ) null, bis );

    Assert.assertNotNull( obj );

    Assert.assertEquals( true, obj instanceof Response );

    Response response = ( Response ) obj;

    Assert.assertEquals( request.getId(), response.getId() );

    Assert.assertTrue( response.getResult() instanceof RpcResult );

    RpcResult result = ( RpcResult ) response.getResult();

    Assert.assertTrue( result.getResult() instanceof String );

    Assert.assertEquals( methodResult.success, result.getResult() );

}
 
Example 14
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testDecodeReplyResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 128 );

    Request request = createRequest();

    DefaultFuture future = new DefaultFuture( channel, request, 10 );

    TMessage message = new TMessage( "echoString", TMessageType.REPLY, ThriftCodec.getSeqId() );

    Demo.echoString_result methodResult = new Demo.echoString_result();

    methodResult.success = "Hello, World!";

    TTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    int messageLength, headerLength;
    // prepare
    protocol.writeI16( ThriftCodec.MAGIC );
    protocol.writeI32( Integer.MAX_VALUE );
    protocol.writeI16( Short.MAX_VALUE );
    protocol.writeByte( ThriftCodec.VERSION );
    protocol.writeString( Demo.Iface.class.getName() );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin( message );
    methodResult.write( protocol );
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try {
        bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX );
        protocol.writeI32( messageLength );
        bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX );
        protocol.writeI16( ( short ) ( 0xffff & headerLength ) );
    } finally {
        bos.setWriteIndex( oldIndex );
    }
    // prepare

    byte[] buf = new byte[ 4 + bos.size()];
    System.arraycopy( bos.toByteArray(), 0, buf, 4, bos.size() );

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf);

    Object obj = codec.decode( ( Channel ) null, bis );

    Assert.assertNotNull( obj );

    Assert.assertEquals( true, obj instanceof Response );

    Response response = ( Response ) obj;

    Assert.assertEquals( request.getId(), response.getId() );

    Assert.assertTrue( response.getResult() instanceof RpcResult );

    RpcResult result = ( RpcResult ) response.getResult();

    Assert.assertTrue( result.getResult() instanceof String );

    Assert.assertEquals( methodResult.success, result.getResult() );

}
 
Example 15
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testDecodeReplyResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 128 );

    Request request = createRequest();

    DefaultFuture future = new DefaultFuture( channel, request, 10 );

    TMessage message = new TMessage( "echoString", TMessageType.REPLY, ThriftCodec.getSeqId() );

    Demo.echoString_result methodResult = new Demo.echoString_result();

    methodResult.success = "Hello, World!";

    TTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    int messageLength, headerLength;
    // prepare
    protocol.writeI16( ThriftCodec.MAGIC );
    protocol.writeI32( Integer.MAX_VALUE );
    protocol.writeI16( Short.MAX_VALUE );
    protocol.writeByte( ThriftCodec.VERSION );
    protocol.writeString( Demo.Iface.class.getName() );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin( message );
    methodResult.write( protocol );
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try {
        bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX );
        protocol.writeI32( messageLength );
        bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX );
        protocol.writeI16( ( short ) ( 0xffff & headerLength ) );
    } finally {
        bos.setWriteIndex( oldIndex );
    }
    // prepare

    byte[] buf = new byte[ 4 + bos.size()];
    System.arraycopy( bos.toByteArray(), 0, buf, 4, bos.size() );

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf);

    Object obj = codec.decode( ( Channel ) null, bis );

    Assert.assertNotNull( obj );

    Assert.assertEquals( true, obj instanceof Response );

    Response response = ( Response ) obj;

    Assert.assertEquals( request.getId(), response.getId() );

    Assert.assertTrue( response.getResult() instanceof RpcResult );

    RpcResult result = ( RpcResult ) response.getResult();

    Assert.assertTrue( result.getResult() instanceof String );

    Assert.assertEquals( methodResult.success, result.getResult() );

}