1 13 package org.ejbca.util; 14 15 import java.security.cert.X509Certificate ; 16 import java.text.DateFormat ; 17 import java.util.Date ; 18 import java.util.HashMap ; 19 import java.util.regex.Matcher ; 20 import java.util.regex.Pattern ; 21 22 import org.ejbca.util.dn.DNFieldExtractor; 23 24 78 79 public class NotificationParamGen { 80 81 private HashMap params = new HashMap (); 82 83 84 private final static Pattern PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); 85 86 89 public NotificationParamGen(String userdn, X509Certificate expiringCert){ 90 populate(null, null, userdn,null,null,null,null,null,null, null,null,null,null,expiringCert); 91 } 92 93 96 public NotificationParamGen(String userUsername, String userPassword, String userDN){ 97 populate(userUsername, userPassword, userDN,null,null,null,null,null,null, null,null,null,null,null); 98 } 99 100 103 public NotificationParamGen(Date approvalRequestDate, Integer approvalRequestID, String approvalRequestType, 104 Integer numberOfApprovalLeft, String approvalRequestURL, String approveComment, String requestAdminUsername, String requestAdminDN, 105 String approvalAdminUsername, String approvalAdminDN){ 106 populate(null, null, null, approvalRequestDate, approvalRequestID, approvalRequestType, 107 numberOfApprovalLeft, approvalRequestURL, approveComment, requestAdminUsername, requestAdminDN, 108 approvalAdminUsername, approvalAdminDN,null); 109 } 110 111 115 public HashMap getParams(){ 116 return params; 117 } 118 119 private void populate(String userUsername, String userPassword, String userDN, 120 Date approvalRequestDate, Integer approvalRequestID, String approvalRequestType, 121 Integer numberOfApprovalLeft, String approvalRequestURL, String approveComment, 122 String requestAdminUsername, String requestAdminDN, 123 String approvalAdminUsername, String approvalAdminDN, 124 X509Certificate expiringCert){ 125 params.put("NL", System.getProperty("line.separator")); 126 String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date ()); 127 params.put("DATE", date); 128 129 paramPut("USERNAME", userUsername); 130 paramPut("user.USERNAME", userUsername); 131 132 paramPut("PASSWORD", userPassword); 133 paramPut("user.PASSWORD", userPassword); 134 135 DNFieldExtractor dnfields = new DNFieldExtractor(userDN, DNFieldExtractor.TYPE_SUBJECTDN); 136 paramPut("CN", dnfields.getField(DNFieldExtractor.CN, 0)); 137 paramPut("user.CN", dnfields.getField(DNFieldExtractor.CN, 0)); 138 paramPut("SN", dnfields.getField(DNFieldExtractor.SN, 0)); 139 paramPut("user.SN", dnfields.getField(DNFieldExtractor.SN, 0)); 140 paramPut("O", dnfields.getField(DNFieldExtractor.O, 0)); 141 paramPut("user.O", dnfields.getField(DNFieldExtractor.O, 0)); 142 paramPut("OU", dnfields.getField(DNFieldExtractor.OU, 0)); 143 paramPut("user.OU", dnfields.getField(DNFieldExtractor.OU, 0)); 144 paramPut("C", dnfields.getField(DNFieldExtractor.C, 0)); 145 paramPut("user.C", dnfields.getField(DNFieldExtractor.C, 0)); 146 147 148 if(approvalRequestDate != null){ 149 String requestDate = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(approvalRequestDate); 150 params.put("approvalRequest.DATE", requestDate); 151 }else{ 152 params.put("approvalRequest.DATE", ""); 153 } 154 155 paramPut("approvalRequest.ID", approvalRequestID); 156 paramPut("approvalRequest.TYPE", approvalRequestType); 157 paramPut("approvalReqiest.APPROVALSLEFT", numberOfApprovalLeft); 158 paramPut("approvalRequest.APROVEURL", approvalRequestURL); 159 paramPut("approvalRequest.APPROVALCOMMENT", approveComment); 160 paramPut("requestAdmin.USERNAME", requestAdminUsername); 161 162 if(requestAdminDN != null){ 163 requestAdminDN = ""; 164 } 165 dnfields = new DNFieldExtractor(requestAdminDN, DNFieldExtractor.TYPE_SUBJECTDN); 166 paramPut("requestAdmin.CN", dnfields.getField(DNFieldExtractor.CN, 0)); 167 paramPut("requestAdmin.SN", dnfields.getField(DNFieldExtractor.SN, 0)); 168 paramPut("requestAdmin.O", dnfields.getField(DNFieldExtractor.O, 0)); 169 paramPut("requestAdmin.OU", dnfields.getField(DNFieldExtractor.OU, 0)); 170 paramPut("requestAdmin.C", dnfields.getField(DNFieldExtractor.C, 0)); 171 172 paramPut("requestAdmin.USERNAME", approvalAdminUsername); 173 174 if(approvalAdminDN == null){ 175 approvalAdminDN = ""; 176 } 177 dnfields = new DNFieldExtractor(approvalAdminDN, DNFieldExtractor.TYPE_SUBJECTDN); 178 paramPut("approvalAdmin.CN", dnfields.getField(DNFieldExtractor.CN, 0)); 179 paramPut("approvalAdmin.SN", dnfields.getField(DNFieldExtractor.SN, 0)); 180 paramPut("approvalAdmin.O", dnfields.getField(DNFieldExtractor.O, 0)); 181 paramPut("approvalAdmin.OU", dnfields.getField(DNFieldExtractor.OU, 0)); 182 paramPut("approvalAdmin.C", dnfields.getField(DNFieldExtractor.C, 0)); 183 184 if(expiringCert != null){ 185 paramPut("expiringCert.CERTSERIAL",expiringCert.getSerialNumber().toString(16)); 186 String dateString = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(expiringCert.getNotAfter()); 187 paramPut("expiringCert.EXPIREDATE",dateString); 188 paramPut("expiringCert.CERTSUBJECTDN",expiringCert.getSubjectDN().toString()); 189 paramPut("expiringCert.CERTISSUERDN",expiringCert.getIssuerDN().toString()); 190 } 191 192 193 } 194 195 200 private void paramPut(String key, String value){ 201 if(value == null){ 202 params.put(key, ""); 203 }else{ 204 params.put(key, value); 205 } 206 } 207 208 213 private void paramPut(String key, Integer value){ 214 if(value == null){ 215 params.put(key, ""); 216 }else{ 217 params.put(key, value.toString()); 218 } 219 } 220 221 227 public static String interpolate(HashMap patterns, String input) { 228 final Matcher m = PATTERN.matcher(input); 229 final StringBuffer sb = new StringBuffer (input.length()); 230 while (m.find()) { 231 String key = m.group(1); 233 String value = (String )patterns.get(key); 234 if (value != null) { 237 m.appendReplacement(sb, value); 238 } else { 239 m.appendReplacement(sb, ""); 242 String unknown = m.group(0); 243 sb.append(unknown); 244 } 245 } 246 m.appendTail(sb); 247 return sb.toString(); 248 } 249 250 } 251 | Popular Tags |