3434import org .apache .cloudstack .quota .dao .QuotaEmailTemplatesDao ;
3535import org .apache .cloudstack .quota .vo .QuotaAccountVO ;
3636import org .apache .cloudstack .quota .vo .QuotaEmailTemplatesVO ;
37- import org .apache .commons .lang3 .text .StrSubstitutor ;
37+ import org .apache .commons .lang .StringEscapeUtils ;
38+ import org .apache .commons .lang .text .StrSubstitutor ;
3839import org .apache .log4j .Logger ;
3940import org .springframework .stereotype .Component ;
4041
@@ -81,6 +82,10 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana
8182
8283 boolean _smtpDebug = false ;
8384
85+ static final String ACCOUNT_NAME = "accountName" ;
86+ static final String ACCOUNT_USERS = "accountUsers" ;
87+ static final String DOMAIN_NAME = "domainName" ;
88+
8489 public QuotaAlertManagerImpl () {
8590 super ();
8691 }
@@ -203,25 +208,20 @@ public void sendQuotaAlert(DeferredQuotaEmail emailToBeSent) {
203208 userNames = userNames .substring (0 , userNames .length () - 1 );
204209 }
205210
206- final Map <String , String > optionMap = new HashMap <String , String >();
207- optionMap .put ("accountName" , account .getAccountName ());
208- optionMap .put ("accountID" , account .getUuid ());
209- optionMap .put ("accountUsers" , userNames );
210- optionMap .put ("domainName" , accountDomain .getName ());
211- optionMap .put ("domainID" , accountDomain .getUuid ());
212- optionMap .put ("quotaBalance" , QuotaConfig .QuotaCurrencySymbol .value () + " " + balance .toString ());
213- if (emailType == QuotaEmailTemplateTypes .QUOTA_STATEMENT ) {
214- optionMap .put ("quotaUsage" , QuotaConfig .QuotaCurrencySymbol .value () + " " + usage .toString ());
215- }
211+ final Map <String , String > subjectOptionMap = generateOptionMap (account , userNames , accountDomain , balance , usage , emailType , false );
212+ final Map <String , String > bodyOptionMap = generateOptionMap (account , userNames , accountDomain , balance , usage , emailType , true );
216213
217214 if (s_logger .isDebugEnabled ()) {
218- s_logger .debug ("accountName" + account . getAccountName () + "accountID" + account . getUuid () + "accountUsers" + userNames + " domainName" + accountDomain . getName () + "domainID"
219- + accountDomain .getUuid ());
215+ s_logger .debug (String . format ( "Sending quota alert with values: accountName [%s], accountID [%s], accountUsers [%s], domainName [%s], domainID [%s]." ,
216+ account . getAccountName (), account . getUuid (), userNames , accountDomain .getName (), accountDomain . getUuid () ));
220217 }
221218
222- final StrSubstitutor templateEngine = new StrSubstitutor (optionMap );
223- final String subject = templateEngine .replace (emailTemplate .getTemplateSubject ());
224- final String body = templateEngine .replace (emailTemplate .getTemplateBody ());
219+ final StrSubstitutor subjectSubstitutor = new StrSubstitutor (subjectOptionMap );
220+ final String subject = subjectSubstitutor .replace (emailTemplate .getTemplateSubject ());
221+
222+ final StrSubstitutor bodySubstitutor = new StrSubstitutor (bodyOptionMap );
223+ final String body = bodySubstitutor .replace (emailTemplate .getTemplateBody ());
224+
225225 try {
226226 sendQuotaAlert (account .getUuid (), emailRecipients , subject , body );
227227 emailToBeSent .sentSuccessfully (_quotaAcc );
@@ -237,6 +237,34 @@ public void sendQuotaAlert(DeferredQuotaEmail emailToBeSent) {
237237 }
238238 }
239239
240+ /*
241+ *
242+ *
243+ */
244+ public Map <String , String > generateOptionMap (AccountVO accountVO , String userNames , DomainVO domainVO , final BigDecimal balance , final BigDecimal usage ,
245+ final QuotaConfig .QuotaEmailTemplateTypes emailType , boolean escapeHtml ) {
246+ final Map <String , String > optionMap = new HashMap <>();
247+ optionMap .put ("accountID" , accountVO .getUuid ());
248+ optionMap .put ("domainID" , domainVO .getUuid ());
249+ optionMap .put ("quotaBalance" , QuotaConfig .QuotaCurrencySymbol .value () + " " + balance .toString ());
250+
251+ if (emailType == QuotaEmailTemplateTypes .QUOTA_STATEMENT ) {
252+ optionMap .put ("quotaUsage" , QuotaConfig .QuotaCurrencySymbol .value () + " " + usage .toString ());
253+ }
254+
255+ if (escapeHtml ) {
256+ optionMap .put (ACCOUNT_NAME , StringEscapeUtils .escapeHtml (accountVO .getAccountName ()));
257+ optionMap .put (ACCOUNT_USERS , StringEscapeUtils .escapeHtml (userNames ));
258+ optionMap .put (DOMAIN_NAME , StringEscapeUtils .escapeHtml (domainVO .getName ()));
259+ return optionMap ;
260+ }
261+
262+ optionMap .put (ACCOUNT_NAME , accountVO .getAccountName ());
263+ optionMap .put (ACCOUNT_USERS , userNames );
264+ optionMap .put (DOMAIN_NAME , domainVO .getName ());
265+ return optionMap ;
266+ }
267+
240268 public static long getDifferenceDays (Date d1 , Date d2 ) {
241269 long diff = d2 .getTime () - d1 .getTime ();
242270 return TimeUnit .DAYS .convert (diff , TimeUnit .MILLISECONDS );
0 commit comments