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

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

Introduction

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

Prototype

public void reset(byte[] buf, int offset, int length) 

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 va 2s.  c om
    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);
}