Java Code Examples for io.grpc.Metadata#getAll()

The following examples show how to use io.grpc.Metadata#getAll() . 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: ClientAuthInterceptorTest.java    From grpc-nebula-java with Apache License 2.0 7 votes vote down vote up
@Test
public void testCopyCredentialToHeaders() throws IOException {
  ListMultimap<String, String> values = LinkedListMultimap.create();
  values.put("Authorization", "token1");
  values.put("Authorization", "token2");
  values.put("Extra-Authorization", "token3");
  values.put("Extra-Authorization", "token4");
  when(credentials.getRequestMetadata(any(URI.class))).thenReturn(Multimaps.asMap(values));
  ClientCall<String, Integer> interceptedCall =
      interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
  Metadata headers = new Metadata();
  interceptedCall.start(listener, headers);
  assertEquals(listener, call.responseListener);
  assertEquals(headers, call.headers);

  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  Assert.assertArrayEquals(new String[]{"token1", "token2"},
      Iterables.toArray(authorization, String.class));
  Iterable<String> extraAuthorization = headers.getAll(EXTRA_AUTHORIZATION);
  Assert.assertArrayEquals(new String[]{"token3", "token4"},
      Iterables.toArray(extraAuthorization, String.class));
}
 
Example 2
Source File: ClientAuthInterceptorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithOAuth2Credential() {
  final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
  final OAuth2Credentials oAuth2Credentials = new OAuth2Credentials() {
    @Override
    public AccessToken refreshAccessToken() throws IOException {
      return token;
    }
  };
  interceptor = new ClientAuthInterceptor(oAuth2Credentials, executor);
  ClientCall<String, Integer> interceptedCall =
      interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
  Metadata headers = new Metadata();
  interceptedCall.start(listener, headers);
  assertEquals(listener, call.responseListener);
  assertEquals(headers, call.headers);
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  Assert.assertArrayEquals(new String[]{"Bearer allyourbase"},
      Iterables.toArray(authorization, String.class));
}
 
Example 3
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void copyCredentialsToHeaders() throws Exception {
  ListMultimap<String, String> values = LinkedListMultimap.create();
  values.put("Authorization", "token1");
  values.put("Authorization", "token2");
  values.put("Extra-Authorization-bin", "dG9rZW4z");  // bytes "token3" in base64
  values.put("Extra-Authorization-bin", "dG9rZW40");  // bytes "token4" in base64
  when(credentials.getRequestMetadata(eq(expectedUri))).thenReturn(Multimaps.asMap(values));

  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials);
  callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);

  verify(credentials).getRequestMetadata(eq(expectedUri));
  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"token1", "token2"},
      Iterables.toArray(authorization, String.class));
  Iterable<byte[]> extraAuthorization = headers.getAll(EXTRA_AUTHORIZATION);
  assertEquals(2, Iterables.size(extraAuthorization));
  assertArrayEquals("token3".getBytes(US_ASCII), Iterables.get(extraAuthorization, 0));
  assertArrayEquals("token4".getBytes(US_ASCII), Iterables.get(extraAuthorization, 1));
}
 
Example 4
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void oauth2Credential() {
  final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
  final OAuth2Credentials credentials = new OAuth2Credentials() {
    @Override
    public AccessToken refreshAccessToken() throws IOException {
      return token;
    }
  };

  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials);
  callCredentials.applyRequestMetadata(
      new RequestInfoImpl(SecurityLevel.NONE), executor, applier);
  assertEquals(1, runPendingRunnables());

  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"Bearer allyourbase"},
      Iterables.toArray(authorization, String.class));
}
 
Example 5
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void googleCredential_privacyAndIntegrityAllowed() {
  final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
  final Credentials credentials = GoogleCredentials.create(token);

  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials);
  callCredentials.applyRequestMetadata(
      new RequestInfoImpl(SecurityLevel.PRIVACY_AND_INTEGRITY), executor, applier);
  runPendingRunnables();

  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"Bearer allyourbase"},
      Iterables.toArray(authorization, String.class));
}
 
Example 6
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void serviceAccountWithScopeNotToJwt() throws Exception {
  final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
  KeyPair pair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
  @SuppressWarnings("deprecation")
  ServiceAccountCredentials credentials = new ServiceAccountCredentials(
      null, "[email protected]", pair.getPrivate(), null, Arrays.asList("somescope")) {
    @Override
    public AccessToken refreshAccessToken() {
      return token;
    }
  };

  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials);
  callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);
  assertEquals(1, runPendingRunnables());

  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"Bearer allyourbase"},
      Iterables.toArray(authorization, String.class));
}
 
Example 7
Source File: AgentHeaderReader.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
protected List<Integer> getSupportCommandCodeList(Metadata headers) {
    List<Integer> supportCommandCodeList = new ArrayList<Integer>();

    boolean hasHeader = headers.containsKey(Header.SUPPORT_COMMAND_CODE);
    if (!hasHeader) {
        return Header.SUPPORT_COMMAND_CODE_LIST_NOT_EXIST;
    }

    final Iterable<String> codeValues = headers.getAll(Header.SUPPORT_COMMAND_CODE);
    try {
        for (String codeValue : codeValues) {
            final int code = Integer.parseInt(codeValue);
            supportCommandCodeList.add(code);
        }
        return Collections.unmodifiableList(supportCommandCodeList);
    } catch (NumberFormatException e) {
        e.printStackTrace();
        return Header.SUPPORT_COMMAND_CODE_LIST_PARSE_ERROR;
    }
}
 
Example 8
Source File: XdsRoutingLoadBalancer.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
  // Index ASCII headers by keys.
  Map<String, Set<String>> asciiHeaders = new HashMap<>();
  Metadata headers = args.getHeaders();
  for (String headerName : headers.keys()) {
    if (headerName.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
      continue;
    }
    Set<String> headerValues = new HashSet<>();
    Metadata.Key<String> key = Metadata.Key.of(headerName, Metadata.ASCII_STRING_MARSHALLER);
    for (String value : headers.getAll(key)) {
      headerValues.add(value);
    }
    asciiHeaders.put(headerName, headerValues);
  }
  for (Map.Entry<RouteMatch, SubchannelPicker> entry : routePickers.entrySet()) {
    RouteMatch routeMatch = entry.getKey();
    if (routeMatch.matches(
        "/" + args.getMethodDescriptor().getFullMethodName(), asciiHeaders)) {
      return entry.getValue().pickSubchannel(args);
    }
  }
  return PickResult.withError(Status.UNAVAILABLE.withDescription("no matching route found"));
}
 
Example 9
Source File: ClientAuthInterceptorTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCopyCredentialToHeaders() throws IOException {
  ListMultimap<String, String> values = LinkedListMultimap.create();
  values.put("Authorization", "token1");
  values.put("Authorization", "token2");
  values.put("Extra-Authorization", "token3");
  values.put("Extra-Authorization", "token4");
  when(credentials.getRequestMetadata(any(URI.class))).thenReturn(Multimaps.asMap(values));
  ClientCall<String, Integer> interceptedCall =
      interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
  Metadata headers = new Metadata();
  interceptedCall.start(listener, headers);
  assertEquals(listener, call.responseListener);
  assertEquals(headers, call.headers);

  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  Assert.assertArrayEquals(new String[]{"token1", "token2"},
      Iterables.toArray(authorization, String.class));
  Iterable<String> extraAuthorization = headers.getAll(EXTRA_AUTHORIZATION);
  Assert.assertArrayEquals(new String[]{"token3", "token4"},
      Iterables.toArray(extraAuthorization, String.class));
}
 
