jdk/src/share/classes/javax/naming/ldap/Rdn.java
author prappo
Fri, 01 Aug 2014 22:32:51 +0100
changeset 25808 e113d0a0fde0
parent 23712 d46a902c1aed
permissions -rw-r--r--
8054158: Fix typos in JNDI-related packages Reviewed-by: rriggs, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21285
e740104a04f1 8026840: Fix new doclint issues in javax.naming
darcy
parents: 10369
diff changeset
     2
 * Copyright (c) 2003, 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.naming.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
    31
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.InvalidNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.directory.BasicAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.naming.directory.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.naming.directory.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.naming.NamingEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.naming.NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class represents a relative distinguished name, or RDN, which is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * component of a distinguished name as specified by
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
    49
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * An example of an RDN is "OU=Sales+CN=J.Smith". In this example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the RDN consist of multiple attribute type/value pairs. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * RDN is parsed as described in the class description for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link javax.naming.ldap.LdapName <tt>LdapName</tt>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The Rdn class represents an RDN as attribute type/value mappings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * which can be viewed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link javax.naming.directory.Attributes Attributes}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * In addition, it contains convenience methods that allow easy retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * of type and value when the Rdn consist of a single type/value pair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * which is how it appears in a typical usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * It also contains helper methods that allow escaping of the unformatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * attribute value and unescaping of the value formatted according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * escaping syntax defined in RFC2253. For methods that take or return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * attribute value as an Object, the value is either a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * (in unescaped form) or a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <code>Rdn</code> will properly parse all valid RDNs, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * does not attempt to detect all possible violations when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * invalid RDNs. It is "generous" in accepting invalid RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The "validity" of a name is determined ultimately when it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * is supplied to an LDAP server, which may accept or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * reject the name based on factors such as its schema information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * and interoperability considerations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * The following code example shows how to construct an Rdn using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * constructor that takes type and value as arguments:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      Rdn rdn = new Rdn("cn", "Juicy, Fruit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      System.out.println(rdn.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * The last line will print <tt>cn=Juicy\, Fruit</tt>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * {@link #unescapeValue(String) <tt>unescapeValue()</tt>} method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * used to unescape the escaped comma resulting in the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * value <tt>"Juicy, Fruit"</tt>. The {@link #escapeValue(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <tt>escapeValue()</tt>} method adds the escape back preceding the comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * This class can be instantiated by a string representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * of the RDN defined in RFC 2253 as shown in the following code example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *      Rdn rdn = new Rdn("cn=Juicy\\, Fruit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *      System.out.println(rdn.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * The last line will print <tt>cn=Juicy\, Fruit</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Concurrent multithreaded read-only access of an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <tt>Rdn</tt> need not be synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * Unless otherwise noted, the behavior of passing a null argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * to a constructor or method in this class will cause NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
public class Rdn implements Serializable, Comparable<Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   108
    private transient ArrayList<RdnEntry> entries;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // The common case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final int DEFAULT_SIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final long serialVersionUID = -5994465067210009656L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Constructs an Rdn from the given attribute set. See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * {@link javax.naming.directory.Attributes Attributes}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   119
     * The string attribute values are not interpreted as
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   120
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * formatted RDN strings. That is, the values are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * literally (not parsed) and assumed to be unescaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param attrSet The non-null and non-empty attributes containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * type/value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *          be used to construct a valid RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public Rdn(Attributes attrSet) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (attrSet.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new InvalidNameException("Attributes cannot be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   133
        entries = new ArrayList<>(attrSet.size());
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   134
        NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            for (int nEntries = 0; attrs.hasMore(); nEntries++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                RdnEntry entry = new RdnEntry();
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   138
                Attribute attr = attrs.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                entry.type = attr.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                entry.value = attr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                entries.add(nEntries, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            InvalidNameException e2 = new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                        e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            e2.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw e2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        sort(); // arrange entries for comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Constructs an Rdn from the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * This constructor takes a string formatted according to the rules
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   155
     * defined in <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * and described in the class description for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * {@link javax.naming.ldap.LdapName}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param rdnString The non-null and non-empty RFC2253 formatted string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws InvalidNameException If a syntax error occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *                  parsing of the rdnString.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public Rdn(String rdnString) throws InvalidNameException {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   164
        entries = new ArrayList<>(DEFAULT_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        (new Rfc2253Parser(rdnString)).parseRdn(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Constructs an Rdn from the given <tt>rdn</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * The contents of the <tt>rdn</tt> are simply copied into the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * created Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param rdn The non-null Rdn to be copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public Rdn(Rdn rdn) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   175
        entries = new ArrayList<>(rdn.entries.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        entries.addAll(rdn.entries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Constructs an Rdn from the given attribute type and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * value.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   182
     * The string attribute values are not interpreted as
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   183
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * formatted RDN strings. That is, the values are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * literally (not parsed) and assumed to be unescaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param type The non-null and non-empty string attribute type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param value The non-null and non-empty attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @throws InvalidNameException If type/value cannot be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *                  construct a valid RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see #toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public Rdn(String type, Object value) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new NullPointerException("Cannot set value to null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (type.equals("") || isEmptyValue(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                "type or value cannot be empty, type:" + type +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                " value:" + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   202
        entries = new ArrayList<>(DEFAULT_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        put(type, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private boolean isEmptyValue(Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return ((val instanceof String) && val.equals("")) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        ((val instanceof byte[]) && (((byte[]) val).length == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    // An empty constructor used by the parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    Rdn() {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   213
        entries = new ArrayList<>(DEFAULT_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Adds the given attribute type and value to this Rdn.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   218
     * The string attribute values are not interpreted as
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   219
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * formatted RDN strings. That is the values are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * literally (not parsed) and assumed to be unescaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param type The non-null and non-empty string attribute type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param value The non-null and non-empty attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return The updated Rdn, not a new one. Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see #toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    Rdn put(String type, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // create new Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        RdnEntry newEntry = new RdnEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        newEntry.type =  type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (value instanceof byte[]) {  // clone the byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            newEntry.value = ((byte[]) value).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            newEntry.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        entries.add(newEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    void sort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (entries.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            Collections.sort(entries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Retrieves one of this Rdn's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * This is a convenience method for obtaining the value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * when the RDN contains a single type and value mapping,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * which is the common RDN usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * For a multi-valued RDN, this method returns value corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * to the type returned by {@link #getType() getType()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return The non-null attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public Object getValue() {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   260
        return entries.get(0).getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Retrieves one of this Rdn's type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * This is a convenience method for obtaining the type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * when the RDN contains a single type and value mapping,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * which is the common RDN usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * For a multi-valued RDN, the type/value pairs have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * no specific order defined on them. In that case, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * returns type of one of the type/value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * The {@link #getValue() getValue()} method returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * value corresponding to the type returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return The non-null attribute type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public String getType() {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   278
        return entries.get(0).getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns this Rdn as a string represented in a format defined by
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   283
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a> and described
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * in the class description for {@link javax.naming.ldap.LdapName LdapName}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return The string representation of the Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        StringBuilder builder = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        int size = entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            builder.append(entries.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        for (int next = 1; next < size; next++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            builder.append('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            builder.append(entries.get(next));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return builder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Compares this Rdn with the specified Object for order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Returns a negative integer, zero, or a positive integer as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Rdn is less than, equal to, or greater than the given Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * If obj is null or not an instance of Rdn, ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * The attribute type and value pairs of the RDNs are lined up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * against each other and compared lexicographically. The order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * components in multi-valued Rdns (such as "ou=Sales+cn=Bob") is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param obj The non-null object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @return  A negative integer, zero, or a positive integer as this Rdn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *          is less than, equal to, or greater than the given Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @exception ClassCastException if obj is null or not a Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public int compareTo(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (!(obj instanceof Rdn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throw new ClassCastException("The obj is not a Rdn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        Rdn that = (Rdn) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int minSize = Math.min(entries.size(), that.entries.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        for (int i = 0; i < minSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // Compare a single pair of type/value pairs.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   331
            int diff = entries.get(i).compareTo(that.entries.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return (entries.size() - that.entries.size());  // longer RDN wins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Compares the specified Object with this Rdn for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Returns true if the given object is also a Rdn and the two Rdns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * represent the same attribute type and value mappings. The order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * components in multi-valued Rdns (such as "ou=Sales+cn=Bob") is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   346
     * Type and value equality matching is done as below:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <li> The types are compared for equality with their case ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <li> String values with different but equivalent usage of quoting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * escaping, or UTF8-hex-encoding are considered equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * The case of the values is ignored during the comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * If obj is null or not an instance of Rdn, false is returned.
23712
d46a902c1aed 8039041: Tidy warnings cleanup for javax.naming
yan
parents: 21428
diff changeset
   355
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param obj object to be compared for equality with this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return true if the specified object is equal to this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @see #hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (!(obj instanceof Rdn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        Rdn that = (Rdn) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (entries.size() != that.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        for (int i = 0; i < entries.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (!entries.get(i).equals(that.entries.get(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Returns the hash code of this RDN. Two RDNs that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * equal (according to the equals method) will have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return An int representing the hash code of this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @see #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // Sum up the hash codes of the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // For each type/value pair...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        for (int i = 0; i < entries.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            hash += entries.get(i).hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Retrieves the {@link javax.naming.directory.Attributes Attributes}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * view of the type/value mappings contained in this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @return  The non-null attributes containing the type/value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *          mappings of this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public Attributes toAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        Attributes attrs = new BasicAttributes(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        for (int i = 0; i < entries.size(); i++) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   409
            RdnEntry entry = entries.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            Attribute attr = attrs.put(entry.getType(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                attr.add(entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                attrs.put(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   420
    private static class RdnEntry implements Comparable<RdnEntry> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        private Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23712
diff changeset
   424
        // If non-null, a canonical representation of the value suitable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // for comparison using String.compareTo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        private String comparable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   436
        public int compareTo(RdnEntry that) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   437
            int diff = type.compareToIgnoreCase(that.type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (value.equals(that.value)) {     // try shortcut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            return getValueComparable().compareTo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        that.getValueComparable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (!(obj instanceof RdnEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            // Any change here must be reflected in hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            RdnEntry that = (RdnEntry) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return (type.equalsIgnoreCase(that.type)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        (getValueComparable().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        that.getValueComparable()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        public int hashCode() {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   464
            return (type.toUpperCase(Locale.ENGLISH).hashCode() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                getValueComparable().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return type + "=" + escapeValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        private String getValueComparable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (comparable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                return comparable;              // return cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // cache result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (value instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                comparable = escapeBinaryValue((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            } else {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   481
                comparable = ((String) value).toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            return comparable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Retrieves the number of attribute type/value pairs in this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @return The non-negative number of type/value pairs in this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Given the value of an attribute, returns a string escaped according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * to the rules specified in
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   498
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * For example, if the val is "Sue, Grabbit and Runn", the escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * value returned by this method is "Sue\, Grabbit and Runn".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * A string value is represented as a String and binary value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param val The non-null object to be escaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @return Escaped string value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @throws ClassCastException if val is is not a String or byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public static String escapeValue(Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return (val instanceof byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                ? escapeBinaryValue((byte[])val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                : escapeStringValue((String)val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Given the value of a string-valued attribute, returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * string suitable for inclusion in a DN.  This is accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * using backslash (\) to escape the following characters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *  leading and trailing whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *  , = + < > # ; " \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private static final String escapees = ",=+<>#;\"\\";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private static String escapeStringValue(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            char[] chars = val.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            StringBuilder builder = new StringBuilder(2 * val.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            // Find leading and trailing whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            int lead;   // index of first char that is not leading whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            for (lead = 0; lead < chars.length; lead++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (!isWhitespace(chars[lead])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            int trail;  // index of last char that is not trailing whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            for (trail = chars.length - 1; trail >= 0; trail--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                if (!isWhitespace(chars[trail])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            for (int i = 0; i < chars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                char c = chars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if ((i < lead) || (i > trail) || (escapees.indexOf(c) >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    builder.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                builder.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return builder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Given the value of a binary attribute, returns a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * suitable for inclusion in a DN (such as "#CEB1DF80").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * TBD: This method should actually generate the ber encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * of the binary value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    private static String escapeBinaryValue(byte[] val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        StringBuilder builder = new StringBuilder(1 + 2 * val.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        builder.append("#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        for (int i = 0; i < val.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            byte b = val[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            builder.append(Character.forDigit(0xF & (b >>> 4), 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            builder.append(Character.forDigit(0xF & b, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return builder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23712
diff changeset
   574
     * Given an attribute value string formatted according to the rules
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * specified in
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   576
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>,
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23712
diff changeset
   577
     * returns the unformatted value.  Escapes and quotes are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * stripped away, and hex-encoded UTF-8 is converted to equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * UTF-16 characters. Returns a string value as a String, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * binary value as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Legal and illegal values are defined in RFC 2253.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * This method is generous in accepting the values and does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * catch all illegal values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Therefore, passing in an illegal value might not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * trigger an <tt>IllegalArgumentException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param   val     The non-null string to be unescaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @return          Unescaped value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @throws          IllegalArgumentException When an Illegal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *                  is provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public static Object unescapeValue(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            char[] chars = val.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            int beg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            int end = chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            // Trim off leading and trailing whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            while ((beg < end) && isWhitespace(chars[beg])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                ++beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            while ((beg < end) && isWhitespace(chars[end - 1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                --end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   608
            // Add back the trailing whitespace with a preceding '\'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            // (escaped or unescaped) that was taken off in the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            // loop. Whether or not to retain this whitespace is decided below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            if (end != chars.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    (beg < end) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    chars[end - 1] == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (beg >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (chars[beg] == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                // Value is binary (eg: "#CEB1DF80").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                return decodeHexPairs(chars, ++beg, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            // Trim off quotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if ((chars[beg] == '\"') && (chars[end - 1] == '\"')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                ++beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                --end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            StringBuilder builder = new StringBuilder(end - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            int esc = -1; // index of the last escaped character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                if ((chars[i] == '\\') && (i + 1 < end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    if (!Character.isLetterOrDigit(chars[i + 1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        ++i;                            // skip backslash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        builder.append(chars[i]);       // snarf escaped char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        esc = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        // Convert hex-encoded UTF-8 to 16-bit chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        byte[] utf8 = getUtf8Octets(chars, i, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        if (utf8.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                builder.append(new String(utf8, "UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                            } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                                // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                            i += utf8.length * 3 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        } else { // no utf8 bytes available, invalid DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                            // '/' has no meaning, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                "Not a valid attribute string value:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                val + ",improper usage of backslash");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    builder.append(chars[i]);   // snarf unescaped char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            // Get rid of the unescaped trailing whitespace with the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   665
            // preceding '\' character that was previously added back.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            int len = builder.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (isWhitespace(builder.charAt(len - 1)) && esc != (end - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                builder.setLength(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return builder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         * Given an array of chars (with starting and ending indexes into it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * representing bytes encoded as hex-pairs (such as "CEB1DF80"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * returns a byte array containing the decoded bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        private static byte[] decodeHexPairs(char[] chars, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            byte[] bytes = new byte[(end - beg) / 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            for (int i = 0; beg + 1 < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                int hi = Character.digit(chars[beg], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                int lo = Character.digit(chars[beg + 1], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                if (hi < 0 || lo < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                bytes[i] = (byte)((hi<<4) + lo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                beg += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (beg != end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        "Illegal attribute value: " + new String(chars));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * Given an array of chars (with starting and ending indexes into it),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * finds the largest prefix consisting of hex-encoded UTF-8 octets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         * and returns a byte array containing the corresponding UTF-8 octets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         * Hex-encoded UTF-8 octets look like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         *      \03\B1\DF\80
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        private static byte[] getUtf8Octets(char[] chars, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            byte[] utf8 = new byte[(end - beg) / 3];    // allow enough room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            int len = 0;        // index of first unused byte in utf8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            while ((beg + 2 < end) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                   (chars[beg++] == '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                int hi = Character.digit(chars[beg++], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                int lo = Character.digit(chars[beg++], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                if (hi < 0 || lo < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                   break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                utf8[len++] = (byte)((hi<<4) + lo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (len == utf8.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                return utf8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                byte[] res = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                System.arraycopy(utf8, 0, res, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * Best guess as to what RFC 2253 means by "whitespace".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    private static boolean isWhitespace(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return (c == ' ' || c == '\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Serializes only the unparsed RDN, for compactness and to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * any implementation dependency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @serialData      The RDN string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        s.writeObject(toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        s.defaultReadObject();
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   749
        entries = new ArrayList<>(DEFAULT_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        String unparsed = (String) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            (new Rfc2253Parser(unparsed)).parseRdn(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    "Invalid name: " + unparsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
}