类io.grpc.ClientInterceptors.CheckedForwardingClientCall源码实例Demo

下面列出了怎么用io.grpc.ClientInterceptors.CheckedForwardingClientCall的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: grpc-swagger   文件: ChannelFactory.java
private static ClientInterceptor metadataInterceptor(Map<String, Object> metaDataMap) {
    return new ClientInterceptor() {
        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
                final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {

            return new CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
                @Override
                protected void checkedStart(Listener<RespT> responseListener, Metadata headers) {
                    metaDataMap.forEach((k, v) -> {
                        Key<String> mKey = Key.of(k, ASCII_STRING_MARSHALLER);
                        headers.put(mKey, String.valueOf(v));
                    });
                    delegate().start(responseListener, headers);
                }
            };
        }
    };
}
 
源代码2 项目: grpc-swagger   文件: ChannelFactory.java
private static ClientInterceptor metadataInterceptor(Map<String, Object> metaDataMap) {
    return new ClientInterceptor() {
        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
                final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {

            return new CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
                @Override
                protected void checkedStart(Listener<RespT> responseListener, Metadata headers) {
                    metaDataMap.forEach((k, v) -> {
                        Key<String> mKey = Key.of(k, ASCII_STRING_MARSHALLER);
                        headers.put(mKey, String.valueOf(v));
                    });
                    delegate().start(responseListener, headers);
                }
            };
        }
    };
}
 
源代码3 项目: grpc-nebula-java   文件: TesterActivity.java
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
  return new CheckedForwardingClientCall<ReqT, RespT>(
      next.newCall(method.toBuilder().setSafe(true).build(), callOptions)) {
    @Override
    public void checkedStart(Listener<RespT> responseListener, Metadata headers) {
      delegate().start(responseListener, headers);
    }
  };
}
 
源代码4 项目: grpc-nebula-java   文件: ClientAuthInterceptor.java
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
  // TODO(ejona86): If the call fails for Auth reasons, this does not properly propagate info that
  // would be in WWW-Authenticate, because it does not yet have access to the header.
  return new 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 (ClientAuthInterceptor.this) {
        // TODO(louiscryan): This is icky but the current auth library stores the same
        // metadata map until the next refresh cycle. This will be fixed once
        // https://github.com/google/google-auth-library-java/issues/3
        // is resolved.
        // getRequestMetadata() 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 cache its
        // return value based on the URI.
        Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
        if (lastMetadata == null || lastMetadata != latestMetadata) {
          lastMetadata = latestMetadata;
          cached = toHeaders(lastMetadata);
        }
        cachedSaved = cached;
      }
      headers.merge(cachedSaved);
      delegate().start(responseListener, headers);
    }
  };
}
 
源代码5 项目: grpc-java   文件: ClientAuthInterceptor.java
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
  // TODO(ejona86): If the call fails for Auth reasons, this does not properly propagate info that
  // would be in WWW-Authenticate, because it does not yet have access to the header.
  return new 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 (ClientAuthInterceptor.this) {
        // TODO(louiscryan): This is icky but the current auth library stores the same
        // metadata map until the next refresh cycle. This will be fixed once
        // https://github.com/google/google-auth-library-java/issues/3
        // is resolved.
        // getRequestMetadata() 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 cache its
        // return value based on the URI.
        Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
        if (lastMetadata == null || lastMetadata != latestMetadata) {
          lastMetadata = latestMetadata;
          cached = toHeaders(lastMetadata);
        }
        cachedSaved = cached;
      }
      headers.merge(cachedSaved);
      delegate().start(responseListener, headers);
    }
  };
}
 
源代码6 项目: grpc-nebula-java   文件: ClientInterceptorsTest.java
@Test
public void exceptionInStart() {
  final Exception error = new Exception("emulated error");
  ClientInterceptor interceptor = new ClientInterceptor() {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions,
        Channel next) {
      ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
      return new CheckedForwardingClientCall<ReqT, RespT>(call) {
        @Override
        protected void checkedStart(ClientCall.Listener<RespT> responseListener, Metadata headers)
            throws Exception {
          throw error;
          // delegate().start will not be called
        }
      };
    }
  };
  Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
  @SuppressWarnings("unchecked")
  ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
  ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
  assertNotSame(call, interceptedCall);
  interceptedCall.start(listener, new Metadata());
  interceptedCall.sendMessage(null /*request*/);
  interceptedCall.halfClose();
  interceptedCall.request(1);
  call.done = true;
  ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
  verify(listener).onClose(captor.capture(), any(Metadata.class));
  assertSame(error, captor.getValue().getCause());

  // Make sure nothing bad happens after the exception.
  ClientCall<?, ?> noop = ((CheckedForwardingClientCall<?, ?>)interceptedCall).delegate();
  // Should not throw, even on bad input
  noop.cancel("Cancel for test", null);
  noop.start(null, null);
  noop.request(-1);
  noop.halfClose();
  noop.sendMessage(null);
  assertFalse(noop.isReady());
}
 
源代码7 项目: grpc-java   文件: ClientInterceptorsTest.java
@Test
public void exceptionInStart() {
  final Exception error = new Exception("emulated error");
  ClientInterceptor interceptor = new ClientInterceptor() {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions,
        Channel next) {
      ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
      return new CheckedForwardingClientCall<ReqT, RespT>(call) {
        @Override
        protected void checkedStart(ClientCall.Listener<RespT> responseListener, Metadata headers)
            throws Exception {
          throw error;
          // delegate().start will not be called
        }
      };
    }
  };
  Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
  @SuppressWarnings("unchecked")
  ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
  ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
  assertNotSame(call, interceptedCall);
  interceptedCall.start(listener, new Metadata());
  interceptedCall.sendMessage(null /*request*/);
  interceptedCall.halfClose();
  interceptedCall.request(1);
  call.done = true;
  ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
  verify(listener).onClose(captor.capture(), any(Metadata.class));
  assertSame(error, captor.getValue().getCause());

  // Make sure nothing bad happens after the exception.
  ClientCall<?, ?> noop = ((CheckedForwardingClientCall<?, ?>)interceptedCall).delegate();
  // Should not throw, even on bad input
  noop.cancel("Cancel for test", null);
  noop.start(null, null);
  noop.request(-1);
  noop.halfClose();
  noop.sendMessage(null);
  assertFalse(noop.isReady());
}
 
 类所在包
 类方法
 同包方法