Java Code Examples for io.grpc.Metadata#merge()

The following examples show how to use io.grpc.Metadata#merge() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpeechService.java    From black-mirror with MIT License 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions,
        final Channel next) {
    return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
            next.newCall(method, callOptions)) {
        @Override
        protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
                throws StatusException {
            Metadata cachedSaved;
            URI uri = serviceUri(next, method);
            synchronized (this) {
                Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
                if (mLastMetadata == null || mLastMetadata != latestMetadata) {
                    mLastMetadata = latestMetadata;
                    mCached = toHeaders(mLastMetadata);
                }
                cachedSaved = mCached;
            }
            headers.merge(cachedSaved);
            delegate().start(responseListener, headers);
        }
    };
}
 
Example 2
Source File: GoogleCredentialsInterceptor.java    From Saiy-PS with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method,
                                                           CallOptions callOptions, final Channel next) {
    return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
            next.newCall(method, callOptions)) {
        @Override
        protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
                throws StatusException {

            Metadata cachedSaved;
            URI uri = serviceUri(next, method);
            synchronized (GoogleCredentialsInterceptor.this) {
                Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
                if (mLastMetadata == null || mLastMetadata != latestMetadata) {
                    mLastMetadata = latestMetadata;
                    mCached = toHeaders(mLastMetadata);
                }
                cachedSaved = mCached;
            }
            headers.merge(cachedSaved);
            delegate().start(responseListener, headers);
        }
    };
}
 
Example 3
Source File: SpeechService.java    From android-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions,
        final Channel next) {
    return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
            next.newCall(method, callOptions)) {
        @Override
        protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
                throws StatusException {
            Metadata cachedSaved;
            URI uri = serviceUri(next, method);
            synchronized (this) {
                Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
                if (mLastMetadata == null || mLastMetadata != latestMetadata) {
                    mLastMetadata = latestMetadata;
                    mCached = toHeaders(mLastMetadata);
                }
                cachedSaved = mCached;
            }
            headers.merge(cachedSaved);
            delegate().start(responseListener, headers);
        }
    };
}
 
Example 4
Source File: AltusMetadataInterceptor.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public <R, S> ClientCall<R, S> interceptCall(
        MethodDescriptor<R, S> method,
        CallOptions callOptions,
        Channel next) {
    return new SimpleForwardingClientCall<R, S>(
            next.newCall(method, callOptions)) {
        @Override
        public void start(
                Listener<S> responseListener,
                Metadata headers) {
            Metadata metadata = new Metadata();
            metadata.put(REQUEST_ID_METADATA_KEY, requestId);
            metadata.put(ACTOR_CRN_METADATA_KEY, actorCrn);
            headers.merge(metadata);
            super.start(responseListener, headers);
        }
    };
}
 
Example 5
Source File: CompressionTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeaders(Metadata headers) {
  super.onHeaders(headers);
  Metadata headersCopy = new Metadata();
  headersCopy.merge(headers);
  clientResponseHeaders = headersCopy;
}
 
Example 6
Source File: ClientAuthInterceptor.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
  // TODO(ejona86): If the call fails for Auth reasons, this does not properly propagate info that
  // would be in WWW-Authenticate, because it does not yet have access to the header.
  return new CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
    @Override
    protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
        throws StatusException {
      Metadata cachedSaved;
      URI uri = serviceUri(next, method);
      synchronized (ClientAuthInterceptor.this) {
        // TODO(louiscryan): This is icky but the current auth library stores the same
        // metadata map until the next refresh cycle. This will be fixed once
        // https://github.com/google/google-auth-library-java/issues/3
        // is resolved.
        // getRequestMetadata() may return a different map based on the provided URI, i.e., for
        // JWT. However, today it does not cache JWT and so we won't bother tring to cache its
        // return value based on the URI.
        Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
        if (lastMetadata == null || lastMetadata != latestMetadata) {
          lastMetadata = latestMetadata;
          cached = toHeaders(lastMetadata);
        }
        cachedSaved = cached;
      }
      headers.merge(cachedSaved);
      delegate().start(responseListener, headers);
    }
  };
}
 
Example 7
Source File: RetriableStream.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/** Adds grpc-previous-rpc-attempts in the headers of a retry/hedging RPC. */
@VisibleForTesting
final Metadata updateHeaders(
    Metadata originalHeaders, int previousAttempts) {
  Metadata newHeaders = new Metadata();
  newHeaders.merge(originalHeaders);
  if (previousAttempts > 0) {
    newHeaders.put(GRPC_PREVIOUS_RPC_ATTEMPTS, String.valueOf(previousAttempts));
  }
  return newHeaders;
}
 
Example 8
Source File: CompressionTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> io.grpc.ServerCall.Listener<ReqT> interceptCall(
    ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
  if (serverEncoding) {
    call.setCompression("fzip");
  }
  call.setMessageCompression(enableServerMessageCompression);
  Metadata headersCopy = new Metadata();
  headersCopy.merge(headers);
  serverResponseHeaders = headersCopy;
  return next.startCall(call, headers);
}
 
