Java Code Examples for org.apache.thrift.TApplicationException#write()

The following examples show how to use org.apache.thrift.TApplicationException#write() . 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: TProtobufProcessor.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
@Override
public boolean process(TProtocol in, TProtocol out) throws TException {
	TMessage msg = in.readMessageBegin();
	Controller<?, ?> fn = (Controller<?, ?>) this.beanFactory
			.getBean(msg.name);
	if (fn == null) {
		if (LOGGER.isWarnEnabled()) {
			LOGGER.warn("Invalid request: failed to find interface="
					+ msg.name + ", from: " + getInetAddress(in));
		}

		TProtocolUtil.skip(in, TType.STRUCT);
		in.readMessageEnd();
		TApplicationException x = new TApplicationException(
				TApplicationException.UNKNOWN_METHOD,
				"Invalid method name: '" + msg.name + "'");
		out.writeMessageBegin(new TMessage(msg.name,
				TMessageType.EXCEPTION, msg.seqid));
		x.write(out);
		out.writeMessageEnd();
		out.getTransport().flush();
		return true;
	}
	process(msg.seqid, msg.name, in, out, fn);
	return true;
}
 
Example 2
Source File: LocatorServiceImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public final boolean process(final TProtocol in, final TProtocol out)
    throws TException {
  final TMessage msg = in.readMessageBegin();
  final ProcessFunction<LocatorServiceImpl, ?> fn = this.fnMap
      .get(msg.name);
  if (fn != null) {
    fn.process(msg.seqid, in, out, this.inst);
    // terminate connection on receiving closeConnection
    // direct class comparison should be the fastest way
    return fn.getClass() != LocatorService.Processor.closeConnection.class;
  }
  else {
    TProtocolUtil.skip(in, TType.STRUCT);
    in.readMessageEnd();
    TApplicationException x = new TApplicationException(
        TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"
            + msg.name + "'");
    out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION,
        msg.seqid));
    x.write(out);
    out.writeMessageEnd();
    out.getTransport().flush();
    return true;
  }
}
 
Example 3
Source File: NettyDispatcher.java    From ikasoa with MIT License 6 votes vote down vote up
private void sendTApplicationException(TApplicationException e, ChannelHandlerContext ctx, TNettyMessage request,
		int responseSequenceId, TNettyTransport requestTransport, TProtocol inProtocol, TProtocol outProtocol) {
	if (ctx.getChannel().isConnected()) {
		try {
			TMessage message = inProtocol.readMessageBegin();
			outProtocol.writeMessageBegin(new TMessage(message.name, TMessageType.EXCEPTION, message.seqid));
			e.write(outProtocol);
			outProtocol.writeMessageEnd();
			requestTransport.setTApplicationException(e);
			outProtocol.getTransport().flush();
			writeResponse(ctx, request.getMessageFactory().create(requestTransport.getOutputBuffer()),
					responseSequenceId, DispatcherContext.isResponseOrderingRequired(ctx));
		} catch (TException ex) {
			onDispatchException(ctx, ex);
		}
	}
}
 
Example 4
Source File: LocatorServiceImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public final boolean process(final TProtocol in, final TProtocol out)
    throws TException {
  final TMessage msg = in.readMessageBegin();
  final ProcessFunction<LocatorServiceImpl, ?> fn = this.fnMap
      .get(msg.name);
  if (fn != null) {
    fn.process(msg.seqid, in, out, this.inst);
    // terminate connection on receiving closeConnection
    // direct class comparison should be the fastest way
    return fn.getClass() != LocatorService.Processor.closeConnection.class;
  }
  else {
    TProtocolUtil.skip(in, TType.STRUCT);
    in.readMessageEnd();
    TApplicationException x = new TApplicationException(
        TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"
            + msg.name + "'");
    out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION,
        msg.seqid));
    x.write(out);
    out.writeMessageEnd();
    out.getTransport().flush();
    return true;
  }
}
 
Example 5
Source File: DefaultNettyProcessor.java    From nettythrift with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes" })
private void writeException(final TProtocol out, final TMessage msg, final WriterHandler onComplete,
		final TApplicationException x, TBase args) {
	Throwable cause = null;
	try {
		onComplete.beforeWrite(msg, args, null);
		out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));
		x.write(out);
		out.writeMessageEnd();
		out.getTransport().flush();
	} catch (Throwable e) {
		cause = e;
	}
	onComplete.afterWrite(msg, cause, TMessageType.EXCEPTION, args, null);
}
 
