jdk/src/share/classes/javax/naming/ldap/Rdn.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 10369 e9d2e59e53f0
child 21428 e408bf58d482
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
     2
 * Copyright (c) 2003, 2011, 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
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public int compareTo(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (!(obj instanceof Rdn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new ClassCastException("The obj is not a Rdn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        Rdn that = (Rdn) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int minSize = Math.min(entries.size(), that.entries.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        for (int i = 0; i < minSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // 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
   332
            int diff = entries.get(i).compareTo(that.entries.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return (entries.size() - that.entries.size());  // longer RDN wins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Compares the specified Object with this Rdn for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Returns true if the given object is also a Rdn and the two Rdns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * represent the same attribute type and value mappings. The order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * components in multi-valued Rdns (such as "ou=Sales+cn=Bob") is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   347
     * Type and value equality matching is done as below:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <li> The types are compared for equality with their case ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <li> String values with different but equivalent usage of quoting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * escaping, or UTF8-hex-encoding are considered equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * The case of the values is ignored during the comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * If obj is null or not an instance of Rdn, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param obj object to be compared for equality with this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return true if the specified object is equal to this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @see #hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (!(obj instanceof Rdn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        Rdn that = (Rdn) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (entries.size() != that.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        for (int i = 0; i < entries.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (!entries.get(i).equals(that.entries.get(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Returns the hash code of this RDN. Two RDNs that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * equal (according to the equals method) will have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return An int representing the hash code of this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @see #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Sum up the hash codes of the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // For each type/value pair...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        for (int i = 0; i < entries.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            hash += entries.get(i).hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Retrieves the {@link javax.naming.directory.Attributes Attributes}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * view of the type/value mappings contained in this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return  The non-null attributes containing the type/value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *          mappings of this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public Attributes toAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        Attributes attrs = new BasicAttributes(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        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
   410
            RdnEntry entry = entries.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            Attribute attr = attrs.put(entry.getType(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                attr.add(entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                attrs.put(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   421
    private static class RdnEntry implements Comparable<RdnEntry> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        private Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // If non-null, a cannonical representation of the value suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // for comparison using String.compareTo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        private String comparable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   437
        public int compareTo(RdnEntry that) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   438
            int diff = type.compareToIgnoreCase(that.type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if (value.equals(that.value)) {     // try shortcut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            return getValueComparable().compareTo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        that.getValueComparable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            if (!(obj instanceof RdnEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // Any change here must be reflected in hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            RdnEntry that = (RdnEntry) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return (type.equalsIgnoreCase(that.type)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        (getValueComparable().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        that.getValueComparable()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        public int hashCode() {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   465
            return (type.toUpperCase(Locale.ENGLISH).hashCode() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                getValueComparable().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return type + "=" + escapeValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        private String getValueComparable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (comparable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                return comparable;              // return cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // cache result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (value instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                comparable = escapeBinaryValue((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } else {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   482
                comparable = ((String) value).toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return comparable;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Retrieves the number of attribute type/value pairs in this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @return The non-negative number of type/value pairs in this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Given the value of an attribute, returns a string escaped according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * to the rules specified in
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   499
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * For example, if the val is "Sue, Grabbit and Runn", the escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * value returned by this method is "Sue\, Grabbit and Runn".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * A string value is represented as a String and binary value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param val The non-null object to be escaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @return Escaped string value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @throws ClassCastException if val is is not a String or byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public static String escapeValue(Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return (val instanceof byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                ? escapeBinaryValue((byte[])val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                : escapeStringValue((String)val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Given the value of a string-valued attribute, returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * string suitable for inclusion in a DN.  This is accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * using backslash (\) to escape the following characters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *  leading and trailing whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *  , = + < > # ; " \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    private static final String escapees = ",=+<>#;\"\\";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    private static String escapeStringValue(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            char[] chars = val.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            StringBuilder builder = new StringBuilder(2 * val.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            // Find leading and trailing whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            int lead;   // index of first char that is not leading whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            for (lead = 0; lead < chars.length; lead++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                if (!isWhitespace(chars[lead])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            int trail;  // index of last char that is not trailing whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            for (trail = chars.length - 1; trail >= 0; trail--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                if (!isWhitespace(chars[trail])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            for (int i = 0; i < chars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                char c = chars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                if ((i < lead) || (i > trail) || (escapees.indexOf(c) >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    builder.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                builder.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            return builder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Given the value of a binary attribute, returns a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * suitable for inclusion in a DN (such as "#CEB1DF80").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * TBD: This method should actually generate the ber encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * of the binary value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    private static String escapeBinaryValue(byte[] val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        StringBuilder builder = new StringBuilder(1 + 2 * val.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        builder.append("#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        for (int i = 0; i < val.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            byte b = val[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            builder.append(Character.forDigit(0xF & (b >>> 4), 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            builder.append(Character.forDigit(0xF & b, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return builder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Given an attribute value string formated according to the rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * specified in
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   577
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * returns the unformated value.  Escapes and quotes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * stripped away, and hex-encoded UTF-8 is converted to equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * UTF-16 characters. Returns a string value as a String, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * binary value as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Legal and illegal values are defined in RFC 2253.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * This method is generous in accepting the values and does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * catch all illegal values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Therefore, passing in an illegal value might not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * trigger an <tt>IllegalArgumentException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param   val     The non-null string to be unescaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @return          Unescaped value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @throws          IllegalArgumentException When an Illegal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *                  is provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public static Object unescapeValue(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            char[] chars = val.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            int beg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            int end = chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            // Trim off leading and trailing whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            while ((beg < end) && isWhitespace(chars[beg])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                ++beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            while ((beg < end) && isWhitespace(chars[end - 1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                --end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   609
            // Add back the trailing whitespace with a preceding '\'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            // (escaped or unescaped) that was taken off in the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            // loop. Whether or not to retain this whitespace is decided below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (end != chars.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    (beg < end) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    chars[end - 1] == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if (beg >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if (chars[beg] == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                // Value is binary (eg: "#CEB1DF80").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                return decodeHexPairs(chars, ++beg, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            // Trim off quotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            if ((chars[beg] == '\"') && (chars[end - 1] == '\"')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                ++beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                --end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            StringBuilder builder = new StringBuilder(end - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            int esc = -1; // index of the last escaped character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                if ((chars[i] == '\\') && (i + 1 < end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    if (!Character.isLetterOrDigit(chars[i + 1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        ++i;                            // skip backslash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        builder.append(chars[i]);       // snarf escaped char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                        esc = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        // Convert hex-encoded UTF-8 to 16-bit chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        byte[] utf8 = getUtf8Octets(chars, i, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        if (utf8.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                builder.append(new String(utf8, "UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                            i += utf8.length * 3 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        } else { // no utf8 bytes available, invalid DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                            // '/' has no meaning, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                "Not a valid attribute string value:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                val + ",improper usage of backslash");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    builder.append(chars[i]);   // snarf unescaped char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            // Get rid of the unescaped trailing whitespace with the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   666
            // preceding '\' character that was previously added back.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            int len = builder.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if (isWhitespace(builder.charAt(len - 1)) && esc != (end - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                builder.setLength(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            return builder.toString();
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
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * Given an array of chars (with starting and ending indexes into it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * representing bytes encoded as hex-pairs (such as "CEB1DF80"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * returns a byte array containing the decoded bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        private static byte[] decodeHexPairs(char[] chars, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            byte[] bytes = new byte[(end - beg) / 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            for (int i = 0; beg + 1 < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                int hi = Character.digit(chars[beg], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                int lo = Character.digit(chars[beg + 1], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                if (hi < 0 || lo < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                bytes[i] = (byte)((hi<<4) + lo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                beg += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (beg != end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        "Illegal attribute value: " + new String(chars));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * Given an array of chars (with starting and ending indexes into it),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         * finds the largest prefix consisting of hex-encoded UTF-8 octets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         * and returns a byte array containing the corresponding UTF-8 octets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         * Hex-encoded UTF-8 octets look like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         *      \03\B1\DF\80
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        private static byte[] getUtf8Octets(char[] chars, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            byte[] utf8 = new byte[(end - beg) / 3];    // allow enough room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            int len = 0;        // index of first unused byte in utf8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            while ((beg + 2 < end) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                   (chars[beg++] == '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                int hi = Character.digit(chars[beg++], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                int lo = Character.digit(chars[beg++], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                if (hi < 0 || lo < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                   break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                utf8[len++] = (byte)((hi<<4) + lo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            if (len == utf8.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                return utf8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                byte[] res = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                System.arraycopy(utf8, 0, res, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                return res;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * Best guess as to what RFC 2253 means by "whitespace".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    private static boolean isWhitespace(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        return (c == ' ' || c == '\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Serializes only the unparsed RDN, for compactness and to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * any implementation dependency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @serialData      The RDN string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        s.writeObject(toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        s.defaultReadObject();
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   750
        entries = new ArrayList<>(DEFAULT_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        String unparsed = (String) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            (new Rfc2253Parser(unparsed)).parseRdn(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    "Invalid name: " + unparsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
}