Implemented sending mail using spring injection and removed jetty jndi.

Change-Id: Ie88772800a380f488aaf958fdd49398d32898aa8
diff --git a/full/src/main/java/de/ids_mannheim/korap/config/FullConfiguration.java b/full/src/main/java/de/ids_mannheim/korap/config/FullConfiguration.java
index ad6aaab..4d51c4e 100644
--- a/full/src/main/java/de/ids_mannheim/korap/config/FullConfiguration.java
+++ b/full/src/main/java/de/ids_mannheim/korap/config/FullConfiguration.java
@@ -17,10 +17,7 @@
     // mail configuration
     private boolean isMailEnabled;
     private String testEmail;
-    private String mailUsername;
-    private String mailPassword;
-    private String mailSmtp;
-    private String mailPort;
+    private String noReply;
 
     private String ldapConfig;
 
@@ -63,13 +60,10 @@
 
     private void setMailConfiguration (Properties properties) {
         setMailEnabled(Boolean.valueOf(properties.getProperty("mail.enabled", "false")));
-        setTestEmail(properties.getProperty("mail.receiver"));
         if (isMailEnabled){
             // other properties must be set in the kustvakt.conf
-            setMailUsername(properties.getProperty("mail.username"));
-            setMailPassword(properties.getProperty("mail.password"));
-            setMailSmtp(properties.getProperty("mail.smtp"));
-            setMailPort(properties.getProperty("mail.port"));
+            setTestEmail(properties.getProperty("mail.receiver"));
+            setNoReply(properties.getProperty("mail.sender"));
         }
     }
 
@@ -248,38 +242,6 @@
         this.testEmail = testEmail;
     }
 
-    public String getMailUsername () {
-        return mailUsername;
-    }
-
-    public void setMailUsername (String mailUsername) {
-        this.mailUsername = mailUsername;
-    }
-
-    public String getMailPassword () {
-        return mailPassword;
-    }
-
-    public void setMailPassword (String mailPassword) {
-        this.mailPassword = mailPassword;
-    }
-
-    public String getMailSmtp () {
-        return mailSmtp;
-    }
-
-    public void setMailSmtp (String mailHost) {
-        this.mailSmtp = mailHost;
-    }
-
-    public String getMailPort () {
-        return mailPort;
-    }
-
-    public void setMailPort (String mailPort) {
-        this.mailPort = mailPort;
-    }
-
     public boolean isMailEnabled () {
         return isMailEnabled;
     }
@@ -288,4 +250,12 @@
         this.isMailEnabled = isMailEnabled;
     }
 
+    public String getNoReply () {
+        return noReply;
+    }
+
+    public void setNoReply (String noReply) {
+        this.noReply = noReply;
+    }
+
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/server/KustvaktServer.java b/full/src/main/java/de/ids_mannheim/korap/server/KustvaktServer.java
index 6871205..b938904 100644
--- a/full/src/main/java/de/ids_mannheim/korap/server/KustvaktServer.java
+++ b/full/src/main/java/de/ids_mannheim/korap/server/KustvaktServer.java
@@ -5,17 +5,6 @@
 import java.net.URL;
 import java.util.Properties;
 
-import javax.naming.NamingException;
-
-import org.eclipse.jetty.jndi.factories.MailSessionReference;
-import org.eclipse.jetty.plus.jndi.Resource;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.handler.ContextHandler;
-import org.eclipse.jetty.server.handler.ResourceHandler;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.webapp.Configuration.ClassList;
-import org.eclipse.jetty.webapp.WebAppContext;
-
 import de.ids_mannheim.korap.config.FullConfiguration;
 import de.ids_mannheim.korap.web.KustvaktBaseServer;
 
@@ -37,7 +26,7 @@
         
         File f = new File("kustvakt.conf");
         if (!f.exists()){
-            URL url = KustvaktServer.class.getClassLoader().getResource("kustvakt.conf");
+            URL url = KustvaktServer.class.getResource("kustvakt.conf");
             if (url!=null){
                 f = new File(url.toURI());
             }
@@ -63,32 +52,4 @@
                 + "de.ids_mannheim.korap.web.service.full";
         server.start();
     }
