1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.security.x509;
19
20import java.io.IOException;
21import java.util.List;
22
23/**
24 * This class implements the values of Subject Alternative Name
25 * (OID is 2.5.29.17) and Issuer Alternative Name extensions
26 * (OID is 2.5.29.18).<br>
27 * For more information about these extensions see RFC 3280
28 * at http://www.ietf.org/rfc/rfc3280.txt
29 */
30public class AlternativeName extends ExtensionValue {
31
32    // constants indicating which alternative name is presented
33    // by this object
34    public static final boolean ISSUER = false;
35    public static final boolean SUBJECT = true;
36
37    // indicating which alternative name is presented by this object
38    private boolean which;
39    // the alternative names
40    private GeneralNames alternativeNames;
41
42    /**
43     * Creates the extension object for given alternative names.
44     * @param which specifies which alternative names are given
45     * (Subject's or Issuer's)
46     */
47    public AlternativeName(boolean which, GeneralNames alternativeNames) {
48        this.which = which;
49        this.alternativeNames = alternativeNames;
50    }
51
52    /**
53     * Creates the extension object on the base of its encoded form.
54     * @param which specifies which alternative names are given
55     * (Subject's or Issuer's)
56     */
57    public AlternativeName(boolean which, byte[] encoding) throws IOException {
58        super(encoding);
59        this.which = which;
60        this.alternativeNames =
61            (GeneralNames) GeneralNames.ASN1.decode(encoding);
62    }
63
64    /**
65     * Returns the list of alternative names.
66     * The list is in the collection of pairs:<br>
67     * [Integer (tag of GeneralName), Object (name value)]
68     */
69    public List getAlternativeNames() {
70        return alternativeNames.getPairsList();
71    }
72
73    /**
74     * Returns ASN.1 encoded form of this X.509 AlternativeName value.
75     * @return a byte array containing ASN.1 encode form.
76     */
77    public byte[] getEncoded() {
78        if (encoding == null) {
79            encoding = GeneralNames.ASN1.encode(alternativeNames);
80        }
81        return encoding;
82    }
83
84    /**
85     * Places the string representation of extension value
86     * into the StringBuffer object.
87     */
88    public void dumpValue(StringBuffer buffer, String prefix) {
89        buffer.append(prefix).append((which) ? "Subject" : "Issuer") //$NON-NLS-1$ //$NON-NLS-2$
90            .append(" Alternative Names [\n"); //$NON-NLS-1$
91        alternativeNames.dumpValue(buffer, prefix + "  "); //$NON-NLS-1$
92        buffer.append(prefix).append("]\n"); //$NON-NLS-1$
93    }
94}
95
96