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