-
-    @Override
-    protected void setupJndi(Server server, WebAppContext webapp) {
-        
-        
-//        ClassList classlist = ClassList.setServerDefault(server);
-//        classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
-//                "org.eclipse.jetty.plus.webapp.EnvConfiguration",
-//                "org.eclipse.jetty.plus.webapp.PlusConfiguration");
-        
-        MailSessionReference mailref = new MailSessionReference();
-        mailref.setUser(fullConfig.getMailUsername());
-        mailref.setPassword(fullConfig.getMailPassword());
-        
-        Properties props = new Properties();
-        props.put("mail.smtp.auth", "false");
-        props.put("mail.smtp.host",fullConfig.getMailSmtp());
-        props.put("mail.from",fullConfig.getMailUsername());
-        props.put("mail.debug", "false");
-        mailref.setProperties(props);
-        try {
-            new Resource(webapp, "mail/Session", mailref);
-        }
-        catch (NamingException e) {
-            e.printStackTrace();
-        }
-    }
-    
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/MailAuthenticator.java b/full/src/main/java/de/ids_mannheim/korap/service/MailAuthenticator.java
new file mode 100644
index 0000000..28e7660
--- /dev/null
+++ b/full/src/main/java/de/ids_mannheim/korap/service/MailAuthenticator.java
@@ -0,0 +1,20 @@
+package de.ids_mannheim.korap.service;
+
+import javax.mail.Authenticator;
+import javax.mail.PasswordAuthentication;
+
+public class MailAuthenticator extends Authenticator {
+
+    private PasswordAuthentication passwordAuthentication;
+
+    public MailAuthenticator (String username, String password) {
+        passwordAuthentication = new PasswordAuthentication(username, password);
+
+    }
+
+    @Override
+    protected PasswordAuthentication getPasswordAuthentication () {
+        return passwordAuthentication;
+    }
+
+}
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/MailService.java b/full/src/main/java/de/ids_mannheim/korap/service/MailService.java
index 74164d5..75d5176 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/MailService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/MailService.java
@@ -9,6 +9,8 @@
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
 import org.apache.velocity.context.Context;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.mail.javamail.JavaMailSender;
 import org.springframework.mail.javamail.MimeMessageHelper;
@@ -20,15 +22,19 @@
 
 @Service
 public class MailService {
+    
+    private static Logger jlog =
+            LoggerFactory.getLogger(MailService.class);
+    
     @Autowired
     private AuthenticationManagerIface authManager;
-//    @Autowired
+    @Autowired
     private JavaMailSender mailSender;
-//    @Autowired
+    @Autowired
     private VelocityEngine velocityEngine;
 
     public void sendMemberInvitationNotification (String inviteeName,
-            String groupName, String inviter) {
+            String sender, String groupName, String inviter) {
 
         MimeMessagePreparator preparator = new MimeMessagePreparator() {
 
@@ -38,7 +44,7 @@
 
                 MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                 message.setTo(new InternetAddress(invitee.getEmail()));
-                message.setFrom("noreply-korap-notification@ids-mannheim.de");
+                message.setFrom(sender);
                 message.setSubject("Invitation to join group");
                 message.setText(prepareText(inviteeName, groupName, inviter),
                         true);
@@ -52,12 +58,20 @@
             String inviter) {
         Context context = new VelocityContext();
         context.put("username", username);
-        context.put("groupName", groupName);
+        context.put("group", groupName);
         context.put("inviter", inviter);
-
+        
         StringWriter stringWriter = new StringWriter();
-        velocityEngine.mergeTemplate("invitationNotification.vm",
-                StandardCharsets.UTF_16.name(), context, stringWriter);
-        return stringWriter.toString();
+//        URL url = getClass().getClassLoader().getResource("notification.vm");
+//        System.out.println(url);
+//        Template t = velocityEngine.getTemplate(url.toString());
+//        System.out.println(t);
+        
+        velocityEngine.mergeTemplate("notification.vm",
+                StandardCharsets.UTF_8.name(), context, stringWriter);
+        
+        String message = stringWriter.toString();
+        jlog.debug(message);
+        return message;
     }
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/UserGroupService.java b/full/src/main/java/de/ids_mannheim/korap/service/UserGroupService.java
index e05053e..d1c9c84 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/UserGroupService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/UserGroupService.java
@@ -252,7 +252,7 @@
 
         if (config.isMailEnabled()) {
             mailService.sendMemberInvitationNotification(username,
-                    userGroup.getName(), createdBy);
+                    config.getNoReply(), userGroup.getName(), createdBy);
         }
     }
 