Example 9
Source File: ErrorResponses.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public static Pair<Status, Metadata> of(Status status, Metadata trailers, boolean debug) {
    Throwable cause = unwrap(status.getCause());
    if (cause == null) {
        return Pair.of(status, trailers);
    }
    Status newStatus = toGrpcStatus(cause)
            .withDescription(getNonNullMessage(cause))
            .withCause(cause);

    Metadata metadata = buildMetadata(newStatus.getCause(), newStatus.getCode().value(), debug);
    metadata.merge(trailers);
    return Pair.of(newStatus, metadata);
}
 
Example 10
Source File: GrpcExceptionMapper.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public Pair<Status, Metadata> of(Status status, Metadata trailers, boolean debug) {
    Throwable cause = unwrap(status.getCause());
    if (cause == null) {
        return Pair.of(status, trailers);
    }
    Status newStatus = toGrpcStatus(cause)
            .withDescription(getNonNullMessage(cause))
            .withCause(cause);

    Metadata metadata = buildMetadata(newStatus.getCause(), newStatus.getCode().value(), debug);
    metadata.merge(trailers);
    return Pair.of(newStatus, metadata);
}
 
Example 11
Source File: RetriableStream.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/** Adds grpc-previous-rpc-attempts in the headers of a retry/hedging RPC. */
@VisibleForTesting
final Metadata updateHeaders(
    Metadata originalHeaders, int previousAttemptCount) {
  Metadata newHeaders = new Metadata();
  newHeaders.merge(originalHeaders);
  if (previousAttemptCount > 0) {
    newHeaders.put(GRPC_PREVIOUS_RPC_ATTEMPTS, String.valueOf(previousAttemptCount));
  }
  return newHeaders;
}
 
Example 12
Source File: ClientAuthInterceptor.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
  // TODO(ejona86): If the call fails for Auth reasons, this does not properly propagate info that
  // would be in WWW-Authenticate, because it does not yet have access to the header.
  return new CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
    @Override
    protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
        throws StatusException {
      Metadata cachedSaved;
      URI uri = serviceUri(next, method);
      synchronized (ClientAuthInterceptor.this) {
        // TODO(louiscryan): This is icky but the current auth library stores the same
        // metadata map until the next refresh cycle. This will be fixed once
        // https://github.com/google/google-auth-library-java/issues/3
        // is resolved.
        // getRequestMetadata() may return a different map based on the provided URI, i.e., for
        // JWT. However, today it does not cache JWT and so we won't bother tring to cache its
        // return value based on the URI.
        Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
        if (lastMetadata == null || lastMetadata != latestMetadata) {
          lastMetadata = latestMetadata;
          cached = toHeaders(lastMetadata);
        }
        cachedSaved = cached;
      }
      headers.merge(cachedSaved);
      delegate().start(responseListener, headers);
    }
  };
}
 
Example 13
Source File: CompressionTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeaders(Metadata headers) {
  super.onHeaders(headers);
  Metadata headersCopy = new Metadata();
  headersCopy.merge(headers);
  clientResponseHeaders = headersCopy;
}
 
Example 14
Source File: CachingRlsLbClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private PickSubchannelArgs getApplyRlsHeader(
    PickSubchannelArgs args, CachedRouteLookupResponse response) {
  if (response.getHeaderData() == null || response.getHeaderData().isEmpty()) {
    return args;
  }

  Metadata headers = new Metadata();
  headers.merge(args.getHeaders());
  headers.put(RLS_DATA_KEY, response.getHeaderData());
  return new PickSubchannelArgsImpl(args.getMethodDescriptor(), headers, args.getCallOptions());
}
 
Example 15
Source File: CompressionTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> io.grpc.ServerCall.Listener<ReqT> interceptCall(
    ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
  if (serverEncoding) {
    call.setCompression("fzip");
  }
  call.setMessageCompression(enableServerMessageCompression);
  Metadata headersCopy = new Metadata();
  headersCopy.merge(headers);
  serverResponseHeaders = headersCopy;
  return next.startCall(call, headers);
}
 
Example 16
Source File: MetadataUtils.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
  headers.merge(extraHeaders);
  super.start(responseListener, headers);
}
 
Example 17
Source File: TracingMetadataUtils.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static ClientInterceptor newExecHeadersInterceptor(RemoteOptions options) {
  Metadata metadata = newMetadataForHeaders(options.remoteHeaders);
  metadata.merge(newMetadataForHeaders(options.remoteExecHeaders));
  return MetadataUtils.newAttachHeadersInterceptor(metadata);
}
 
Example 18
Source File: TracingMetadataUtils.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static ClientInterceptor newDownloaderHeadersInterceptor(RemoteOptions options) {
  Metadata metadata = newMetadataForHeaders(options.remoteHeaders);
  metadata.merge(newMetadataForHeaders(options.remoteDownloaderHeaders));
  return MetadataUtils.newAttachHeadersInterceptor(metadata);
}
 
Example 19
Source File: TracingMetadataUtils.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static ClientInterceptor newCacheHeadersInterceptor(RemoteOptions options) {
  Metadata metadata = newMetadataForHeaders(options.remoteHeaders);
  metadata.merge(newMetadataForHeaders(options.remoteCacheHeaders));
  return MetadataUtils.newAttachHeadersInterceptor(metadata);
}
 
Example 20
Source File: MetadataUtils.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
  headers.merge(extraHeaders);
  super.start(responseListener, headers);
}