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 4763705 Migrating mailgenerator to freemarker templates
4763705 is described below
commit 4763705ea81080c9caa02acb21479d6f9a2d65f6
Author: Martin Stockhammer <[email protected]>
AuthorDate: Sat Oct 5 18:24:51 2019 +0200
Migrating mailgenerator to freemarker templates
---
pom.xml | 10 +--
.../redback-common-integrations/pom.xml | 8 +-
...Generator.java => FreemarkerMailGenerator.java} | 90 ++++++++--------------
.../redback/integration/mail/MailerImpl.java | 2 +-
.../src/main/resources/META-INF/spring-context.xml | 13 ++--
...ationEmail.vm => newAccountValidationEmail.ftl} | 21 ++---
...asswordResetEmail.vm => passwordResetEmail.ftl} | 20 ++---
.../integration/mail/MailGeneratorTest.java | 2 +-
.../src/test/resources/spring-context.xml | 4 +-
9 files changed, 72 insertions(+), 98 deletions(-)
diff --git a/pom.xml b/pom.xml
index 7700ec6..bade8b2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -626,11 +626,6 @@
</dependency>
<dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity</artifactId>
- <version>1.7</version>
- </dependency>
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
@@ -645,6 +640,11 @@
<artifactId>assertj-core</artifactId>
<version>1.7.1</version>
</dependency>
+ <dependency>
+ <groupId>org.freemarker</groupId>
+ <artifactId>freemarker</artifactId>
+ <version>2.3.29</version>
+ </dependency>
<!-- Dependencies for JDK >=9 update -->
<dependency>
diff --git a/redback-integrations/redback-common-integrations/pom.xml
b/redback-integrations/redback-common-integrations/pom.xml
index 477ec8d..5b40b69 100644
--- a/redback-integrations/redback-common-integrations/pom.xml
+++ b/redback-integrations/redback-common-integrations/pom.xml
@@ -96,10 +96,6 @@
<artifactId>redback-integrations-security</artifactId>
</dependency>
<dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity</artifactId>
- </dependency>
- <dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
@@ -114,6 +110,10 @@
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
+ <groupId>org.freemarker</groupId>
+ <artifactId>freemarker</artifactId>
+ </dependency>
+ <dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<scope>provided</scope>
diff --git
a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/VelocityMailGenerator.java
b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java
similarity index 56%
rename from
redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/VelocityMailGenerator.java
rename to
redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java
index 9bb5840..cdd97cf 100644
---
a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/VelocityMailGenerator.java
+++
b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java
@@ -19,45 +19,38 @@ package org.apache.archiva.redback.integration.mail;
* under the License.
*/
+import freemarker.template.Configuration;
import org.apache.archiva.redback.configuration.UserConfiguration;
import org.apache.archiva.redback.configuration.UserConfigurationKeys;
import org.apache.archiva.redback.keys.AuthenticationKey;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.exception.MethodInvocationException;
-import org.apache.velocity.exception.ParseErrorException;
-import org.apache.velocity.exception.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
+import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import javax.inject.Inject;
import javax.inject.Named;
-import java.io.StringWriter;
import java.text.SimpleDateFormat;
+import java.util.HashMap;
import java.util.Locale;
+import java.util.Map;
/**
- * Mail generator component implementation using velocity.
- *
- * @author <a href="mailto:[email protected]">Brett Porter</a>
+ * @author Martin Stockhammer <[email protected]>
*/
-@Service("mailGenerator#velocity")
-public class VelocityMailGenerator
- implements MailGenerator
+@Service("mailGenerator#freemarker")
+public class FreemarkerMailGenerator implements MailGenerator
{
- private Logger log = LoggerFactory.getLogger( VelocityMailGenerator.class
);
+ private Logger log = LoggerFactory.getLogger(
FreemarkerMailGenerator.class );
public static final String DEFAULT_ENCODING = "UTF-8";
@Inject
- @Named(value = "userConfiguration#default")
+ @Named( value = "userConfiguration#default" )
private UserConfiguration config;
- // FIXME use the spring directly
@Inject
- @Named(value = "velocityEngine#redback")
- private VelocityEngine velocityEngine;
+ Configuration freemarkerConfiguration;
private String encoding;
@@ -68,43 +61,25 @@ public class VelocityMailGenerator
return this.encoding;
}
+ @Override
public String generateMail( String templateName, AuthenticationKey
authkey, String baseUrl )
{
- VelocityContext context = createVelocityContext( authkey, baseUrl );
-
- String packageName = getClass().getPackage().getName().replace( '.',
'/' );
- String templateFile = packageName + "/template/" + templateName +
".vm";
-
- StringWriter writer = new StringWriter();
-
- try
- {
- velocityEngine.mergeTemplate( templateFile, getEncoding(),
context, writer );
- }
- catch ( ResourceNotFoundException e )
- {
- log.error( "No such template: '{}'.", templateFile );
- }
- catch ( ParseErrorException e )
- {
- log.error( "Unable to generate email for template '{}': {}",
templateFile, e.getMessage(), e );
- }
- catch ( MethodInvocationException e )
- {
- log.error( "Unable to generate email for template '{}': {}",
templateFile, e.getMessage(), e );
+ Map<String, String> context = createModel( authkey, baseUrl );
+
+ 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());
}
- catch ( Exception e )
- {
- log.error( "Unable to generate email for template '{}': {}",
templateFile, e.getMessage(), e );
- }
-
- return writer.getBuffer().toString();
+ return "";
}
- private VelocityContext createVelocityContext( AuthenticationKey authkey,
String appUrl )
+ private Map<String, String> createModel( AuthenticationKey authkey, String
appUrl )
{
- VelocityContext context = new VelocityContext();
-
+ Map<String, String> context = new HashMap<>( );
context.put( "applicationUrl", config.getString(
UserConfigurationKeys.APPLICATION_URL, appUrl ) );
String feedback = config.getString(
UserConfigurationKeys.EMAIL_FEEDBACK_PATH );
@@ -120,7 +95,7 @@ public class VelocityMailGenerator
}
context.put( "urlPath",
- config.getString( UserConfigurationKeys.EMAIL_URL_PATH,
"security/login!login.action" ) );
+ config.getString( UserConfigurationKeys.EMAIL_URL_PATH,
"security/login!login.action" ) );
context.put( "authkey", authkey.getKey() );
@@ -142,24 +117,23 @@ public class VelocityMailGenerator
return context;
}
-
- public UserConfiguration getConfig()
+ public Configuration getFreemarkerConfiguration( )
{
- return config;
+ return freemarkerConfiguration;
}
- public void setConfig( UserConfiguration config )
+ public void setFreemarkerConfiguration( Configuration
freemarkerConfiguration )
{
- this.config = config;
+ this.freemarkerConfiguration = freemarkerConfiguration;
}
- public VelocityEngine getVelocityEngine()
+ public UserConfiguration getConfig( )
{
- return velocityEngine;
+ return config;
}
- public void setVelocityEngine( VelocityEngine velocityEngine )
+ public void setConfig( UserConfiguration config )
{
- this.velocityEngine = velocityEngine;
+ this.config = config;
}
}
diff --git
a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/MailerImpl.java
b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/MailerImpl.java
index f5054fe..462699d 100644
---
a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/MailerImpl.java
+++
b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/MailerImpl.java
@@ -57,7 +57,7 @@ public class MailerImpl
protected Logger log = LoggerFactory.getLogger( getClass() );
@Inject
- @Named( value = "mailGenerator#velocity" )
+ @Named( value = "mailGenerator#default" )
private MailGenerator generator;
@Inject
diff --git
a/redback-integrations/redback-common-integrations/src/main/resources/META-INF/spring-context.xml
b/redback-integrations/redback-common-integrations/src/main/resources/META-INF/spring-context.xml
index 9ddd2a6..7dc6237 100644
---
a/redback-integrations/redback-common-integrations/src/main/resources/META-INF/spring-context.xml
+++
b/redback-integrations/redback-common-integrations/src/main/resources/META-INF/spring-context.xml
@@ -30,15 +30,12 @@
<context:annotation-config />
<context:component-scan
base-package="org.apache.archiva.redback.integration"/>
- <bean name="velocityEngine#redback"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
- <property name="velocityProperties">
- <props>
- <prop key="resource.loader">classpath</prop>
- <prop
key="classpath.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
- <prop key="classpath.resource.loader.class.cache">true</prop>
- <prop key="resource.manager.logwhenfound">true</prop>
- </props>
+ <bean name="freemarkerConfiguration#redback"
class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean" >
+ <property name="templateLoaderPath">
+
<value>classpath:/org/apache/archiva/redback/integration/mail/template</value>
</property>
</bean>
+
+ <alias name="mailGenerator#freemarker" alias="mailGenerator#default" />
</beans>
\ No newline at end of file
diff --git
a/redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/newAccountValidationEmail.vm
b/redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/newAccountValidationEmail.ftl
similarity index 82%
rename from
redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/newAccountValidationEmail.vm
rename to
redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/newAccountValidationEmail.ftl
index f6cc6e0..fab23f3 100644
---
a/redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/newAccountValidationEmail.vm
+++
b/redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/newAccountValidationEmail.ftl
@@ -1,4 +1,5 @@
-#*
+<#--
+ *
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -15,20 +16,20 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
- *#
-
+ *
+ -->
Welcome
-Username: $accountId
+Username: ${accountId}
-This account was requested on $requestedOn
+This account was requested on ${requestedOn}
Use the following URL to validate your account.
-$applicationUrl/$urlPath?validateMe=$authkey
+${applicationUrl}/${urlPath}?validateMe=${authkey}
-This url will be valid until $expiresOn
+This url will be valid until ${expiresOn}
Once validated, your account will only have the most basic rights on the
system.
Please contact the administrator to be assigned a more appropriate set of Roles
@@ -36,8 +37,8 @@ and Permissions.
Please keep this email for future reference.
-#if ( $feedback )
-Questions/Comments? $feedback
-#end
+<#if feedback?? >
+Questions/Comments? ${feedback}
+</#if>
diff --git
a/redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/passwordResetEmail.vm
b/redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/passwordResetEmail.ftl
similarity index 77%
rename from
redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/passwordResetEmail.vm
rename to
redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/passwordResetEmail.ftl
index 60b15fe..081852e 100644
---
a/redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/passwordResetEmail.vm
+++
b/redback-integrations/redback-common-integrations/src/main/resources/org/apache/archiva/redback/integration/mail/template/passwordResetEmail.ftl
@@ -1,4 +1,5 @@
-#*
+<#--
+ *
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -15,23 +16,24 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
- *#
+ *
+-->
Password Reset
-Username: $accountId
+Username: ${accountId}
-Someone requested a password reset for this account on $requestedOn
+Someone requested a password reset for this account on ${requestedOn}
Use the following URL to reset the password on your account.
-$applicationUrl/$urlPath?resetPassword=$authkey
+${applicationUrl}/${urlPath}?resetPassword=${authkey}
-This url will be valid until $expiresOn
+This url will be valid until ${expiresOn}
-#if ( $feedback )
-Questions/Comments? $feedback
-#end
+<#if feedback?? >
+Questions/Comments? ${feedback}
+</#if>
diff --git
a/redback-integrations/redback-common-integrations/src/test/java/org/apache/archiva/redback/integration/mail/MailGeneratorTest.java
b/redback-integrations/redback-common-integrations/src/test/java/org/apache/archiva/redback/integration/mail/MailGeneratorTest.java
index c4f63b5..3b10691 100644
---
a/redback-integrations/redback-common-integrations/src/test/java/org/apache/archiva/redback/integration/mail/MailGeneratorTest.java
+++
b/redback-integrations/redback-common-integrations/src/test/java/org/apache/archiva/redback/integration/mail/MailGeneratorTest.java
@@ -45,7 +45,7 @@ public class MailGeneratorTest
extends TestCase
{
@Inject
- @Named(value = "mailGenerator#velocity")
+ @Named(value = "mailGenerator#freemarker")
private MailGenerator generator;
@Inject
diff --git
a/redback-integrations/redback-common-integrations/src/test/resources/spring-context.xml
b/redback-integrations/redback-common-integrations/src/test/resources/spring-context.xml
index 97f151c..3a2900f 100644
---
a/redback-integrations/redback-common-integrations/src/test/resources/spring-context.xml
+++
b/redback-integrations/redback-common-integrations/src/test/resources/spring-context.xml
@@ -36,9 +36,9 @@
</property>
</bean>
- <bean name="mailGenerator#custom-url"
class="org.apache.archiva.redback.integration.mail.VelocityMailGenerator">
+ <bean name="mailGenerator#custom-url"
class="org.apache.archiva.redback.integration.mail.FreemarkerMailGenerator">
<property name="config" ref="userConfiguration#custom-url"/>
- <property name="velocityEngine" ref="velocityEngine#redback"/>
+ <property name="freemarkerConfiguration"
ref="freemarkerConfiguration#redback"/>
</bean>
<alias name="userConfiguration#redback" alias="userConfiguration#default"/>