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.ArrayList;
22import java.util.Collection;
23import java.util.Iterator;
24import java.util.List;
25
26import org.apache.harmony.security.asn1.ASN1SequenceOf;
27import org.apache.harmony.security.asn1.ASN1Type;
28import org.apache.harmony.security.asn1.BerInputStream;
29import org.apache.harmony.security.internal.nls.Messages;
30
31/**
32 * The class encapsulates the ASN.1 DER encoding/decoding work
33 * with the SubjectInfoAccessSyntax and AuthorityInfoAccessSyntax
34 * which are a part of X.509 framework
35 * (as specified in RFC 3280 -
36 *  Internet X.509 Public Key Infrastructure.
37 *  Certificate and Certificate Revocation List (CRL) Profile.
38 *  http://www.ietf.org/rfc/rfc3280.txt):
39 *
40 *  SubjectInfoAccessSyntax  ::=
41 *      SEQUENCE SIZE (1..MAX) OF AccessDescriptions
42
43 *  AuthorityInfoAccessSyntax  ::=
44 *      SEQUENCE SIZE (1..MAX) OF AccessDescriptions
45 *
46 *  AccessDescription  ::=  SEQUENCE {
47 *      accessMethod          OBJECT IDENTIFIER,
48 *      accessLocation        GeneralName  }
49 *
50 */
51public class InfoAccessSyntax extends ExtensionValue {
52
53    private final List accessDescriptions;
54
55    public InfoAccessSyntax(List accessDescriptions) throws IOException {
56        this(accessDescriptions, null);
57    }
58
59    private InfoAccessSyntax(List accessDescriptions, byte[] encoding)
60            throws IOException {
61        if (accessDescriptions == null || accessDescriptions.isEmpty()) {
62            // "AccessDescriptions list is null or empty"
63            throw new IOException(Messages.getString("security.1A3")); //$NON-NLS-1$
64        }
65        this.accessDescriptions = accessDescriptions;
66        this.encoding = encoding;
67    }
68
69    public List getAccessDescriptions() {
70        return new ArrayList(accessDescriptions);
71    }
72
73    /**
74     * Returns ASN.1 encoded form of this X.509 InfoAccessSyntax.
75     * @return a byte array containing ASN.1 encoded form.
76     */
77    public byte[] getEncoded() {
78        if (encoding == null) {
79            encoding = ASN1.encode(this);
80        }
81        return encoding;
82    }
83
84    public static InfoAccessSyntax decode(byte[] encoding) throws IOException {
85        return ((InfoAccessSyntax) ASN1.decode(encoding));
86    }
87
88    public String toString() {
89        StringBuilder res = new StringBuilder();
90        res.append("\n---- InfoAccessSyntax:"); //$NON-NLS-1$
91        if (accessDescriptions != null) {
92            for (Iterator it = accessDescriptions.iterator(); it.hasNext();) {
93                res.append('\n');
94                res.append(it.next());
95            }
96        }
97        res.append("\n---- InfoAccessSyntax END\n"); //$NON-NLS-1$
98        return res.toString();
99    }
100
101    /**
102     * Places the string representation of extension value
103     * into the StringBuffer object.
104     */
105    public void dumpValue(StringBuffer buffer, String prefix) {
106        buffer.append(prefix).append("AccessDescriptions:\n"); //$NON-NLS-1$
107        if (accessDescriptions == null || accessDescriptions.isEmpty()) {
108            buffer.append("NULL\n"); //$NON-NLS-1$
109        } else {
110            Iterator itr = accessDescriptions.iterator();
111            while (itr.hasNext()) {
112                buffer.append(itr.next().toString());
113            }
114        }
115    }
116
117
118    /**
119     * ASN.1 DER X.509 AuthorityInfoAccessSyntax and SubjectInfoAccessSyntax
120     * encoder/decoder class.
121     */
122    public static final ASN1Type ASN1 = new ASN1SequenceOf(AccessDescription.ASN1) {
123
124        public Object getDecodedObject(BerInputStream in) throws IOException {
125            return new InfoAccessSyntax((List)in.content, in.getEncoded());
126        }
127
128        public Collection getValues(Object object) {
129            InfoAccessSyntax aias = (InfoAccessSyntax) object;
130            return aias.accessDescriptions;
131        }
132    };
133
134}
135
136