1 13 package org.ejbca.core.model.ra; 14 15 import java.util.regex.Matcher ; 16 import java.util.regex.Pattern ; 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 46 public class UsernameGenerator { 47 48 private static Logger log = Logger.getLogger(UsernameGenerator.class); 49 50 private UsernameGeneratorParams params = null; 52 53 public static UsernameGenerator getInstance(String mode) { 54 return new UsernameGenerator(mode); 55 } 56 public static UsernameGenerator getInstance(UsernameGeneratorParams params) { 57 return new UsernameGenerator(params); 58 } 59 private UsernameGenerator(String 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 generateUsername() { 68 String ret = null; 69 if (params.getMode() != UsernameGeneratorParams.MODE_RANDOM) { 70 throw new IllegalArgumentException ("this method can only be used in mode RANDOM"); 71 } 72 ret = getRandomString(params.getRandomNameLength()); 73 return addPrePostFix(ret); 74 } 75 76 public String generateUsername(String name) { 77 String 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 ("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 ("Input name can not be null in MODE_USERNAME!"); 91 } 92 break; 93 default: 94 break; 95 } 96 String ret = addPrePostFix(str); 97 log.debug("Generated username: "+ret); 98 return ret; 99 } 100 101 private String getRandomString(int length) { 102 IPasswordGenerator gen = PasswordGeneratorFactory.getInstance(params.getRandomGeneratorType()); 103 String ret = gen.getNewPassword(length, length); 104 return ret; 105 } 106 107 private String addPrePostFix(String in) { 108 String ret = in; 109 String pre = getPrefix(); 110 String 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 getPostfix() { 121 return interpolate(params.getPostfix()); 122 } 123 124 private String getPrefix() { 125 return interpolate(params.getPrefix()); 126 } 127 128 129 private final static Pattern PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); 130 135 private String interpolate(String input) { 136 if (input == null) 137 return null; 138 final Matcher m = PATTERN.matcher(input); 139 final StringBuffer sb = new StringBuffer (input.length()); 140 while (m.find()) { 141 String key = m.group(1); 143 String value = null; 144 if (StringUtils.equals(key, "RANDOM")) { 145 value = getRandomString(params.getRandomPrefixLength()); 146 } 147 if (value != null) { 150 m.appendReplacement(sb, value); 151 } else { 152 m.appendReplacement(sb, ""); 155 String 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 |