io.grpc.Metadata.containsKey()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(107)

本文整理了Java中io.grpc.Metadata.containsKey()方法的一些代码示例,展示了Metadata.containsKey()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Metadata.containsKey()方法的具体详情如下:
包路径:io.grpc.Metadata
类名称:Metadata
方法名:containsKey

Metadata.containsKey介绍

[英]Returns true if a value is defined for the given key.

This is done by linear search, so if it is followed by #get or #getAll, prefer calling them directly and checking the return value against null.
[中]如果为给定键定义了值,则返回true。
这是通过线性搜索完成的,所以如果后面跟着#get或#getAll,最好直接调用它们并检查返回值是否为null。

代码示例

代码示例来源:origin: googleapis/google-cloud-java

static long extractRetryDelay(Throwable cause) {
  if (cause != null) {
   Metadata trailers = Status.trailersFromThrowable(cause);
   if (trailers != null && trailers.containsKey(KEY_RETRY_INFO)) {
    RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO);
    if (retryInfo.hasRetryDelay()) {
     return Durations.toMillis(retryInfo.getRetryDelay());
    }
   }
  }
  return -1L;
 }
}

代码示例来源:origin: Alluxio/alluxio

if (e.getTrailers() != null && e.getTrailers().containsKey(sInnerCauseKey)) {
 try {
  cause = (Throwable) SerializationUtils.deserialize(e.getTrailers().get(sInnerCauseKey));

代码示例来源:origin: com.salesforce.servicelibs/grpc-contrib

/**
 * Returns true if a value is defined for the given key.
 *
 * <p>This is done by linear search, so if it is followed by {@link #get} or {@link #getAll},
 * prefer calling them directly and checking the return value against {@code null}.
 */
public boolean containsKey(Metadata.Key<?> key) {
  return contextMetadata.containsKey(key);
}

代码示例来源:origin: salesforce/grpc-java-contrib

/**
 * Returns true if a value is defined for the given key.
 *
 * <p>This is done by linear search, so if it is followed by {@link #get} or {@link #getAll},
 * prefer calling them directly and checking the return value against {@code null}.
 */
public boolean containsKey(Metadata.Key<?> key) {
  return contextMetadata.containsKey(key);
}

代码示例来源:origin: GoogleCloudPlatform/cloud-bigtable-client

@VisibleForTesting
 public void updateHeaders(Metadata headers) {
  if (!headers.containsKey(GRPC_RESOURCE_PREFIX_KEY)) {
   headers.put(GRPC_RESOURCE_PREFIX_KEY, defaultValue);
  }
 }
}

代码示例来源:origin: net.spals.appbuilder/spals-appbuilder-app-examples

@Override
  public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
    final ServerCall<ReqT, RespT> call,
    final Metadata headers,
    final ServerCallHandler<ReqT, RespT> next
  ) {
    if (!headers.containsKey(tokenKey)) {
      call.close(Status.PERMISSION_DENIED.withDescription("no authorization token"), new Metadata());
      return new ServerCall.Listener<ReqT>(){};
    }

    final String authToken = headers.get(tokenKey);
    LOGGER.debug("Got authentication token (" + authToken + ")");
    return next.startCall(call, headers);
  }
}

代码示例来源:origin: lumongo/lumongo

public Lumongo.FetchResponse executeFetch(Member m, Lumongo.FetchRequest request) throws Exception {
  ReadWriteLock lock = getLockForMember(m);
  lock.readLock().lock();
  InternalRpcConnection rpcConnection = null;
  try {
    rpcConnection = getInternalRpcConnection(m);
    Lumongo.FetchResponse response = rpcConnection.getService().fetch(request);
    returnInternalBlockingConnection(m, rpcConnection, true);
    return response;
  }
  catch (StatusRuntimeException e) {
    Metadata trailers = e.getTrailers();
    if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
      throw new Exception(trailers.get(MetaKeys.ERROR_KEY));
    }
    else {
      throw e;
    }
  }
  catch (Exception e) {
    returnInternalBlockingConnection(m, rpcConnection, false);
    throw e;
  }
  finally {
    lock.readLock().unlock();
  }
}

代码示例来源:origin: lumongo/lumongo

public Lumongo.GetTermsResponseInternal getTerms(Member m, GetTermsRequest request) throws Exception {
  ReadWriteLock lock = getLockForMember(m);
  lock.readLock().lock();
  InternalRpcConnection rpcConnection = null;
  try {
    rpcConnection = getInternalRpcConnection(m);
    Lumongo.GetTermsResponseInternal response = rpcConnection.getService().getTerms(request);
    returnInternalBlockingConnection(m, rpcConnection, true);
    return response;
  }
  catch (StatusRuntimeException e) {
    Metadata trailers = e.getTrailers();
    if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
      throw new Exception(trailers.get(MetaKeys.ERROR_KEY));
    }
    else {
      throw e;
    }
  }
  catch (Exception e) {
    returnInternalBlockingConnection(m, rpcConnection, false);
    throw e;
  }
  finally {
    lock.readLock().unlock();
  }
}

