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

The following examples show how to use io.grpc.Metadata#keys() . 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: CensusModulesTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void traceHeaders_clientMissingCensusImpl_preservingHeaders() throws Exception {
  reset(spyClientSpanBuilder);
  when(spyClientSpanBuilder.startSpan()).thenReturn(BlankSpan.INSTANCE);
  Metadata headers = new Metadata();
  headers.put(
      Metadata.Key.of("never-used-key-bin", Metadata.BINARY_BYTE_MARSHALLER),
      new byte[] {});
  Set<String> originalHeaderKeys = new HashSet<String>(headers.keys());

  CensusTracingModule.ClientCallTracer callTracer =
      censusTracing.newClientCallTracer(BlankSpan.INSTANCE, method);
  callTracer.newClientStreamTracer(CallOptions.DEFAULT, headers);

  assertThat(headers.keys()).containsExactlyElementsIn(originalHeaderKeys);
}
 
Example 2
Source File: XdsRoutingLoadBalancer.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
  // Index ASCII headers by keys.
  Map<String, Set<String>> asciiHeaders = new HashMap<>();
  Metadata headers = args.getHeaders();
  for (String headerName : headers.keys()) {
    if (headerName.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
      continue;
    }
    Set<String> headerValues = new HashSet<>();
    Metadata.Key<String> key = Metadata.Key.of(headerName, Metadata.ASCII_STRING_MARSHALLER);
    for (String value : headers.getAll(key)) {
      headerValues.add(value);
    }
    asciiHeaders.put(headerName, headerValues);
  }
  for (Map.Entry<RouteMatch, SubchannelPicker> entry : routePickers.entrySet()) {
    RouteMatch routeMatch = entry.getKey();
    if (routeMatch.matches(
        "/" + args.getMethodDescriptor().getFullMethodName(), asciiHeaders)) {
      return entry.getValue().pickSubchannel(args);
    }
  }
  return PickResult.withError(Status.UNAVAILABLE.withDescription("no matching route found"));
}
 
Example 3
Source File: CensusModulesTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void traceHeaders_clientMissingCensusImpl_preservingHeaders() throws Exception {
  reset(spyClientSpanBuilder);
  when(spyClientSpanBuilder.startSpan()).thenReturn(BlankSpan.INSTANCE);
  Metadata headers = new Metadata();
  headers.put(
      Metadata.Key.of("never-used-key-bin", Metadata.BINARY_BYTE_MARSHALLER),
      new byte[] {});
  Set<String> originalHeaderKeys = new HashSet<>(headers.keys());

  CensusTracingModule.ClientCallTracer callTracer =
      censusTracing.newClientCallTracer(BlankSpan.INSTANCE, method);
  callTracer.newClientStreamTracer(STREAM_INFO, headers);

  assertThat(headers.keys()).containsExactlyElementsIn(originalHeaderKeys);
}
 
Example 4
Source File: GoogleAdsResponseMetadata.java    From google-ads-java with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeaders(Metadata metadata) {
  super.onHeaders(metadata);
  ImmutableMap.Builder<String, String> headerBuilder = ImmutableMap.builder();
  for (String key : metadata.keys()) {
    // Ignore binary headers
    if (!key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
      Key<String> stringKey = Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
      if (metadata.containsKey(stringKey)) {
        headerBuilder.put(key, metadata.get(stringKey));
      }
    }
  }
  headerMap = headerBuilder.build();
}
 
Example 5
Source File: ConsumerInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeaders(Metadata headers) {
    logger.info("on Headers");
    for (String key : headers.keys()) {
        logger.info("Receive key: {}", key);
    }
    delegate().onHeaders(headers);
}