1
|
package eu.dnetlib.uoamonitorservice.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoahelptexts.emailSender.EmailSender;
|
4
|
import eu.dnetlib.uoahelptexts.entities.EmailRecaptcha;
|
5
|
import eu.dnetlib.uoahelptexts.entities.Email;
|
6
|
import eu.dnetlib.uoahelptexts.handlers.InvalidReCaptchaException;
|
7
|
import eu.dnetlib.uoahelptexts.recaptcha.VerifyRecaptcha;
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.web.bind.annotation.*;
|
11
|
|
12
|
import java.util.*;
|
13
|
|
14
|
@RestController
|
15
|
@CrossOrigin(origins = "*")
|
16
|
public class EmailController {
|
17
|
private final Logger log = Logger.getLogger(this.getClass());
|
18
|
|
19
|
@Autowired
|
20
|
private EmailSender emailSender;
|
21
|
|
22
|
@Autowired
|
23
|
private VerifyRecaptcha verifyRecaptcha;
|
24
|
|
25
|
@RequestMapping(value = "/contact", method = RequestMethod.POST)
|
26
|
public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException {
|
27
|
verifyRecaptcha.processResponse(form.getRecaptcha());
|
28
|
Email email = form.getEmail();
|
29
|
ArrayList<String> sendTo = new ArrayList<>();
|
30
|
for(String userMail: email.getRecipients()){
|
31
|
sendTo.add(userMail);
|
32
|
}
|
33
|
return emailSender.send(sendTo, email.getSubject(), email.getBody(), false);
|
34
|
}
|
35
|
|
36
|
@RequestMapping(value = "/sendMail", method = RequestMethod.POST)
|
37
|
public Map<String, ArrayList<String>> sendEmail(@RequestBody Email email,
|
38
|
@RequestParam(required = false) Optional<Boolean> optional) {
|
39
|
String successString = "success";
|
40
|
String failureString = "failure";
|
41
|
Map<String, ArrayList<String>> mailResults = new HashMap<>();
|
42
|
boolean bcc = (optional.isPresent())?optional.get():true;
|
43
|
for(String userMail:email.getRecipients()){
|
44
|
ArrayList<String> sendTo = new ArrayList<>();
|
45
|
sendTo.add(userMail);
|
46
|
boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), bcc);
|
47
|
if(success){
|
48
|
if(!mailResults.containsKey(successString)) {
|
49
|
mailResults.put(successString, new ArrayList<>());
|
50
|
}
|
51
|
mailResults.get(successString).add(userMail);
|
52
|
} else {
|
53
|
if(!mailResults.containsKey(failureString)) {
|
54
|
mailResults.put(failureString, new ArrayList<>());
|
55
|
}
|
56
|
mailResults.get(failureString).add(userMail);
|
57
|
}
|
58
|
}
|
59
|
return mailResults;
|
60
|
|
61
|
}
|
62
|
|
63
|
}
|