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