代码示例来源:origin: sitewhere/sitewhere

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
  ServerCallHandler<ReqT, RespT> next) {
if (headers.containsKey(JwtClientInterceptor.JWT_KEY)) {
  String jwt = headers.get(JwtClientInterceptor.JWT_KEY);
  LOGGER.trace("Server received jwt key: " + jwt);
  Context ctx = Context.current().withValue(GrpcContextKeys.JWT_KEY, jwt);
  return Contexts.interceptCall(ctx, call, headers, next);
} else {
  call.close(Status.UNAUTHENTICATED.withDescription("JWT not passed in metadata."), headers);
  return new ServerCall.Listener<ReqT>() {
  };
}
}

代码示例来源:origin: lumongo/lumongo

public InternalQueryResponse executeQuery(Member m, QueryRequest request) throws Exception {
  ReadWriteLock lock = getLockForMember(m);
  lock.readLock().lock();
  InternalRpcConnection rpcConnection = null;
  try {
    rpcConnection = getInternalRpcConnection(m);
    InternalQueryResponse response = rpcConnection.getService().query(request);
    returnInternalBlockingConnection(m, rpcConnection, true);
    return response;
  }
  catch (StatusRuntimeException e) {
    Metadata trailers = e.getTrailers();
    if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
      throw new Exception(trailers.get(MetaKeys.ERROR_KEY));
    }
    else {
      throw e;
    }
  }
  catch (Exception e) {
    returnInternalBlockingConnection(m, rpcConnection, false);
    throw e;
  }
  finally {
    lock.readLock().unlock();
  }
}

代码示例来源:origin: lumongo/lumongo

public GetFieldNamesResponse getFieldNames(Member m, GetFieldNamesRequest request) throws Exception {
  ReadWriteLock lock = getLockForMember(m);
  lock.readLock().lock();
  InternalRpcConnection rpcConnection = null;
  try {
    rpcConnection = getInternalRpcConnection(m);
    GetFieldNamesResponse response = rpcConnection.getService().getFieldNames(request);
    returnInternalBlockingConnection(m, rpcConnection, true);
    return response;
  }
  catch (StatusRuntimeException e) {
    Metadata trailers = e.getTrailers();
    if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
      throw new Exception(trailers.get(MetaKeys.ERROR_KEY));
    }
    else {
      throw e;
    }
  }
  catch (Exception e) {
    returnInternalBlockingConnection(m, rpcConnection, false);
    throw e;
  }
  finally {
    lock.readLock().unlock();
  }
}

代码示例来源:origin: sitewhere/sitewhere

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
  ServerCallHandler<ReqT, RespT> next) {
if (headers.containsKey(TenantTokenClientInterceptor.TENANT_ID_KEY)) {
  String tenantId = headers.get(TenantTokenClientInterceptor.TENANT_ID_KEY);
  LOGGER.trace("Server received tenant id key: " + tenantId);
  Context ctx = Context.current().withValue(GrpcContextKeys.TENANT_ID_KEY, tenantId);
  return Contexts.interceptCall(ctx, call, headers, next);
} else {
  call.close(Status.UNAUTHENTICATED.withDescription("Tenant token not passed in metadata."), headers);
  return new ServerCall.Listener<ReqT>() {
  };
}
}

代码示例来源:origin: lumongo/lumongo

public StoreResponse executeStore(Member m, StoreRequest request) throws Exception {
  ReadWriteLock lock = getLockForMember(m);
  lock.readLock().lock();
  InternalRpcConnection rpcConnection = null;
  try {
    rpcConnection = getInternalRpcConnection(m);
    StoreResponse response = rpcConnection.getService().store(request);
    returnInternalBlockingConnection(m, rpcConnection, true);
    return response;
  }
  catch (StatusRuntimeException e) {
    Metadata trailers = e.getTrailers();
    if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
      throw new Exception(trailers.get(MetaKeys.ERROR_KEY));
    }
    else {
      throw e;
    }
  }
  catch (Exception e) {
    returnInternalBlockingConnection(m, rpcConnection, false);
    throw e;
  }
  finally {
    lock.readLock().unlock();
  }
}

代码示例来源:origin: lumongo/lumongo

