KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > IetfAttrSyntax


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 package org.apache.geronimo.util.asn1.x509;
19
20 import java.util.Enumeration JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.apache.geronimo.util.asn1.ASN1Encodable;
24 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
25 import org.apache.geronimo.util.asn1.ASN1OctetString;
26 import org.apache.geronimo.util.asn1.ASN1Sequence;
27 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
28 import org.apache.geronimo.util.asn1.DERObject;
29 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
30 import org.apache.geronimo.util.asn1.DEROctetString;
31 import org.apache.geronimo.util.asn1.DERSequence;
32 import org.apache.geronimo.util.asn1.DERTaggedObject;
33 import org.apache.geronimo.util.asn1.DERUTF8String;
34
35 /**
36  * Implementation of <code>IetfAttrSyntax</code> as specified by RFC3281.
37  */

38 public class IetfAttrSyntax
39     extends ASN1Encodable
40 {
41     public static final int VALUE_OCTETS = 1;
42     public static final int VALUE_OID = 2;
43     public static final int VALUE_UTF8 = 3;
44     GeneralNames policyAuthority = null;
45     Vector JavaDoc values = new Vector JavaDoc();
46     int valueChoice = -1;
47
48     /**
49      *
50      */

51     public IetfAttrSyntax(ASN1Sequence seq)
52     {
53         int i = 0;
54
55         if (seq.getObjectAt(0) instanceof ASN1TaggedObject)
56         {
57             policyAuthority = GeneralNames.getInstance(((ASN1TaggedObject)seq.getObjectAt(0)), false);
58             i++;
59         }
60         else if (seq.size() == 2)
61         { // VOMS fix
62
policyAuthority = GeneralNames.getInstance(seq.getObjectAt(0));
63             i++;
64         }
65
66         if (!(seq.getObjectAt(i) instanceof ASN1Sequence))
67         {
68             throw new IllegalArgumentException JavaDoc("Non-IetfAttrSyntax encoding");
69         }
70
71         seq = (ASN1Sequence)seq.getObjectAt(i);
72
73         for (Enumeration JavaDoc e = seq.getObjects(); e.hasMoreElements();)
74         {
75             DERObject obj = (DERObject)e.nextElement();
76             int type;
77
78             if (obj instanceof DERObjectIdentifier)
79             {
80                 type = VALUE_OID;
81             }
82             else if (obj instanceof DERUTF8String)
83             {
84                 type = VALUE_UTF8;
85             }
86             else if (obj instanceof DEROctetString)
87             {
88                 type = VALUE_OCTETS;
89             }
90             else
91             {
92                 throw new IllegalArgumentException JavaDoc("Bad value type encoding IetfAttrSyntax");
93             }
94
95             if (valueChoice < 0)
96             {
97                 valueChoice = type;
98             }
99
100             if (type != valueChoice)
101             {
102                 throw new IllegalArgumentException JavaDoc("Mix of value types in IetfAttrSyntax");
103             }
104
105             values.addElement(obj);
106         }
107     }
108
109     public GeneralNames getPolicyAuthority()
110     {
111         return policyAuthority;
112     }
113
114     public int getValueType()
115     {
116         return valueChoice;
117     }
118
119     public Object JavaDoc[] getValues()
120     {
121         if (this.getValueType() == VALUE_OCTETS)
122         {
123             ASN1OctetString[] tmp = new ASN1OctetString[values.size()];
124
125             for (int i = 0; i != tmp.length; i++)
126             {
127                 tmp[i] = (ASN1OctetString)values.elementAt(i);
128             }
129
130             return tmp;
131         }
132         else if (this.getValueType() == VALUE_OID)
133         {
134             DERObjectIdentifier[] tmp = new DERObjectIdentifier[values.size()];
135
136             for (int i = 0; i != tmp.length; i++)
137             {
138                 tmp[i] = (DERObjectIdentifier)values.elementAt(i);
139             }
140
141             return tmp;
142         }
143         else
144         {
145             DERUTF8String[] tmp = new DERUTF8String[values.size()];
146
147             for (int i = 0; i != tmp.length; i++)
148             {
149                 tmp[i] = (DERUTF8String)values.elementAt(i);
150             }
151
152             return tmp;
153         }
154     }
155
156     /**
157      *
158      * <pre>
159      *
160      * IetfAttrSyntax ::= SEQUENCE {
161      * policyAuthority [0] GeneralNames OPTIONAL,
162      * values SEQUENCE OF CHOICE {
163      * octets OCTET STRING,
164      * oid OBJECT IDENTIFIER,
165      * string UTF8String
166      * }
167      * }
168      *
169      * </pre>
170      */

171     public DERObject toASN1Object()
172     {
173         ASN1EncodableVector v = new ASN1EncodableVector();
174
175         if (policyAuthority != null)
176         {
177             v.add(new DERTaggedObject(0, policyAuthority));
178         }
179
180         ASN1EncodableVector v2 = new ASN1EncodableVector();
181
182         for (Enumeration JavaDoc i = values.elements(); i.hasMoreElements();)
183         {
184             v2.add((ASN1Encodable)i.nextElement());
185         }
186
187         v.add(new DERSequence(v2));
188
189         return new DERSequence(v);
190     }
191 }
192
Popular Tags