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  import java.util.Objects;
23  
24  import org.eclipse.aether.repository.Authentication;
25  import org.eclipse.aether.repository.AuthenticationContext;
26  import org.eclipse.aether.repository.AuthenticationDigest;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * Authentication block that manages a single authentication key and its string value.
32   */
33  final class StringAuthentication implements Authentication {
34  
35      private final String key;
36  
37      private final String value;
38  
39      StringAuthentication(String key, String 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          digest.update(key, value);
55      }
56  
57      @Override
58      public boolean equals(Object obj) {
59          if (this == obj) {
60              return true;
61          }
62          if (obj == null || !getClass().equals(obj.getClass())) {
63              return false;
64          }
65          StringAuthentication that = (StringAuthentication) obj;
66          return Objects.equals(key, that.key) && Objects.equals(value, that.value);
67      }
68  
69      @Override
70      public int hashCode() {
71          int hash = 17;
72          hash = hash * 31 + key.hashCode();
73          hash = hash * 31 + ((value != null) ? value.hashCode() : 0);
74          return hash;
75      }
76  
77      @Override
78      public String toString() {
79          return key + "=" + value;
80      }
81  }