View Javadoc
1   /*
2    * Copyright 2020 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.handler.ssl.util;
18  
19  import static io.netty.util.internal.ObjectUtil.checkNotNull;
20  import static io.netty.util.internal.ObjectUtil.checkNotNullWithIAE;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  /**
27   * A builder for creating {@link FingerprintTrustManagerFactory}.
28   */
29  public final class FingerprintTrustManagerFactoryBuilder {
30  
31      /**
32       * A hash algorithm for fingerprints.
33       */
34      private final String algorithm;
35  
36      /**
37       * A list of fingerprints.
38       */
39      private final List<String> fingerprints = new ArrayList<String>();
40  
41      /**
42       * Creates a builder.
43       *
44       * @param algorithm a hash algorithm
45       */
46      FingerprintTrustManagerFactoryBuilder(String algorithm) {
47          this.algorithm = checkNotNull(algorithm, "algorithm");
48      }
49  
50      /**
51       * Adds fingerprints.
52       *
53       * @param fingerprints a number of fingerprints
54       * @return the same builder
55       */
56      public FingerprintTrustManagerFactoryBuilder fingerprints(CharSequence... fingerprints) {
57          return fingerprints(Arrays.asList(checkNotNull(fingerprints, "fingerprints")));
58      }
59  
60      /**
61       * Adds fingerprints.
62       *
63       * @param fingerprints a number of fingerprints
64       * @return the same builder
65       */
66      public FingerprintTrustManagerFactoryBuilder fingerprints(Iterable<? extends CharSequence> fingerprints) {
67          checkNotNull(fingerprints, "fingerprints");
68          for (CharSequence fingerprint : fingerprints) {
69              checkNotNullWithIAE(fingerprint, "fingerprint");
70              this.fingerprints.add(fingerprint.toString());
71          }
72          return this;
73      }
74  
75      /**
76       * Creates a {@link FingerprintTrustManagerFactory}.
77       *
78       * @return a new {@link FingerprintTrustManagerFactory}
79       */
80      public FingerprintTrustManagerFactory build() {
81          if (fingerprints.isEmpty()) {
82              throw new IllegalStateException("No fingerprints provided");
83          }
84          return new FingerprintTrustManagerFactory(this.algorithm,
85                                                    FingerprintTrustManagerFactory.toFingerprintArray(this.fingerprints));
86      }
87  }