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.Map;
23  import java.util.Objects;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  import org.eclipse.aether.repository.Authentication;
28  import org.eclipse.aether.repository.AuthenticationContext;
29  import org.eclipse.aether.repository.AuthenticationDigest;
30  
31  /**
32   * Authentication block that manages a single authentication key and its string value.
33   */
34  final class StringAuthentication
35      implements Authentication
36  {
37  
38      private final String key;
39  
40      private final String value;
41  
42      StringAuthentication( String key, String value )
43      {
44          this.key = requireNonNull( key, "authentication key cannot be null" );
45          if ( key.length() == 0 )
46          {
47              throw new IllegalArgumentException( "authentication key cannot be empty" );
48          }
49          this.value = value;
50      }
51  
52      public void fill( AuthenticationContext context, String key, Map<String, String> data )
53      {
54          requireNonNull( context, "context cannot be null" );
55          context.put( this.key, value );
56      }
57  
58      public void digest( AuthenticationDigest digest )
59      {
60          requireNonNull( digest, "digest cannot be null" );
61          digest.update( key, value );
62      }
63  
64      @Override
65      public boolean equals( Object obj )
66      {
67          if ( this == obj )
68          {
69              return true;
70          }
71          if ( obj == null || !getClass().equals( obj.getClass() ) )
72          {
73              return false;
74          }
75          StringAuthentication that = (StringAuthentication) obj;
76          return Objects.equals( key, that.key )
77                  && Objects.equals( value, that.value );
78      }
79  
80      @Override
81      public int hashCode()
82      {
83          int hash = 17;
84          hash = hash * 31 + key.hashCode();
85          hash = hash * 31 + ( ( value != null ) ? value.hashCode() : 0 );
86          return hash;
87      }
88  
89      @Override
90      public String toString()
91      {
92          return key + "=" + value;
93      }
94  
95  }