KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.BufferedReader JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.apache.commons.lang.ArrayUtils;
26 import org.apache.commons.lang.BooleanUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.log4j.Logger;
29 import org.bouncycastle.asn1.DERObjectIdentifier;
30 import org.bouncycastle.asn1.x509.X509Name;
31
32 /** Class holding information and utilitites for handling different DN components, CN, O etc
33  *
34  * This is a very complex class with lots of maps and stuff. It is because it is a first step of refactoring the DN/AltName/DirAttr handling.
35  * This previously consisted of lotsa of different arrays spread out all over the place, now it's gathered here in order to be able to get a view of it.
36  * The underlying implementations have not changed much though, in order to still have things working, therefore there are lots of different maps and arrays, with
37  * seemingly similar contents.
38  *
39  * @author tomas
40  * @version $Id: DnComponents.java,v 1.8 2007/01/16 11:46:13 anatom Exp $
41  */

42 public class DnComponents {
43     private static Logger log = Logger.getLogger(DnComponents.class);
44
45     /** This class should be instantiated immediately */
46     private static DnComponents obj = new DnComponents();
47     
48     /** BC X509Name contains some lookup tables that could maybe be used here.
49      *
50      * This map is used in CertTools so sort and order DN strings so they all look the same in the database.
51      * */

52     private static HashMap JavaDoc oids = new HashMap JavaDoc();
53     // Default values
54
static {
55         oids.put("c", X509Name.C);
56         oids.put("dc", X509Name.DC);
57         oids.put("st", X509Name.ST);
58         oids.put("l", X509Name.L);
59         oids.put("o", X509Name.O);
60         oids.put("ou", X509Name.OU);
61         oids.put("t", X509Name.T);
62         oids.put("surname", X509Name.SURNAME);
63         oids.put("initials", X509Name.INITIALS);
64         oids.put("givenname", X509Name.GIVENNAME);
65         oids.put("gn", X509Name.GIVENNAME);
66         oids.put("sn", X509Name.SN);
67         oids.put("serialnumber", X509Name.SN);
68         oids.put("cn", X509Name.CN);
69         oids.put("uid", X509Name.UID);
70         oids.put("dn", X509Name.DN_QUALIFIER);
71         oids.put("emailaddress", X509Name.EmailAddress);
72         oids.put("e", X509Name.EmailAddress);
73         oids.put("email", X509Name.EmailAddress);
74         oids.put("unstructuredname", X509Name.UnstructuredName); //unstructuredName
75
oids.put("unstructuredaddress", X509Name.UnstructuredAddress); //unstructuredAddress
76
}
77     /** Default values used when constructing DN strings that are put in the database
78      *
79      */

80     private static String JavaDoc[] dNObjectsForward = {
81         "unstructuredaddress", "unstructuredname", "emailaddress", "e", "email", "dn", "uid", "cn", "sn", "serialnumber", "gn", "givenname",
82         "initials", "surname", "t", "ou", "o", "l", "st", "dc", "c"
83     };
84     // Default values
85
private static String JavaDoc[] dNObjectsReverse = null;
86
87     /**
88      * These maps and constants are used in the admin-GUI and in End Entity profiles
89      */

90
91     /** These constants can be used when reffering to standard, build in components
92      *
93      */

94     // DN components
95
public static final String JavaDoc DNEMAIL = "DNEMAIL";
96     public static final String JavaDoc DNQUALIFIER = "DN";
97     public static final String JavaDoc UID = "UID";
98     public static final String JavaDoc COMMONNAME = "COMMONNAME";
99     public static final String JavaDoc SN = "SN";
100     public static final String JavaDoc GIVENNAME = "GIVENNAME";
101     public static final String JavaDoc INITIALS = "INITIALS";
102     public static final String JavaDoc SURNAME = "SURNAME";
103     public static final String JavaDoc TITLE = "TITLE";
104     public static final String JavaDoc ORGANIZATIONUNIT = "ORGANIZATIONUNIT";
105     public static final String JavaDoc ORGANIZATION = "ORGANIZATION";
106     public static final String JavaDoc LOCALE = "LOCALE";
107     public static final String JavaDoc STATE = "STATE";
108     public static final String JavaDoc DOMAINCOMPONENT = "DOMAINCOMPONENT";
109     public static final String JavaDoc COUNTRY = "COUNTRY";
110     public static final String JavaDoc UNSTRUCTUREDADDRESS = "UNSTRUCTUREDADDRESS";
111     public static final String JavaDoc UNSTRUCTUREDNAME = "UNSTRUCTUREDNAME";
112     
113     // AltNames
114
public static final String JavaDoc RFC822NAME = "RFC822NAME";
115     public static final String JavaDoc DNSNAME = "DNSNAME";
116     public static final String JavaDoc IPADDRESS = "IPADDRESS";
117     public static final String JavaDoc UNIFORMRESOURCEID = "UNIFORMRESOURCEID";
118     public static final String JavaDoc DIRECTORYNAME = "DIRECTORYNAME";
119     public static final String JavaDoc UPN = "UPN";
120     public static final String JavaDoc GUID = "GUID";
121     // Below are altNames that are not implemented yet
122
public static final String JavaDoc OTHERNAME = "OTHERNAME";
123     public static final String JavaDoc X400ADDRESS = "X400ADDRESS";
124     public static final String JavaDoc EDIPARTNAME = "EDIPARTNAME";
125     public static final String JavaDoc REGISTEREDID = "REGISTEREDID";
126     
127     // Subject directory attributes
128
public static final String JavaDoc DATEOFBIRTH = "DATEOFBIRTH";
129     public static final String JavaDoc PLACEOFBIRTH = "PLACEOFBIRTH";
130     public static final String JavaDoc GENDER = "GENDER";
131     public static final String JavaDoc COUNTRYOFCITIZENSHIP = "COUNTRYOFCITIZENSHIP";
132     public static final String JavaDoc COUNTRYOFRESIDENCE = "COUNTRYOFRESIDENCE";
133
134     private static HashMap JavaDoc dnNameIdMap = new HashMap JavaDoc();
135     private static HashMap JavaDoc profileNameIdMap = new HashMap JavaDoc();
136     private static HashMap JavaDoc dnIdToProfileNameMap = new HashMap JavaDoc();
137     private static HashMap JavaDoc dnIdToProfileIdMap = new HashMap JavaDoc();
138     private static HashMap JavaDoc profileIdToDnIdMap = new HashMap JavaDoc();
139     private static HashMap JavaDoc dnErrorTextMap = new HashMap JavaDoc();
140     private static HashMap JavaDoc profileNameLanguageMap = new HashMap JavaDoc();
141     private static HashMap JavaDoc profileIdLanguageMap = new HashMap JavaDoc();
142     private static HashMap JavaDoc dnIdErrorMap = new HashMap JavaDoc();
143     private static HashMap JavaDoc dnIdToExtractorFieldMap = new HashMap JavaDoc();
144     private static HashMap JavaDoc altNameIdToExtractorFieldMap = new HashMap JavaDoc();
145     private static HashMap JavaDoc dirAttrIdToExtractorFieldMap = new HashMap JavaDoc();
146     private static ArrayList JavaDoc dnProfileFields = new ArrayList JavaDoc();
147     private static ArrayList JavaDoc dnLanguageTexts = new ArrayList JavaDoc();
148     private static ArrayList JavaDoc dnDnIds = new ArrayList JavaDoc();
149     private static ArrayList JavaDoc altNameFields = new ArrayList JavaDoc();
150     private static ArrayList JavaDoc altNameLanguageTexts = new ArrayList JavaDoc();
151     private static ArrayList JavaDoc altNameDnIds = new ArrayList JavaDoc();
152     private static ArrayList JavaDoc dirAttrFields = new ArrayList JavaDoc();
153     private static ArrayList JavaDoc dirAttrLanguageTexts = new ArrayList JavaDoc();
154     private static ArrayList JavaDoc dirAttrDnIds = new ArrayList JavaDoc();
155     private static ArrayList JavaDoc dnExtractorFields = new ArrayList JavaDoc();
156     private static ArrayList JavaDoc altNameExtractorFields = new ArrayList JavaDoc();
157     private static ArrayList JavaDoc dirAttrExtractorFields = new ArrayList JavaDoc();
158     
159
160     // Load values from a properties file, if it exists
161
static {
162         DnComponents.load();
163     }
164     /** This property is true if reverse DN order should be used. Default value is false (forward order).
165      * This setting is changed from ejbca.properties
166      */

167     private static final boolean reverseOrder = BooleanUtils.toBoolean("@certtools.dnorderreverse@");
168     
169     
170     public static DERObjectIdentifier getOid(String JavaDoc o) {
171         return (DERObjectIdentifier) oids.get(o.toLowerCase());
172     } // getOid
173

174     public static ArrayList JavaDoc getDnProfileFields() {
175         return dnProfileFields;
176     }
177     public static ArrayList JavaDoc getDnLanguageTexts() {
178         return dnLanguageTexts;
179     }
180     public static ArrayList JavaDoc getAltNameFields() {
181         return altNameFields;
182     }
183     public static ArrayList JavaDoc getAltNameLanguageTexts() {
184         return altNameLanguageTexts;
185     }
186     public static ArrayList JavaDoc getDirAttrFields() {
187         return dirAttrFields;
188     }
189     // Used by DNFieldExtractor and EntityProfile, don't USE
190
public static ArrayList JavaDoc getDirAttrDnIds() {
191         return dirAttrDnIds;
192     }
193     // Used by DNFieldExtractor and EntityProfile, don't USE
194
public static ArrayList JavaDoc getAltNameDnIds() {
195         return altNameDnIds;
196     }
197     // Used by DNFieldExtractor and EntityProfile, don't USE
198
public static ArrayList JavaDoc getDnDnIds() {
199         return dnDnIds;
200     }
201     // Used only by DNFieldExtractor, don't USE
202
protected static ArrayList JavaDoc getDnExtractorFields() {
203         return dnExtractorFields;
204     }
205     protected static String JavaDoc getDnExtractorFieldFromDnId(int field) {
206         String JavaDoc val = (String JavaDoc)dnIdToExtractorFieldMap.get(new Integer JavaDoc(field));
207         return val;
208     }
209     // Used only by DNFieldExtractor, don't USE
210
protected static ArrayList JavaDoc getAltNameExtractorFields() {
211         return altNameExtractorFields;
212     }
213     protected static String JavaDoc getAltNameExtractorFieldFromDnId(int field) {
214         String JavaDoc val = (String JavaDoc)altNameIdToExtractorFieldMap.get(new Integer JavaDoc(field));
215         return val;
216     }
217     // Used only by DNFieldExtractor, don't USE
218
protected static ArrayList JavaDoc getDirAttrExtractorFields() {
219         return dirAttrExtractorFields;
220     }
221     protected static String JavaDoc getDirAttrExtractorFieldFromDnId(int field) {
222         String JavaDoc val = (String JavaDoc)dirAttrIdToExtractorFieldMap.get(new Integer JavaDoc(field));
223         return val;
224     }
225     
226     public static String JavaDoc dnIdToProfileName(int dnid) {
227         String JavaDoc val = (String JavaDoc)dnIdToProfileNameMap.get(new Integer JavaDoc(dnid));
228         return val;
229     }
230     public static int dnIdToProfileId(int dnid) {
231         Integer JavaDoc val = (Integer JavaDoc)dnIdToProfileIdMap.get(new Integer JavaDoc(dnid));
232         return val.intValue();
233     }
234     /**
235      * Method to get a language error constant for the admin-GUI from a profile name
236      */

237     public static String JavaDoc getLanguageConstantFromProfileName(String JavaDoc name) {
238         String JavaDoc ret = (String JavaDoc)profileNameLanguageMap.get(name);
239         return ret;
240     }
241     /**
242      * Method to get a language error constant for the admin-GUI from a profile id
243      */

244     public static String JavaDoc getLanguageConstantFromProfileId(int id) {
245         String JavaDoc ret = (String JavaDoc)profileIdLanguageMap.get(new Integer JavaDoc(id));
246         return ret;
247     }
248     /**
249      * Method to get a clear text error msg for the admin-GUI from a dn id
250      */

251     public static String JavaDoc getErrTextFromDnId(int id) {
252         String JavaDoc ret = (String JavaDoc)dnIdErrorMap.get(new Integer JavaDoc(id));
253         return ret;
254     }
255     
256     
257     /** This method is only used to initialize EndEntityProfile, because of legacy baggage.
258      * Should be refactored sometime! Please don't use this whatever you do!
259      * @return
260      */

261     public static HashMap JavaDoc getProfilenameIdMap() {
262         return profileNameIdMap;
263         
264     }
265     /** A function that takes an fieldid pointing to a coresponding id in UserView and DnFieldExctractor.
266      * For example : profileFieldIdToUserFieldIdMapper(EndEntityProfile.COMMONNAME) returns DnFieldExctractor.COMMONNAME.
267      *
268      * Should only be used with subjectDN, Subject Alternative Names and subject directory attribute fields.
269      */

270     public static int profileIdToDnId(int profileid) {
271         Integer JavaDoc val = (Integer JavaDoc)profileIdToDnIdMap.get(new Integer JavaDoc(profileid));
272         if (val == null) {
273             log.error("No dn id mapping from profile id "+profileid);
274             // We allow it to fail here
275
}
276         return val.intValue();
277     }
278     /**
279      * Returns the dnObject (forward or reverse) that is in use
280      */

281     public static String JavaDoc[]getDnObjects() {
282         if (!reverseOrder) {
283             return dNObjectsForward;
284         }
285         return getDnObjectsReverse();
286     }
287     
288     /**
289      * Returns the reversed dnObjects.
290      * Protected to allow testing
291      */

292     protected static String JavaDoc[] getDnObjectsReverse() {
293         // Create and reverse the order if it has not been initialized already
294
if (dNObjectsReverse == null) {
295             // this cast is not needed in java 5, but is needed for java 1.4
296
dNObjectsReverse = (String JavaDoc[])dNObjectsForward.clone();
297             ArrayUtils.reverse(dNObjectsReverse);
298         }
299         return dNObjectsReverse;
300     }
301     
302     private static void load() {
303         loadOrdering();
304         loadMappings();
305     }
306     /**
307      * Load DN ordering used in CertTools.stringToBCDNString etc.
308      * Loads from file placed in src/dncomponents.properties
309      *
310      * A line is:
311      * DNName;DNid;ProfileName;ProfileId,ErrorString,LanguageConstant
312      *
313      */

314     private static void loadMappings() {
315         // Read the file to an array of lines
316
String JavaDoc line;
317         
318         BufferedReader JavaDoc in = null;
319         InputStreamReader JavaDoc inf = null;
320         try
321         {
322             InputStream JavaDoc is = obj.getClass().getResourceAsStream("/profilemappings.properties");
323             //log.info("is is: " + is);
324
if (is != null) {
325                 inf = new InputStreamReader JavaDoc(is);
326                 //inf = new FileReader("c:\\foo.properties");
327
in = new BufferedReader JavaDoc(inf);
328                 if (!in.ready())
329                     throw new IOException JavaDoc();
330                 String JavaDoc[] splits = null;
331                 int lines = 0;
332                 while ((line = in.readLine()) != null) {
333                     if (!line.startsWith("#")) { // # is a comment line
334
splits = StringUtils.split(line, ';');
335                         if ( (splits != null) && (splits.length > 5) ) {
336                             String JavaDoc type = splits[0];
337                             String JavaDoc dnname = splits[1];
338                             Integer JavaDoc dnid = new Integer JavaDoc(splits[2]);
339                             String JavaDoc profilename = splits[3];
340                             Integer JavaDoc profileid = new Integer JavaDoc(splits[4]);
341                             String JavaDoc errstr = splits[5];
342                             String JavaDoc langstr = splits[6];
343                             // Fill maps
344
dnNameIdMap.put(dnname, dnid);
345                             profileNameIdMap.put(profilename, profileid);
346                             dnIdToProfileNameMap.put(dnid, profilename);
347                             dnIdToProfileIdMap.put(dnid, profileid);
348                             dnIdErrorMap.put(dnid, errstr);
349                             profileIdToDnIdMap.put(profileid, dnid);
350                             dnErrorTextMap.put(dnid, errstr);
351                             profileNameLanguageMap.put(profilename, langstr);
352                             profileIdLanguageMap.put(profileid, langstr);
353                             if (type.equals("DN")) {
354                                 dnProfileFields.add(profilename);
355                                 dnLanguageTexts.add(langstr);
356                                 dnDnIds.add(dnid);
357                                 dnExtractorFields.add(dnname+"=");
358                                 dnIdToExtractorFieldMap.put(dnid, dnname+"=");
359                             }
360                             if (type.equals("ALTNAME")) {
361                                 altNameFields.add(dnname);
362                                 altNameLanguageTexts.add(langstr);
363                                 altNameDnIds.add(dnid);
364                                 altNameExtractorFields.add(dnname+"=");
365                                 altNameIdToExtractorFieldMap.put(dnid, dnname+"=");
366                             }
367                             if (type.equals("DIRATTR")) {
368                                 dirAttrFields.add(dnname);
369                                 dirAttrLanguageTexts.add(langstr);
370                                 dirAttrDnIds.add(dnid);
371                                 dirAttrExtractorFields.add(dnname+"=");
372                                 dirAttrIdToExtractorFieldMap.put(dnid, dnname+"=");
373                             }
374                             lines++;
375                         }
376                     }
377                 }
378                 in.close();
379                 log.debug("Read profile maps with "+lines+" lines.");
380             } else {
381                 throw new IOException JavaDoc("Input stream for /profilemappings.properties is null");
382             }
383         }
384         catch (IOException JavaDoc e) {
385             log.error("Can not load profile mappings: ", e);
386         } finally {
387             try {
388                 if (inf != null) inf.close();
389                 if (in != null) in.close();
390             } catch (IOException JavaDoc e) {}
391         }
392
393     }
394     /**
395      * Load DN ordering used in CertTools.stringToBCDNString etc.
396      * Loads from file placed in src/dncomponents.properties
397      *
398      */

399     private static void loadOrdering() {
400         // Read the file to an array of lines
401
String JavaDoc line;
402         LinkedHashMap JavaDoc map = new LinkedHashMap JavaDoc();
403         BufferedReader JavaDoc in = null;
404         InputStreamReader JavaDoc inf = null;
405         try
406         {
407             InputStream JavaDoc is = obj.getClass().getResourceAsStream("/dncomponents.properties");
408             //log.info("is is: " + is);
409
if (is != null) {
410                 inf = new InputStreamReader JavaDoc(is);
411                 //inf = new FileReader("c:\\foo.properties");
412
in = new BufferedReader JavaDoc(inf);
413                 if (!in.ready())
414                     throw new IOException JavaDoc();
415                 String JavaDoc[] splits = null;
416                 while ((line = in.readLine()) != null) {
417                     if (!line.startsWith("#")) { // # is a comment line
418
splits = StringUtils.split(line, '=');
419                         if ( (splits != null) && (splits.length > 1) ) {
420                             String JavaDoc name = splits[0];
421                             DERObjectIdentifier oid = new DERObjectIdentifier(splits[1]);
422                             map.put(name, oid);
423                         }
424                     }
425                 }
426                 in.close();
427                 // Now we have read it in, transfer it to the main oid map
428
log.info("Using DN components from properties file");
429                 oids.clear();
430                 oids.putAll(map);
431                 Set JavaDoc keys = map.keySet();
432                 // Set the maps to the desired ordering
433
dNObjectsForward = (String JavaDoc[])keys.toArray(new String JavaDoc[0]);
434             } else {
435                 log.debug("Using default values for DN components");
436             }
437         }
438         catch (IOException JavaDoc e) {
439             log.debug("Using default values for DN components");
440         } finally {
441             try {
442                 if (inf != null) inf.close();
443                 if (in != null) in.close();
444             } catch (IOException JavaDoc e) {}
445         }
446
447     }
448
449 }
450
Popular Tags