Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access message request in gRPC interceptor?

I'd like to user the same interceptor on server side for multiple methods as all of them have the same request type (StateRequest):

rpc apply (StateRequest) returns (StateResponse) {} 
rpc cancel (StateRequest) returns (StateResponse) {}
rpc remove (StateRequest) returns (StateResponse) {}
rpc development (StateRequest) returns (StateResponse) {}
rpc implement (StateRequest) returns (StateResponse) {}
rpc draft (StateRequest) returns (StateResponse) {}

interceptor:

public class ServerUserRoleAuthInterceptor implements ServerInterceptor {

    @Override
    public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
        ServerCall<ReqT, RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next) {
        String callName = call.getMethodDescriptor().getFullMethodName();
        logger.info("Call: {}", callName);

        // TODO: validate request (`StateRequest` instance)

        return next.startCall(call, headers);
    }
}

How can i do it?

like image 368
4ntoine Avatar asked Dec 19 '22 02:12

4ntoine


1 Answers

You need to observe the onMessage(ReqT) call-back on the Listener.

return new SimpleForwardingServerCallListener<ReqT>(next.startCall(call, headers)) {
  @Override 
  public void onMessage(ReqT request) {
    // TODO:validate request
    super.onMessage(request);
  }
};
like image 87
Eric Anderson Avatar answered Jan 05 '23 00:01

Eric Anderson