src/java.security.jgss/share/classes/sun/security/krb5/Realm.java
author weijun
Tue, 14 Aug 2018 22:39:34 +0800
changeset 51398 3c389a284345
parent 47216 71c04702a3d5
child 58611 53ddf218eddd
permissions -rw-r--r--
8209416: Refactoring GetPropertyAction calls in security libs Reviewed-by: xuelei, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, 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: 3949
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: 3949
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: 3949
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
    34
import sun.security.action.GetBooleanAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.Krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
    38
import java.util.*;
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
    39
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2163
diff changeset
    40
import sun.security.krb5.internal.util.KerberosString;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Implements the ASN.1 Realm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
    45
 * {@code Realm ::= GeneralString}
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
    46
 *
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    47
 * This class is immutable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class Realm implements Cloneable {
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32275
diff changeset
    50
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
    51
    public static final boolean AUTODEDUCEREALM = GetBooleanAction
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
    52
            .privilegedGetProperty("sun.security.krb5.autodeducerealm");
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32275
diff changeset
    53
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    54
    private final String realm; // not null nor empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public Realm(String name) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        realm = parseRealm(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    60
    public static Realm getDefault() throws RealmException {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    61
        try {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    62
            return new Realm(Config.getInstance().getDefaultRealm());
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    63
        } catch (RealmException re) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    64
            throw re;
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    65
        } catch (KrbException ke) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    66
            throw new RealmException(ke);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    67
        }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    68
    }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    69
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    70
    // Immutable class, no need to clone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public Object clone() {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    72
        return this;
