jdk/src/java.base/share/classes/sun/security/x509/RDN.java
author darcy
Thu, 23 Apr 2015 18:51:18 -0700
changeset 30033 b9c86c17164a
parent 26967 c182469301ee
child 30374 2abaf49910ea
permissions -rw-r--r--
8078468: Update security libraries to use diamond with anonymous classes Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2002, 2014, 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 sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.StringReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * RDNs are a set of {attribute = value} assertions.  Some of those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * attributes are "distinguished" (unique w/in context).  Order is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * never relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Some X.500 names include only a single distinguished attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * per RDN.  This style is currently common.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Note that DER-encoded RDNs sort AVAs by assertion OID ... so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * when we parse this data we don't have to worry about canonicalizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * it, but we'll need to sort them when we expose the RDN class more.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The ASN.1 for RDNs is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * RelativeDistinguishedName ::=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   SET OF AttributeTypeAndValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * AttributeTypeAndValue ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   type     AttributeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   value    AttributeValue }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * AttributeType ::= OBJECT IDENTIFIER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * AttributeValue ::= ANY DEFINED BY AttributeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Note that instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class RDN {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // currently not private, accessed directly from X500Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    final AVA[] assertion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // cached immutable List of the AVAs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private volatile List<AVA> avaList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // cache canonical String form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private volatile String canonicalString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Constructs an RDN from its printable representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * An RDN may consist of one or multiple Attribute Value Assertions (AVAs),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * using '+' as a separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * If the '+' should be considered part of an AVA value, it must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * preceded by '\'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param name String form of RDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @throws IOException on parsing error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public RDN(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this(name, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Constructs an RDN from its printable representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * An RDN may consist of one or multiple Attribute Value Assertions (AVAs),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * using '+' as a separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * If the '+' should be considered part of an AVA value, it must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * preceded by '\'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param name String form of RDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param keyword an additional mapping of keywords to OIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @throws IOException on parsing error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public RDN(String name, Map<String, String> keywordMap) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int quoteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int searchOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int avaOffset = 0;
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 26967
diff changeset
   105
        List<AVA> avaVec = new ArrayList<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int nextPlus = name.indexOf('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        while (nextPlus >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            quoteCount += X500Name.countQuotes(name, searchOffset, nextPlus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             * We have encountered an AVA delimiter (plus sign).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             * If the plus sign in the RDN under consideration is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             * preceded by a backslash (escape), or by a double quote, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
             * is part of the AVA. Otherwise, it is used as a separator, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             * delimit the AVA under consideration from any subsequent AVAs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (nextPlus > 0 && name.charAt(nextPlus - 1) != '\\'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                && quoteCount != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                 * Plus sign is a separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                String avaString = name.substring(avaOffset, nextPlus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (avaString.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    throw new IOException("empty AVA in RDN \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // Parse AVA, and store it in vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                AVA ava = new AVA(new StringReader(avaString), keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                avaVec.add(ava);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                // Increase the offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                avaOffset = nextPlus + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                // Set quote counter back to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                quoteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            searchOffset = nextPlus + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            nextPlus = name.indexOf('+', searchOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // parse last or only AVA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String avaString = name.substring(avaOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (avaString.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new IOException("empty AVA in RDN \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        AVA ava = new AVA(new StringReader(avaString), keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        avaVec.add(ava);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        assertion = avaVec.toArray(new AVA[avaVec.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Constructs an RDN from its printable representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * An RDN may consist of one or multiple Attribute Value Assertions (AVAs),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * using '+' as a separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * If the '+' should be considered part of an AVA value, it must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * preceded by '\'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param name String form of RDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws IOException on parsing error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    RDN(String name, String format) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        this(name, format, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Constructs an RDN from its printable representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * An RDN may consist of one or multiple Attribute Value Assertions (AVAs),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * using '+' as a separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * If the '+' should be considered part of an AVA value, it must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * preceded by '\'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param name String form of RDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param keyword an additional mapping of keywords to OIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @throws IOException on parsing error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    RDN(String name, String format, Map<String, String> keywordMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (format.equalsIgnoreCase("RFC2253") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IOException("Unsupported format " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int searchOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int avaOffset = 0;
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 26967
diff changeset
   185
        List<AVA> avaVec = new ArrayList<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int nextPlus = name.indexOf('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        while (nextPlus >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             * We have encountered an AVA delimiter (plus sign).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
             * If the plus sign in the RDN under consideration is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
             * preceded by a backslash (escape), or by a double quote, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
             * is part of the AVA. Otherwise, it is used as a separator, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
             * delimit the AVA under consideration from any subsequent AVAs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (nextPlus > 0 && name.charAt(nextPlus - 1) != '\\' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                 * Plus sign is a separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                String avaString = name.substring(avaOffset, nextPlus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (avaString.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    throw new IOException("empty AVA in RDN \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // Parse AVA, and store it in vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                AVA ava = new AVA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    (new StringReader(avaString), AVA.RFC2253, keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                avaVec.add(ava);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                // Increase the offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                avaOffset = nextPlus + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            searchOffset = nextPlus + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            nextPlus = name.indexOf('+', searchOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // parse last or only AVA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        String avaString = name.substring(avaOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (avaString.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new IOException("empty AVA in RDN \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        AVA ava = new AVA(new StringReader(avaString), AVA.RFC2253, keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        avaVec.add(ava);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        assertion = avaVec.toArray(new AVA[avaVec.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Constructs an RDN from an ASN.1 encoded value.  The encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * of the name in the stream uses DER (a BER/1 subset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param value a DER-encoded value holding an RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @throws IOException on parsing error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    RDN(DerValue rdn) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (rdn.tag != DerValue.tag_Set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new IOException("X500 RDN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        DerInputStream dis = new DerInputStream(rdn.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        DerValue[] avaset = dis.getSet(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        assertion = new AVA[avaset.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        for (int i = 0; i < avaset.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            assertion[i] = new AVA(avaset[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
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
     * Creates an empty RDN with slots for specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * number of AVAs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param i number of AVAs to be in RDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    RDN(int i) { assertion = new AVA[i]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public RDN(AVA ava) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (ava == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        assertion = new AVA[] { ava };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public RDN(AVA[] avas) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        assertion = avas.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        for (int i = 0; i < assertion.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (assertion[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Return an immutable List of the AVAs in this RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public List<AVA> avas() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        List<AVA> list = avaList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            list = Collections.unmodifiableList(Arrays.asList(assertion));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            avaList = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Return the number of AVAs in this RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return assertion.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (obj instanceof RDN == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        RDN other = (RDN)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (this.assertion.length != other.assertion.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        String thisCanon = this.toRFC2253String(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        String otherCanon = other.toRFC2253String(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return thisCanon.equals(otherCanon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Calculates a hash code value for the object.  Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * which are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @returns int hashCode value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return toRFC2253String(true).hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * return specified attribute value from RDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @params oid ObjectIdentifier of attribute to be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @returns DerValue of attribute value; null if attribute does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    DerValue findAttribute(ObjectIdentifier oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        for (int i = 0; i < assertion.length; i++) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   324
            if (assertion[i].oid.equals((Object)oid)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return assertion[i].value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Encode the RDN in DER-encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param out DerOutputStream to which RDN is to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        out.putOrderedSetOf(DerValue.tag_Set, assertion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Returns a printable form of this RDN, using RFC 1779 style catenation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * of attribute/value assertions, and emitting attribute type keywords
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   344
     * from RFCs 1779, 2253, and 5280.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (assertion.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return assertion[0].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for (int i = 0; i < assertion.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                sb.append(" + ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            sb.append(assertion[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Returns a printable form of this RDN using the algorithm defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * RFC 1779. Only RFC 1779 attribute type keywords are emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public String toRFC1779String() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return toRFC1779String(Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Returns a printable form of this RDN using the algorithm defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * RFC 1779. RFC 1779 attribute type keywords are emitted, as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * as keywords contained in the OID/keyword map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public String toRFC1779String(Map<String, String> oidMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (assertion.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return assertion[0].toRFC1779String(oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        for (int i = 0; i < assertion.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                sb.append(" + ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            sb.append(assertion[i].toRFC1779String(oidMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Returns a printable form of this RDN using the algorithm defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * RFC 2253. Only RFC 2253 attribute type keywords are emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public String toRFC2253String() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return toRFC2253StringInternal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            (false, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Returns a printable form of this RDN using the algorithm defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * RFC 2253. RFC 2253 attribute type keywords are emitted, as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * keywords contained in the OID/keyword map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public String toRFC2253String(Map<String, String> oidMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return toRFC2253StringInternal(false, oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Returns a printable form of this RDN using the algorithm defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * RFC 2253. Only RFC 2253 attribute type keywords are emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * If canonical is true, then additional canonicalizations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * documented in X500Principal.getName are performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public String toRFC2253String(boolean canonical) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (canonical == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            return toRFC2253StringInternal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                (false, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        String c = canonicalString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            c = toRFC2253StringInternal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                (true, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            canonicalString = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private String toRFC2253StringInternal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        (boolean canonical, Map<String, String> oidMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * Section 2.2: When converting from an ASN.1 RelativeDistinguishedName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * to a string, the output consists of the string encodings of each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         * AttributeTypeAndValue (according to 2.3), in any order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * Where there is a multi-valued RDN, the outputs from adjoining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * AttributeTypeAndValues are separated by a plus ('+' ASCII 43)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        // normally, an RDN only contains one AVA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (assertion.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return canonical ? assertion[0].toRFC2253CanonicalString() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                               assertion[0].toRFC2253String(oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        StringBuilder relname = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (!canonical) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            for (int i = 0; i < assertion.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    relname.append('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                relname.append(assertion[i].toRFC2253String(oidMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            // order the string type AVA's alphabetically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            // followed by the oid type AVA's numerically
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 26967
diff changeset
   456
            List<AVA> avaList = new ArrayList<>(assertion.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            for (int i = 0; i < assertion.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                avaList.add(assertion[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            java.util.Collections.sort(avaList, AVAComparator.getInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            for (int i = 0; i < avaList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    relname.append('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                relname.append(avaList.get(i).toRFC2253CanonicalString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return relname.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
class AVAComparator implements Comparator<AVA> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private static final Comparator<AVA> INSTANCE = new AVAComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private AVAComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    static Comparator<AVA> getInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return INSTANCE;
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
     * AVA's containing a standard keyword are ordered alphabetically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * followed by AVA's containing an OID keyword, ordered numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public int compare(AVA a1, AVA a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        boolean a1Has2253 = a1.hasRFC2253Keyword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        boolean a2Has2253 = a2.hasRFC2253Keyword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (a1Has2253 == a2Has2253) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            return a1.toRFC2253CanonicalString().compareTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        (a2.toRFC2253CanonicalString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (a1Has2253) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
}