public FrameBuffer(final TNonblockingTransport trans, final SelectionKey selectionKey, final AbstractSelectThread selectThread) { trans_ = trans; selectionKey_ = selectionKey; selectThread_ = selectThread; buffer_ = ByteBuffer.allocate(4); frameTrans_ = new TMemoryInputTransport(); response_ = new TByteArrayOutputStream(); inTrans_ = inputTransportFactory_.getTransport(frameTrans_); outTrans_ = outputTransportFactory_.getTransport(new TIOStreamTransport(response_)); inProt_ = inputProtocolFactory_.getProtocol(inTrans_); outProt_ = outputProtocolFactory_.getProtocol(outTrans_); if (eventHandler_ != null) { context_ = eventHandler_.createContext(inProt_, outProt_); } else { context_ = null; } }
public boolean process(final TProtocol inProt, final TProtocol outProt) throws TException { //populating request context ReqContext req_context = ReqContext.context(); TTransport trans = inProt.getTransport(); if (trans instanceof TMemoryInputTransport) { try { req_context.setRemoteAddress(InetAddress.getLocalHost()); } catch (UnknownHostException e) { throw new RuntimeException(e); } } else if (trans instanceof TSocket) { TSocket tsocket = (TSocket)trans; //remote address Socket socket = tsocket.getSocket(); req_context.setRemoteAddress(socket.getInetAddress()); } //anonymous user req_context.setSubject(null); //invoke service handler return wrapped.process(inProt, outProt); }
@Override public Future<Void> apply(byte[] responseBytes) { TBinaryProtocol iprot = new TBinaryProtocol(new TMemoryInputTransport(responseBytes)); try { if (InternalScribeCodec.readLogResponse(0, iprot)) { return Future.Void(); } else { return Future.exception(new IllegalStateException("try later")); } } catch (Exception e) { return Future.exception(e); } }
/** * Deserialize the given byte array into any type of Thrift tokens * This method avoid an explicit cast on the deserialized token * @param base The Thrift instance * @param bytes the serialized thrift token */ private void deserializeThriftToken(TBase<?, ?> base, byte[] bytes) throws TException { // Thrift deserialization TMemoryInputTransport trans_ = new TMemoryInputTransport(); TProtocol protocol_ = new TCompactProtocol.Factory().getProtocol(trans_); try { trans_.reset(bytes); // TRASH THE 8 fist bytes (SIP HASH) trans_.consumeBuffer(8); base.read(protocol_); } finally { trans_.clear(); protocol_.reset(); } }
static AccessControlEntry deserialize(String zkPath, byte[] data) throws IOException { if (data.length == 0) { return DEFAULT_ACCESS_CONTROL_ENTRY; } AccessControlEntry ace = new AccessControlEntry(); TMemoryInputTransport transport = new TMemoryInputTransport(data); TJSONProtocol protocol = new TJSONProtocol(transport); try { ace.read(protocol); } catch (TException e) { throw new CorruptedAccessControlException(zkPath, e); } return ace; }
public static Message parseMessage(byte[] data) throws TException { Message msg = new Message(); TMemoryInputTransport transport = new TMemoryInputTransport(data); TBinaryProtocol protocol = new TBinaryProtocol(transport); msg.read(protocol); return msg; }
private void processError(TProtocolFactory protocolFactory, MessageTransalator translator, TException e) throws Exception { byte[] thriftBody = translator.processError(e); ExternalTestService.Client externalServiceClient = new ExternalTestService.Client( protocolFactory.getProtocol(new TMemoryInputTransport(thriftBody)), protocolFactory.getProtocol(new TMemoryBufferWithLength(1024)) ); externalServiceClient.send_getSomeData( new AuthToken().setToken("token").setChecksum(128), new RequestData().setSomeStringField("somevalue").setSomeIntField(8) ); externalServiceClient.recv_getSomeData(); }
@Test public void shouldCreateService() { TestService service = thrift.create(TestService.class, new Thrift.SimpleTProtocolFactory() { @Override public TProtocol get() { return new TBinaryProtocol(new TMemoryInputTransport(new byte[]{})); } }); assertThat(service).isNotNull(); }
@Nonnull public static <T extends TBase<?, ?>> T load(@Nonnull T object, @Nonnull byte[] data, @Nonnegative int off, @Nonnegative int len) throws TException { TTransport transport = new TMemoryInputTransport(data, off, len); TProtocol protocol = new TCompactProtocol(transport); object.read(protocol); return object; }
@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<>(); }
@BeforeMethod public void before() { outputMemoryBuf = new TMemoryBuffer(0); TBinaryProtocol outputBinaryProtocol = new TBinaryProtocol(new RememberingTransport(outputMemoryBuf)); outputIntegrityValidatingProtocol = new IntegrityCheckingProtocol(outputBinaryProtocol, MAC_KEYS); inputMemoryTrans = new TMemoryInputTransport(); TBinaryProtocol inputBinaryProtocol = new TBinaryProtocol(new RememberingTransport(inputMemoryTrans)); inputIntegrityValidatingProtocol = new IntegrityCheckingProtocol(inputBinaryProtocol, MAC_KEYS); }
public ResultCode getResult() throws org.apache.thrift.TException { if (getState() != org.apache.thrift.async.TAsyncMethodCall.State.RESPONSE_READ) { throw new IllegalStateException("Method call not finished!"); } TMemoryInputTransport memoryTransport = new TMemoryInputTransport(getFrameBuffer().array()); TProtocol prot = client.getProtocolFactory().getProtocol(memoryTransport); return (new Client(prot)).recv_Log(); }
public static <T extends TBase> void testDeserialization(TProtocolFactory factory, T object, Class<T> klass) throws Exception { TMemoryBuffer buf = new TMemoryBuffer(0); object.write(factory.getProtocol(buf)); byte[] serialized = new byte[100*1024]; buf.read(serialized, 0, 100*1024); long startTime = System.currentTimeMillis(); for (int i = 0; i < HOW_MANY; i++) { T o2 = klass.newInstance(); o2.read(factory.getProtocol(new TMemoryInputTransport(serialized))); } long endTime = System.currentTimeMillis(); System.out.println("Deserialization test time: " + (endTime - startTime) + " ms"); }
public String getResult() throws TException { if (getState() != TAsyncMethodCall.State.RESPONSE_READ) throw new IllegalStateException("Method call not finished !"); TProtocol prot = client.getProtocolFactory().getProtocol(new TMemoryInputTransport(getFrameBuffer().array())); return (new ServiceClientImpl(prot)).recvGet(); }
@Override public void deserialize(byte[] data) throws IOException { BKDLConfigFormat configFormat = new BKDLConfigFormat(); TMemoryInputTransport transport = new TMemoryInputTransport(data); TJSONProtocol protocol = new TJSONProtocol(transport); try { configFormat.read(protocol); } catch (TException e) { throw new IOException("Failed to deserialize data '" + new String(data, UTF_8) + "' : ", e); } // bookkeeper cluster settings if (configFormat.isSetBkZkServers()) { bkZkServersForWriter = configFormat.getBkZkServers(); } if (configFormat.isSetBkZkServersForReader()) { bkZkServersForReader = configFormat.getBkZkServersForReader(); } else { bkZkServersForReader = bkZkServersForWriter; } if (configFormat.isSetBkLedgersPath()) { bkLedgersPath = configFormat.getBkLedgersPath(); } // dl zookeeper cluster settings if (configFormat.isSetDlZkServersForWriter()) { dlZkServersForWriter = configFormat.getDlZkServersForWriter(); } if (configFormat.isSetDlZkServersForReader()) { dlZkServersForReader = configFormat.getDlZkServersForReader(); } else { dlZkServersForReader = dlZkServersForWriter; } // dl settings sanityCheckTxnID = !configFormat.isSetSanityCheckTxnID() || configFormat.isSanityCheckTxnID(); encodeRegionID = configFormat.isSetEncodeRegionID() && configFormat.isEncodeRegionID(); if (configFormat.isSetAclRootPath()) { aclRootPath = configFormat.getAclRootPath(); } if (configFormat.isSetFirstLogSegmentSeqNo()) { firstLogSegmentSeqNo = configFormat.getFirstLogSegmentSeqNo(); } isFederatedNamespace = configFormat.isSetFederatedNamespace() && configFormat.isFederatedNamespace(); // Validate the settings if (null == bkZkServersForWriter || null == bkZkServersForReader || null == bkLedgersPath || null == dlZkServersForWriter || null == dlZkServersForReader) { throw new IOException("Missing zk/bk settings in BKDL Config : " + new String(data, UTF_8)); } }
private TProtocol createProtocol(byte[] thriftBody) { return protocolFactory.getProtocol(new TMemoryInputTransport(thriftBody)); }
public boolean process(final TProtocol inProt, final TProtocol outProt) throws TException { // populating request context ReqContext req_context = ReqContext.context(); TTransport trans = inProt.getTransport(); if (trans instanceof TMemoryInputTransport) { try { req_context.setRemoteAddress(InetAddress.getLocalHost()); } catch (UnknownHostException e) { throw new RuntimeException(e); } } else if (trans instanceof TSocket) { TSocket tsocket = (TSocket) trans; // remote address Socket socket = tsocket.getSocket(); req_context.setRemoteAddress(socket.getInetAddress()); } // anonymous user Subject s = getDefaultSubject(); if (s == null) { final String user = (String) storm_conf.get("debug.simple.transport.user"); if (user != null) { HashSet<Principal> principals = new HashSet<Principal>(); principals.add(new Principal() { public String getName() { return user; } public String toString() { return user; } }); s = new Subject(true, principals, new HashSet<Object>(), new HashSet<Object>()); } } req_context.setSubject(s); // invoke service handler return wrapped.process(inProt, outProt); }
private void handle(ClientRequestContext ctx, int seqId, DefaultRpcResponse reply, ThriftFunction func, HttpData content) throws TException { if (func.isOneWay()) { handleSuccess(ctx, reply, null, null); return; } if (content.isEmpty()) { throw new TApplicationException(TApplicationException.MISSING_RESULT); } final TMemoryInputTransport inputTransport = new TMemoryInputTransport(content.array(), content.offset(), content.length()); final TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport); final TMessage header = inputProtocol.readMessageBegin(); final TApplicationException appEx = readApplicationException(seqId, func, inputProtocol, header); if (appEx != null) { handleException(ctx, reply, new ThriftReply(header, appEx), appEx); return; } TBase<?, ?> result = func.newResult(); result.read(inputProtocol); inputProtocol.readMessageEnd(); final ThriftReply rawResponseContent = new ThriftReply(header, result); for (TFieldIdEnum fieldIdEnum : func.exceptionFields()) { if (ThriftFieldAccess.isSet(result, fieldIdEnum)) { final TException cause = (TException) ThriftFieldAccess.get(result, fieldIdEnum); handleException(ctx, reply, rawResponseContent, cause); return; } } final TFieldIdEnum successField = func.successField(); if (successField == null) { // void method handleSuccess(ctx, reply, null, rawResponseContent); return; } if (ThriftFieldAccess.isSet(result, successField)) { final Object returnValue = ThriftFieldAccess.get(result, successField); handleSuccess(ctx, reply, returnValue, rawResponseContent); return; } handleException( ctx, reply, rawResponseContent, new TApplicationException(TApplicationException.MISSING_RESULT, result.getClass().getName() + '.' + successField.getFieldName())); }
@Override protected TProtocol initialValue() { return protoFactory.getProtocol(new TMemoryInputTransport()); }
ChunkHeaderTBaseDeserializer(TProtocolFactory protocolFactory, TBaseLocator locator) { this.trans = new TMemoryInputTransport(); this.protocol = protocolFactory.getProtocol(trans); this.locator = locator; }
/** * Create a new TDeserializer. It will use the TProtocol specified by the * factory that is passed in. * * @param protocolFactory Factory to create a protocol */ public TDeserializer(TProtocolFactory protocolFactory) { trans_ = new TMemoryInputTransport(); protocol_ = protocolFactory.getProtocol(trans_); }
/** * Create a new TDeserializer. It will use the TProtocol specified by the * factory that is passed in. * * @param protocolFactory Factory to create a protocol */ HeaderTBaseDeserializer(TProtocolFactory protocolFactory, TBaseLocator locator) { this.trans = new TMemoryInputTransport(); this.protocol = protocolFactory.getProtocol(trans); this.locator = locator; }
/** * Wrap the read buffer in a memory-based transport so a processor can read * the data it needs to handle an invocation. */ private TTransport getInputTransport() { return new TMemoryInputTransport(buffer_.array()); }