Search in sources :

Example 1 with Metadata

use of 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));
            }
        }
    });
}
Also used : StatusException(io.grpc.StatusException) Metadata(io.grpc.Metadata) URI(java.net.URI) Map(java.util.Map)

Example 2 with Metadata

use of 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);
}
Also used : StatsContext(com.google.instrumentation.stats.StatsContext) Metadata(io.grpc.Metadata)

Example 3 with Metadata

use of 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();
        }
    }
}
Also used : Metadata(io.grpc.Metadata)

Example 4 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class DelayedStream method start.

@Override
public void start(ClientStreamListener listener) {
    checkState(this.listener == null, "already started");
    Status savedError;
    boolean savedPassThrough;
    synchronized (this) {
        this.listener = checkNotNull(listener, "listener");
        // If error != null, then cancel() has been called and was unable to close the listener
        savedError = error;
        savedPassThrough = passThrough;
        if (!savedPassThrough) {
            listener = delayedListener = new DelayedStreamListener(listener);
        }
    }
    if (savedError != null) {
        listener.closed(savedError, new Metadata());
        return;
    }
    if (savedPassThrough) {
        realStream.start(listener);
    } else {
        final ClientStreamListener finalListener = listener;
        delayOrExecute(new Runnable() {

            @Override
            public void run() {
                realStream.start(finalListener);
            }
        });
    }
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata)

Example 5 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class DelayedStream method cancel.

// When this method returns, passThrough is guaranteed to be true
@Override
public void cancel(final Status reason) {
    checkNotNull(reason, "reason");
    boolean delegateToRealStream = true;
    ClientStreamListener listenerToClose = null;
    synchronized (this) {
        // If realStream != null, then either setStream() or cancel() has been called
        if (realStream == null) {
            realStream = NoopClientStream.INSTANCE;
            delegateToRealStream = false;
            // If listener == null, then start() will later call listener with 'error'
            listenerToClose = listener;
            error = reason;
        }
    }
    if (delegateToRealStream) {
        delayOrExecute(new Runnable() {

            @Override
            public void run() {
                realStream.cancel(reason);
            }
        });
    } else {
        if (listenerToClose != null) {
            listenerToClose.closed(reason, new Metadata());
        }
        drainPendingCalls();
    }
}
Also used : Metadata(io.grpc.Metadata)

Aggregations

Metadata (io.grpc.Metadata)269 Test (org.junit.Test)222 Status (io.grpc.Status)77 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ClientStream (io.grpc.internal.ClientStream)20 InputStream (java.io.InputStream)19 Buffer (okio.Buffer)16 ClientCall (io.grpc.ClientCall)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 ServerStreamListener (io.grpc.internal.ServerStreamListener)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 ServerCall (io.grpc.ServerCall)12 StatsContext (com.google.instrumentation.stats.StatsContext)11 IOException (java.io.IOException)11 CallOptions (io.grpc.CallOptions)10 Context (io.grpc.Context)10 Matchers.anyString (org.mockito.Matchers.anyString)10 ServerStream (io.grpc.internal.ServerStream)9 ServiceDescriptor (io.grpc.ServiceDescriptor)8 ExecutionException (java.util.concurrent.ExecutionException)8