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

The following examples show how to use io.grpc.Metadata#discardAll() . 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: Utils.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
public static Http2Headers convertClientHeaders(Metadata headers,
    AsciiString scheme,
    AsciiString defaultPath,
    AsciiString authority,
    AsciiString method,
    AsciiString userAgent) {
  Preconditions.checkNotNull(defaultPath, "defaultPath");
  Preconditions.checkNotNull(authority, "authority");
  Preconditions.checkNotNull(method, "method");

  // Discard any application supplied duplicates of the reserved headers
  headers.discardAll(CONTENT_TYPE_KEY);
  headers.discardAll(GrpcUtil.TE_HEADER);
  headers.discardAll(GrpcUtil.USER_AGENT_KEY);

  return GrpcHttp2OutboundHeaders.clientRequestHeaders(
      toHttp2Headers(headers),
      authority,
      defaultPath,
      method,
      scheme,
      userAgent);
}
 
Example 2
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public ClientStreamTracer newClientStreamTracer(
    ClientStreamTracer.StreamInfo info, Metadata headers) {
  ClientTracer tracer = new ClientTracer(module, startCtx);
  // TODO(zhangkun83): Once retry or hedging is implemented, a ClientCall may start more than
  // one streams.  We will need to update this file to support them.
  if (streamTracerUpdater != null) {
    checkState(
        streamTracerUpdater.compareAndSet(this, null, tracer),
        "Are you creating multiple streams per call? This class doesn't yet support this case");
  } else {
    checkState(
        streamTracer == null,
        "Are you creating multiple streams per call? This class doesn't yet support this case");
    streamTracer = tracer;
  }
  if (module.propagateTags) {
    headers.discardAll(module.statsHeader);
    if (!module.tagger.empty().equals(parentCtx)) {
      headers.put(module.statsHeader, parentCtx);
    }
  }
  return tracer;
}
 
Example 3
Source File: CensusStatsModule.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Override
public ClientStreamTracer newClientStreamTracer(CallOptions callOptions, Metadata headers) {
  ClientTracer tracer = new ClientTracer();
  // TODO(zhangkun83): Once retry or hedging is implemented, a ClientCall may start more than
  // one streams.  We will need to update this file to support them.
  if (streamTracerUpdater != null) {
    checkState(
        streamTracerUpdater.compareAndSet(this, null, tracer),
        "Are you creating multiple streams per call? This class doesn't yet support this case");
  } else {
    checkState(
        streamTracer == null,
        "Are you creating multiple streams per call? This class doesn't yet support this case");
    streamTracer = tracer;
  }
  if (module.propagateTags) {
    headers.discardAll(module.statsHeader);
    if (!module.tagger.empty().equals(parentCtx)) {
      headers.put(module.statsHeader, parentCtx);
    }
  }
  return tracer;
}
 
Example 4
Source File: ClientCallImpl.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static void prepareHeaders(
    Metadata headers,
    DecompressorRegistry decompressorRegistry,
    Compressor compressor,
    boolean fullStreamDecompression) {
  headers.discardAll(MESSAGE_ENCODING_KEY);
  if (compressor != Codec.Identity.NONE) {
    headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());
  }

  headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
  byte[] advertisedEncodings =
      InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(decompressorRegistry);
  if (advertisedEncodings.length != 0) {
    headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
  }

  headers.discardAll(CONTENT_ENCODING_KEY);
  headers.discardAll(CONTENT_ACCEPT_ENCODING_KEY);
  if (fullStreamDecompression) {
    headers.put(CONTENT_ACCEPT_ENCODING_KEY, FULL_STREAM_DECOMPRESSION_ENCODINGS);
  }
}
 
