KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ra > UsernameGenerator


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 package org.ejbca.core.model.ra;
14
15 import java.util.regex.Matcher JavaDoc;
16 import java.util.regex.Pattern JavaDoc;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.ejbca.util.CertTools;
21 import org.ejbca.util.passgen.IPasswordGenerator;
22 import org.ejbca.util.passgen.PasswordGeneratorFactory;
23
24 /**
25  * Class used to generate special usernames.
26  *
27  * Configuration parameters:
28  *
29  * NameGenerationScheme = "Which generation scheme should be used, RANDOM, USERNAME or DN"
30  * RANDOM will generate a random username with length set in 'randomNameLength'.
31  *
32  * NameGenerationParameters = "Parameters for name generation, for DN it can be CN or UID".
33  * If mode is DN, the CN or UID is taken from the DN to be used as username (adding pre- and postfix off-course).
34  *
35  * NameGenerationPrefix = "Prefix to generated name, a string that can contain the variable ${RANDOM}"
36  * exmaple: "Prefix - "
37  *
38  * NameGenerationPostfix="Postfix to generated name, a string that can contain the variable ${RANDOM}"
39  * example: " - Postfix"
40  *
41  * The variable ${RANDOM} will be replaced by a random value of length set in 'randomPrefixLength'.
42  *
43  * @author tomas
44  * @version $Id: UsernameGenerator.java,v 1.3 2006/09/25 12:54:59 anatom Exp $
45  */

46 public class UsernameGenerator {
47
48     private static Logger log = Logger.getLogger(UsernameGenerator.class);
49
50     // Generator configuration parameters, with good default values
51
private UsernameGeneratorParams params = null;
52     
53     public static UsernameGenerator getInstance(String JavaDoc mode) {
54         return new UsernameGenerator(mode);
55     }
56     public static UsernameGenerator getInstance(UsernameGeneratorParams params) {
57         return new UsernameGenerator(params);
58     }
59     private UsernameGenerator(String JavaDoc mode) {
60         this.params = new UsernameGeneratorParams();
61         params.setMode(mode);
62     }
63     private UsernameGenerator(UsernameGeneratorParams params) {
64         this.params = params;
65     }
66     
67     public String JavaDoc generateUsername() {
68         String JavaDoc ret = null;
69         if (params.getMode() != UsernameGeneratorParams.MODE_RANDOM) {
70             throw new IllegalArgumentException JavaDoc("this method can only be used in mode RANDOM");
71         }
72         ret = getRandomString(params.getRandomNameLength());
73         return addPrePostFix(ret);
74     }
75     
76     public String JavaDoc generateUsername(String JavaDoc name) {
77         String JavaDoc str = name;
78         switch (params.getMode()) {
79         case UsernameGeneratorParams.MODE_RANDOM:
80             str = getRandomString(params.getRandomNameLength());
81             break;
82         case UsernameGeneratorParams.MODE_DN:
83             if (str == null) {
84                 throw new IllegalArgumentException JavaDoc("Input name can not be null in MODE_DN!");
85             }
86             str = CertTools.getPartFromDN(name, params.getDNGeneratorComponent());
87             break;
88         case UsernameGeneratorParams.MODE_USERNAME:
89             if (str == null) {
90                 throw new IllegalArgumentException JavaDoc("Input name can not be null in MODE_USERNAME!");
91             }
92             break;
93         default:
94             break;
95         }
96         String JavaDoc ret = addPrePostFix(str);
97         log.debug("Generated username: "+ret);
98         return ret;
99     }
100     
101     private String JavaDoc getRandomString(int length) {
102         IPasswordGenerator gen = PasswordGeneratorFactory.getInstance(params.getRandomGeneratorType());
103         String JavaDoc ret = gen.getNewPassword(length, length);
104         return ret;
105     }
106     
107     private String JavaDoc addPrePostFix(String JavaDoc in) {
108         String JavaDoc ret = in;
109         String JavaDoc pre = getPrefix();
110         String JavaDoc post = getPostfix();
111         if (pre != null) {
112             ret = pre + ret;
113         }
114         if (post != null) {
115             ret = ret + post;
116         }
117         return ret;
118     }
119
120     private String JavaDoc getPostfix() {
121         return interpolate(params.getPostfix());
122     }
123
124     private String JavaDoc getPrefix() {
125         return interpolate(params.getPrefix());
126     }
127
128     /** regexp pattern to match ${identifier} patterns */
129     private final static Pattern JavaDoc PATTERN = Pattern.compile("\\$\\{(.+?)\\}");
130     /**
131      * Interpolate the patterns that exists on the input on the form '${pattern}'.
132      * @param input the input content to be interpolated
133      * @return the interpolated content
134      */

135     private String JavaDoc interpolate(String JavaDoc input) {
136         if (input == null)
137             return null;
138         final Matcher JavaDoc m = PATTERN.matcher(input);
139         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(input.length());
140         while (m.find()) {
141             // when the pattern is ${identifier}, group 0 is 'identifier'
142
String JavaDoc key = m.group(1);
143             String JavaDoc value = null;
144             if (StringUtils.equals(key, "RANDOM")) {
145                 value = getRandomString(params.getRandomPrefixLength());
146             }
147             // if the pattern does exists, replace it by its value
148
// otherwise keep the pattern ( it is group(0) )
149
if (value != null) {
150                 m.appendReplacement(sb, value);
151             } else {
152                 // I'm doing this to avoid the backreference problem as there will be a $
153
// if I replace directly with the group 0 (which is also a pattern)
154
m.appendReplacement(sb, "");
155                 String JavaDoc unknown = m.group(0);
156                 sb.append(unknown);
157             }
158         }
159         m.appendTail(sb);
160         return sb.toString();
161     }
162     public UsernameGeneratorParams getParams() {
163         return params;
164     }
165     public void setParams(UsernameGeneratorParams params) {
166         this.params = params;
167     }
168
169 }
170
Popular Tags