How to define a client interceptor ?
See original GitHub issueI have a spring-boot app and I’m trying to define a client interceptor for my grpc service, so that I can set some custom headers in the response (which will be transcoded to html)
My interceptor definition looks like this:
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
@GrpcGlobalClientInterceptor
public class HeaderClientInterceptor implements ClientInterceptor {
static final Metadata.Key<String> CUSTOM_HEADER_KEY =
Metadata.Key.of("custom_client_header_key", Metadata.ASCII_STRING_MARSHALLER);
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel next) {
System.out.println("====log====================");
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void sendMessage(ReqT message) {
System.out.println("====log send====================");
super.sendMessage(message);
}
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
headers.put(CUSTOM_HEADER_KEY, "customRequestValue");
super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onHeaders(Metadata headers) {
System.out.println("====head===================="+headers);
super.onHeaders(headers);
}
}, headers);
}
};
}
}
No matter what I’ve tried, the response doesn’t want o go through that code. Just note that I also have a ServerInterceptor define in my code, and that logic works fine.
My .proto api definition looks like:
rpc DownloadBuild(DownloadBuildRequest) returns (stream google.api.HttpBody) {
option (google.api.http) = {
get : "/v4/projects/{projectId}/types/{buildType}/builds/{buildVersion}/.download"
};
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
description: "Download build.";
summary: "Download build.";
tags: "Builds";
responses: {
key: "200"
value: {
description: "Download build";
}
}
responses: {
key: "401"
value: {
description: "Request could not be authorized";
}
}
responses: {
key: "404"
value: {
description: "Build not found";
}
}
responses: {
key: "500"
value: {
description: "Internal server error";
}
}
};
}
while my grpc implementation looks like this:
...
@GrpcService
public class GrpcAPIService extends MixAPIGrpc.MixAPIImplBase {
...
@Override
public void downloadBuild(DownloadBuildRequest request, StreamObserver<com.google.api.HttpBody> responseObserver) {
handleDownloadGrpcCall("downloadBuild", mwProxy::downloadBuild, request, responseObserver);
}
...
private <T, R> void handleDownloadGrpcCall(String grpcMethodName, Function<T, byte[]> serviceMethod, T request,
StreamObserver<com.google.api.HttpBody> streamObserver) {
log.debug("{}: >>>, request:[{}]", grpcMethodName, request);
try {
Instant start = Instant.now();
byte[] bytes = serviceMethod.apply(request);
BufferedInputStream stream = new BufferedInputStream(new ByteArrayInputStream(bytes));
int bufferSize = 1 * 1024;// 1KB
byte[] buffer = new byte[bufferSize];
int length;
boolean addExtension = true;
while ((length = stream.read(buffer, 0, bufferSize)) != -1) {
streamObserver.onNext(com.google.api.HttpBody.newBuilder()
.setData(ByteString.copyFrom(buffer, 0, length))
.setContentType("application/octet-stream")
.build());
}
stream.close();
streamObserver.onCompleted();
Instant end = Instant.now();
timingService.logMethodExecution(grpcMethodName, start, end, request);
} catch (Exception e) {
handleException(e, streamObserver, request, grpcMethodName);
}
log.debug("{}: <<<<", grpcMethodName);
}
...
Any idea what could be wrong ?
Issue Analytics
- State:
- Created 3 years ago
- Comments:13
Top Results From Across the Web
Using the Spring RestTemplate Interceptor - Baeldung
Spring RestTemplate allows us to add interceptors that implement ClientHttpRequestInterceptor interface. The intercept(HttpRequest, byte[], ...
Read more >A Guide to gRPC and Interceptors - The Edgehog Portal
Client Stream Interceptor. These serve the same purpose as the unary interceptor, only for when we are streaming data from a client to...
Read more >gRPC interceptors on .NET - Microsoft Learn
gRPC client interceptors intercept outgoing RPC invocations. They provide access to the sent request, the incoming response, and the context ...
Read more >Interceptors — An Important feature of Http Client | by jinal shah
Creating Interceptor is very simple; just declare a class that implements the intercept () method of the HttpInterceptor Interface, which can be imported ......
Read more >Chapter 7. Container and Client Interceptors
7.3. Configure a Container Interceptor · Use the urn:container-interceptors:1.0 namespace to specify configuration of container interceptor elements. · Use the < ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ST-DDT Thanks! I’ve managed to set it.
No I’m good!