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