public Future<byte[]> apply(byte[] request) { TTransport inputTransport = new TMemoryInputTransport(request); TProtocol iprot = protocolFactory.getProtocol(inputTransport); TMessage msg; try { msg = iprot.readMessageBegin(); } catch (Exception e) { return Future.exception(e); } Function2<TProtocol, Integer, Future<byte[]>> fn = functionMap.get(msg.name); if (fn == null) { try { TProtocolUtil.skip(iprot, TType.STRUCT); iprot.readMessageEnd(); TApplicationException x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"+msg.name+"'"); TMemoryBuffer memoryBuffer = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(memoryBuffer); oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid)); x.write(oprot); oprot.writeMessageEnd(); oprot.getTransport().flush(); return Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length())); } catch (Exception e) { return Future.exception(e); } } return fn.apply(iprot, msg.seqid); } }
String serialize(BKDLConfigFormat configFormat) { TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE); TJSONProtocol protocol = new TJSONProtocol(transport); try { configFormat.write(protocol); transport.flush(); return transport.toString("UTF-8"); } catch (TException e) { throw new RuntimeException("Failed to serialize BKDLConfig : ", e); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Failed to serialize BKDLConfig : ", e); } }
public String toBinaryString() throws IOException { org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan(); TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5); TBinaryProtocol oprot = new TBinaryProtocol(tmb); try { q.write(oprot); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); return q.toString(); } byte[] buf = new byte[tmb.length()]; tmb.read(buf, 0, tmb.length()); return new String(buf); // return getQueryPlan().toString(); }
private static String writeThriftObjectAsTText(Consumer<TProtocol> writer) { final TMemoryBuffer buffer = new TMemoryBuffer(1024); final TProtocol protocol = new TTextProtocol.Factory().getProtocol(buffer); writer.accept(protocol); return new String(buffer.getArray(), 0, buffer.length()); }
public Future<ServerInfo> handshake() { try { // TODO: size TMemoryBuffer __memoryTransport__ = new TMemoryBuffer(512); TProtocol __prot__ = this.protocolFactory.getProtocol(__memoryTransport__); __prot__.writeMessageBegin(new TMessage("handshake", TMessageType.CALL, 0)); handshake_args __args__ = new handshake_args(); __args__.write(__prot__); __prot__.writeMessageEnd(); byte[] __buffer__ = Arrays.copyOfRange(__memoryTransport__.getArray(), 0, __memoryTransport__.length()); ThriftClientRequest __request__ = new ThriftClientRequest(__buffer__, false); Future<byte[]> __done__ = this.service.apply(__request__); return __done__.flatMap(new Function<byte[], Future<ServerInfo>>() { public Future<ServerInfo> apply(byte[] __buffer__) { TMemoryInputTransport __memoryTransport__ = new TMemoryInputTransport(__buffer__); TProtocol __prot__ = ServiceToClient.this.protocolFactory.getProtocol(__memoryTransport__); try { return Future.value((new Client(__prot__)).recv_handshake()); } catch (Exception e) { return Future.exception(e); } } }); } catch (TException e) { return Future.exception(e); } } public Future<ServerInfo> handshakeWithClientInfo(ClientInfo clientInfo) {
final TMemoryBuffer outTransport = new TMemoryBuffer(128); final TProtocol tProtocol = protocolFactory.getProtocol(outTransport); final TMessage header = new TMessage(fullMethod(ctx, method), func.messageType(), seqId); tProtocol.writeMessageBegin(header); @SuppressWarnings("rawtypes") final TBase tArgs = func.newArgs(args); tArgs.write(tProtocol); tProtocol.writeMessageEnd(); HttpHeaders.of(HttpMethod.POST, ctx.path()) .contentType(mediaType), HttpData.of(outTransport.getArray(), 0, outTransport.length()));
@Test public void testMultipleInheritance() throws Exception { final NameService.Client client1 = new NameService.Client.Factory().getClient(inProto, outProto); client1.send_removeMiddle(new Name(BAZ, BAR, FOO)); assertThat(out.length()).isGreaterThan(0); final HttpData req1 = HttpData.of(out.getArray(), 0, out.length()); out = new TMemoryBuffer(128); outProto = ThriftProtocolFactories.get(defaultSerializationFormat).getProtocol(out); final NameSortService.Client client2 = new NameSortService.Client.Factory().getClient(inProto, outProto); client2.send_sort(Arrays.asList(NAME_C, NAME_B, NAME_A)); assertThat(out.length()).isGreaterThan(0); final HttpData req2 = HttpData.of(out.getArray(), 0, out.length()); final THttpService service = THttpService.of( (UberNameService) (names, callback) -> callback.onComplete( names.stream().sorted().collect(toImmutableList())), defaultSerializationFormat); invoke0(service, req1, promise); invoke0(service, req2, promise2); final HttpData res1 = promise.get(); final HttpData res2 = promise2.get(); in.reset(res1.array(), res1.offset(), res1.length()); assertThat(client1.recv_removeMiddle()).isEqualTo(new Name(BAZ, null, FOO)); in.reset(res2.array(), res2.offset(), res2.length()); assertThat(client2.recv_sort()).containsExactly(NAME_A, NAME_B, NAME_C); }
try { iprot.readMessageEnd(); TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage()); TMemoryBuffer memoryBuffer = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(memoryBuffer); TMemoryBuffer memoryBuffer = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(memoryBuffer); try { iprot.readMessageEnd(); TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage()); TMemoryBuffer memoryBuffer = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(memoryBuffer); TMemoryBuffer memoryBuffer = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(memoryBuffer); try { iprot.readMessageEnd(); TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage()); TMemoryBuffer memoryBuffer = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(memoryBuffer); TMemoryBuffer memoryBuffer = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(memoryBuffer); TMemoryBuffer memoryBuffer = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
@Before public void before() { in = new TMemoryInputTransport(); out = new TMemoryBuffer(128); inProto = ThriftProtocolFactories.get(defaultSerializationFormat).getProtocol(in); outProto = ThriftProtocolFactories.get(defaultSerializationFormat).getProtocol(out); promise = new CompletableFuture<>(); promise2 = new CompletableFuture<>(); }
@Test public void testSync_OnewayHelloService_hello() throws Exception { final AtomicReference<String> actualName = new AtomicReference<>(); final OnewayHelloService.Client client = new OnewayHelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (OnewayHelloService.Iface) actualName::set, defaultSerializationFormat); invoke(service); assertThat(promise.get().isEmpty()).isTrue(); assertThat(actualName.get()).isEqualTo(FOO); }
public String toThriftJSONString() throws IOException { org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan(); TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5); TJSONProtocol oprot = new TJSONProtocol(tmb); try { q.write(oprot); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); return q.toString(); } return tmb.toString("UTF-8"); }
@Test public void testSync_FileService_create_exception() throws Exception { final FileService.Client client = new FileService.Client.Factory().getClient(inProto, outProto); client.send_create(BAZ); assertThat(out.length()).isGreaterThan(0); final RuntimeException exception = Exceptions.clearTrace(new RuntimeException()); final THttpService service = THttpService.of((FileService.Iface) path -> { throw exception; }, defaultSerializationFormat); invoke(service); try { client.recv_create(); fail(TApplicationException.class.getSimpleName() + " not raised."); } catch (TApplicationException e) { assertThat(e.getType()).isEqualTo(TApplicationException.INTERNAL_ERROR); assertThat(e.getMessage()).contains(exception.toString()); } }
public Future<ServerInfo> handshakeWithClientInfo(ClientInfo clientInfo) { try { // TODO: size TMemoryBuffer __memoryTransport__ = new TMemoryBuffer(512); TProtocol __prot__ = this.protocolFactory.getProtocol(__memoryTransport__); __prot__.writeMessageBegin(new TMessage("handshakeWithClientInfo", TMessageType.CALL, 0)); handshakeWithClientInfo_args __args__ = new handshakeWithClientInfo_args(); __args__.setClientInfo(clientInfo); __args__.write(__prot__); __prot__.writeMessageEnd(); byte[] __buffer__ = Arrays.copyOfRange(__memoryTransport__.getArray(), 0, __memoryTransport__.length()); ThriftClientRequest __request__ = new ThriftClientRequest(__buffer__, false); Future<byte[]> __done__ = this.service.apply(__request__); return __done__.flatMap(new Function<byte[], Future<ServerInfo>>() { public Future<ServerInfo> apply(byte[] __buffer__) { TMemoryInputTransport __memoryTransport__ = new TMemoryInputTransport(__buffer__); TProtocol __prot__ = ServiceToClient.this.protocolFactory.getProtocol(__memoryTransport__); try { return Future.value((new Client(__prot__)).recv_handshakeWithClientInfo()); } catch (Exception e) { return Future.exception(e); } } }); } catch (TException e) { return Future.exception(e); } } public Future<WriteResponse> heartbeat(String stream, WriteContext ctx) {
protected ThriftClientRequest encodeRequest(String name, ThriftStruct args) { TMemoryBuffer buf = new TMemoryBuffer(512); TProtocol oprot = protocolFactory.getProtocol(buf); try { oprot.writeMessageBegin(new TMessage(name, TMessageType.CALL, 0)); args.write(oprot); oprot.writeMessageEnd(); } catch (TException e) { // not real. } byte[] bytes = Arrays.copyOfRange(buf.getArray(), 0, buf.length()); return new ThriftClientRequest(bytes, false); }
/** * Initialize buffers. * @throws TException if buffer initialization fails */ protected void prepareMethodCall() throws TException { TMemoryBuffer memoryBuffer = new TMemoryBuffer(INITIAL_MEMORY_BUFFER_SIZE); TProtocol protocol = protocolFactory.getProtocol(memoryBuffer); write_args(protocol); int length = memoryBuffer.length(); frameBuffer = ByteBuffer.wrap(memoryBuffer.getArray(), 0, length); TFramedTransport.encodeFrameSize(length, sizeBufferArray); sizeBuffer = ByteBuffer.wrap(sizeBufferArray); }
private <T> void testRoundTripSerialize(ThriftType type, T value, TProtocolFactory protocolFactory) throws Exception { // write value TMemoryBuffer transport = new TMemoryBuffer(10 * 1024); TProtocol protocol = protocolFactory.getProtocol(transport); codecManager.write(type, value, protocol); // read value back T copy = (T) codecManager.read(type, protocol); assertNotNull(copy); // verify they are the same assertEquals(copy, value); }
private static String writeThriftObjectAsTText(Consumer<TProtocol> writer) { final TMemoryBuffer buffer = new TMemoryBuffer(1024); final TProtocol protocol = new TTextProtocol.Factory().getProtocol(buffer); writer.accept(protocol); return new String(buffer.getArray(), 0, buffer.length()); }
@Test public void testAsync_OnewayHelloService_hello() throws Exception { final AtomicReference<String> actualName = new AtomicReference<>(); final OnewayHelloService.Client client = new OnewayHelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of((OnewayHelloService.AsyncIface) (name, resultHandler) -> { actualName.set(name); resultHandler.onComplete(null); }, defaultSerializationFormat); invoke(service); assertThat(promise.get().isEmpty()).isTrue(); assertThat(actualName.get()).isEqualTo(FOO); }
public String toThriftJSONString() throws IOException { org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan(); TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5); TJSONProtocol oprot = new TJSONProtocol(tmb); try { q.write(oprot); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); return q.toString(); } return tmb.toString("UTF-8"); }