View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.httpclient;
16  
17  import java.io.Serializable;
18  import java.security.Principal;
19  
20  import org.apache.http.auth.Credentials;
21  import org.apache.http.auth.UsernamePasswordCredentials;
22  import org.apache.http.util.LangUtils;
23  
24  /**
25   * Wrapper for {@link UsernamePasswordCredentials} to avoid direct references spread around.
26   *
27   * @author Ronald Brill
28   * @author Lai Quang Duong
29   */
30  public class HtmlUnitUsernamePasswordCredentials implements Credentials, Serializable {
31  
32      private final UsernamePasswordCredentials httpClientUsernamePasswordCredentials_;
33  
34      /**
35       * The constructor with the username and password arguments.
36       *
37       * @param userName the user name
38       * @param password the password
39       */
40      public HtmlUnitUsernamePasswordCredentials(final String userName, final char[] password) {
41          httpClientUsernamePasswordCredentials_ = new UsernamePasswordCredentials(
42                  userName,
43                  password == null ? null : String.valueOf(password));
44      }
45  
46      @Override
47      public String getPassword() {
48          return httpClientUsernamePasswordCredentials_.getPassword();
49      }
50  
51      @Override
52      public Principal getUserPrincipal() {
53          return httpClientUsernamePasswordCredentials_.getUserPrincipal();
54      }
55  
56      @Override
57      public int hashCode() {
58          return getUserPrincipal().hashCode();
59      }
60  
61      @Override
62      public boolean equals(final Object o) {
63          if (this == o) {
64              return true;
65          }
66          if (o instanceof HtmlUnitUsernamePasswordCredentials) {
67              final HtmlUnitUsernamePasswordCredentials that = (HtmlUnitUsernamePasswordCredentials) o;
68              if (LangUtils.equals(this.getUserPrincipal(), that.getUserPrincipal())) {
69                  return true;
70              }
71          }
72          return false;
73      }
74  
75      @Override
76      public String toString() {
77          return getUserPrincipal().toString();
78      }
79  }