如何在gRPC拦截器中访问消息请求?

时间:2017-12-17 13:46:35

标签: java rpc interceptor grpc

我想在服务器端为多个方法使用相同的拦截器 因为它们都具有相同的请求类型(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) {}

拦截器:

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);
    }
}

我该怎么做?

1 个答案:

答案 0 :(得分:7)

您需要观察onMessage(ReqT)上的Listener回电。

return new SimpleForwardingServerCallListener<ReqT>(next.startCall(call, headers)) {
  @Override 
  public void onMessage(ReqT request) {
    // TODO:validate request
    super.onMessage(request);
  }
};