Java 类io.grpc.auth.ClientAuthInterceptor 实例源码

项目:pubsub    文件:ConnectorUtils.java   
/** Return {@link io.grpc.Channel} which is used by Cloud Pub/Sub gRPC API's. */
public static Channel getChannel() throws IOException {
  ManagedChannel channelImpl =
      NettyChannelBuilder.forAddress(ENDPOINT, 443).negotiationType(NegotiationType.TLS).build();
  final ClientAuthInterceptor interceptor =
      new ClientAuthInterceptor(
          GoogleCredentials.getApplicationDefault().createScoped(CPS_SCOPE),
          Executors.newCachedThreadPool());
  return ClientInterceptors.intercept(channelImpl, interceptor);
}
项目:cloud-pubsub-samples-java    文件:Main.java   
public static void main(final String[] args) throws Exception {

        if (args.length == 0) {
            System.err.println("Please specify your project name.");
            System.exit(1);
        }
        final String project = args[0];
        ManagedChannelImpl channelImpl = NettyChannelBuilder
            .forAddress("pubsub.googleapis.com", 443)
            .negotiationType(NegotiationType.TLS)
            .build();
        GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
        // Down-scope the credential to just the scopes required by the service
        creds = creds.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
        // Intercept the channel to bind the credential
        ExecutorService executor = Executors.newSingleThreadExecutor();
        ClientAuthInterceptor interceptor = new ClientAuthInterceptor(creds, executor);
        Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
        // Create a stub using the channel that has the bound credential
        PublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
        ListTopicsRequest request = ListTopicsRequest.newBuilder()
                .setPageSize(10)
                .setProject("projects/" + project)
                .build();
        ListTopicsResponse resp = publisherStub.listTopics(request);
        System.out.println("Found " + resp.getTopicsCount() + " topics.");
        for (Topic topic : resp.getTopicsList()) {
            System.out.println(topic.getName());
        }
    }
项目:utils-java    文件:GenomicsChannel.java   
/**
 * Create a new gRPC channel to the Google Genomics API, using the provided credentials for auth.
 *
 * @param creds The credential.
 * @param fields Which fields to return in the partial response, or null for none.
 * @return The ManagedChannel.
 * @throws SSLException
 */
public static ManagedChannel fromCreds(GoogleCredentials creds, String fields) throws SSLException {
  List<ClientInterceptor> interceptors = new ArrayList();
  interceptors.add(new ClientAuthInterceptor(creds.createScoped(Arrays.asList(GENOMICS_SCOPE)),
      Executors.newSingleThreadExecutor()));
  if (!Strings.isNullOrEmpty(fields)) {
    Metadata headers = new Metadata();
    Metadata.Key<String> partialResponseHeader =
        Metadata.Key.of(PARTIAL_RESPONSE_HEADER, Metadata.ASCII_STRING_MARSHALLER);
    headers.put(partialResponseHeader, fields);
    interceptors.add(MetadataUtils.newAttachHeadersInterceptor(headers));
  }
  return getGenomicsManagedChannel(interceptors);
}
项目:polyglot    文件:ChannelFactory.java   
public Channel createChannelWithCredentials(HostAndPort endpoint, Credentials credentials) {
  return ClientInterceptors.intercept(
      createChannel(endpoint), new ClientAuthInterceptor(credentials, authExecutor));
}
项目:beam    文件:PubsubGrpcClient.java   
/**
 * Return channel with interceptor for returning credentials.
 */
private Channel newChannel() throws IOException {
  checkState(publisherChannel != null, "PubsubGrpcClient has been closed");
  ClientAuthInterceptor interceptor =
      new ClientAuthInterceptor(credentials, Executors.newSingleThreadExecutor());
  return ClientInterceptors.intercept(publisherChannel, interceptor);
}
项目:cloud-bigtable-client    文件:BigtableChannels.java   
private static CloseableChannel wrapChannel(ChannelOptions channelOptions,
    ExecutorService executor, Channel channel, ClientCloseHandler onClientClose) {
  List<ClientInterceptor> interceptors = new ArrayList<>();
  if (channelOptions.getCredential() != null) {
    interceptors.add(new ClientAuthInterceptor(channelOptions.getCredential(), executor));
  }

  if (channelOptions.getAuthority() != null) {
    Metadata.Headers headers = new Metadata.Headers();
    headers.setAuthority(channelOptions.getAuthority());
    interceptors.add(MetadataUtils.newAttachHeadersInterceptor(headers));
  }

  CallCompletionStatusInterceptor preRetryCallStatusInterceptor = null;
  if (!Strings.isNullOrEmpty(channelOptions.getCallStatusReportPath())) {
    preRetryCallStatusInterceptor = new CallCompletionStatusInterceptor();
    interceptors.add(preRetryCallStatusInterceptor);
  }

  if (!interceptors.isEmpty()) {
    channel = ClientInterceptors.intercept(channel, interceptors);
    interceptors.clear();
  }

  if (channelOptions.getUnaryCallRetryOptions().enableRetries()) {
    ScheduledExecutorService scheduledRetries;
    if (channelOptions.getScheduledExecutorService() != null) {
      scheduledRetries = channelOptions.getScheduledExecutorService();
    } else {
      scheduledRetries = createScheduledRetryPool();

      onClientClose = createChainedCloseHandler(
          onClientClose, createExecutorCloseHandler(scheduledRetries));
    }

    RetryOptions unaryCallRetryOptions = channelOptions.getUnaryCallRetryOptions();
    channel = new UnaryCallRetryInterceptor(
        channel,
        scheduledRetries,
        METHODS_TO_RETRY_MAP,
        unaryCallRetryOptions.getInitialBackoffMillis(),
        unaryCallRetryOptions.getBackoffMultiplier(),
        unaryCallRetryOptions.getMaxElaspedBackoffMillis());
  }

  if (!Strings.isNullOrEmpty(channelOptions.getCallStatusReportPath())) {
    CallCompletionStatusInterceptor postRetryCallStatusInterceptor =
        new CallCompletionStatusInterceptor();

    registerCallStatusReportingShutdownHook(
        channelOptions.getCallStatusReportPath(),
        preRetryCallStatusInterceptor,
        postRetryCallStatusInterceptor);

    channel = ClientInterceptors.intercept(channel, postRetryCallStatusInterceptor);
  }

  return createCloseableChannel(channel, onClientClose);
}