From 01f0182324bda09bc3cdc744a03e2e135ceca9c5 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Sat, 5 Oct 2019 19:26:39 +0200 Subject: [PATCH] Adding additional documentation and optional parameters --- .../configuration/UserConfigurationKeys.java | 10 ++ .../mail/FreemarkerMailGenerator.java | 109 ++++++++++++++---- .../integration/mail/MailGenerator.java | 43 +++++++ 3 files changed, 141 insertions(+), 21 deletions(-) diff --git a/redback-configuration/src/main/java/org/apache/archiva/redback/configuration/UserConfigurationKeys.java b/redback-configuration/src/main/java/org/apache/archiva/redback/configuration/UserConfigurationKeys.java index 5bdde914..d0a91afd 100644 --- a/redback-configuration/src/main/java/org/apache/archiva/redback/configuration/UserConfigurationKeys.java +++ b/redback-configuration/src/main/java/org/apache/archiva/redback/configuration/UserConfigurationKeys.java @@ -169,4 +169,14 @@ public interface UserConfigurationKeys String REST_CSRF_ENABLED = "rest.csrffilter.enabled"; String REST_CSRF_DISABLE_TOKEN_VALIDATION = "rest.csrffilter.disableTokenValidation"; + + /** + * Encoding used for reading mail templates / Default is UTF-8 + */ + String MAIL_TEMPLATE_ENCODING = "mail.template.encoding"; + + /** + * The locale to use for sending mails and finding mail templates + */ + String MAIL_DEFAULT_LOCALE = "mail.locale"; } diff --git a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java index cdd97cfd..9302d869 100644 --- a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java +++ b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java @@ -36,9 +36,32 @@ import java.util.Locale; import java.util.Map; /** + * Mail generator that uses freemarker templates. + * + * This implementation sets the following model values that can be used in templates: + * + * + * The additional template data is added for interpolation, if not null. + * + * This implementation is location enabled. That means, it will try to find templates in the following order: + * + * + * The default encoding used for reading the template is UTF-8 + * * @author Martin Stockhammer */ -@Service("mailGenerator#freemarker") +@Service( "mailGenerator#freemarker" ) public class FreemarkerMailGenerator implements MailGenerator { private Logger log = LoggerFactory.getLogger( FreemarkerMailGenerator.class ); @@ -54,32 +77,67 @@ public class FreemarkerMailGenerator implements MailGenerator private String encoding; - private String getEncoding() { - if (this.encoding==null) { - this.encoding = config.getString( "mail.encoding", DEFAULT_ENCODING ); + private String getEncoding( ) + { + if ( this.encoding == null ) + { + this.encoding = config.getString( UserConfigurationKeys.MAIL_TEMPLATE_ENCODING, DEFAULT_ENCODING ); } return this.encoding; } - @Override - public String generateMail( String templateName, AuthenticationKey authkey, String baseUrl ) - { - Map context = createModel( authkey, baseUrl ); + private Locale getMailLocale() { + String localeString = config.getString( UserConfigurationKeys.MAIL_DEFAULT_LOCALE ); + if (localeString == null || "".equals(localeString)) { + return Locale.getDefault( ); + } else { + return Locale.forLanguageTag( localeString ); + } + } - StringBuffer content = new StringBuffer(); - try{ + /** + * + * @param templateName the template name without extension + * @param locale the locale used to find the template file + * @param authkey the authentication key + * @param baseUrl the base url + * @param templateData additional template data, may be null + * @return the string generated from the template + */ + @Override + public String generateMail( String templateName, Locale locale, AuthenticationKey authkey, String baseUrl, + Map templateData ) + { + Map context = createModel( authkey, baseUrl, templateData ); + StringBuffer content = new StringBuffer( ); + try + { content.append( FreeMarkerTemplateUtils.processTemplateIntoString( - freemarkerConfiguration.getTemplate(templateName+".ftl"),context)); - return content.toString(); - }catch(Exception e){ - System.out.println("Exception occured while processing fmtemplate:"+e.getMessage()); + freemarkerConfiguration.getTemplate( templateName + ".ftl", locale, getEncoding( ) ), context ) ); + return content.toString( ); + } + catch ( Exception e ) + { + log.error( "Could not parse the mail template {}: {}", templateName, e.getMessage( ), e ); } return ""; } - private Map createModel( AuthenticationKey authkey, String appUrl ) + @Override + public String generateMail( String templateName, AuthenticationKey authenticationKey, String baseUrl ) { - Map context = new HashMap<>( ); + return generateMail( templateName, getMailLocale(), authenticationKey, baseUrl ); + } + + @Override + public String generateMail( String templateName, Locale locale, AuthenticationKey authenticationKey, String baseUrl ) + { + return generateMail( templateName, locale, authenticationKey, baseUrl, null ); + } + + private Map createModel( AuthenticationKey authkey, String appUrl, Map templateData ) + { + Map context = new HashMap<>( ); context.put( "applicationUrl", config.getString( UserConfigurationKeys.APPLICATION_URL, appUrl ) ); String feedback = config.getString( UserConfigurationKeys.EMAIL_FEEDBACK_PATH ); @@ -97,23 +155,32 @@ public class FreemarkerMailGenerator implements MailGenerator context.put( "urlPath", config.getString( UserConfigurationKeys.EMAIL_URL_PATH, "security/login!login.action" ) ); - context.put( "authkey", authkey.getKey() ); + context.put( "authkey", authkey.getKey( ) ); - context.put( "accountId", authkey.getForPrincipal() ); + context.put( "accountId", authkey.getForPrincipal( ) ); SimpleDateFormat dateformatter = new SimpleDateFormat( config.getString( UserConfigurationKeys.APPLICATION_TIMESTAMP ), Locale.US ); - context.put( "requestedOn", dateformatter.format( authkey.getDateCreated() ) ); + context.put( "requestedOn", dateformatter.format( authkey.getDateCreated( ) ) ); - if ( authkey.getDateExpires() != null ) + if ( authkey.getDateExpires( ) != null ) { - context.put( "expiresOn", dateformatter.format( authkey.getDateExpires() ) ); + context.put( "expiresOn", dateformatter.format( authkey.getDateExpires( ) ) ); } else { context.put( "expiresOn", "(does not expire)" ); } + + if (templateData!=null) + { + for ( Map.Entry entry : templateData.entrySet( ) ) + { + context.put( entry.getKey( ), entry.getValue( ) ); + } + } + return context; } diff --git a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/MailGenerator.java b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/MailGenerator.java index b9b009e2..695dccda 100644 --- a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/MailGenerator.java +++ b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/MailGenerator.java @@ -21,6 +21,9 @@ package org.apache.archiva.redback.integration.mail; import org.apache.archiva.redback.keys.AuthenticationKey; +import java.util.Locale; +import java.util.Map; + /** * Mail generator component. * @@ -29,5 +32,45 @@ import org.apache.archiva.redback.keys.AuthenticationKey; */ public interface MailGenerator { + + + /** + * Generates a mail string from a template. How the template will be located depends on the underlying + * implementation. + * It uses a default locale. + * + * @param templateName the template name without extension + * @param authkey the authentication key of the current user + * @param baseUrl the base url + * @return A string for the mail body generated from the template + */ String generateMail( String templateName, AuthenticationKey authkey, String baseUrl ); + + /** + * Generates a mail string from a template. The given locale is used for retrieving the template. + * How the template will be located depends on the underlying implementation. + * + * @param templateName the template name without extension + * @param locale the locale used to find the template file + * @param authenticationKey the authentication key of the current user + * @param baseUrl the base url + * @return a string for the mail body generated from the template + */ + String generateMail( String templateName, Locale locale, AuthenticationKey authenticationKey, String baseUrl ); + + /** + * Generates a mail string from a template. The given locale is used for retrieving the template. + * How the template will be located depends on the underlying implementation. + * The templateData is used as model data that is interpolated from the template. + * + * @param templateName the template name without extension + * @param locale the locale used to find the template file + * @param authenticationKey the authentication key of the current user + * @param baseUrl the base url + * @param templateData additional data used for interpolation in the template + * @return a string for the mail body generated from the template + */ + String generateMail( String templateName, Locale locale, AuthenticationKey authenticationKey, String baseUrl, + Map templateData ); + }