io.grpc.ClientInterceptors.CheckedForwardingClientCall Java Examples

The following examples show how to use io.grpc.ClientInterceptors.CheckedForwardingClientCall. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChannelFactory.java    From grpc-swagger with MIT License 7 votes vote down vote up
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);
                }
            };
        }
    };
}
 
Example #2
Source File: ChannelFactory.java    From grpc-swagger with MIT License 6 votes vote down vote up
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);
                }
            };
        }
    };
}
 
Example #3
Source File: TesterActivity.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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);
    }
  };
}
 
Example #4
Source File: ClientAuthInterceptor.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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);
    }
  };
}
 
Example #5
Source File: ClientAuthInterceptor.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@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);
    }
  };
}
 
Example #6
Source File: ClientInterceptorsTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example #7
Source File: ClientInterceptorsTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@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());
}