All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.turbospaces.grpc.GrpcServerInterpector Maven / Gradle / Ivy

There is a newer version: 2.0.33
Show newest version
package com.turbospaces.grpc;

import io.grpc.Context;
import io.grpc.Contexts;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCall.Listener;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;

public class GrpcServerInterpector implements ServerInterceptor {
    @Override
    public  Listener interceptCall(
            ServerCall call,
            Metadata meta,
            ServerCallHandler next) {
        String messageId = meta.get(GrpcHeaders.X_MESSAGE_ID);
        String topic = meta.get(GrpcHeaders.X_TOPIC);
        long timestamp = Long.parseLong(meta.get(GrpcHeaders.X_TIMESTAMP));

        Context context = Context.current()
                .withValue(GrpcHeaders.CONTEXT_MESSAGE_ID, messageId)
                .withValue(GrpcHeaders.CONTEXT_TOPIC, topic)
                .withValue(GrpcHeaders.CONTEXT_TIMESTAMP, timestamp);

        if (meta.containsKey(GrpcHeaders.X_TRACE_ID)) {
            String traceId = meta.get(GrpcHeaders.X_TRACE_ID);
            context = context.withValue(GrpcHeaders.CONTEXT_TRACE_ID, traceId);
        }
        if (meta.containsKey(GrpcHeaders.X_ROUTING_KEY)) {
            String routingKey = meta.get(GrpcHeaders.X_ROUTING_KEY);
            context = context.withValue(GrpcHeaders.CONTEXT_ROUTING_KEY, routingKey);
        }

        return Contexts.interceptCall(context, call, meta, next);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy