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