Migrating mailgenerator to freemarker templates
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/FreemarkerMailGenerator.java b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java
new file mode 100644
index 0000000..cdd97cf
--- /dev/null
+++ b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/FreemarkerMailGenerator.java
@@ -0,0 +1,139 @@
+package org.apache.archiva.redback.integration.mail;
+
+/*
+ * 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
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * 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.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.text.SimpleDateFormat;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * @author Martin Stockhammer <martin_s@apache.org>
+ */
+@Service("mailGenerator#freemarker")
+public class FreemarkerMailGenerator implements MailGenerator
+{
+    private Logger log = LoggerFactory.getLogger( FreemarkerMailGenerator.class );
+
+    public static final String DEFAULT_ENCODING = "UTF-8";
+
+    @Inject
+    @Named( value = "userConfiguration#default" )
+    private UserConfiguration config;
+
+    @Inject
+    Configuration freemarkerConfiguration;
+
+    private String encoding;
+
+    private String getEncoding() {
+        if (this.encoding==null) {
+            this.encoding = config.getString( "mail.encoding", DEFAULT_ENCODING );
+        }
+        return this.encoding;
+    }
+
+    @Override
+    public String generateMail( String templateName, AuthenticationKey authkey, String baseUrl )
+    {
+        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());
+        }
+        return "";
+    }
+
+    private Map<String, String> createModel( AuthenticationKey authkey, String appUrl )
+    {
+        Map<String, String> context = new HashMap<>( );
+        context.put( "applicationUrl", config.getString( UserConfigurationKeys.APPLICATION_URL, appUrl ) );
+
+        String feedback = config.getString( UserConfigurationKeys.EMAIL_FEEDBACK_PATH );
+
+        if ( feedback != null )
+        {
+            if ( feedback.startsWith( "/" ) )
+            {
+                feedback = appUrl + feedback;
+            }
+
+            context.put( "feedback", feedback );
+        }
+
+        context.put( "urlPath",
+            config.getString( UserConfigurationKeys.EMAIL_URL_PATH, "security/login!login.action" ) );
+
+        context.put( "authkey", authkey.getKey() );
+
+        context.put( "accountId", authkey.getForPrincipal() );
+
+        SimpleDateFormat dateformatter =
+            new SimpleDateFormat( config.getString( UserConfigurationKeys.APPLICATION_TIMESTAMP ), Locale.US );
+
+        context.put( "requestedOn", dateformatter.format( authkey.getDateCreated() ) );
+
+        if ( authkey.getDateExpires() != null )
+        {
+            context.put( "expiresOn", dateformatter.format( authkey.getDateExpires() ) );
+        }
+        else
+        {
+            context.put( "expiresOn", "(does not expire)" );
+        }
+        return context;
+    }
+
+    public Configuration getFreemarkerConfiguration( )
+    {
+        return freemarkerConfiguration;
+    }
+
+    public void setFreemarkerConfiguration( Configuration freemarkerConfiguration )
+    {
+        this.freemarkerConfiguration = freemarkerConfiguration;
+    }
+
+    public UserConfiguration getConfig( )
+    {
+        return config;
+    }
+
+    public void setConfig( UserConfiguration config )
+    {
+        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 @@
     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/java/org/apache/archiva/redback/integration/mail/VelocityMailGenerator.java b/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/VelocityMailGenerator.java
deleted file mode 100644
index 9bb5840..0000000
--- a/redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/mail/VelocityMailGenerator.java
+++ /dev/null
@@ -1,165 +0,0 @@
-package org.apache.archiva.redback.integration.mail;
-
-/*
- * 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
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-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 javax.inject.Inject;
-import javax.inject.Named;
-import java.io.StringWriter;
-import java.text.SimpleDateFormat;
-import java.util.Locale;
-
-/**
- * Mail generator component implementation using velocity.
- *
- * @author <a href="mailto:brett@apache.org">Brett Porter</a>
- */
-@Service("mailGenerator#velocity")
-public class VelocityMailGenerator
-    implements MailGenerator
-{
-    private Logger log = LoggerFactory.getLogger( VelocityMailGenerator.class );
-
-    public static final String DEFAULT_ENCODING = "UTF-8";
-
-    @Inject
-    @Named(value = "userConfiguration#default")
-    private UserConfiguration config;
-
-    // FIXME use the spring directly 
-    @Inject
-    @Named(value = "velocityEngine#redback")
-    private VelocityEngine velocityEngine;
-
-    private String encoding;
-
-    private String getEncoding() {
-        if (this.encoding==null) {
-            this.encoding = config.getString( "mail.encoding", DEFAULT_ENCODING );
-        }
-        return this.encoding;
-    }
-
-    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 );
-        }
-        catch ( Exception e )
-        {
-            log.error( "Unable to generate email for template '{}': {}", templateFile, e.getMessage(), e );
-        }
-
-        return writer.getBuffer().toString();
-    }
-
-    private VelocityContext createVelocityContext( AuthenticationKey authkey, String appUrl )
-    {
-        VelocityContext context = new VelocityContext();
-
-        context.put( "applicationUrl", config.getString( UserConfigurationKeys.APPLICATION_URL, appUrl ) );
-
-        String feedback = config.getString( UserConfigurationKeys.EMAIL_FEEDBACK_PATH );
-
-        if ( feedback != null )
-        {
-            if ( feedback.startsWith( "/" ) )
-            {
-                feedback = appUrl + feedback;
-            }
-
-            context.put( "feedback", feedback );
-        }
-
-        context.put( "urlPath",
-                     config.getString( UserConfigurationKeys.EMAIL_URL_PATH, "security/login!login.action" ) );
-
-        context.put( "authkey", authkey.getKey() );
-
-        context.put( "accountId", authkey.getForPrincipal() );
-
-        SimpleDateFormat dateformatter =
-            new SimpleDateFormat( config.getString( UserConfigurationKeys.APPLICATION_TIMESTAMP ), Locale.US );
-
-        context.put( "requestedOn", dateformatter.format( authkey.getDateCreated() ) );
-
-        if ( authkey.getDateExpires() != null )
-        {
-            context.put( "expiresOn", dateformatter.format( authkey.getDateExpires() ) );
-        }
-        else
-        {
-            context.put( "expiresOn", "(does not expire)" );
-        }
-        return context;
-    }
-
-
-    public UserConfiguration getConfig()
-    {
-        return config;
-    }
-
-    public void setConfig( UserConfiguration config )
-    {
-        this.config = config;
-    }
-
-    public VelocityEngine getVelocityEngine()
-    {
-        return velocityEngine;
-    }
-
-    public void setVelocityEngine( VelocityEngine velocityEngine )
-    {
-        this.velocityEngine = velocityEngine;
-    }
-}
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 81%
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 @@
 
 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 76%
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 @@
     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"/>