cloud.prefab.client.ClientAuthenticationInterceptor Maven / Gradle / Ivy
package cloud.prefab.client;
import io.grpc.*;
public class ClientAuthenticationInterceptor implements ClientInterceptor {
public static final Metadata.Key CUSTOM_HEADER_KEY =
Metadata.Key.of("auth", Metadata.ASCII_STRING_MARSHALLER);
public static final Metadata.Key CLIENT_HEADER_KEY =
Metadata.Key.of("client", Metadata.ASCII_STRING_MARSHALLER);
private String apikey;
public ClientAuthenticationInterceptor(String apikey) {
this.apikey = apikey;
}
@Override
public ClientCall interceptCall(MethodDescriptor methodDescriptor, CallOptions callOptions, Channel channel) {
return new ForwardingClientCall.SimpleForwardingClientCall(channel.newCall(methodDescriptor, callOptions)) {
@Override
public void start(Listener responseListener, Metadata headers) {
headers.put(CLIENT_HEADER_KEY, "prefab-cloud-java.0.1.2");
headers.put(CUSTOM_HEADER_KEY, apikey);
super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener(responseListener) {
}, headers);
}
};
}
}