KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > dn > DNFieldExtractor


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.util.dn;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.log4j.Logger;
21 import org.ejbca.util.CertTools;
22 import org.ietf.ldap.LDAPDN;
23
24
25 /**
26  * A class used to retrieve different fields from a Distiguished Name or Subject Alternate Name
27  * or Subject Directory Attributes strings.
28  *
29  * @author Philip Vendil
30  * @version $Id: DNFieldExtractor.java,v 1.7 2007/01/16 11:46:13 anatom Exp $
31  */

32 public class DNFieldExtractor implements java.io.Serializable JavaDoc {
33     private static final Logger log = Logger.getLogger(DNFieldExtractor.class);
34     // Public constants
35
public static final int TYPE_SUBJECTDN = 0;
36     public static final int TYPE_SUBJECTALTNAME = 1;
37     public static final int TYPE_SUBJECTDIRATTR = 2;
38
39     // Subject DN Fields.
40
public static final int E = 0;
41     public static final int UID = 1;
42     public static final int CN = 2;
43     public static final int SN = 3;
44     public static final int GIVENNAME = 4;
45     public static final int INITIALS = 5;
46     public static final int SURNAME = 6;
47     public static final int T = 7;
48     public static final int OU = 8;
49     public static final int O = 9;
50     public static final int L = 10;
51     public static final int ST = 11;
52     public static final int DC = 12;
53     public static final int C = 13;
54     public static final int UNSTRUCTUREDADDRESS = 14;
55     public static final int UNSTRUCTUREDNAME = 15;
56     
57     // Subject Alternative Names.
58
public static final int OTHERNAME = 16;
59     public static final int RFC822NAME = 17;
60     public static final int DNSNAME = 18;
61     public static final int IPADDRESS = 19;
62     public static final int X400ADDRESS = 20;
63     public static final int DIRECTORYNAME = 21;
64     public static final int EDIPARTNAME = 22;
65     public static final int URI = 23;
66     public static final int REGISTEREDID = 24;
67     public static final int UPN = 25;
68     public static final int GUID = 26;
69     
70     // Subject Directory Attributes
71
public static final int DATEOFBIRTH = 27;
72     public static final int PLACEOFBIRTH = 28;
73     public static final int GENDER = 29;
74     public static final int COUNTRYOFCITIZENSHIP = 30;
75     public static final int COUNTRYOFRESIDENCE = 31;
76     
77     /**
78      * Creates a new instance of DNFieldExtractor
79      *
80      * @param dn DOCUMENT ME!
81      * @param type DOCUMENT ME!
82      */

83     public DNFieldExtractor(String JavaDoc dn, int type) {
84         dnfields = new HashMap JavaDoc();
85         setDN(dn, type);
86     }
87     
88     /** Fields that can be selected in Certificate profile and Publisher
89      */

90     public static Integer JavaDoc[] getUseFields(int type) {
91         if (type == DNFieldExtractor.TYPE_SUBJECTDN) {
92             return (Integer JavaDoc[])DnComponents.getDnDnIds().toArray(new Integer JavaDoc[0]);
93         } else if (type == DNFieldExtractor.TYPE_SUBJECTALTNAME) {
94             return (Integer JavaDoc[])DnComponents.getAltNameDnIds().toArray(new Integer JavaDoc[0]);
95         } else if (type == DNFieldExtractor.TYPE_SUBJECTDIRATTR) {
96             return (Integer JavaDoc[])DnComponents.getDirAttrDnIds().toArray(new Integer JavaDoc[0]);
97         } else {
98             return new Integer JavaDoc[0];
99         }
100     }
101     
102     public static String JavaDoc getFieldComponent(int field, int type) {
103         if (type == DNFieldExtractor.TYPE_SUBJECTDN) {
104             String JavaDoc ret = DnComponents.getDnExtractorFieldFromDnId(field);
105             return ret;
106         } else if (type == DNFieldExtractor.TYPE_SUBJECTALTNAME) {
107             String JavaDoc ret = DnComponents.getAltNameExtractorFieldFromDnId(field);
108             return ret;
109         } else {
110             String JavaDoc ret = DnComponents.getDirAttrExtractorFieldFromDnId(field);
111             return ret;
112         }
113     }
114
115     /**
116      * DOCUMENT ME!
117      *
118      * @param dn DOCUMENT ME!
119      * @param type DOCUMENT ME!
120      */

121     public void setDN(String JavaDoc dn, int type) {
122         this.type = type;
123         ArrayList JavaDoc ids;
124         if (type == TYPE_SUBJECTDN) {
125             ids = DnComponents.getDnDnIds();
126         } else if (type == TYPE_SUBJECTALTNAME){
127             ids = DnComponents.getAltNameDnIds();
128         } else if (type == TYPE_SUBJECTDIRATTR){
129             ids = DnComponents.getDirAttrDnIds();
130         } else {
131             ids = new ArrayList JavaDoc();
132         }
133         fieldnumbers = new HashMap JavaDoc();
134         Iterator JavaDoc it = ids.iterator();
135         while (it.hasNext()) {
136             Integer JavaDoc id = (Integer JavaDoc)it.next();
137             fieldnumbers.put(id, new Integer JavaDoc(0));
138         }
139
140         if ((dn != null) && !dn.equalsIgnoreCase("null")) {
141             dnfields = new HashMap JavaDoc();
142
143             try {
144                 String JavaDoc[] dnexploded = LDAPDN.explodeDN(dn, false);
145
146                 for (int i = 0; i < dnexploded.length; i++) {
147                     boolean exists = false;
148                     Iterator JavaDoc iter = ids.iterator();
149                     while (iter.hasNext()) {
150                         Integer JavaDoc id = (Integer JavaDoc)iter.next();
151                         Integer JavaDoc number = (Integer JavaDoc)fieldnumbers.get(id);
152                         String JavaDoc field;
153                         if (type == TYPE_SUBJECTDN) {
154                             field = DnComponents.getDnExtractorFieldFromDnId(id.intValue());
155                         } else if (type == TYPE_SUBJECTALTNAME){
156                             field = DnComponents.getAltNameExtractorFieldFromDnId(id.intValue());
157                         } else {
158                             field = DnComponents.getDirAttrExtractorFieldFromDnId(id.intValue());
159                         }
160                         String JavaDoc dnex = dnexploded[i].toUpperCase();
161                         if (id.intValue() == DNFieldExtractor.URI) {
162                             // Fix up URI, which can have several forms
163
if (dnex.indexOf(CertTools.URI.toUpperCase()+"=") > -1) {
164                                 field = CertTools.URI.toUpperCase()+"=";
165                             }
166                             if (dnex.indexOf(CertTools.URI1.toUpperCase()+"=") > -1) {
167                                 field = CertTools.URI1.toUpperCase()+"=";
168                             }
169                         }
170                         if (dnex.startsWith(field)) {
171                             exists = true;
172                             String JavaDoc rdn = LDAPDN.unescapeRDN(dnexploded[i]);
173                             // We don't want the CN= (or whatever) part of the RDN
174
if (rdn.toUpperCase().startsWith(field)) {
175                                 rdn = rdn.substring(field.length(),rdn.length());
176                             }
177
178                             if (type == TYPE_SUBJECTDN) {
179                                 dnfields.put(new Integer JavaDoc((id.intValue() * BOUNDRARY) + number.intValue()), rdn);
180                             } else if (type == TYPE_SUBJECTALTNAME) {
181                                 dnfields.put(new Integer JavaDoc((id.intValue() * BOUNDRARY) +
182                                         number.intValue()), rdn);
183                             } else if (type == TYPE_SUBJECTDIRATTR) {
184                                 dnfields.put(new Integer JavaDoc((id.intValue() * BOUNDRARY) +
185                                         number.intValue()), rdn);
186                             }
187                             number = new Integer JavaDoc(number.intValue()+1);
188                             fieldnumbers.put(id, number);
189                         }
190                     }
191                     if (!exists) {
192                         existsother = true;
193                     }
194                 }
195             } catch (Exception JavaDoc e) {
196                 log.error("setDN: ", e);
197                 illegal = true;
198                 if (type == TYPE_SUBJECTDN) {
199                     dnfields.put(new Integer JavaDoc((CN * BOUNDRARY)), "Illegal DN : " + dn);
200                 } else if (type == TYPE_SUBJECTALTNAME){
201                     dnfields.put(new Integer JavaDoc((RFC822NAME * BOUNDRARY)),
202                         "Illegal Subjectaltname : " + dn);
203                 } else if (type == TYPE_SUBJECTDIRATTR){
204                     dnfields.put(new Integer JavaDoc((PLACEOFBIRTH * BOUNDRARY)),
205                         "Illegal Subjectdirectory attribute : " + dn);
206                 }
207             }
208         }
209     }
210
211     /**
212      * Returns the value of a certain DN component.
213      *
214      * @param field the DN component, one of the constants DNFieldExtractor.CN, ...
215      * @param number the number of the component if several entries for this component exists, normally 0 fir the first
216      *
217      * @return A String for example "PrimeKey" if DNFieldExtractor.O and 0 was passed, "PrimeKey" if DNFieldExtractor.DC and 0 was passed
218      * or "com" if DNFieldExtractor.DC and 1 was passed.
219      * Returns an empty String "", if no such field with the number exists.
220      */

221     public String JavaDoc getField(int field, int number) {
222         String JavaDoc returnval;
223         returnval = (String JavaDoc) dnfields.get(new Integer JavaDoc((field * BOUNDRARY) + number));
224
225         if (returnval == null) {
226             returnval = "";
227         }
228
229         return returnval;
230     }
231
232     /** Returns a string representation of a certain DN component
233      *
234      * @param field the DN component, one of the constants DNFieldExtractor.CN, ...
235      * @return A String for example "CN=Tomas Gustavsson" if DNFieldExtractor.CN was passed, "DC=PrimeKey,DC=com" if DNFieldExtractor.DC was passed.
236      */

237     public String JavaDoc getFieldString(int field){
238         String JavaDoc retval = "";
239         String JavaDoc fieldname = DnComponents.getDnExtractorFieldFromDnId(field);
240         if(type != TYPE_SUBJECTDN){
241             fieldname = DnComponents.getAltNameExtractorFieldFromDnId(field);
242         }
243         int num = getNumberOfFields(field);
244         for(int i=0;i<num;i++){
245             if(retval.length() == 0)
246               retval += fieldname + getField(field,i);
247             else
248               retval += "," + fieldname + getField(field,i);
249         }
250         return retval;
251     }
252     
253
254     /**
255      * Function that returns true if non standard DN field exists in dn string.
256      *
257      * @return true if non standard DN field exists, false otherwise
258      */

259     public boolean existsOther() {
260         return existsother;
261     }
262
263     /**
264      * Returns the number of one kind of dn field.
265      *
266      * @param field the DN component, one of the constants DNFieldExtractor.CN, ...
267      *
268      * @return number of componenets available for a fiels, for example 1 if DN is "dc=primekey" and 2 if DN is "dc=primekey,dc=com"
269      */

270     public int getNumberOfFields(int field) {
271         Integer JavaDoc ret = (Integer JavaDoc)fieldnumbers.get(new Integer JavaDoc(field));
272         if (ret == null) {
273             log.error("Not finding fieldnumber value for "+field);
274         }
275         return ret.intValue();
276     }
277
278     /**
279      * Returns the complete array determining the number of DN components of the various types
280      * (i.e. if there are two CNs but 0 Ls etc)
281      *
282      * @return DOCUMENT ME!
283      */

284     public HashMap JavaDoc getNumberOfFields() {
285         return fieldnumbers;
286     }
287
288     public boolean isIllegal(){
289         return illegal;
290     }
291
292     private static final int BOUNDRARY = 100;
293     // Mapping dnid to number of occurances in this DN
294
private HashMap JavaDoc fieldnumbers;
295     private HashMap JavaDoc dnfields;
296     private boolean existsother = false;
297     private boolean illegal = false;
298     private int type;
299 }
300
Popular Tags