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  /**
31   * Authentication that aggregates other authentication blocks. When multiple input authentication blocks provide the
32   * same authentication key, the last written value wins.
33   */
34  final class ChainedAuthentication
35      implements Authentication
36  {
37  
38      private final Authentication[] authentications;
39  
40      ChainedAuthentication( Authentication... authentications )
41      {
42          if ( authentications != null && authentications.length > 0 )
43          {
44              this.authentications = authentications.clone();
45          }
46          else
47          {
48              this.authentications = new Authentication[0];
49          }
50      }
51  
52      ChainedAuthentication( Collection<? extends Authentication> authentications )
53      {
54          if ( authentications != null && !authentications.isEmpty() )
55          {
56              this.authentications = authentications.toArray( new Authentication[authentications.size()] );
57          }
58          else
59          {
60              this.authentications = new Authentication[0];
61          }
62      }
63  
64      public void fill( AuthenticationContext context, String key, Map<String, String> data )
65      {
66          for ( Authentication authentication : authentications )
67          {
68              authentication.fill( context, key, data );
69          }
70      }
71  
72      public void digest( AuthenticationDigest digest )
73      {
74          for ( Authentication authentication : authentications )
75          {
76              authentication.digest( digest );
77          }
78      }
79  
80      @Override
81      public boolean equals( Object obj )
82      {
83          if ( this == obj )
84          {
85              return true;
86          }
87          if ( obj == null || !getClass().equals( obj.getClass() ) )
88          {
89              return false;
90          }
91          ChainedAuthentication that = (ChainedAuthentication) obj;
92          return Arrays.equals( authentications, that.authentications );
93      }
94  
95      @Override
96      public int hashCode()
97      {
98          return Arrays.hashCode( authentications );
99      }
100 
101     @Override
102     public String toString()
103     {
104         StringBuilder buffer = new StringBuilder( 256 );
105         for ( Authentication authentication : authentications )
106         {
107             if ( buffer.length() > 0 )
108             {
109                 buffer.append( ", " );
110             }
111             buffer.append( authentication );
112         }
113         return buffer.toString();
114     }
115 
116 }