Example 5
Source File: TokenAttachingTracerFactory.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public ClientStreamTracer newClientStreamTracer(
    ClientStreamTracer.StreamInfo info, Metadata headers) {
  Attributes transportAttrs = checkNotNull(info.getTransportAttrs(), "transportAttrs");
  Attributes eagAttrs =
      checkNotNull(transportAttrs.get(GrpcAttributes.ATTR_CLIENT_EAG_ATTRS), "eagAttrs");
  String token = eagAttrs.get(GrpclbConstants.TOKEN_ATTRIBUTE_KEY);
  headers.discardAll(GrpclbConstants.TOKEN_METADATA_KEY);
  if (token != null) {
    headers.put(GrpclbConstants.TOKEN_METADATA_KEY, token);
  }
  if (delegate != null) {
    return delegate.newClientStreamTracer(info, headers);
  } else {
    return NOOP_TRACER;
  }
}
 
Example 6
Source File: CensusTracingModule.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public ClientStreamTracer newClientStreamTracer(CallOptions callOptions, Metadata headers) {
  if (span != BlankSpan.INSTANCE) {
    headers.discardAll(tracingHeader);
    headers.put(tracingHeader, span.getContext());
  }
  return new ClientTracer(span);
}
 
Example 7
Source File: ServerCallImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void sendHeadersInternal(Metadata headers) {
  checkState(!sendHeadersCalled, "sendHeaders has already been called");
  checkState(!closeCalled, "call is closed");

  headers.discardAll(MESSAGE_ENCODING_KEY);
  if (compressor == null) {
    compressor = Codec.Identity.NONE;
  } else {
    if (messageAcceptEncoding != null) {
      // TODO(carl-mastrangelo): remove the string allocation.
      if (!GrpcUtil.iterableContains(
          ACCEPT_ENCODING_SPLITTER.split(new String(messageAcceptEncoding, GrpcUtil.US_ASCII)),
          compressor.getMessageEncoding())) {
        // resort to using no compression.
        compressor = Codec.Identity.NONE;
      }
    } else {
      compressor = Codec.Identity.NONE;
    }
  }

  // Always put compressor, even if it's identity.
  headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());

  stream.setCompressor(compressor);

  headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
  byte[] advertisedEncodings =
      InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(decompressorRegistry);
  if (advertisedEncodings.length != 0) {
    headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
  }

  // Don't check if sendMessage has been called, since it requires that sendHeaders was already
  // called.
  sendHeadersCalled = true;
  stream.writeHeaders(headers);
}
 
Example 8
Source File: CensusTracingModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public ClientStreamTracer newClientStreamTracer(
    ClientStreamTracer.StreamInfo info, Metadata headers) {
  if (span != BlankSpan.INSTANCE) {
    headers.discardAll(tracingHeader);
    headers.put(tracingHeader, span.getContext());
  }
  return new ClientTracer(span);
}
 
Example 9
Source File: AbstractServerStream.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void addStatusToTrailers(Metadata trailers, Status status) {
  trailers.discardAll(InternalStatus.CODE_KEY);
  trailers.discardAll(InternalStatus.MESSAGE_KEY);
  trailers.put(InternalStatus.CODE_KEY, status);
  if (status.getDescription() != null) {
    trailers.put(InternalStatus.MESSAGE_KEY, status.getDescription());
  }
}
 
Example 10
Source File: GrpclbState.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public PickResult picked(Metadata headers) {
  headers.discardAll(GrpclbConstants.TOKEN_METADATA_KEY);
  if (token != null) {
    headers.put(GrpclbConstants.TOKEN_METADATA_KEY, token);
  }
  return result;
}
 
Example 11
Source File: GrpclbState.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public PickResult picked(Metadata headers) {
  headers.discardAll(GrpclbConstants.TOKEN_METADATA_KEY);
  if (token != null) {
    headers.put(GrpclbConstants.TOKEN_METADATA_KEY, token);
  }
  return result;
}
 
