You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ma...@apache.org on 2019/10/05 17:26:49 UTC
[archiva-redback-core] branch master updated: Adding additional
documentation and optional parameters
This is an automated email from the ASF dual-hosted git repository.
martin_s pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/archiva-redback-core.git
The following commit(s) were added to refs/heads/master by this push:
new 01f0182 Adding additional documentation and optional parameters
01f0182 is described below
commit 01f0182324bda09bc3cdc744a03e2e135ceca9c5
Author: Martin Stockhammer <ma...@apache.org>
AuthorDate: Sat Oct 5 19:26:39 2019 +0200
Adding additional documentation and optional parameters
---
.../configuration/UserConfigurationKeys.java | 10 ++
.../integration/mail/FreemarkerMailGenerator.java | 107 +++++++++++++++++----
.../redback/integration/mail/MailGenerator.java | 43 +++++++++
3 files changed, 140 insertions(+), 20 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 5bdde91..d0a91af 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 cdd97cf..9302d86 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:
+ * <ul>
+ * <li>applicationUrl</li>
+ * <li>urlPath</li>
+ * <li>authKey</li>
+ * <li>accountId</li>
+ * <li>requestedOn</li>
+ * <li>expiresOn</li>
+ * </ul>
+ *
+ * The additional template data is added for interpolation, if not <code>null</code>.
+ *
+ * This implementation is location enabled. That means, it will try to find templates in the following order:
+ * <ul>
+ * <li><i>templateName</i>_<i>language</i>_<i>country</i>.ftl</li>
+ * <li><i>templateName</i>_<i>language</i>.ftl</li>
+ * <li><i>templateName</i>.ftl</li>
+ * </ul>
+ *
+ * The default encoding used for reading the template is UTF-8
+ *
* @author Martin Stockhammer <ma...@apache.org>
*/
-@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;
}
+ private Locale getMailLocale() {
+ String localeString = config.getString( UserConfigurationKeys.MAIL_DEFAULT_LOCALE );
+ if (localeString == null || "".equals(localeString)) {
+ return Locale.getDefault( );
+ } else {
+ return Locale.forLanguageTag( localeString );
+ }
+ }
+
+ /**
+ *
+ * @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 <code>null</code>
+ * @return the string generated from the template
+ */
@Override
- public String generateMail( String templateName, AuthenticationKey authkey, String baseUrl )
+ public String generateMail( String templateName, Locale locale, AuthenticationKey authkey, String baseUrl,
+ Map<String, Object> templateData )
{
- Map<String, String> context = createModel( authkey, baseUrl );
-
- StringBuffer content = new StringBuffer();
- try{
+ Map<String, Object> 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<String, String> createModel( AuthenticationKey authkey, String appUrl )
+ @Override
+ public String generateMail( String templateName, AuthenticationKey authenticationKey, String baseUrl )
+ {
+ 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<String, Object> createModel( AuthenticationKey authkey, String appUrl, Map<String, Object> templateData )
{
- Map<String, String> context = new HashMap<>( );
+ Map<String, Object> 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<String, Object> 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 b9b009e..695dccd 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<String, Object> templateData );
+
}