View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.auth;
28  
29  import java.io.Serializable;
30  import java.security.Principal;
31  import java.util.Locale;
32  import java.util.Objects;
33  
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.util.Args;
37  import org.apache.hc.core5.util.LangUtils;
38  
39  /**
40   * Microsoft Windows specific user principal implementation.
41   *
42   * @since 4.0
43   */
44  @Contract(threading = ThreadingBehavior.IMMUTABLE)
45  public class NTUserPrincipal implements Principal, Serializable {
46  
47      private static final long serialVersionUID = -6870169797924406894L;
48  
49      private final String username;
50      private final String domain;
51      private final String ntname;
52  
53      public NTUserPrincipal(
54              final String domain,
55              final String username) {
56          super();
57          Args.notNull(username, "User name");
58          this.username = username;
59          if (domain != null) {
60              this.domain = domain.toUpperCase(Locale.ROOT);
61          } else {
62              this.domain = null;
63          }
64          if (this.domain != null && !this.domain.isEmpty()) {
65              final StringBuilder buffer = new StringBuilder();
66              buffer.append(this.domain);
67              buffer.append('\\');
68              buffer.append(this.username);
69              this.ntname = buffer.toString();
70          } else {
71              this.ntname = this.username;
72          }
73      }
74  
75      @Override
76      public String getName() {
77          return this.ntname;
78      }
79  
80      public String getDomain() {
81          return this.domain;
82      }
83  
84      public String getUsername() {
85          return this.username;
86      }
87  
88      @Override
89      public int hashCode() {
90          int hash = LangUtils.HASH_SEED;
91          hash = LangUtils.hashCode(hash, this.username);
92          hash = LangUtils.hashCode(hash, this.domain);
93          return hash;
94      }
95  
96      @Override
97      public boolean equals(final Object o) {
98          if (this == o) {
99              return true;
100         }
101         if (o instanceof NTUserPrincipal) {
102             final NTUserPrincipal that = (NTUserPrincipal) o;
103             return Objects.equals(this.username, that.username)
104                     && Objects.equals(this.domain, that.domain);
105         }
106         return false;
107     }
108 
109     @Override
110     public String toString() {
111         return this.ntname;
112     }
113 
114 }