Example 12
Source File: AbstractServerStream.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private void addStatusToTrailers(Metadata trailers, Status status) {
  trailers.discardAll(InternalStatus.CODE_KEY);
  trailers.discardAll(InternalStatus.MESSAGE_KEY);
  trailers.put(InternalStatus.CODE_KEY, status);
  if (status.getDescription() != null) {
    trailers.put(InternalStatus.MESSAGE_KEY, status.getDescription());
  }
}
 
Example 13
Source File: ServerCallImpl.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public void sendHeaders(Metadata headers) {
  checkState(!sendHeadersCalled, "sendHeaders has already been called");
  checkState(!closeCalled, "call is closed");

  headers.discardAll(MESSAGE_ENCODING_KEY);
  if (compressor == null) {
    compressor = Codec.Identity.NONE;
  } else {
    if (messageAcceptEncoding != null) {
      // TODO(carl-mastrangelo): remove the string allocation.
      if (!GrpcUtil.iterableContains(
          ACCEPT_ENCODING_SPLITTER.split(new String(messageAcceptEncoding, GrpcUtil.US_ASCII)),
          compressor.getMessageEncoding())) {
        // resort to using no compression.
        compressor = Codec.Identity.NONE;
      }
    } else {
      compressor = Codec.Identity.NONE;
    }
  }

  // Always put compressor, even if it's identity.
  headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());

  stream.setCompressor(compressor);

  headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
  byte[] advertisedEncodings =
      InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(decompressorRegistry);
  if (advertisedEncodings.length != 0) {
    headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
  }

  // Don't check if sendMessage has been called, since it requires that sendHeaders was already
  // called.
  sendHeadersCalled = true;
  stream.writeHeaders(headers);
}
 
Example 14
Source File: Utils.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
public static Http2Headers convertServerHeaders(Metadata headers) {
  // Discard any application supplied duplicates of the reserved headers
  headers.discardAll(CONTENT_TYPE_KEY);
  headers.discardAll(GrpcUtil.TE_HEADER);
  headers.discardAll(GrpcUtil.USER_AGENT_KEY);

  return GrpcHttp2OutboundHeaders.serverResponseHeaders(toHttp2Headers(headers));
}
 
Example 15
Source File: Http2ClientStreamTransportState.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Strip HTTP transport implementation details so they don't leak via metadata into
 * the application layer.
 */
private static void stripTransportDetails(Metadata metadata) {
  metadata.discardAll(HTTP2_STATUS);
  metadata.discardAll(InternalStatus.CODE_KEY);
  metadata.discardAll(InternalStatus.MESSAGE_KEY);
}
 
Example 16
Source File: StatusProto.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
private static Metadata toMetadata(com.google.rpc.Status statusProto, Metadata metadata) {
  checkNotNull(metadata, "metadata must not be null");
  metadata.discardAll(STATUS_DETAILS_KEY);
  metadata.put(STATUS_DETAILS_KEY, statusProto);
  return metadata;
}
 
Example 17
Source File: Headers.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Serializes the given headers and creates a list of OkHttp {@link Header}s to be used when
 * creating a stream. Since this serializes the headers, this method should be called in the
 * application thread context.
 */
