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 org.apache.harmony.security.asn1.ASN1Oid;
21import org.apache.harmony.security.asn1.ASN1Sequence;
22import org.apache.harmony.security.asn1.ASN1Type;
23import org.apache.harmony.security.asn1.BerInputStream;
24import org.apache.harmony.security.asn1.ObjectIdentifier;
25
26/**
27 * The class encapsulates the ASN.1 DER encoding/decoding work
28 * with the AccessDescription which is a part of X.509 framework
29 * (as specified in RFC 3280 -
30 *  Internet X.509 Public Key Infrastructure.
31 *  Certificate and Certificate Revocation List (CRL) Profile.
32 *  http://www.ietf.org/rfc/rfc3280.txt):
33 *
34 *  AccessDescription  ::=  SEQUENCE {
35 *      accessMethod          OBJECT IDENTIFIER,
36 *      accessLocation        GeneralName  }
37 */
38public final class AccessDescription {
39
40    /** the value of access method */
41    private final String accessMethod;
42
43    /** the value of accessLocation */
44    private final GeneralName accessLocation;
45
46    private byte[] encoding;
47
48    private AccessDescription(String accessMethod, GeneralName accessLocation, byte[] encoding) {
49        this.accessMethod = accessMethod;
50        this.accessLocation = accessLocation;
51        this.encoding = encoding;
52    }
53
54    /**
55     * Returns ASN.1 encoded form of this X.509 AccessDescription.
56     */
57    public byte[] getEncoded() {
58        if (encoding == null) {
59            encoding = ASN1.encode(this);
60        }
61        return encoding;
62    }
63
64    @Override public String toString() {
65        StringBuilder res = new StringBuilder();
66        res.append("\n-- AccessDescription:");
67        res.append("\naccessMethod:  ");
68        res.append(accessMethod);
69        res.append("\naccessLocation:  ");
70        res.append(accessLocation);
71        res.append("\n-- AccessDescription END\n");
72        return res.toString();
73    }
74
75    /**
76     * Custom AccessDescription DER encoder/decoder
77     */
78    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
79            ASN1Oid.getInstance(),
80            GeneralName.ASN1 }) {
81
82        @Override protected Object getDecodedObject(BerInputStream in) {
83            Object[] values = (Object[]) in.content;
84            return new AccessDescription(
85                    ObjectIdentifier.toString((int[]) values[0]),
86                    (GeneralName) values[1], in.getEncoded());
87        }
88
89        @Override protected void getValues(Object object, Object[] values) {
90            AccessDescription ad = (AccessDescription) object;
91            values[0] = ObjectIdentifier.toIntArray(ad.accessMethod);
92            values[1] = ad.accessLocation;
93        }
94    };
95}
96
97