Package io.grpc.stub
Class MetadataUtils
- java.lang.Object
-
- io.grpc.stub.MetadataUtils
-
public final class MetadataUtils extends Object
Utility functions for binding and receiving headers.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static io.grpc.ClientInterceptor
newAttachHeadersInterceptor(io.grpc.Metadata extraHeaders)
Returns a client interceptor that attaches a set of headers to requests.static io.grpc.ServerInterceptor
newAttachMetadataServerInterceptor(io.grpc.Metadata extras)
Returns a ServerInterceptor that adds the specified Metadata to every response stream, one way or another.static io.grpc.ClientInterceptor
newCaptureMetadataInterceptor(AtomicReference<io.grpc.Metadata> headersCapture, AtomicReference<io.grpc.Metadata> trailersCapture)
Captures the last received metadata on a channel.
-
-
-
Method Detail
-
newAttachHeadersInterceptor
public static io.grpc.ClientInterceptor newAttachHeadersInterceptor(io.grpc.Metadata extraHeaders)
Returns a client interceptor that attaches a set of headers to requests.- Parameters:
extraHeaders
- the headers to be passed by each call that is processed by the returned interceptor
-
newCaptureMetadataInterceptor
public static io.grpc.ClientInterceptor newCaptureMetadataInterceptor(AtomicReference<io.grpc.Metadata> headersCapture, AtomicReference<io.grpc.Metadata> trailersCapture)
Captures the last received metadata on a channel. Useful for testing.- Parameters:
headersCapture
- to record the last received headerstrailersCapture
- to record the last received trailers- Returns:
- an implementation of the channel with captures installed.
-
newAttachMetadataServerInterceptor
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/11462") public static io.grpc.ServerInterceptor newAttachMetadataServerInterceptor(io.grpc.Metadata extras)
Returns a ServerInterceptor that adds the specified Metadata to every response stream, one way or another.If, absent this interceptor, a stream would have headers, 'extras' will be added to those headers. Otherwise, 'extras' will be sent as trailers. This pattern is useful when you have some fixed information, server identity say, that should be included no matter how the call turns out. The fallback to trailers avoids artificially committing clients to error responses that could otherwise be retried (see https://grpc.io/docs/guides/retry/ for more).
For correct operation, be sure to arrange for this interceptor to run *before* any others that might add headers.
- Parameters:
extras
- the Metadata to be added to each stream. Caller gives up ownership.
-
-