KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > NotificationParamGen


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.util;
14
15 import java.security.cert.X509Certificate JavaDoc;
16 import java.text.DateFormat JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.regex.Matcher JavaDoc;
20 import java.util.regex.Pattern JavaDoc;
21
22 import org.ejbca.util.dn.DNFieldExtractor;
23
24 /**
25  * Class generating parameter data for email notifications. Both
26  * regular userdata and approval notifications.
27  *
28  * All parameters isn't always set, it depends on the input data.
29  *
30  * The follwing parameters can be set
31  * ${NL} = New Line in message
32  * ${DATE} or ${current.DATE} = The current date
33  *
34  * Variables used with userdata
35  * ${USERNAME} or ${user.USERNAME} = The users username
36  * ${PASSWORD} or ${user.PASSWORD} = The users password
37  * ${CN} or ${user.CN} = The common name of the user.
38  * ${SN} or ${user.SN} = The serial number (in DN) of the user.
39  * ${O} or ${user.O} = The user's organization
40  * ${OU} or ${user.OU} = The user's organization unit
41  * ${C} or ${user.C} = The user's country
42  *
43  * Variables used with approvals
44  *
45  * ${approvalRequest.DATE} = The time the approval request was created
46  * ${approvalRequest.ID} = The id of the approval request
47  * ${approvalRequest.TYPE} = The type of approval request
48  * ${approvalRequest.APROVEURL} = A URL to the review approval page with the current request.
49  * ${approvalReqiest.APPROVALSLEFT} = The number of approvals remaining.
50  * ${approvalRequest.APPROVALCOMMENT} = The comment made by the approving/rejecting administrator
51  *
52  * ${requestAdmin.USERNAME} = The requesting administrator's username
53  * ${requestAdmin.CN} = The common name of the requesting administrator.
54  * ${requestAdmin.SN} = The common name of the requesting administrator.
55  * ${requestAdmin.O} = The requesting administrator's organization
56  * ${requestAdmin.OU} = The requesting administrator's organization unit
57  * ${requestAdmin.C} = The requesting administrator's country
58  *
59  * ${approvalAdmin.USERNAME} = The requesting administrator's username
60  * ${approvalAdmin.CN} = The common name of the requesting administrator.
61  * ${approvalAdmin.SN} = The common name of the requesting administrator.
62  * ${approvalAdmin.O} = The requesting administrator's organization
63  * ${approvalAdmin.OU} = The requesting administrator's organization unit
64  * ${approvalAdmin.C} = The requesting administrator's country
65  *
66  * Variables used with expiring certificates.
67  * ${expiringCert.CERTSERIAL} = The serial number of the certificate about to expire
68  * ${expiringCert.EXPIREDATE} = The date the certificate will expire
69  * ${expiringCert.CERTSUBJECTDN} = The certificate subject dn
70  * ${expiringCert.CERTISSUERDN} = The certificate issuer dn
71  * The variables ${CN}, ${SN}, ${O}, ${OU}, ${C} are also available.
72  *
73  *
74  * @author Philip Vendil 2006 sep 26
75  *
76  * @version $Id: NotificationParamGen.java,v 1.4 2006/12/02 11:18:30 anatom Exp $
77  */

78
79 public class NotificationParamGen {
80
81   private HashMap JavaDoc params = new HashMap JavaDoc();
82   
83   /** regexp pattern to match ${identifier} patterns */
84   private final static Pattern JavaDoc PATTERN = Pattern.compile("\\$\\{(.+?)\\}");
85   
86   /**
87    * Constuctor that mainly should be used when notifining about expiring certificates.
88    */

89   public NotificationParamGen(String JavaDoc userdn, X509Certificate JavaDoc expiringCert){
90       populate(null, null, userdn,null,null,null,null,null,null, null,null,null,null,expiringCert);
91   }
92     
93   /**
94    * Constuctor that mainly should be used when generating user data notifications
95    */

96   public NotificationParamGen(String JavaDoc userUsername, String JavaDoc userPassword, String JavaDoc userDN){
97       populate(userUsername, userPassword, userDN,null,null,null,null,null,null, null,null,null,null,null);
98   }
99
100   /**
101    * Constuctor that mainly should be used when generating approval notifications
102    */

103   public NotificationParamGen(Date JavaDoc approvalRequestDate, Integer JavaDoc approvalRequestID, String JavaDoc approvalRequestType,
104           Integer JavaDoc numberOfApprovalLeft, String JavaDoc approvalRequestURL, String JavaDoc approveComment, String JavaDoc requestAdminUsername, String JavaDoc requestAdminDN,
105           String JavaDoc approvalAdminUsername, String JavaDoc approvalAdminDN){
106       populate(null, null, null, approvalRequestDate, approvalRequestID, approvalRequestType,
107               numberOfApprovalLeft, approvalRequestURL, approveComment, requestAdminUsername, requestAdminDN,
108                approvalAdminUsername, approvalAdminDN,null);
109   }
110   
111   /**
112    * Method used to retrieve the populated parameter hashmap with the notification text.
113    * @return
114    */

115   public HashMap JavaDoc getParams(){
116       return params;
117   }
118   
119   private void populate(String JavaDoc userUsername, String JavaDoc userPassword, String JavaDoc userDN,
120                         Date JavaDoc approvalRequestDate, Integer JavaDoc approvalRequestID, String JavaDoc approvalRequestType,
121                         Integer JavaDoc numberOfApprovalLeft, String JavaDoc approvalRequestURL, String JavaDoc approveComment,
122                         String JavaDoc requestAdminUsername, String JavaDoc requestAdminDN,
123                         String JavaDoc approvalAdminUsername, String JavaDoc approvalAdminDN,
124                         X509Certificate JavaDoc expiringCert){
125       params.put("NL", System.getProperty("line.separator"));
126       String JavaDoc date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date JavaDoc());
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 JavaDoc 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 JavaDoc 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   /**
196    * method that makes sure that a "" is inserted instead of null
197    * @param key
198    * @param value
199    */

200   private void paramPut(String JavaDoc key, String JavaDoc value){
201       if(value == null){
202           params.put(key, "");
203       }else{
204           params.put(key, value);
205       }
206   }
207   
208   /**
209    * method that makes sure that a "" is inserted instead of null
210    * @param key
211    * @param value
212    */

213   private void paramPut(String JavaDoc key, Integer JavaDoc value){
214       if(value == null){
215           params.put(key, "");
216       }else{
217           params.put(key, value.toString());
218       }
219   }
220     
221   // Help method used to populate a message
222
/**
223    * Interpolate the patterns that exists on the input on the form '${pattern}'.
224    * @param input the input content to be interpolated
225    * @return the interpolated content
226    */

227   public static String JavaDoc interpolate(HashMap JavaDoc patterns, String JavaDoc input) {
228       final Matcher JavaDoc m = PATTERN.matcher(input);
229       final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(input.length());
230       while (m.find()) {
231           // when the pattern is ${identifier}, group 0 is 'identifier'
232
String JavaDoc key = m.group(1);
233           String JavaDoc value = (String JavaDoc)patterns.get(key);
234           // if the pattern does exists, replace it by its value
235
// otherwise keep the pattern ( it is group(0) )
236
if (value != null) {
237               m.appendReplacement(sb, value);
238           } else {
239               // I'm doing this to avoid the backreference problem as there will be a $
240
// if I replace directly with the group 0 (which is also a pattern)
241
m.appendReplacement(sb, "");
242               String JavaDoc unknown = m.group(0);
243               sb.append(unknown);
244           }
245       }
246       m.appendTail(sb);
247       return sb.toString();
248   }
249   
250 }
251
Popular Tags