Example 6
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean process(final TProtocol in, final TProtocol out)
    throws TException {
  final TMessage msg = in.readMessageBegin();
  final ProcessFunction<GFXDServiceImpl, ?> fn = this.fnMap.get(msg.name);
  if (fn != null) {
    fn.process(msg.seqid, in, out, this.inst);
    // terminate connection on receiving closeConnection
    // direct class comparison should be the fastest way
    // TODO: SW: also need to clean up connection artifacts in the case of
    // client connection failure (ConnectionListener does get a notification
    // but how to tie the socket/connectionNumber to the connectionID?)
    return fn.getClass() != GFXDService.Processor.closeConnection.class;
  }
  else {
    TProtocolUtil.skip(in, TType.STRUCT);
    in.readMessageEnd();
    TApplicationException x = new TApplicationException(
        TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"
            + msg.name + "'");
    out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION,
        msg.seqid));
    x.write(out);
    out.writeMessageEnd();
    out.getTransport().flush();
    return true;
  }
}
 
Example 7
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean process(final TProtocol in, final TProtocol out)
    throws TException {
  final TMessage msg = in.readMessageBegin();
  final ProcessFunction<GFXDServiceImpl, ?> fn = this.fnMap.get(msg.name);
  if (fn != null) {
    fn.process(msg.seqid, in, out, this.inst);
    // terminate connection on receiving closeConnection
    // direct class comparison should be the fastest way
    // TODO: SW: also need to clean up connection artifacts in the case of
    // client connection failure (ConnectionListener does get a notification
    // but how to tie the socket/connectionNumber to the connectionID?)
    return fn.getClass() != GFXDService.Processor.closeConnection.class;
  }
  else {
    TProtocolUtil.skip(in, TType.STRUCT);
    in.readMessageEnd();
    TApplicationException x = new TApplicationException(
        TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"
            + msg.name + "'");
    out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION,
        msg.seqid));
    x.write(out);
    out.writeMessageEnd();
    out.getTransport().flush();
    return true;
  }
}
 
Example 8
Source File: ThriftCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 2 votes vote down vote up
@Test
public void testDecodeExceptionResponse() throws Exception {

    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.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.EXCEPTION, ThriftCodec.getSeqId());

    TTransport transport = new TIOStreamTransport(bos);

    TBinaryProtocol protocol = new TBinaryProtocol(transport);

    TApplicationException exception = new TApplicationException();

    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.class.getName());
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin(message);
    exception.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

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));

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

    Assert.assertNotNull(obj);

    Assert.assertTrue(obj instanceof Response);

    Response response = (Response) obj;

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

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

    Assert.assertTrue(result.hasException());

    Assert.assertTrue(result.getException() instanceof RpcException);

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

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.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.EXCEPTION, ThriftCodec.getSeqId() );

    TTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    TApplicationException exception = new TApplicationException();

    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.class.getName() );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin( message );
    exception.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

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));

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

    Assert.assertNotNull( obj );

    Assert.assertTrue( obj instanceof Response );

    Response response = ( Response ) obj;

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

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

    Assert.assertTrue( result.hasException() );

    Assert.assertTrue( result.getException() instanceof RpcException );

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

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.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.EXCEPTION, ThriftCodec.getSeqId() );

    TTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    TApplicationException exception = new TApplicationException();

    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.class.getName() );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin( message );
    exception.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

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));

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

    Assert.assertNotNull( obj );

    Assert.assertTrue( obj instanceof Response );

    Response response = ( Response ) obj;

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

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

    Assert.assertTrue( result.hasException() );

    Assert.assertTrue( result.getException() instanceof RpcException );

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

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.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.EXCEPTION, ThriftCodec.getSeqId() );

    TTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    TApplicationException exception = new TApplicationException();

    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.class.getName() );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin( message );
    exception.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

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));

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

    Assert.assertNotNull( obj );

    Assert.assertTrue( obj instanceof Response );

    Response response = ( Response ) obj;

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

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

    Assert.assertTrue( result.hasException() );

    Assert.assertTrue( result.getException() instanceof RpcException );

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

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.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.EXCEPTION, ThriftCodec.getSeqId() );

    TTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    TApplicationException exception = new TApplicationException();

    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.class.getName() );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    protocol.writeMessageBegin( message );
    exception.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

    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));

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

    Assert.assertNotNull( obj );

    Assert.assertTrue( obj instanceof Response );

    Response response = ( Response ) obj;

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

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

    Assert.assertTrue( result.hasException() );

    Assert.assertTrue( result.getException() instanceof RpcException );

}