5

I am using gRPC and I need to get the name of the service of the request from the ServerInterceptor but it seems it is not possible.

Basically from an implementation of ServerInterceptor I need to know the name of the ServiceGrpc (as a string) that will be invoked.

public class PermissionInterceptor implements ServerInterceptor {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
                ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> handler
        ) {
            // here I need the name of RPC service that has been requested

            return handler.startCall(serverCall, metadata);
        }
}

Please note that I have tried with serverCall.getMethodDescriptor() but it returns the name of the proto service rather than the real name of the (java) service.

It returns:

co.test.domain.transaction.TransactionService

But I need this:

co.test.domain.transaction.TransactionNeoBasicImpl

Thanks

db80
  • 4,157
  • 1
  • 38
  • 38

2 Answers2

3

There is a way to do this.

String fullMethodName = serverCall.getMethodDescriptor().getFullMethodName();
String serviceName = MethodDescriptor.extractFullServiceName(fullMethodName);

Basically fullMethodName has the fully qualified service name as prefix (followed by a '/'). Something like this fullyqualified.ServiceName/MethodName

1

That is not possible.

If your interceptor is the last one before the application, you may be able to inspect handler's class and see its parent class' name. But that's very limited and not necessarily supported.

Eric Anderson
  • 24,057
  • 5
  • 55
  • 76
  • thanks for the answer. So there's no way to read the annotations present in the TransactionNeoBasicImpl class from the ServerInterceptor. Am I correct? – db80 Feb 16 '18 at 16:54
  • Correct. You can't from an interceptor without manual plumbing. – Eric Anderson Feb 17 '18 at 17:20