Search in sources :

Example 51 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project ballerina by ballerina-lang.

the class ServerHeaderInterceptor method interceptCall.

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
    MessageContext ctx = MessageContext.DATA_KEY.get();
    // Only initialize ctx if not yet initialized
    ctx = ctx != null ? ctx : new MessageContext();
    boolean found = false;
    for (String keyName : headers.keys()) {
        if (keyName.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
            Metadata.Key<byte[]> key = Metadata.Key.of(keyName, Metadata.BINARY_BYTE_MARSHALLER);
            Iterable<byte[]> values = headers.getAll(key);
            if (values == null) {
                continue;
            }
            for (byte[] value : values) {
                ctx.put(key, value);
            }
        } else {
            Metadata.Key<String> key = Metadata.Key.of(keyName, Metadata.ASCII_STRING_MARSHALLER);
            Iterable<String> values = headers.getAll(key);
            if (values == null) {
                continue;
            }
            for (String value : values) {
                ctx.put(key, value);
            }
        }
        found = true;
    }
    if (found) {
        return Contexts.interceptCall(Context.current().withValue(MessageContext.DATA_KEY, ctx), new HeaderForwardingServerCall<>(call), headers, next);
    } else {
        // Don't attach a context if there is nothing to attach
        return next.startCall(new HeaderForwardingServerCall<>(call), headers);
    }
}
Also used : Metadata(io.grpc.Metadata) MessageContext(org.ballerinalang.net.grpc.MessageContext)

Example 52 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project Saiy-PS by brandall76.

the class GoogleCredentialsInterceptor method interceptCall.

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
    return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

        @Override
        protected void checkedStart(Listener<RespT> responseListener, Metadata headers) throws StatusException {
            Metadata cachedSaved;
            URI uri = serviceUri(next, method);
            synchronized (GoogleCredentialsInterceptor.this) {
                Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
                if (mLastMetadata == null || mLastMetadata != latestMetadata) {
                    mLastMetadata = latestMetadata;
                    mCached = toHeaders(mLastMetadata);
                }
                cachedSaved = mCached;
            }
            headers.merge(cachedSaved);
            delegate().start(responseListener, headers);
        }
    };
}
Also used : Metadata(io.grpc.Metadata) List(java.util.List) URI(java.net.URI)

Example 53 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project zipkin-gcp by openzipkin.

the class UnaryClientCall method beginUnaryCall.

void beginUnaryCall(ClientCall.Listener<RespT> listener) {
    try {
        call.start(listener, new Metadata());
        call.request(1);
        call.sendMessage(request);
        call.halfClose();
    } catch (RuntimeException | Error t) {
        call.cancel(null, t);
        throw t;
    }
}
Also used : Metadata(io.grpc.Metadata)

Example 54 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project motan by weibocom.

the class MotanServerCallHandler method unaryCall.

private <ReqT, RespT> Listener<ReqT> unaryCall(final ServerCall<ReqT, RespT> call, final Metadata headers) {
    final ServerCallStreamObserverImpl<ReqT, RespT> responseObserver = new ServerCallStreamObserverImpl<ReqT, RespT>(call);
    // see ServerCalls
    call.request(2);
    return new ServerCall.Listener<ReqT>() {

        ReqT request;

        @Override
        public void onMessage(ReqT request) {
            this.request = request;
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onHalfClose() {
            if (request != null) {
                DefaultRequest motanRequest = getBaseMotanRequest(headers);
                String ip = NetUtils.getHostName(call.attributes().get(ServerCall.REMOTE_ADDR_KEY));
                if (ip != null) {
                    motanRequest.setAttachment(URLParamType.host.getName(), ip);
                }
                if (responseStream) {
                    motanRequest.setArguments(new Object[] { request, responseObserver });
                } else {
                    motanRequest.setArguments(new Object[] { request });
                }
                Response response = null;
                try {
                    response = provider.call(motanRequest);
                    if (response.getValue() != null) {
                        responseObserver.onNext((RespT) response.getValue());
                        responseObserver.onCompleted();
                    }
                } catch (Exception e) {
                    responseObserver.onError(e);
                    return;
                }
                responseObserver.freeze();
                if (call.isReady()) {
                    onReady();
                }
            } else {
                call.close(Status.INTERNAL.withDescription("Half-closed without a request"), new Metadata());
            }
        }

        @Override
        public void onCancel() {
            responseObserver.cancelled = true;
            if (responseObserver.onCancelHandler != null) {
                responseObserver.onCancelHandler.run();
            }
        }

        @Override
        public void onReady() {
            if (responseObserver.onReadyHandler != null) {
                responseObserver.onReadyHandler.run();
            }
        }
    };
}
Also used : Response(com.weibo.api.motan.rpc.Response) Listener(io.grpc.ServerCall.Listener) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Metadata(io.grpc.Metadata) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 55 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project pinpoint by naver.

the class AgentHeaderFactory method newHeader.

public Metadata newHeader() {
    Metadata headers = new Metadata();
    headers.put(Header.AGENT_ID_KEY, agentId);
    headers.put(Header.APPLICATION_NAME_KEY, applicationName);
    headers.put(Header.SERVICE_TYPE_KEY, Integer.toString(serviceType));
    headers.put(Header.AGENT_START_TIME_KEY, Long.toString(agentStartTime));
    if (!StringUtils.isEmpty(agentName)) {
        headers.put(Header.AGENT_NAME_KEY, agentName);
    }
    return headers;
}
Also used : Metadata(io.grpc.Metadata)

Aggregations

Metadata (io.grpc.Metadata)701 Test (org.junit.Test)559 Status (io.grpc.Status)190 CallOptions (io.grpc.CallOptions)56 ClientStreamTracer (io.grpc.ClientStreamTracer)51 ServerCall (io.grpc.ServerCall)48 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)44 InOrder (org.mockito.InOrder)41 Subchannel (io.grpc.LoadBalancer.Subchannel)40 ByteArrayInputStream (java.io.ByteArrayInputStream)40 InputStream (java.io.InputStream)38 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 IOException (java.io.IOException)32 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 Context (io.grpc.Context)31 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)31 InternalMetadata (io.grpc.InternalMetadata)30 MethodDescriptor (io.grpc.MethodDescriptor)30 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)29 ManagedChannel (io.grpc.ManagedChannel)27