KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ietf > jgss > Oid


1 /*
2  * @(#)Oid.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7  
8 package org.ietf.jgss;
9
10 import java.io.InputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import sun.security.util.DerValue;
13 import sun.security.util.DerOutputStream;
14 import sun.security.util.ObjectIdentifier;
15
16 /**
17  * This class represents Universal Object Identifiers (Oids) and their
18  * associated operations.<p>
19  *
20  * Oids are hierarchically globally-interpretable identifiers used
21  * within the GSS-API framework to identify mechanisms and name formats.<p>
22  *
23  * The structure and encoding of Oids is defined in ISOIEC-8824 and
24  * ISOIEC-8825. For example the Oid representation of Kerberos V5
25  * mechanism is "1.2.840.113554.1.2.2"<p>
26  *
27  * The GSSName name class contains public static Oid objects
28  * representing the standard name types defined in GSS-API.
29  *
30  * @author Mayank Upadhyay
31  * @version 1.8, 12/19/03
32  * @since 1.4
33  */

34 public class Oid {
35
36     private ObjectIdentifier oid;
37     private byte[] derEncoding;
38
39     /**
40      * Constructs an Oid object from a string representation of its
41      * integer components.
42      *
43      * @param strOid the dot separated string representation of the oid.
44      * For instance, "1.2.840.113554.1.2.2".
45      * @exception GSSException may be thrown when the string is incorrectly
46      * formatted
47      */

48     public Oid(String JavaDoc strOid) throws GSSException JavaDoc {
49
50         try {
51         oid = new ObjectIdentifier(strOid);
52         derEncoding = null;
53         } catch (Exception JavaDoc e) {
54             throw new GSSException JavaDoc(GSSException.FAILURE,
55                           "Improperly formatted Object Identifier String - "
56                           + strOid);
57     }
58     }
59
60     /**
61      * Creates an Oid object from its ASN.1 DER encoding. This refers to
62      * the full encoding including tag and length. The structure and
63      * encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825. This
64      * method is identical in functionality to its byte array counterpart.
65      *
66      * @param derOid stream containing the DER encoded oid
67      * @exception GSSException may be thrown when the DER encoding does not
68      * follow the prescribed format.
69      */

70     public Oid(InputStream JavaDoc derOid) throws GSSException JavaDoc {
71     try {
72         DerValue derVal = new DerValue(derOid);
73         derEncoding = derVal.toByteArray();
74         oid = derVal.getOID();
75     } catch (IOException JavaDoc e) {
76             throw new GSSException JavaDoc(GSSException.FAILURE,
77                           "Improperly formatted ASN.1 DER encoding for Oid");
78     }
79     }
80
81
82     /**
83      * Creates an Oid object from its ASN.1 DER encoding. This refers to
84      * the full encoding including tag and length. The structure and
85      * encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825. This
86      * method is identical in functionality to its InputStream conterpart.
87      *
88      * @param data byte array containing the DER encoded oid
89      * @exception GSSException may be thrown when the DER encoding does not
90      * follow the prescribed format.
91      */

92     public Oid(byte [] data) throws GSSException JavaDoc {
93     try {
94         DerValue derVal = new DerValue(data);
95         derEncoding = derVal.toByteArray();
96         oid = derVal.getOID();
97     } catch (IOException JavaDoc e) {
98             throw new GSSException JavaDoc(GSSException.FAILURE,
99                           "Improperly formatted ASN.1 DER encoding for Oid");
100     }
101     }
102
103     /**
104      * Only for calling by initializators used with declarations.
105      *
106      * @param strOid
107      */

108     static Oid JavaDoc getInstance(String JavaDoc strOid) {
109         Oid JavaDoc retVal = null;
110         try {
111             retVal = new Oid JavaDoc(strOid);
112         } catch (GSSException JavaDoc e) {
113             // squelch it!
114
}
115         return retVal;
116     }
117  
118     /**
119      * Returns a string representation of the oid's integer components
120      * in dot separated notation.
121      *
122      * @return string representation in the following format: "1.2.3.4.5"
123      */

124     public String JavaDoc toString() {
125     return oid.toString();
126     }
127
128     /**
129      * Tests if two Oid objects represent the same Object identifier
130      * value.
131      *
132      * @return <code>true</code> if the two Oid objects represent the same
133      * value, <code>false</code> otherwise.
134      * @param other the Oid object that has to be compared to this one
135      */

136     public boolean equals(Object JavaDoc other) {
137
138         //check if both reference the same object
139
if (this == other)
140             return (true);
141
142     if (other instanceof Oid JavaDoc)
143         return this.oid.equals(((Oid JavaDoc) other).oid);
144     else if (other instanceof ObjectIdentifier)
145         return this.oid.equals(other);
146     else
147         return false;
148     }
149
150     
151     /**
152      * Returns the full ASN.1 DER encoding for this oid object, which
153      * includes the tag and length.
154      *
155      * @return byte array containing the DER encoding of this oid object.
156      * @exception GSSException may be thrown when the oid can't be encoded
157      */

158     public byte[] getDER() throws GSSException JavaDoc {
159     
160         if (derEncoding == null) {
161         DerOutputStream dout = new DerOutputStream();
162         try {
163         dout.putOID(oid);
164         } catch (IOException JavaDoc e) {
165         throw new GSSException JavaDoc(GSSException.FAILURE, e.getMessage());
166         }
167         derEncoding = dout.toByteArray();
168     }
169
170         return derEncoding;
171     }
172     
173     /**
174      * A utility method to test if this Oid value is contained within the
175      * supplied Oid array.
176      *
177      * @param oids the array of Oid's to search
178      * @return true if the array contains this Oid value, false otherwise
179      */

180     public boolean containedIn(Oid JavaDoc[] oids) {
181     
182         for (int i = 0; i < oids.length; i++) {
183             if (oids[i].equals(this))
184                 return (true);
185         }
186     
187         return (false);
188     }
189     
190
191     /**
192      * Returns a hashcode value for this Oid.
193      *
194      * @return a hashCode value
195      */

196     public int hashCode() {
197     return oid.hashCode();
198     }
199 }
200
Popular Tags