diff --git a/full/src/main/resources/default-config.xml b/full/src/main/resources/default-config.xml
index 8466d6a..5d3f9e9 100644
--- a/full/src/main/resources/default-config.xml
+++ b/full/src/main/resources/default-config.xml
@@ -41,9 +41,12 @@
 		<property name="ignoreResourceNotFound" value="true" />
 		<property name="locations">
 			<array>
-				<value>classpath:*/jdbc.properties</value>
+				<value>classpath:properties/jdbc.properties</value>
 				<value>file:./jdbc.properties</value>
-				<value>classpath:*/hibernate.properties</value>
+				<value>classpath:properties/mail.properties</value>
+				<value>file:./mail.properties</value>
+				<value>classpath:properties/hibernate.properties</value>
+				
 				<value>classpath:kustvakt.conf</value>
 				<value>file:./kustvakt.conf</value>
 			</array>
@@ -313,19 +316,34 @@
 	</bean>
 	
 	<!-- mail -->
-	<!-- <bean id="smtpSession" class="org.springframework.jndi.JndiObjectFactoryBean">
-		<property name="jndiName" value="java:comp/env/mail/jetty" />
+		<bean id="authenticator" class="de.ids_mannheim.korap.service.MailAuthenticator">
+		<constructor-arg index="0" value="${mail.username}"/>
+		<constructor-arg index="1" value="${mail.password}"/>
+	</bean>
+	<bean id="smtpSession" class="javax.mail.Session" factory-method="getInstance">
+		<constructor-arg index="0">
+			<props>
+				<prop key="mail.smtp.submitter">${mail.username}</prop>
+				<prop key="mail.smtp.auth">${mail.auth}</prop>
+				<prop key="mail.smtp.host">${mail.host}</prop>
+				<prop key="mail.smtp.port">${mail.port}</prop>
+				<prop key="mail.smtp.starttls.enable">${mail.starttls.enable}</prop>
+				<prop key="mail.smtp.connectiontimeout">${mail.connectiontimeout}</prop>
+			</props>
+		</constructor-arg>
+		<constructor-arg index="1" ref="authenticator"/>
 	</bean>
 	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
+		<property name="username" value="${mail.username}" />
+	    <property name="password" value="${mail.password}" />
 		<property name="session" ref="smtpSession" />
 	</bean>
 	<bean id="velocityEngine" class="org.apache.velocity.app.VelocityEngine">
 		<constructor-arg index="0">
 			<props>
 				<prop key="resource.loader">class</prop>
-				<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
-				</prop>
+				<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
 			</props>
 		</constructor-arg>
-	</bean> -->
+	</bean>
 </beans>
\ No newline at end of file
diff --git a/full/src/main/resources/kustvakt.conf b/full/src/main/resources/kustvakt.conf
index 2e29c02..edb1bce 100644
--- a/full/src/main/resources/kustvakt.conf
+++ b/full/src/main/resources/kustvakt.conf
@@ -10,8 +10,14 @@
 ldap.config = file-path-to-ldap-config
 
 # Kustvakt
+## server
+server.port=8089
+server.host=localhost
+
 ## mail settings
 mail.enabled = false
+mail.receiver = test-email
+mail.sender = noreply@ids-mannheim.de
 
 ## default layers
 default.layer.p = tt
diff --git a/full/src/main/resources/properties/mail.properties b/full/src/main/resources/properties/mail.properties
new file mode 100644
index 0000000..492b322
--- /dev/null
+++ b/full/src/main/resources/properties/mail.properties
@@ -0,0 +1,7 @@
+mail.host = smtp.host
+mail.port = 25
+mail.connectiontimeout = 3000
+mail.auth = true
+mail.starttls.enable = true
+mail.username = username
+mail.password = password
\ No newline at end of file