Set mail using localhost & added mail template in kustvakt config.

Change-Id: I41eea78cf1fb19b44f46411e25ecb27b52c74982
diff --git a/README.md b/README.md
index a744b66..634aae8 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@
 # Setup
 
 
-Prerequisites: Jdk 1.7, Git, Maven 3, MySQL (optional).
+Prerequisites: Jdk 1.8, Git, Maven 3, MySQL (optional), Postfix (optional).
 
 Clone the latest version of Kustvakt
 <pre>
@@ -147,6 +147,92 @@
 ```full/src/main/resources/db/new-mysql``` and other paths specified in 
 ```jdbc.schemaPath```.
 
+## Enabling Mail
+
+Kustvakt supports email notification, for instance of a invitation to a user-group.
+The mail setting is by default configured for a mail server at localhost post 25. 
+You can setup a mail server for example by using [Postfix](http://www.postfix.org/).
+
+In kustvakt.conf or kustvakt-test.conf, set  
+
+	mail.enabled = true
+	mail.receiver = test@localhost
+	mail.sender = noreply@ids-mannheim.de
+
+
+You can change ```mail.sender``` value to any email address.
+```mail.receiver``` is only used for testing purpose. Change 
+```test``` to any username available in your system, or create an alias for 
+```test@localhost```. 
+
+To view the mailbox, you can use ```mailx``` 
+
+	$ mailx -u test
+	s-nail version v14.8.6.  Type ? for help.
+	"/var/mail/test": 1 messages 0 unread
+	 O  1 noreply@ids-mannhe Wed Feb 21 18:07   30/1227  Invitation to join
+
+
+### Creating email aliases 
+
+	sudo vi /etc/postfix/main.cf
+	
+In main.cf, set
+
+	"virtual_alias_maps = hash:/etc/postfix/alias"
+
+Create alias file:
+
+	sudo vi /etc/postfix/alias
+
+In the alias file, create an alias for ```test@localhost``` to a user in your system
+
+	test@localhost username
+
+Create alias database
+
+	sudo postmap /etc/postfix/alias
+	
+Restart postfix
+
+	sudo /etc/init.d/postfix restart
+
+
+By default, any emails sent to ```test@localhost``` will be available at 
+```/var/mail/username```.
+
+### Customizing SMTP configuration
+
+To connect to an external/remote mail server instead of using local Postfix,
+copy ```full/src/main/resources/properties/mail.properties``` to 
+the ```full/``` folder. Customize the properties in the file according to
+the mail server setup.  
+
+	mail.host = smtp.host.address
+	mail.port = 123
+	mail.connectiontimeout = 3000
+	mail.auth = true
+	mail.starttls.enable = true
+	mail.username = username
+	mail.password = password
+ 
+### Customizing Mail template
+
+Kustvakt uses [Apache Velocity](http://velocity.apache.org/) as the email template engine and searches for templates located at:
+
+```full/src/main/resources/templates```.
+
+For instance, the template for email notification of user-group invitation is 
+
+```full/src/main/resources/templates/notification.vm```
+
+You can change the template according to Velocity Template Language.
+
+In ```kustvakt.conf``` or ```kustvakt-test.conf```, specify which template should be used. 
+	
+	template.group.invitation = notification.vm
+
+
 # Known issues
 Tests are verbose - they do not necessarily imply system errors.
 
diff --git a/core/pom.xml b/core/pom.xml
index 9089163..ee9b4b0 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -108,7 +108,7 @@
 			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>
 				<artifactId>maven-surefire-plugin</artifactId>
-				<version>2.19.1</version>
+				<version>2.20.1</version>
 
 				<configuration>
 					<reuseForks>false</reuseForks>
diff --git a/full/Changes b/full/Changes
index 40a067f..7d83752 100644
--- a/full/Changes
+++ b/full/Changes
@@ -1,3 +1,10 @@
+version 0.60 release
+21/02/2018
+	- set up mail settings using localhost port 25 (margaretha)
+	- added mail template in kustvakt configuration (margaretha)
+	- added mail settings to readme (margaretha)
+	- disabled email notification for auto group (margaretha)
+
 version 0.59.10	
 20/02/2018 
 	- added sort VC by id (margaretha)
@@ -18,6 +25,7 @@
 	- testing mail implementation using embedded jetty jndi (margaretha)
 	- fixed collection rewrite regarding OR operation with other fields (margaretha)
 	- implemented sending mail using spring injection and removed jetty jndi (margaretha)
+	- fixed unrecognized application/json (margaretha)
 	- fixed and updated velocity template (margaretha)
 	
 version 0.59.9 
diff --git a/full/pom.xml b/full/pom.xml
index 1391cc4..b1b92c8 100644
--- a/full/pom.xml
+++ b/full/pom.xml
@@ -3,7 +3,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>de.ids_mannheim.korap</groupId>
 	<artifactId>Kustvakt-full</artifactId>
-	<version>0.59.10</version>
+	<version>0.60</version>
 	<properties>
 		<java.version>1.8</java.version>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -86,12 +86,12 @@
 			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>
 				<artifactId>maven-surefire-plugin</artifactId>
-				<version>2.19.1</version>
+				<version>2.20.1</version>
 				<configuration>
 					<reuseForks>false</reuseForks>
 					<forkCount>2</forkCount>
 					<threadCount>10</threadCount>
-
+					<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
 					<excludes>
 						<!-- <exclude>de/ids_mannheim/korap/suites/*.java</exclude> -->
 						<!-- <exclude>de/ids_mannheim/korap/dao/*.java</exclude> -->
diff --git a/full/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java b/full/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
index 564832c..0015e89 100644
--- a/full/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
+++ b/full/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
@@ -203,7 +203,6 @@
 
 	@Override
 	public void setAccessAndLocation(User user, HttpHeaders headers) {
-		Boolean DEBUG_LOG = true;
 		MultivaluedMap<String, String> headerMap = headers.getRequestHeaders();
 		Location location = Location.EXTERN;
 		CorpusAccess corpusAccess = CorpusAccess.FREE;
@@ -212,8 +211,7 @@
 	    {
 	    	// to be absolutely sure:
 	    	user.setCorpusAccess(User.CorpusAccess.FREE);
-	    	if( DEBUG_LOG == true )
-	    		System.out.printf("setAccessAndLocation: DemoUser: location=%s, access=%s.\n", user.locationtoString(), user.accesstoString());
+	    	jlog.debug("setAccessAndLocation: DemoUser: location=%s, access=%s.\n", user.locationtoString(), user.accesstoString());
 	     	return;
 	    }
 		
@@ -245,8 +243,8 @@
 
 			user.setLocation(location);
 			user.setCorpusAccess(corpusAccess);
-	    	if( DEBUG_LOG == true )
-	    		System.out.printf("setAccessAndLocation: KorAPUser: location=%s, access=%s.\n", user.locationtoString(), user.accesstoString());
+	    	
+	    	jlog.debug("setAccessAndLocation: KorAPUser: location=%s, access=%s.\n", user.locationtoString(), user.accesstoString());
 
 		}
 	} // getAccess
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 4d51c4e..201fc4b 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
@@ -19,6 +19,8 @@
     private String testEmail;
     private String noReply;
 
+    private String groupInvitationTemplate;
+    
     private String ldapConfig;
 
     private String freeOnlyRegex;
@@ -64,6 +66,7 @@
             // other properties must be set in the kustvakt.conf
             setTestEmail(properties.getProperty("mail.receiver"));
             setNoReply(properties.getProperty("mail.sender"));
+            setGroupInvitationTemplate(properties.getProperty("template.group.invitation"));
         }
     }
 
@@ -258,4 +261,12 @@
         this.noReply = noReply;
     }
 
+    public String getGroupInvitationTemplate () {
+        return groupInvitationTemplate;
+    }
+
+    public void setGroupInvitationTemplate (String groupInvitationTemplate) {
+        this.groupInvitationTemplate = groupInvitationTemplate;
+    }
+
 }
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 ff05bbe..866a0a9 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
@@ -17,24 +17,26 @@
 import org.springframework.mail.javamail.MimeMessagePreparator;
 import org.springframework.stereotype.Service;
 
+import de.ids_mannheim.korap.config.FullConfiguration;
 import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface;
 import de.ids_mannheim.korap.user.User;
 
 @Service
 public class MailService {
-    
-    private static Logger jlog =
-            LoggerFactory.getLogger(MailService.class);
-    
+
+    private static Logger jlog = LoggerFactory.getLogger(MailService.class);
+
     @Autowired
     private AuthenticationManagerIface authManager;
     @Autowired
     private JavaMailSender mailSender;
     @Autowired
     private VelocityEngine velocityEngine;
+    @Autowired
+    private FullConfiguration config;
 
     public void sendMemberInvitationNotification (String inviteeName,
-            String sender, String groupName, String inviter) {
+            String groupName, String inviter) {
 
         MimeMessagePreparator preparator = new MimeMessagePreparator() {
 
@@ -44,28 +46,29 @@
 
                 MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                 message.setTo(new InternetAddress(invitee.getEmail()));
-                message.setFrom(sender);
-                message.setSubject("Invitation to join "+groupName);
-                message.setText(prepareText(inviteeName, groupName, inviter),
-                        true);
+                message.setFrom(config.getNoReply());
+                message.setSubject("Invitation to join " + groupName);
+                message.setText(prepareGroupInvitationText(inviteeName,
+                        groupName, inviter), true);
             }
 
         };
         mailSender.send(preparator);
     }
 
-    private String prepareText (String username, String groupName,
-            String inviter) {
+    private String prepareGroupInvitationText (String username,
+            String groupName, String inviter) {
         Context context = new VelocityContext();
         context.put("username", username);
         context.put("group", groupName);
         context.put("inviter", inviter);
-        
+
         StringWriter stringWriter = new StringWriter();
-        
-        velocityEngine.mergeTemplate("templates/notification.vm",
+
+        velocityEngine.mergeTemplate(
+                "templates/" + config.getGroupInvitationTemplate(),
                 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 d1c9c84..9c3e245 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
@@ -250,9 +250,8 @@
         member.setUserId(username);
         groupMemberDao.addMember(member);
 
-        if (config.isMailEnabled()) {
-            mailService.sendMemberInvitationNotification(username,
-                    config.getNoReply(), userGroup.getName(), createdBy);
+        if (config.isMailEnabled() && userGroup.getStatus() != UserGroupStatus.HIDDEN) {
+            mailService.sendMemberInvitationNotification(username,userGroup.getName(), createdBy);
         }
     }
 
diff --git a/full/src/main/resources/kustvakt.conf b/full/src/main/resources/kustvakt.conf
index edb1bce..d737a06 100644
--- a/full/src/main/resources/kustvakt.conf
+++ b/full/src/main/resources/kustvakt.conf
@@ -16,9 +16,12 @@
 
 ## mail settings
 mail.enabled = false
-mail.receiver = test-email
+mail.receiver = test@localhost
 mail.sender = noreply@ids-mannheim.de
 
+## mail.templates
+template.group.invitation = notification.vm
+
 ## default layers
 default.layer.p = tt
 default.layer.l = tt
diff --git a/full/src/main/resources/properties/mail.properties b/full/src/main/resources/properties/mail.properties
index 492b322..29d1ca1 100644
--- a/full/src/main/resources/properties/mail.properties
+++ b/full/src/main/resources/properties/mail.properties
@@ -1,7 +1,7 @@
-mail.host = smtp.host
+mail.host = localhost
 mail.port = 25
 mail.connectiontimeout = 3000
-mail.auth = true
-mail.starttls.enable = true
+mail.auth = false
+mail.starttls.enable = false
 mail.username = username
 mail.password = password
\ No newline at end of file
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/UserGroupControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/UserGroupControllerTest.java
index be39d02..c907338 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/UserGroupControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/UserGroupControllerTest.java
@@ -472,7 +472,6 @@
                                 "pass"))
                 .entity(userGroup).post(ClientResponse.class);
 
-        String entity = response.getEntity(String.class);
         assertEquals(Status.OK.getStatusCode(), response.getStatus());
 
         // check member
diff --git a/full/src/test/resources/kustvakt-test.conf b/full/src/test/resources/kustvakt-test.conf
index aa76a83..ca6b233 100644
--- a/full/src/test/resources/kustvakt-test.conf
+++ b/full/src/test/resources/kustvakt-test.conf
@@ -10,15 +10,18 @@
 ldap.config = file-path-to-ldap-config
 
 # Kustvakt
-## mail settings
-mail.enabled = false
-mail.receiver = test-email
-mail.sender = noreply@ids-mannheim.de
-
 ## server
 server.port=8089
 server.host=localhost
 
+## mail settings
+mail.enabled = false
+mail.receiver = test@localhost
+mail.sender = noreply@ids-mannheim.de
+
+## mail.templates
+template.group.invitation = notification.vm
+
 ## default layers
 default.layer.p = tt
 default.layer.l = tt