2
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 boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (!(obj instanceof Realm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Realm that = (Realm)obj;
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    85
        return this.realm.equals(that.realm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public int hashCode() {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
    89
        return realm.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Constructs a Realm object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param encoding a Der-encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception RealmException if an error occurs while parsing a Realm object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public Realm(DerValue encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throws Asn1Exception, RealmException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IllegalArgumentException("encoding can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2163
diff changeset
   104
        realm = new KerberosString(encoding).toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (realm == null || realm.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new RealmException(Krb5.REALM_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (!isValidRealmString(realm))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RealmException(Krb5.REALM_ILLCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
   115
    // Extract realm from a string like dummy@REALM
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static String parseRealmAtSeparator(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                ("null input name is not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        String temp = new String(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        while (i < temp.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (temp.charAt(i) == PrincipalName.NAME_REALM_SEPARATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (i == 0 || temp.charAt(i - 1) != '\\') {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
   128
                    if (i + 1 < temp.length()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        result = temp.substring(i + 1, temp.length());
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
   130
                    } else {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
   131
                        throw new IllegalArgumentException
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
   132
                                ("empty realm part not allowed");
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
   133
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (result.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throw new RealmException(Krb5.REALM_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (!isValidRealmString(result))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                throw new RealmException(Krb5.REALM_ILLCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static String parseRealmComponent(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                ("null input name is not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        String temp = new String(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        while (i < temp.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (temp.charAt(i) == PrincipalName.REALM_COMPONENT_SEPARATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (i == 0 || temp.charAt(i - 1) != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if (i + 1 < temp.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        result = temp.substring(i + 1, temp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected static String parseRealm(String name) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        String result = parseRealmAtSeparator(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            result = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (result == null || result.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new RealmException(Krb5.REALM_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (!isValidRealmString(result))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new RealmException(Krb5.REALM_ILLCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // This is protected because the definition of a realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // string is fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected static boolean isValidRealmString(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (name.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        for (int i = 0; i < name.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (name.charAt(i) == '/' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                name.charAt(i) == ':' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                name.charAt(i) == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Encodes a Realm object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return the byte array of encoded KrbCredInfo object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        DerOutputStream out = new DerOutputStream();
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2163
diff changeset
   206
        out.putDerValue(new KerberosString(this.realm).toDerValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Parse (unmarshal) a realm from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception Asn1Exception on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param data the Der input stream value, which contains one or more marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param optional indicate if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return an instance of Realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
   223
    public static Realm parse(DerInputStream data, byte explicitTag, boolean optional)
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9050
diff changeset
   224
            throws Asn1Exception, IOException, RealmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return new Realm(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Returns an array of realms that may be traversed to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * a TGT from the initiating realm cRealm to the target realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * sRealm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <br>
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   242
     * This method would read [capaths] to create a path, or generate a
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   243
     * hierarchical path if [capaths] does not contain a sub-stanza for cRealm
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   244
     * or the sub-stanza does not contain a tag for sRealm.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <br>
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   246
     * The returned list would never be null, and it always contains
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   247
     * cRealm as the head entry. sRealm is not included as the tail.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   249
     * @param cRealm the initiating realm, not null
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   250
     * @param sRealm the target realm, not null, not equals to cRealm
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   251
     * @return array of realms including at least cRealm as the first
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   252
     *         element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   254
    public static String[] getRealmsList(String cRealm, String sRealm) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   255
        try {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   256
            // Try [capaths]
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   257
            return parseCapaths(cRealm, sRealm);
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   258
        } catch (KrbException ke) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   259
            // Now assume the realms are organized hierarchically.
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   260
            return parseHierarchy(cRealm, sRealm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   262
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   265
     * Parses the [capaths] stanza of the configuration file for a
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   266
     * list of realms to traverse to obtain credentials from the
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   267
     * initiating realm cRealm to the target realm sRealm.
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   268
     *
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   269
     * For a given client realm C there is a tag C in [capaths] whose
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   270
     * subtag S has a value which is a (possibly partial) path from C
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   271
     * to S. When the path is partial, it contains only the tail of the
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   272
     * full path. Values of other subtags will be used to build the full
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   273
     * path. The value "." means a direct path from C to S. If realm S
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   274
     * does not appear as a subtag, there is no path defined here.
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   275
     *
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   276
     * The implementation ignores all values which equals to C or S, or
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   277
     * a "." in multiple values, or any duplicated realm names.
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   278
     *
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   279
     * When a path value has more than two realms, they can be specified
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   280
     * with multiple key-value pairs each having a single value, but the
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   281
     * order must not change.
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   282
     *
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   283
     * For example:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * [capaths]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *    TIVOLI.COM = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *        IBM.COM = IBM_LDAPCENTRAL.COM MOONLITE.ORG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *        IBM_LDAPCENTRAL.COM = LDAPCENTRAL.NET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *        LDAPCENTRAL.NET = .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   292
     * TIVOLI.COM has a direct path to LDAPCENTRAL.NET, which has a direct
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   293
     * path to IBM_LDAPCENTRAL.COM. It also has a partial path to IBM.COM
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   294
     * being "IBM_LDAPCENTRAL.COM MOONLITE.ORG". Merging these info together,
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   295
     * a full path from TIVOLI.COM to IBM.COM will be
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   296
     *
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   297
     *   TIVOLI.COM -> LDAPCENTRAL.NET -> IBM_LDAPCENTRAL.COM
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   298
     *              -> IBM_LDAPCENTRAL.COM -> MOONLITE.ORG
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   299
     *
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   300
     * Please note the sRealm IBM.COM does not appear in the path.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   302
     * @param cRealm the initiating realm
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   303
     * @param sRealm the target realm, not the same as cRealm
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 32003
diff changeset
   304
     * @return array of realms including at least cRealm as the first
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   305
     *          element
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   306
     * @throws KrbException if the config does not contain a sub-stanza
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   307
     *          for cRealm in [capaths] or the sub-stanza does not contain
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   308
     *          sRealm as a tag
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   310
    private static String[] parseCapaths(String cRealm, String sRealm)
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   311
            throws KrbException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   313
        // This line could throw a KrbException
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   314
        Config cfg = Config.getInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   316
        if (!cfg.exists("capaths", cRealm, sRealm)) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   317
            throw new KrbException("No conf");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   320
        LinkedList<String> path = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   322
        String head = sRealm;
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   323
        while (true) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   324
            String value = cfg.getAll("capaths", cRealm, head);
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   325
            if (value == null) {
2064
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   326
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   328
            String[] more = value.split("\\s+");
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   329
            boolean changed = false;
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   330
            for (int i=more.length-1; i>=0; i--) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   331
                if (path.contains(more[i])
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   332
                        || more[i].equals(".")
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   333
                        || more[i].equals(cRealm)
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   334
                        || more[i].equals(sRealm)
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   335
                        || more[i].equals(head)) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   336
                    // Ignore invalid values
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   337
                    continue;
2064
d690c8a2acea 6789935: cross-realm capath search error
weijun
parents: 2
diff changeset
   338
                }
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   339
                changed = true;
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   340
                path.addFirst(more[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   342
            if (!changed) break;
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   343
            head = path.getFirst();
9007
05f25362fb76 7019384: Realm.getRealmsList returns realms list in wrong (reverse) order
weijun
parents: 7977
diff changeset
   344
        }
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   345
        path.addFirst(cRealm);
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   346
        return path.toArray(new String[path.size()]);
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   347
   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Build a list of realm that can be traversed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * to obtain credentials from the initiating realm cRealm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * for a service in the target realm sRealm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param cRealm the initiating realm
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   354
     * @param sRealm the target realm, not the same as cRealm
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 32003
diff changeset
   355
     * @return array of realms including cRealm as the first element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   357
    private static String[] parseHierarchy(String cRealm, String sRealm) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   359
        String[] cComponents = cRealm.split("\\.");
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   360
        String[] sComponents = sRealm.split("\\.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   362
        int cPos = cComponents.length;
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   363
        int sPos = sComponents.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   365
        boolean hasCommon = false;
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   366
        for (sPos--, cPos--; sPos >=0 && cPos >= 0 &&
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   367
                sComponents[sPos].equals(cComponents[cPos]);
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   368
                sPos--, cPos--) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   369
            hasCommon = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   372
        // For those with common components:
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   373
        //                            length  pos
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   374
        // SITES1.SALES.EXAMPLE.COM   4       1
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   375
        //   EVERYWHERE.EXAMPLE.COM   3       0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   377
        // For those without common components:
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   378
        //                     length  pos
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   379
        // DEVEL.EXAMPLE.COM   3       2
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   380
        // PROD.EXAMPLE.ORG    3       2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   382
        LinkedList<String> path = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   384
        // Un-common ones for client side
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   385
        for (int i=0; i<=cPos; i++) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   386
            path.addLast(subStringFrom(cComponents, i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   389
        // Common one
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   390
        if (hasCommon) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   391
            path.addLast(subStringFrom(cComponents, cPos+1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   394
        // Un-common ones for server side
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   395
        for (int i=sPos; i>=0; i--) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   396
            path.addLast(subStringFrom(sComponents, i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   399
        // Remove sRealm from path. Note that it might be added at last loop
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   400
        // or as a common component, if sRealm is a parent of cRealm
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   401
        path.removeLast();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   403
        return path.toArray(new String[path.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   406
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20175
diff changeset
   407
     * Creates a realm name using components from the given position.
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   408
     * For example, subStringFrom({"A", "B", "C"}, 1) is "B.C".
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   409
     */
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   410
    private static String subStringFrom(String[] components, int from) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   411
        StringBuilder sb = new StringBuilder();
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   412
        for (int i=from; i<components.length; i++) {
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   413
            if (sb.length() != 0) sb.append('.');
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   414
            sb.append(components[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
20175
a65ad0a49e3c 8012615: Realm.getRealmsList returns realms list in wrong
weijun
parents: 14342
diff changeset
   416
        return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
}