OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / org / apache / harmony / security / x509 / GeneralNames.java
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
18 /**
19 * @author Alexander Y. Kleymenov
20 * @version $Revision$
21 */
22
23 package org.apache.harmony.security.x509;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Iterator;
28 import java.util.List;
29 import org.apache.harmony.security.asn1.ASN1SequenceOf;
30 import org.apache.harmony.security.asn1.ASN1Type;
31 import org.apache.harmony.security.asn1.BerInputStream;
32
33
34 /**
35  * The class encapsulates the ASN.1 DER encoding/decoding work
36  * with the GeneralNames structure which is a part of X.509 certificate
37  * (as specified in RFC 3280 -
38  *  Internet X.509 Public Key Infrastructure.
39  *  Certificate and Certificate Revocation List (CRL) Profile.
40  *  http://www.ietf.org/rfc/rfc3280.txt):
41  *
42  *
43  * <pre>
44  *   GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
45  * </pre>
46  *
47  * @see org.apache.harmony.security.x509.NameConstraints
48  * @see org.apache.harmony.security.x509.GeneralSubtree
49  */
50 public class GeneralNames {
51
52     // the values of GeneralName
53     private List generalNames;
54     // the ASN.1 encoded form of GeneralNames
55     private byte[] encoding;
56
57     /**
58      * Constructs an object representing the value of GeneralNames.
59      */
60     public GeneralNames() {
61         generalNames = new ArrayList();
62     }
63
64     /**
65      * TODO
66      * @param   generalNames:   List
67      */
68     public GeneralNames(List generalNames) {
69         this.generalNames = generalNames;
70     }
71
72     //
73     // TODO
74     // @param   generalNames:   List
75     // @param   encoding:   byte[]
76     //
77     private GeneralNames(List generalNames, byte[] encoding) {
78         this.generalNames = generalNames;
79         this.encoding = encoding;
80     }
81
82     /**
83      * Returns the list of values.
84      * @return  names
85      */
86     public List getNames() {
87         if ((generalNames == null) || (generalNames.size() == 0)) {
88             return null;
89         }
90         return new ArrayList(generalNames);
91     }
92
93     /**
94      * Returns the collection of pairs: (Integer (tag), Object (name value))*
95      * @return the collection of pairs: (Integer (tag), Object (name value))*
96      */
97     public List getPairsList() {
98         ArrayList result = new ArrayList();
99         if (generalNames == null) {
100             return result;
101         }
102         Iterator it = generalNames.iterator();
103         while (it.hasNext()) {
104             result.add(((GeneralName) it.next()).getAsList());
105         }
106         return result;
107     }
108
109     /**
110      * TODO
111      * @param   name:   GeneralName
112      * @return
113      */
114     public void addName(GeneralName name) {
115         encoding = null;
116         if (generalNames == null) {
117             generalNames = new ArrayList();
118         }
119         generalNames.add(name);
120     }
121
122     /* *
123      * TODO
124      * @param   name:   GeneralName
125      * @return
126      *
127     public GeneralName getNameByTag(int tag) {
128         encoding = null;
129         if ((generalNames == null) || (generalNames.size() == 0)) {
130             return null;
131         }
132         for (int i=0; i<generalNames.size(); i++) {
133             if (((GeneralName) generalName.get(i)).getTag() == tag) {
134             }
135         }
136         generalNames.add(name);
137     }
138      */
139
140     /**
141      * Returns ASN.1 encoded form of this X.509 GeneralNames value.
142      * @return a byte array containing ASN.1 encode form.
143      */
144     public byte[] getEncoded() {
145         if (encoding == null) {
146             encoding = ASN1.encode(this);
147         }
148         return encoding;
149     }
150
151     /**
152      * Places the string representation of extension value
153      * into the StringBuffer object.
154      */
155     public void dumpValue(StringBuffer buffer, String prefix) {
156         if (generalNames == null) {
157             return;
158         }
159         for (Iterator it=generalNames.iterator(); it.hasNext();) {
160             buffer.append(prefix);
161             buffer.append(it.next());
162             buffer.append('\n');
163         }
164     }
165
166     /**
167      * ASN.1 DER X.509 GeneralNames encoder/decoder class.
168      */
169     public static final ASN1Type ASN1 = new ASN1SequenceOf(GeneralName.ASN1) {
170
171         public Object getDecodedObject(BerInputStream in) {
172             return new GeneralNames((List)in.content, in.getEncoded());
173         }
174
175         public Collection getValues(Object object) {
176             GeneralNames gns = (GeneralNames) object;
177             return gns.generalNames;
178         }
179     };
180 }