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.ROOT);
59          } else {
60              this.domain = null;
61          }
62          if (this.domain != null && !this.domain.isEmpty()) {
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      @Override
74      public String getName() {
75          return this.ntname;
76      }
77  
78      public String getDomain() {
79          return this.domain;
80      }
81  
82      public String getUsername() {
83          return this.username;
84      }
85  
86      @Override
87      public int hashCode() {
88          int hash = LangUtils.HASH_SEED;
89          hash = LangUtils.hashCode(hash, this.username);
90          hash = LangUtils.hashCode(hash, this.domain);
91          return hash;
92      }
93  
94      @Override
95      public boolean equals(final Object o) {
96          if (this == o) {
97              return true;
98          }
99          if (o instanceof NTUserPrincipal) {
100             final NTUserPrincipal that = (NTUserPrincipal) o;
101             if (LangUtils.equals(this.username, that.username)
102                     && LangUtils.equals(this.domain, that.domain)) {
103                 return true;
104             }
105         }
106         return false;
107     }
108 
109     @Override
110     public String toString() {
111         return this.ntname;
112     }
113 
114 }