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  
32  import org.apache.http.annotation.Immutable;
33  
34  import org.apache.http.util.LangUtils;
35  
36  /**
37   * Basic user principal used for HTTP authentication
38   *
39   * @since 4.0
40   */
41  @Immutable
42  public final class BasicUserPrincipal implements Principal, Serializable {
43  
44      private static final long serialVersionUID = -2266305184969850467L;
45  
46      private final String username;
47  
48      public BasicUserPrincipal(final String username) {
49          super();
50          if (username == null) {
51              throw new IllegalArgumentException("User name may not be null");
52          }
53          this.username = username;
54      }
55  
56      public String getName() {
57          return this.username;
58      }
59  
60      @Override
61      public int hashCode() {
62          int hash = LangUtils.HASH_SEED;
63          hash = LangUtils.hashCode(hash, this.username);
64          return hash;
65      }
66  
67      @Override
68      public boolean equals(Object o) {
69          if (this == o) return true;
70          if (o instanceof BasicUserPrincipal) {
71              BasicUserPrincipal that = (BasicUserPrincipal) o;
72              if (LangUtils.equals(this.username, that.username)) {
73                  return true;
74              }
75          }
76          return false;
77      }
78  
79      @Override
80      public String toString() {
81          StringBuilder buffer = new StringBuilder();
82          buffer.append("[principal: ");
83          buffer.append(this.username);
84          buffer.append("]");
85          return buffer.toString();
86      }
87  
88  }
89