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