Example usage for org.apache.thrift.transport TMemoryInputTransport clear

List of usage examples for org.apache.thrift.transport TMemoryInputTransport clear

Introduction

In this page you can find the example usage for org.apache.thrift.transport TMemoryInputTransport clear.

Prototype

public void clear() 

Source Link

Usage

From source file:com.linecorp.armeria.server.thrift.THttpService.java

License:Apache License

private void decodeAndInvoke(ServiceRequestContext ctx, AggregatedHttpMessage req,
        SerializationFormat serializationFormat, HttpResponseWriter res) {

    final TProtocol inProto = FORMAT_TO_THREAD_LOCAL_INPUT_PROTOCOL.get(serializationFormat).get();
    inProto.reset();//  w w w. ja  v  a  2 s .  com
    final TMemoryInputTransport inTransport = (TMemoryInputTransport) inProto.getTransport();
    final HttpData content = req.content();
    inTransport.reset(content.array(), content.offset(), content.length());

    final int seqId;
    final ThriftFunction f;
    final RpcRequest decodedReq;

    try {
        final TMessage header;
        final TBase<TBase<?, ?>, TFieldIdEnum> args;

        try {
            header = inProto.readMessageBegin();
        } catch (Exception e) {
            logger.debug("{} Failed to decode Thrift header:", ctx, e);
            res.respond(HttpStatus.BAD_REQUEST, MediaType.PLAIN_TEXT_UTF_8,
                    "Failed to decode Thrift header: " + Throwables.getStackTraceAsString(e));
            return;
        }

        seqId = header.seqid;

        final byte typeValue = header.type;
        final int colonIdx = header.name.indexOf(':');
        final String serviceName;
        final String methodName;
        if (colonIdx < 0) {
            serviceName = "";
            methodName = header.name;
        } else {
            serviceName = header.name.substring(0, colonIdx);
            methodName = header.name.substring(colonIdx + 1);
        }

        // Basic sanity check. We usually should never fail here.
        if (typeValue != TMessageType.CALL && typeValue != TMessageType.ONEWAY) {
            final TApplicationException cause = new TApplicationException(
                    TApplicationException.INVALID_MESSAGE_TYPE,
                    "unexpected TMessageType: " + typeString(typeValue));

            handlePreDecodeException(ctx, res, cause, serializationFormat, seqId, methodName);
            return;
        }

        // Ensure that such a method exists.
        final ThriftServiceEntry entry = entries().get(serviceName);
        f = entry != null ? entry.metadata.function(methodName) : null;
        if (f == null) {
            final TApplicationException cause = new TApplicationException(TApplicationException.UNKNOWN_METHOD,
                    "unknown method: " + header.name);

            handlePreDecodeException(ctx, res, cause, serializationFormat, seqId, methodName);
            return;
        }

        // Decode the invocation parameters.
        try {
            if (f.isAsync()) {
                AsyncProcessFunction<Object, TBase<TBase<?, ?>, TFieldIdEnum>, Object> asyncFunc = f
                        .asyncFunc();

                args = asyncFunc.getEmptyArgsInstance();
                args.read(inProto);
                inProto.readMessageEnd();
            } else {
                ProcessFunction<Object, TBase<TBase<?, ?>, TFieldIdEnum>> syncFunc = f.syncFunc();

                args = syncFunc.getEmptyArgsInstance();
                args.read(inProto);
                inProto.readMessageEnd();
            }

            decodedReq = toRpcRequest(f.serviceType(), header.name, args);
            ctx.logBuilder().requestContent(decodedReq, new ThriftCall(header, args));
        } catch (Exception e) {
            // Failed to decode the invocation parameters.
            logger.debug("{} Failed to decode Thrift arguments:", ctx, e);

            final TApplicationException cause = new TApplicationException(TApplicationException.PROTOCOL_ERROR,
                    "failed to decode arguments: " + e);

            handlePreDecodeException(ctx, res, cause, serializationFormat, seqId, methodName);
            return;
        }
    } finally {
        inTransport.clear();
        ctx.logBuilder().requestContent(null, null);
    }

    invoke(ctx, serializationFormat, seqId, f, decodedReq, res);
}

From source file:io.warp10.quasar.encoder.QuasarTokenDecoder.java

License:Apache License

/**
 * 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//  www. j a  v  a2  s.  co  m
 * @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();
    }
}