Skip to content

Instantly share code, notes, and snippets.

@nandak522
Created October 30, 2018 16:36
Show Gist options
  • Save nandak522/86f3844894ff86c66fd77529568c5b40 to your computer and use it in GitHub Desktop.
Save nandak522/86f3844894ff86c66fd77529568c5b40 to your computer and use it in GitHub Desktop.
package example.grpc.clients.testing_service;
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;
public class HeaderClientInterceptor implements ClientInterceptor {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
if (headers != null) {
Metadata fixedHeaders = new Metadata();
Metadata.Key<String> key = Metadata.Key.of("header", Metadata.ASCII_STRING_MARSHALLER);
fixedHeaders.put(key, "hiiii");
headers.merge(fixedHeaders);
}
super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onHeaders(Metadata headers) {
super.onHeaders(headers);
}
}, headers);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment