1 /* 2 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.security.auth; 27 28 import java.security.Principal; 29 import javax.naming.InvalidNameException; 30 import javax.naming.ldap.LdapName; 31 32 /** 33 * A principal identified by a distinguished name as specified by 34 * <a href="http://www.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 */ 48 @jdk.Supported 49 public final class LdapPrincipal implements Principal, java.io.Serializable { 50 51 private static final long serialVersionUID = 6820120005580754861L; 52 53 /** 54 * The principal's string name 55 * 56 * @serial 57 */ 58 private final String nameString; 59 60 /** 61 * The principal's name 62 * 63 * @serial 64 */ 65 private final LdapName name; 66 67 /** 68 * Creates an LDAP principal. 69 * 70 * @param name The principal's string distinguished name. 71 * @throws InvalidNameException If a syntax violation is detected. 72 * @exception NullPointerException If the <code>name</code> is 73 * <code>null</code>. 74 */ 75 public LdapPrincipal(String name) throws InvalidNameException { 76 if (name == null) { 77 throw new NullPointerException("null name is illegal"); 78 } 79 this.name = getLdapName(name); 80 nameString = name; 81 } 82 83 /** 84 * Compares this principal to the specified object. 85 * 86 * @param object The object to compare this principal against. 87 * @return true if they are equal; false otherwise. 88 */ 89 public boolean equals(Object object) { 90 if (this == object) { 91 return true; 92 } 93 if (object instanceof LdapPrincipal) { 94 try { 95 96 return 97 name.equals(getLdapName(((LdapPrincipal)object).getName())); 98 99 } catch (InvalidNameException e) { 100 return false; 101 } 102 } 103 return false; 104 } 105 106 /** 107 * Computes the hash code for this principal. 108 * 109 * @return The principal's hash code. 110 */ 111 public int hashCode() { 112 return name.hashCode(); 113 } 114 115 /** 116 * Returns the name originally used to create this principal. 117 * 118 * @return The principal's string name. 119 */ 120 public String getName() { 121 return nameString; 122 } 123 124 /** 125 * Creates a string representation of this principal's name in the format 126 * defined by <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>. 127 * If the name has zero components an empty string is returned. 128 * 129 * @return The principal's string name. 130 */ 131 public String toString() { 132 return name.toString(); 133 } 134 135 // Create an LdapName object from a string distinguished name. 136 private LdapName getLdapName(String name) throws InvalidNameException { 137 return new LdapName(name); 138 } 139 }