jdk/src/share/classes/javax/security/auth/kerberos/KerberosPrincipal.java
author weijun
Tue, 27 Aug 2013 17:50:52 +0800
changeset 19597 7a3478d94601
parent 18830 90956ead732f
permissions -rw-r--r--
8015669: KerberosPrincipal::equals should ignore name-type Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.security.auth.kerberos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.krb5.PrincipalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.krb5.Realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class encapsulates a Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public final class KerberosPrincipal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    implements java.security.Principal, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final long serialVersionUID = -7374788026156829911L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    //name types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * unknown name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final int KRB_NT_UNKNOWN =   0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * user principal name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final int KRB_NT_PRINCIPAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * service and other unique instance (krbtgt) name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final int KRB_NT_SRV_INST =  2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * service with host name as instance (telnet, rcommands) name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final int KRB_NT_SRV_HST =   3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * service with host as remaining components name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final int KRB_NT_SRV_XHST =  4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * unique ID name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int KRB_NT_UID = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private transient String fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private transient String realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private transient int nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Constructs a KerberosPrincipal from the provided string input. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * name type for this  principal defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * {@link #KRB_NT_PRINCIPAL KRB_NT_PRINCIPAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * This string is assumed to contain a name in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * that is specified in Section 2.1.1. (Kerberos Principal Name Form) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <a href=http://www.ietf.org/rfc/rfc1964.txt> RFC 1964 </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * (for example, <i>duke@FOO.COM</i>, where <i>duke</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * represents a principal, and <i>FOO.COM</i> represents a realm).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <p>If the input name does not contain a realm, the default realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * is used. The default realm can be specified either in a Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * configuration file or via the java.security.krb5.realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * system property. For more information,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <a href="../../../../../technotes/guides/security/jgss/tutorials/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Kerberos Requirements </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param name the principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws IllegalArgumentException if name is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * formatted, if name is null, or if name does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * the realm to use and the default realm is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * in either a Kerberos configuration file or via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * java.security.krb5.realm system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public KerberosPrincipal(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        PrincipalName krb5Principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // Appends the default realm if it is missing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            krb5Principal = new PrincipalName(name, KRB_NT_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        nameType = KRB_NT_PRINCIPAL;  // default name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        fullName = krb5Principal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        realm = krb5Principal.getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Constructs a KerberosPrincipal from the provided string and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * name type input.  The string is assumed to contain a name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * format that is specified in Section 2.1 (Mandatory Name Forms) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Valid name types are specified in Section 6.2 (Principal Names) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * The input name must be consistent with the provided name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * (for example, <i>duke@FOO.COM</i>, is a valid input string for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * name type, KRB_NT_PRINCIPAL where <i>duke</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * represents a principal, and <i>FOO.COM</i> represents a realm).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p> If the input name does not contain a realm, the default realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * is used. The default realm can be specified either in a Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * configuration file or via the java.security.krb5.realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * system property. For more information, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <a href="../../../../../technotes/guides/security/jgss/tutorials/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Kerberos Requirements</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param name the principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param nameType the name type of the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws IllegalArgumentException if name is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * formatted, if name is null, if the nameType is not supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * or if name does not contain the realm to use and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * realm is not specified in either a Kerberos configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * file or via the java.security.krb5.realm system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public KerberosPrincipal(String name, int nameType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        PrincipalName krb5Principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            // Appends the default realm if it is missing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            krb5Principal  = new PrincipalName(name,nameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.nameType = nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        fullName = krb5Principal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        realm = krb5Principal.getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Returns the realm component of this Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return the realm component of this Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public String getRealm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Returns a hashcode for this principal. The hash code is defined to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * be the result of the following  calculation:
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   184
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *  hashCode = getName().hashCode();
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   186
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   188
     * @return a hashCode() for the {@code KerberosPrincipal}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Compares the specified Object with this Principal for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns true if the given object is also a
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   197
     * {@code KerberosPrincipal} and the two
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   198
     * {@code KerberosPrincipal} instances are equivalent.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   199
     * More formally two {@code KerberosPrincipal} instances are equal
19597
7a3478d94601 8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents: 18830
diff changeset
   200
     * if the values returned by {@code getName()} are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param other the Object to compare to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return true if the Object passed in represents the same principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * as this one, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (other == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (! (other instanceof KerberosPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
19597
7a3478d94601 8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents: 18830
diff changeset
   214
        String myFullName = getName();
7a3478d94601 8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents: 18830
diff changeset
   215
        String otherFullName = ((KerberosPrincipal) other).getName();
7a3478d94601 8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents: 18830
diff changeset
   216
        return myFullName.equals(otherFullName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Save the KerberosPrincipal object to a stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   222
     * @serialData this {@code KerberosPrincipal} is serialized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *          by writing out the PrincipalName and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *          realm in their DER-encoded form as specified in Section 5.2.2 of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *          <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private void writeObject(ObjectOutputStream oos)
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   228
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   230
        PrincipalName krb5Principal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        try {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   232
            krb5Principal  = new PrincipalName(fullName, nameType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            oos.writeObject(krb5Principal.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            oos.writeObject(krb5Principal.getRealm().asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } catch (Exception e) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   236
            throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Reads this object from a stream (i.e., deserializes it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private void readObject(ObjectInputStream ois)
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   244
            throws IOException, ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        byte[] asn1EncPrincipal = (byte [])ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        byte[] encRealm = (byte [])ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        try {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   248
           Realm realmObject = new Realm(new DerValue(encRealm));
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   249
           PrincipalName krb5Principal = new PrincipalName(
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   250
                   new DerValue(asn1EncPrincipal), realmObject);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   251
           realm = realmObject.toString();
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   252
           fullName = krb5Principal.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
           nameType = krb5Principal.getNameType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (Exception e) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   255
            throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * The returned string corresponds to the single-string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * representation of a Kerberos Principal name as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Section 2.1 of <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return the principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns the name type of the KerberosPrincipal. Valid name types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * are specified in Section 6.2 of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return the name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public int getNameType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // Inherits javadocs from Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
}