View Javadoc

1   /*
2    * ====================================================================
3    *
4    *  Licensed to the Apache Software Foundation (ASF) under one or more
5    *  contributor license agreements.  See the NOTICE file distributed with
6    *  this work for additional information regarding copyright ownership.
7    *  The ASF licenses this file to You under the Apache License, Version 2.0
8    *  (the "License"); you may not use this file except in compliance with
9    *  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, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   * ====================================================================
19   *
20   * This software consists of voluntary contributions made by many
21   * individuals on behalf of the Apache Software Foundation.  For more
22   * information on the Apache Software Foundation, please see
23   * <http://www.apache.org/>.
24   *
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  
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          if (username == null) {
56              throw new IllegalArgumentException("User name may not be null");
57          }
58          this.username = username;
59          if (domain != null) {
60              this.domain = domain.toUpperCase(Locale.ENGLISH);
61          } else {
62              this.domain = null;
63          }
64          if (this.domain != null && this.domain.length() > 0) {
65              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      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(Object o) {
97          if (this == o) return true;
98          if (o instanceof NTUserPrincipal) {
99              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 }