public static List<Header> createRequestHeaders(
    Metadata headers,
    String defaultPath,
    String authority,
    String userAgent,
    boolean useGet,
    boolean usePlaintext) {
  Preconditions.checkNotNull(headers, "headers");
  Preconditions.checkNotNull(defaultPath, "defaultPath");
  Preconditions.checkNotNull(authority, "authority");

  // Discard any application supplied duplicates of the reserved headers
  headers.discardAll(GrpcUtil.CONTENT_TYPE_KEY);
  headers.discardAll(GrpcUtil.TE_HEADER);
  headers.discardAll(GrpcUtil.USER_AGENT_KEY);

  // 7 is the number of explicit add calls below.
  List<Header> okhttpHeaders = new ArrayList<>(7 + InternalMetadata.headerCount(headers));

  // Set GRPC-specific headers.
  if (usePlaintext) {
    okhttpHeaders.add(HTTP_SCHEME_HEADER);
  } else {
    okhttpHeaders.add(HTTPS_SCHEME_HEADER);
  }
  if (useGet) {
    okhttpHeaders.add(METHOD_GET_HEADER);
  } else {
    okhttpHeaders.add(METHOD_HEADER);
  }

  okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority));
  String path = defaultPath;
  okhttpHeaders.add(new Header(Header.TARGET_PATH, path));

  okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent));

  // All non-pseudo headers must come after pseudo headers.
  okhttpHeaders.add(CONTENT_TYPE_HEADER);
  okhttpHeaders.add(TE_HEADER);

  // Now add any application-provided headers.
  byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers);
  for (int i = 0; i < serializedHeaders.length; i += 2) {
    ByteString key = ByteString.of(serializedHeaders[i]);
    String keyString = key.utf8();
    if (isApplicationHeader(keyString)) {
      ByteString value = ByteString.of(serializedHeaders[i + 1]);
      okhttpHeaders.add(new Header(key, value));
    }
  }

  return okhttpHeaders;
}
 
Example 18
Source File: Http2ClientStreamTransportState.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Strip HTTP transport implementation details so they don't leak via metadata into
 * the application layer.
 */
private static void stripTransportDetails(Metadata metadata) {
  metadata.discardAll(HTTP2_STATUS);
  metadata.discardAll(InternalStatus.CODE_KEY);
  metadata.discardAll(InternalStatus.MESSAGE_KEY);
}
 
Example 19
Source File: Headers.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Serializes the given headers and creates a list of OkHttp {@link Header}s to be used when
 * creating a stream. Since this serializes the headers, this method should be called in the
 * application thread context.
 */
public static List<Header> createRequestHeaders(
    Metadata headers, String defaultPath, String authority, String userAgent, boolean useGet) {
  Preconditions.checkNotNull(headers, "headers");
  Preconditions.checkNotNull(defaultPath, "defaultPath");
  Preconditions.checkNotNull(authority, "authority");

  // Discard any application supplied duplicates of the reserved headers
  headers.discardAll(GrpcUtil.CONTENT_TYPE_KEY);
  headers.discardAll(GrpcUtil.TE_HEADER);
  headers.discardAll(GrpcUtil.USER_AGENT_KEY);

  // 7 is the number of explicit add calls below.
  List<Header> okhttpHeaders = new ArrayList<>(7 + InternalMetadata.headerCount(headers));

  // Set GRPC-specific headers.
  okhttpHeaders.add(SCHEME_HEADER);
  if (useGet) {
    okhttpHeaders.add(METHOD_GET_HEADER);
  } else {
    okhttpHeaders.add(METHOD_HEADER);
  }

  okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority));
  String path = defaultPath;
  okhttpHeaders.add(new Header(Header.TARGET_PATH, path));

  okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent));

  // All non-pseudo headers must come after pseudo headers.
  okhttpHeaders.add(CONTENT_TYPE_HEADER);
  okhttpHeaders.add(TE_HEADER);

  // Now add any application-provided headers.
  byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers);
  for (int i = 0; i < serializedHeaders.length; i += 2) {
    ByteString key = ByteString.of(serializedHeaders[i]);
    String keyString = key.utf8();
    if (isApplicationHeader(keyString)) {
      ByteString value = ByteString.of(serializedHeaders[i + 1]);
      okhttpHeaders.add(new Header(key, value));
    }
  }

  return okhttpHeaders;
}
 
Example 20
Source File: StatusProto.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
private static Metadata toMetadata(com.google.rpc.Status statusProto, Metadata metadata) {
  checkNotNull(metadata, "metadata must not be null");
  metadata.discardAll(STATUS_DETAILS_KEY);
  metadata.put(STATUS_DETAILS_KEY, statusProto);
  return metadata;
}