1
|
package eu.dnetlib.repo.manager.server.utils;
|
2
|
|
3
|
import eu.dnetlib.domain.functionality.UserProfile;
|
4
|
import eu.dnetlib.repo.manager.server.config.CascadingPropertyLoader;
|
5
|
import eu.dnetlib.utils.MailLibrary;
|
6
|
import org.apache.log4j.Logger;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.beans.factory.annotation.Value;
|
9
|
import org.springframework.stereotype.Component;
|
10
|
|
11
|
import java.io.PrintWriter;
|
12
|
import java.io.StringWriter;
|
13
|
import java.io.Writer;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.List;
|
16
|
|
17
|
/**
|
18
|
* Created by nikonas on 11/12/15.
|
19
|
*/
|
20
|
|
21
|
@Component
|
22
|
public class EmailUtilsImpl implements EmailUtils {
|
23
|
|
24
|
private static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class);
|
25
|
|
26
|
private List<String> specialRecipients = new ArrayList<String>();
|
27
|
private boolean override = false, logonly = false;
|
28
|
private String overrideEmail = null, from = null;
|
29
|
|
30
|
@Autowired
|
31
|
private MailLibrary mailLibrary;
|
32
|
|
33
|
@Autowired
|
34
|
private CascadingPropertyLoader pLoader;
|
35
|
|
36
|
@Value("${services.repo-manager.baseUrl}")
|
37
|
private String baseUrl;
|
38
|
|
39
|
@Value("${services.repo-manager.adminEmail}")
|
40
|
private String adminEmail;
|
41
|
|
42
|
@Override
|
43
|
public void sendActivationEmail(UserProfile user, String activationId) throws Exception {
|
44
|
try {
|
45
|
|
46
|
this.sendMail(user.getEmail(), getEmailProperty("user.registration.mail.subject"), "Dear " + user.getFirstname() + " " + user.getLastname() + ",\n" + getEmailProperty("user.registration.mail.message") + ": " + this.baseUrl + "?activationId=" + activationId + "#activateAccount", false, null);
|
47
|
|
48
|
} catch (Exception e) {
|
49
|
LOGGER.error("Error while sending activation email to user: " + user.getEmail(), e);
|
50
|
throw e;
|
51
|
}
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
public void sendResetPasswordEmail(String email, String securityCode) throws Exception {
|
56
|
try {
|
57
|
|
58
|
this.sendMail(email, getEmailProperty("user.forgotPassword.mail.Subject"), getEmailProperty("user.forgotPassword.mail.Body1") + ": " + this.baseUrl + "?securityCode=" + securityCode + "#resetPassword" + "\n\n" + getEmailProperty("user.forgotPassword.mail.Body2") + ": " + securityCode, false, null);
|
59
|
|
60
|
} catch (Exception e) {
|
61
|
LOGGER.error("Error while sending activation email to user: " + email, e);
|
62
|
throw e;
|
63
|
}
|
64
|
}
|
65
|
|
66
|
|
67
|
@Override
|
68
|
public void reportException(Exception exception) {
|
69
|
Writer writer = new StringWriter();
|
70
|
PrintWriter printWriter = new PrintWriter(writer);
|
71
|
exception.printStackTrace(printWriter);
|
72
|
|
73
|
List<String> recipients = new ArrayList<String>();
|
74
|
|
75
|
try {
|
76
|
recipients.add(this.adminEmail);
|
77
|
String message = "An exception has occurred:\n"+writer.toString();
|
78
|
String subject = "Automatic Bug Report";
|
79
|
this.sendMail(recipients, subject, message, false, null);
|
80
|
} catch (Exception e) {
|
81
|
LOGGER.error("Error sending error report", e);
|
82
|
}
|
83
|
}
|
84
|
|
85
|
private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
|
86
|
ArrayList<String> to = new ArrayList<String>();
|
87
|
to.add(email);
|
88
|
this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
|
89
|
}
|
90
|
|
91
|
private void sendMail(List<String> recipients, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
|
92
|
|
93
|
/* try {
|
94
|
if (sendToSpecial) {
|
95
|
recipients.addAll(this.specialRecipients);
|
96
|
}
|
97
|
|
98
|
if (repoAdminMails != null)
|
99
|
recipients.addAll(repoAdminMails);
|
100
|
|
101
|
if (this.override) {
|
102
|
recipients.clear();
|
103
|
recipients.add(overrideEmail);
|
104
|
}
|
105
|
if (!logonly)
|
106
|
mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
|
107
|
LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
|
108
|
} catch (Exception e) {
|
109
|
LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
|
110
|
throw new Exception(e);
|
111
|
}*/
|
112
|
}
|
113
|
|
114
|
private String getEmailProperty(String key) {
|
115
|
return pLoader.getProperties().getProperty(key);
|
116
|
}
|
117
|
|
118
|
public void setSpecialRecipients(String specialRecipients) {
|
119
|
String[] recps = specialRecipients.split(",");
|
120
|
|
121
|
for (String recp : recps) {
|
122
|
recp = recp.trim();
|
123
|
|
124
|
this.specialRecipients.add(recp);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
|
129
|
public void setOverride(boolean override) {
|
130
|
this.override = override;
|
131
|
}
|
132
|
|
133
|
public void setOverrideEmail(String overrideEmail) {
|
134
|
this.overrideEmail = overrideEmail;
|
135
|
}
|
136
|
|
137
|
public String getFrom() {
|
138
|
return from;
|
139
|
}
|
140
|
|
141
|
public void setFrom(String from) {
|
142
|
this.from = from;
|
143
|
}
|
144
|
|
145
|
public boolean isLogonly() {
|
146
|
return logonly;
|
147
|
}
|
148
|
|
149
|
public void setLogonly(boolean logonly) {
|
150
|
this.logonly = logonly;
|
151
|
}
|
152
|
|
153
|
|
154
|
}
|