How to access message request in gRPC interceptor?

折月煮酒 提交于 2021-01-18 06:26:00

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/47855763/how-to-access-message-request-in-grpc-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!