001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.x509;
019:
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.harmony.security.asn1.ASN1SequenceOf;
027: import org.apache.harmony.security.asn1.ASN1Type;
028: import org.apache.harmony.security.asn1.BerInputStream;
029: import org.apache.harmony.security.internal.nls.Messages;
030:
031: /**
032: * The class encapsulates the ASN.1 DER encoding/decoding work
033: * with the SubjectInfoAccessSyntax and AuthorityInfoAccessSyntax
034: * which are a part of X.509 framework
035: * (as specified in RFC 3280 -
036: * Internet X.509 Public Key Infrastructure.
037: * Certificate and Certificate Revocation List (CRL) Profile.
038: * http://www.ietf.org/rfc/rfc3280.txt):
039: *
040: * SubjectInfoAccessSyntax ::=
041: * SEQUENCE SIZE (1..MAX) OF AccessDescriptions
042:
043: * AuthorityInfoAccessSyntax ::=
044: * SEQUENCE SIZE (1..MAX) OF AccessDescriptions
045: *
046: * AccessDescription ::= SEQUENCE {
047: * accessMethod OBJECT IDENTIFIER,
048: * accessLocation GeneralName }
049: *
050: */
051: public class InfoAccessSyntax extends ExtensionValue {
052:
053: private final List accessDescriptions;
054:
055: public InfoAccessSyntax(List accessDescriptions) throws IOException {
056: this (accessDescriptions, null);
057: }
058:
059: private InfoAccessSyntax(List accessDescriptions, byte[] encoding)
060: throws IOException {
061: if (accessDescriptions == null || accessDescriptions.isEmpty()) {
062: // "AccessDescriptions list is null or empty"
063: throw new IOException(Messages.getString("security.1A3")); //$NON-NLS-1$
064: }
065: this .accessDescriptions = accessDescriptions;
066: this .encoding = encoding;
067: }
068:
069: public List getAccessDescriptions() {
070: return new ArrayList(accessDescriptions);
071: }
072:
073: /**
074: * Returns ASN.1 encoded form of this X.509 InfoAccessSyntax.
075: * @return a byte array containing ASN.1 encoded form.
076: */
077: public byte[] getEncoded() {
078: if (encoding == null) {
079: encoding = ASN1.encode(this );
080: }
081: return encoding;
082: }
083:
084: public static InfoAccessSyntax decode(byte[] encoding)
085: throws IOException {
086: return ((InfoAccessSyntax) ASN1.decode(encoding));
087: }
088:
089: public String toString() {
090: StringBuffer res = new StringBuffer();
091: res.append("\n---- InfoAccessSyntax:"); //$NON-NLS-1$
092: if (accessDescriptions != null) {
093: for (Iterator it = accessDescriptions.iterator(); it
094: .hasNext();) {
095: res.append('\n');
096: res.append(it.next());
097: }
098: }
099: res.append("\n---- InfoAccessSyntax END\n"); //$NON-NLS-1$
100: return res.toString();
101: }
102:
103: /**
104: * Places the string representation of extension value
105: * into the StringBuffer object.
106: */
107: public void dumpValue(StringBuffer buffer, String prefix) {
108: buffer.append(prefix).append("AccessDescriptions:\n"); //$NON-NLS-1$
109: if (accessDescriptions == null || accessDescriptions.isEmpty()) {
110: buffer.append("NULL\n"); //$NON-NLS-1$
111: } else {
112: Iterator itr = accessDescriptions.iterator();
113: while (itr.hasNext()) {
114: buffer.append(itr.next().toString());
115: }
116: }
117: }
118:
119: /**
120: * ASN.1 DER X.509 AuthorityInfoAccessSyntax and SubjectInfoAccessSyntax
121: * encoder/decoder class.
122: */
123: public static final ASN1Type ASN1 = new ASN1SequenceOf(
124: AccessDescription.ASN1) {
125:
126: public Object getDecodedObject(BerInputStream in)
127: throws IOException {
128: return new InfoAccessSyntax((List) in.content, in
129: .getEncoded());
130: }
131:
132: public Collection getValues(Object object) {
133: InfoAccessSyntax aias = (InfoAccessSyntax) object;
134: return aias.accessDescriptions;
135: }
136: };
137:
138: }
|