use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project core-java by SpineEventEngine.
the class MetadataConverter method toMetadata.
/**
* Returns the {@link Metadata}, containing the {@link Error} as a byte array.
*
* @param error the error to convert
* @return the metadata containing error
*/
public static Metadata toMetadata(Error error) {
checkNotNull(error);
final Metadata metadata = new Metadata();
metadata.put(KEY, error.toByteArray());
return metadata;
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.
the class GoogleAuthLibraryCallCredentials method applyRequestMetadata.
@Override
public void applyRequestMetadata(MethodDescriptor<?, ?> method, Attributes attrs, Executor appExecutor, final MetadataApplier applier) {
String authority = checkNotNull(attrs.get(ATTR_AUTHORITY), "authority");
final URI uri;
try {
uri = serviceUri(authority, method);
} catch (StatusException e) {
applier.fail(e.getStatus());
return;
}
appExecutor.execute(new Runnable() {
@Override
public void run() {
try {
// Credentials is expected to manage caching internally if the metadata is fetched over
// the network.
//
// TODO(zhangkun83): we don't know whether there is valid cache data. If there is, we
// would waste a context switch by always scheduling in executor. However, we have to
// do so because we can't risk blocking the network thread. This can be resolved after
// https://github.com/google/google-auth-library-java/issues/3 is resolved.
//
// Some implementations may return null here.
Map<String, List<String>> metadata = creds.getRequestMetadata(uri);
// Re-use the headers if getRequestMetadata() returns the same map. It 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 save its return value based on the URI.
Metadata headers;
synchronized (GoogleAuthLibraryCallCredentials.this) {
if (lastMetadata == null || lastMetadata != metadata) {
lastMetadata = metadata;
lastHeaders = toHeaders(metadata);
}
headers = lastHeaders;
}
applier.apply(headers);
} catch (Throwable e) {
applier.fail(Status.UNAUTHENTICATED.withCause(e));
}
}
});
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.
the class StatsTraceContext method newServerContext.
/**
* Creates a {@code StatsTraceContext} for an incoming RPC, using the StatsContext deserialized
* from the headers.
*
* <p>The current time is used as the start time of the RPC.
*/
public static StatsTraceContext newServerContext(String methodName, StatsContextFactory statsFactory, Metadata headers, Supplier<Stopwatch> stopwatchSupplier) {
Metadata.Key<StatsContext> statsHeader = createStatsHeader(statsFactory);
StatsContext parentCtx = headers.get(statsHeader);
if (parentCtx == null) {
parentCtx = statsFactory.getDefault();
}
return new StatsTraceContext(Side.SERVER, methodName, parentCtx, stopwatchSupplier, statsHeader);
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.
the class AbstractClientStream method inboundDataReceived.
/**
* Processes the contents of a received data frame from the server.
*
* @param frame the received data frame. Its ownership is transferred to this method.
*/
protected void inboundDataReceived(ReadableBuffer frame) {
Preconditions.checkNotNull(frame, "frame");
boolean needToCloseFrame = true;
try {
if (inboundPhase() == Phase.STATUS) {
return;
}
if (inboundPhase() == Phase.HEADERS) {
// Have not received headers yet so error
inboundTransportError(Status.INTERNAL.withDescription("headers not received before payload"), new Metadata());
return;
}
inboundPhase(Phase.MESSAGE);
needToCloseFrame = false;
deframe(frame, false);
} finally {
if (needToCloseFrame) {
frame.close();
}
}
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.
the class ClientCallImplTest method contextDeadlineShouldBePropagatedInMetadata.
@Test
public void contextDeadlineShouldBePropagatedInMetadata() {
long deadlineNanos = TimeUnit.SECONDS.toNanos(1);
Context context = Context.current().withDeadlineAfter(deadlineNanos, TimeUnit.NANOSECONDS, deadlineCancellationExecutor);
context.attach();
ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor);
Metadata headers = new Metadata();
call.start(callListener, headers);
assertTrue(headers.containsKey(GrpcUtil.TIMEOUT_KEY));
Long timeout = headers.get(GrpcUtil.TIMEOUT_KEY);
assertNotNull(timeout);
long deltaNanos = TimeUnit.MILLISECONDS.toNanos(400);
assertTimeoutBetween(timeout, deadlineNanos - deltaNanos, deadlineNanos);
}
Aggregations