public DeleteResponse executeDelete(Member m, DeleteRequest request) throws Exception {
  ReadWriteLock lock = getLockForMember(m);
  lock.readLock().lock();
  InternalRpcConnection rpcConnection = null;
  try {
    rpcConnection = getInternalRpcConnection(m);
    DeleteResponse response = rpcConnection.getService().delete(request);
    returnInternalBlockingConnection(m, rpcConnection, true);
    return response;
  }
  catch (StatusRuntimeException e) {
    Metadata trailers = e.getTrailers();
    if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
      throw new Exception(trailers.get(MetaKeys.ERROR_KEY));
    }
    else {
      throw e;
    }
  }
  catch (Exception e) {
    returnInternalBlockingConnection(m, rpcConnection, false);
    throw e;
  }
  finally {
    lock.readLock().unlock();
  }
}

代码示例来源:origin: lumongo/lumongo

public OptimizeResponse optimize(Member m, OptimizeRequest request) throws Exception {
  ReadWriteLock lock = getLockForMember(m);
  lock.readLock().lock();
  InternalRpcConnection rpcConnection = null;
  try {
    rpcConnection = getInternalRpcConnection(m);
    OptimizeResponse response = rpcConnection.getService().optimize(request);
    returnInternalBlockingConnection(m, rpcConnection, true);
    return response;
  }
  catch (StatusRuntimeException e) {
    Metadata trailers = e.getTrailers();
    if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
      throw new Exception(trailers.get(MetaKeys.ERROR_KEY));
    }
    else {
      throw e;
    }
  }
  catch (Exception e) {
    returnInternalBlockingConnection(m, rpcConnection, false);
    throw e;
  }
  finally {
    lock.readLock().unlock();
  }
}

代码示例来源:origin: com.xorlev.grpc-jersey/jersey-rpc-support

RESPONSE_HEADERS, HashMultimap.create());
boolean sideChannelOn = headers.containsKey(HEADERS_KEY);
ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT> simpleForwardingServerCall =
    new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {

代码示例来源:origin: com.google.cloud/google-cloud-spanner

static long extractRetryDelay(Throwable cause) {
  if (cause != null) {
   Metadata trailers = Status.trailersFromThrowable(cause);
   if (trailers != null && trailers.containsKey(KEY_RETRY_INFO)) {
    RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO);
    if (retryInfo.hasRetryDelay()) {
     return Durations.toMillis(retryInfo.getRetryDelay());
    }
   }
  }
  return -1L;
 }
}

代码示例来源:origin: pravega/pravega

@Override
  public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, final Metadata headers,
                                 ServerCallHandler<ReqT, RespT> next) {
    // Check if this RPC has tags to track request (e.g., older clients).
    if (headers != null && headers.containsKey(DESCRIPTOR_HEADER) && headers.containsKey(ID_HEADER)) {
      RequestTag requestTag = new RequestTag(headers.get(DESCRIPTOR_HEADER), Long.parseLong(headers.get(ID_HEADER)));
      requestTracker.trackRequest(requestTag);
      log.debug(requestTag.getRequestId(), "Received tag from RPC request {}.",
          requestTag.getRequestDescriptor());
    } else {
      log.debug("No tags provided for call {} in headers: {}.", call.getMethodDescriptor().getFullMethodName(),
          headers);
    }
    return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
      @Override
      public void sendHeaders(Metadata responseHeaders) {
        super.sendHeaders(responseHeaders);
      }
    }, headers);
  }
}

代码示例来源:origin: com.xorlev.grpc-jersey/jersey-rpc-support

public static GrpcError throwableToStatus(Throwable t) {
  Status status = Status.fromThrowable(t);
  if (t instanceof InvalidProtocolBufferException) {
    status = Status.INVALID_ARGUMENT.withCause(t);
  }
  Metadata trailer = Status.trailersFromThrowable(t);
  int statusCode = grpcToHttpStatus(status);
  com.google.rpc.Status.Builder payload = com.google.rpc.Status.newBuilder();
  payload.setCode(status.getCode().value());
  StringBuilder errorMessage = new StringBuilder(
      "HTTP " + statusCode + " (gRPC: " + status.getCode().name() + ")");
  if (!Strings.isNullOrEmpty(status.getDescription())) {
    errorMessage.append(": ").append(Strings.nullToEmpty(status.getDescription()));
  }
  payload.setMessage(errorMessage.toString());
  if (trailer != null) {
    if (trailer.containsKey(RETRY_INFO_KEY)) {
      RetryInfo retryInfo = trailer.get(RETRY_INFO_KEY);
      payload.addDetails(Any.pack(retryInfo));
    }
    if (trailer.containsKey(DEBUG_INFO_KEY)) {
      DebugInfo debugInfo = trailer.get(DEBUG_INFO_KEY);
      payload.addDetails(Any.pack(debugInfo));
    }
  }
  return new GrpcError(
      status,
      payload.build()
  );
}

代码示例来源:origin: pravega/pravega

assertFalse(headers.containsKey(RPCTracingHelpers.DESCRIPTOR_HEADER));
assertFalse(headers.containsKey(RPCTracingHelpers.ID_HEADER));

相关文章