View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.repository;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Map;
24  
25  import org.eclipse.aether.repository.Authentication;
26  import org.eclipse.aether.repository.AuthenticationContext;
27  import org.eclipse.aether.repository.AuthenticationDigest;
28  
29  import static java.util.Objects.requireNonNull;
30  
31  /**
32   * Authentication that aggregates other authentication blocks. When multiple input authentication blocks provide the
33   * same authentication key, the last written value wins.
34   */
35  final class ChainedAuthentication implements Authentication {
36  
37      private final Authentication[] authentications;
38  
39      ChainedAuthentication(Authentication... authentications) {
40          if (authentications != null && authentications.length > 0) {
41              this.authentications = authentications.clone();
42          } else {
43              this.authentications = new Authentication[0];
44          }
45      }
46  
47      ChainedAuthentication(Collection<? extends Authentication> authentications) {
48          if (authentications != null && !authentications.isEmpty()) {
49              this.authentications = authentications.toArray(new Authentication[0]);
50          } else {
51              this.authentications = new Authentication[0];
52          }
53      }
54  
55      public void fill(AuthenticationContext context, String key, Map<String, String> data) {
56          requireNonNull(context, "context cannot be null");
57          for (Authentication authentication : authentications) {
58              authentication.fill(context, key, data);
59          }
60      }
61  
62      public void digest(AuthenticationDigest digest) {
63          requireNonNull(digest, "digest cannot be null");
64          for (Authentication authentication : authentications) {
65              authentication.digest(digest);
66          }
67      }
68  
69      @Override
70      public boolean equals(Object obj) {
71          if (this == obj) {
72              return true;
73          }
74          if (obj == null || !getClass().equals(obj.getClass())) {
75              return false;
76          }
77          ChainedAuthentication that = (ChainedAuthentication) obj;
78          return Arrays.equals(authentications, that.authentications);
79      }
80  
81      @Override
82      public int hashCode() {
83          return Arrays.hashCode(authentications);
84      }
85  
86      @Override
87      public String toString() {
88          StringBuilder buffer = new StringBuilder(256);
89          for (Authentication authentication : authentications) {
90              if (buffer.length() > 0) {
91                  buffer.append(", ");
92              }
93              buffer.append(authentication);
94          }
95          return buffer.toString();
96      }
97  }