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

com.tigrisdata.db.client.AbstractTigrisDBClient Maven / Gradle / Ivy

package com.tigrisdata.db.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tigrisdata.db.client.auth.AuthorizationToken;
import com.tigrisdata.db.client.config.TigrisConfiguration;
import com.tigrisdata.tools.schema.core.ModelToJsonSchema;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

abstract class AbstractTigrisDBClient {
  protected final ManagedChannel channel;
  protected final ObjectMapper objectMapper;
  protected final ModelToJsonSchema modelToJsonSchema;

  private static final Metadata.Key USER_AGENT_KEY =
      Metadata.Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER);
  private static final Metadata.Key CLIENT_VERSION_KEY =
      Metadata.Key.of("client-version", Metadata.ASCII_STRING_MARSHALLER);
  private static final Metadata.Key INTENDED_DESTINATION_NAME =
      Metadata.Key.of("destination-name", Metadata.ASCII_STRING_MARSHALLER);
  private static final String USER_AGENT_VALUE = "tigrisdb-client-java.grpc";
  private static final String CLIENT_VERSION_VALUE = "1.0";
  private static final Logger log = LoggerFactory.getLogger(AbstractTigrisDBClient.class);

  protected AbstractTigrisDBClient(
      TigrisConfiguration configuration,
      Optional authorizationToken,
      ModelToJsonSchema modelToJsonSchema) {

    ManagedChannelBuilder channelBuilder =
        ManagedChannelBuilder.forTarget(configuration.getServerURL())
            .intercept(MetadataUtils.newAttachHeadersInterceptor(getDefaultHeaders(configuration)));
    if (authorizationToken.isPresent()) {
      channelBuilder.intercept(new AuthHeaderInterceptor(authorizationToken.get()));
    }
    if (configuration.getNetwork().isUsePlainText()) {
      log.warn(
          "Client is configured to use plaintext communication. It is advised to not use plaintext communication");
      channelBuilder.usePlaintext();
    }
    this.channel = channelBuilder.build();
    this.objectMapper = configuration.getObjectMapper();
    this.modelToJsonSchema = modelToJsonSchema;
  }

  protected AbstractTigrisDBClient(
      AuthorizationToken authorizationToken,
      TigrisConfiguration configuration,
      ManagedChannelBuilder managedChannelBuilder,
      ModelToJsonSchema modelToJsonSchema) {
    this.channel =
        managedChannelBuilder
            .intercept(new AuthHeaderInterceptor(authorizationToken))
            .intercept(MetadataUtils.newAttachHeadersInterceptor(getDefaultHeaders(configuration)))
            .build();
    this.objectMapper = configuration.getObjectMapper();
    this.modelToJsonSchema = modelToJsonSchema;
  }

  private static Metadata getDefaultHeaders(TigrisConfiguration configuration) {
    Metadata defaultHeaders = new Metadata();
    defaultHeaders.put(USER_AGENT_KEY, USER_AGENT_VALUE);
    defaultHeaders.put(CLIENT_VERSION_KEY, CLIENT_VERSION_VALUE);
    defaultHeaders.put(INTENDED_DESTINATION_NAME, configuration.getServerURL());
    return defaultHeaders;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy