• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc;
18 
19 import static java.util.Collections.unmodifiableList;
20 
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 
25 /**
26  * Provides a list of {@link ChannelCredentials}, where any one may be used. The credentials are in
27  * preference order.
28  */
29 public final class ChoiceChannelCredentials extends ChannelCredentials {
30   /**
31    * Constructs with the provided {@code creds} as options, with preferred credentials first.
32    *
33    * @throws IllegalArgumentException if no creds are provided
34    */
create(ChannelCredentials... creds)35   public static ChannelCredentials create(ChannelCredentials... creds) {
36     if (creds.length == 0) {
37       throw new IllegalArgumentException("At least one credential is required");
38     }
39     for (ChannelCredentials cred : creds) {
40       if (cred == null) {
41         throw new NullPointerException();
42       }
43     }
44     return new ChoiceChannelCredentials(unmodifiableList(new ArrayList<>(Arrays.asList(creds))));
45   }
46 
47   private final List<ChannelCredentials> creds;
48 
ChoiceChannelCredentials(List<ChannelCredentials> creds)49   private ChoiceChannelCredentials(List<ChannelCredentials> creds) {
50     this.creds = creds;
51   }
52 
53   /** Non-empty list of credentials, in preference order. */
getCredentialsList()54   public List<ChannelCredentials> getCredentialsList() {
55     return creds;
56   }
57 
58   @Override
withoutBearerTokens()59   public ChannelCredentials withoutBearerTokens() {
60     List<ChannelCredentials> credsWithoutTokens = new ArrayList<>();
61     for (ChannelCredentials cred : creds) {
62       credsWithoutTokens.add(cred.withoutBearerTokens());
63     }
64     return new ChoiceChannelCredentials(unmodifiableList(credsWithoutTokens));
65   }
66 }
67