Example 10
Source File: ClientAuthInterceptorTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithOAuth2Credential() {
  final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
  final OAuth2Credentials oAuth2Credentials = new OAuth2Credentials() {
    @Override
    public AccessToken refreshAccessToken() throws IOException {
      return token;
    }
  };
  interceptor = new ClientAuthInterceptor(oAuth2Credentials, executor);
  ClientCall<String, Integer> interceptedCall =
      interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
  Metadata headers = new Metadata();
  interceptedCall.start(listener, headers);
  assertEquals(listener, call.responseListener);
  assertEquals(headers, call.headers);
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  Assert.assertArrayEquals(new String[]{"Bearer allyourbase"},
      Iterables.toArray(authorization, String.class));
}
 
Example 11
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void copyCredentialsToHeaders() throws Exception {
  ListMultimap<String, String> values = LinkedListMultimap.create();
  values.put("Authorization", "token1");
  values.put("Authorization", "token2");
  values.put("Extra-Authorization-bin", "dG9rZW4z");  // bytes "token3" in base64
  values.put("Extra-Authorization-bin", "dG9rZW40");  // bytes "token4" in base64
  when(credentials.getRequestMetadata(eq(expectedUri))).thenReturn(Multimaps.asMap(values));

  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials);
  callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);

  verify(credentials).getRequestMetadata(eq(expectedUri));
  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"token1", "token2"},
      Iterables.toArray(authorization, String.class));
  Iterable<byte[]> extraAuthorization = headers.getAll(EXTRA_AUTHORIZATION);
  assertEquals(2, Iterables.size(extraAuthorization));
  assertArrayEquals("token3".getBytes(US_ASCII), Iterables.get(extraAuthorization, 0));
  assertArrayEquals("token4".getBytes(US_ASCII), Iterables.get(extraAuthorization, 1));
}
 
Example 12
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void oauth2Credential() {
  final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
  OAuth2Credentials credentials = new OAuth2Credentials() {
    @Override
    public AccessToken refreshAccessToken() throws IOException {
      return token;
    }
  };

  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials);
  callCredentials.applyRequestMetadata(
      new RequestInfoImpl(SecurityLevel.NONE), executor, applier);
  assertEquals(1, runPendingRunnables());

  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"Bearer allyourbase"},
      Iterables.toArray(authorization, String.class));
}
 
Example 13
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void googleCredential_privacyAndIntegrityAllowed() {
  final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
  final Credentials credentials = GoogleCredentials.create(token);

  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials);
  callCredentials.applyRequestMetadata(
      new RequestInfoImpl(SecurityLevel.PRIVACY_AND_INTEGRITY), executor, applier);
  runPendingRunnables();

  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"Bearer allyourbase"},
      Iterables.toArray(authorization, String.class));
}
 
Example 14
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void oauthClassesNotInClassPath() throws Exception {
  ListMultimap<String, String> values = LinkedListMultimap.create();
  values.put("Authorization", "token1");
  when(credentials.getRequestMetadata(eq(expectedUri))).thenReturn(Multimaps.asMap(values));

  assertNull(GoogleAuthLibraryCallCredentials.createJwtHelperOrNull(null));
  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials, null);
  callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);

  verify(credentials).getRequestMetadata(eq(expectedUri));
  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"token1"},
      Iterables.toArray(authorization, String.class));
}
 
Example 15
Source File: GoogleAuthLibraryCallCredentialsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void oauthClassesNotInClassPath() throws Exception {
  ListMultimap<String, String> values = LinkedListMultimap.create();
  values.put("Authorization", "token1");
  when(credentials.getRequestMetadata(eq(expectedUri))).thenReturn(Multimaps.asMap(values));

  assertNull(GoogleAuthLibraryCallCredentials.createJwtHelperOrNull(null));
  GoogleAuthLibraryCallCredentials callCredentials =
      new GoogleAuthLibraryCallCredentials(credentials, null);
  callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);

  verify(credentials).getRequestMetadata(eq(expectedUri));
  verify(applier).apply(headersCaptor.capture());
  Metadata headers = headersCaptor.getValue();
  Iterable<String> authorization = headers.getAll(AUTHORIZATION);
  assertArrayEquals(new String[]{"token1"},
      Iterables.toArray(authorization, String.class));
}