All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.spanner.spi.v1.EncodingInterceptor Maven / Gradle / Ivy

There is a newer version: 6.81.1
Show newest version
/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.cloud.spanner.spi.v1;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.Metadata.Key;
import io.grpc.MethodDescriptor;

class EncodingInterceptor implements ClientInterceptor {
  private static final String RESPONSE_ENCODING_KEY_NAME = "x-response-encoding";
  private static final Key RESPONSE_ENCODING_KEY =
      Metadata.Key.of(RESPONSE_ENCODING_KEY_NAME, Metadata.ASCII_STRING_MARSHALLER);

  private final String encoding;

  EncodingInterceptor(String encoding) {
    this.encoding = encoding;
  }

  @Override
  public  ClientCall interceptCall(
      MethodDescriptor method, CallOptions callOptions, Channel next) {
    return new ForwardingClientCall.SimpleForwardingClientCall(
        next.newCall(method, callOptions)) {
      @Override
      public void start(Listener responseListener, Metadata headers) {
        headers.put(RESPONSE_ENCODING_KEY, encoding);
        super.start(responseListener, headers);
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy