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.http.auth;
28  
29  import java.io.Serializable;
30  import java.security.Principal;
31  import java.util.Locale;
32  
33  import org.apache.http.annotation.Contract;
34  import org.apache.http.annotation.ThreadingBehavior;
35  import org.apache.http.util.Args;
36  import org.apache.http.util.LangUtils;
37  
38  /**
39   * Microsoft Windows specific user principal implementation.
40   *
41   * @since 4.0
42   */
43  @Contract(threading = ThreadingBehavior.IMMUTABLE)
44  public class NTUserPrincipal implements Principal, Serializable {
45  
46      private static final long serialVersionUID = -6870169797924406894L;
47  
48      private final String username;
49      private final String domain;
50      private final String ntname;
51  
52      public NTUserPrincipal(
53              final String domain,
54              final String username) {
55          super();
56          Args.notNull(username, "User name");
57          this.username = username;
58          if (domain != null) {
59              this.domain = domain.toUpperCase(Locale.ROOT);
60          } else {
61              this.domain = null;
62          }
63          if (this.domain != null && !this.domain.isEmpty()) {
64              final StringBuilder buffer = new StringBuilder();
65              buffer.append(this.domain);
66              buffer.append('\\');
67              buffer.append(this.username);
68              this.ntname = buffer.toString();
69          } else {
70              this.ntname = this.username;
71          }
72      }
73  
74      @Override
75      public String getName() {
76          return this.ntname;
77      }
78  
79      public String getDomain() {
80          return this.domain;
81      }
82  
83      public String getUsername() {
84          return this.username;
85      }
86  
87      @Override
88      public int hashCode() {
89          int hash = LangUtils.HASH_SEED;
90          hash = LangUtils.hashCode(hash, this.username);
91          hash = LangUtils.hashCode(hash, this.domain);
92          return hash;
93      }
94  
95      @Override
96      public boolean equals(final Object o) {
97          if (this == o) {
98              return true;
99          }
100         if (o instanceof NTUserPrincipal) {
101             final NTUserPrincipal that = (NTUserPrincipal) o;
102             if (LangUtils.equals(this.username, that.username)
103                     && LangUtils.equals(this.domain, that.domain)) {
104                 return true;
105             }
106         }
107         return false;
108     }
109 
110     @Override
111     public String toString() {
112         return this.ntname;
113     }
114 
115 }