blob: 146621838fe09452c709f8af396befafbcb29d51 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.security.auth;
27
28import java.security.Principal;
29import javax.naming.InvalidNameException;
30import javax.naming.ldap.LdapName;
31
32/**
33 * A principal identified by a distinguished name as specified by
34 * <a href="http://ietf.org//rfc/rfc2253.txt">RFC 2253</a>.
35 *
36 * <p>
37 * After successful authentication, a user {@link java.security.Principal}
38 * can be associated with a particular {@link javax.security.auth.Subject}
39 * to augment that <code>Subject</code> with an additional identity.
40 * Authorization decisions can then be based upon the
41 * <code>Principal</code>s that are associated with a <code>Subject</code>.
42 *
43 * <p>
44 * This class is immutable.
45 *
46 * @since 1.6
47 */
48public final class LdapPrincipal implements Principal, java.io.Serializable {
49
50 private static final long serialVersionUID = 6820120005580754861L;
51
52 /**
53 * The principal's string name
54 *
55 * @serial
56 */
57 private final String nameString;
58
59 /**
60 * The principal's name
61 *
62 * @serial
63 */
64 private final LdapName name;
65
66 /**
67 * Creates an LDAP principal.
68 *
69 * @param name The principal's string distinguished name.
70 * @throws InvalidNameException If a syntax violation is detected.
71 * @exception NullPointerException If the <code>name</code> is
72 * <code>null</code>.
73 */
74 public LdapPrincipal(String name) throws InvalidNameException {
75 if (name == null) {
76 throw new NullPointerException("null name is illegal");
77 }
78 this.name = getLdapName(name);
79 nameString = name;
80 }
81
82 /**
83 * Compares this principal to the specified object.
84 *
85 * @param object The object to compare this principal against.
86 * @return true if they are equal; false otherwise.
87 */
88 public boolean equals(Object object) {
89 if (this == object) {
90 return true;
91 }
92 if (object instanceof LdapPrincipal) {
93 try {
94
95 return
96 name.equals(getLdapName(((LdapPrincipal)object).getName()));
97
98 } catch (InvalidNameException e) {
99 return false;
100 }
101 }
102 return false;
103 }
104
105 /**
106 * Computes the hash code for this principal.
107 *
108 * @return The principal's hash code.
109 */
110 public int hashCode() {
111 return name.hashCode();
112 }
113
114 /**
115 * Returns the name originally used to create this principal.
116 *
117 * @return The principal's string name.
118 */
119 public String getName() {
120 return nameString;
121 }
122
123 /**
124 * Creates a string representation of this principal's name in the format
125 * defined by <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
126 * If the name has zero components an empty string is returned.
127 *
128 * @return The principal's string name.
129 */
130 public String toString() {
131 return name.toString();
132 }
133
134 // Create an LdapName object from a string distinguished name.
135 private LdapName getLdapName(String name) throws InvalidNameException {
136 return new LdapName(name);
137 }
138}