Project

General

Profile

1
package eu.dnetlib.repo.manager.service;
2

    
3
import eu.dnetlib.domain.data.PiwikInfo;
4
import eu.dnetlib.domain.data.Repository;
5
import eu.dnetlib.domain.functionality.validator.JobForValidation;
6
import eu.dnetlib.repo.manager.config.CascadingPropertyLoader;
7
import eu.dnetlib.utils.MailLibrary;
8
import org.apache.log4j.Logger;
9
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Value;
12
import org.springframework.security.core.Authentication;
13
import org.springframework.stereotype.Component;
14

    
15
import javax.annotation.PostConstruct;
16
import java.io.PrintWriter;
17
import java.io.StringWriter;
18
import java.io.Writer;
19
import java.util.ArrayList;
20
import java.util.List;
21

    
22

    
23
@Component("emailUtils")
24
public class EmailUtilsImpl implements EmailUtils {
25

    
26
    private static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class);
27

    
28
    private List<String> specialRecipients = new ArrayList<String>();
29
    private boolean override = false, logonly = false;
30
    private String overrideEmail = null, from = null;
31

    
32
    @Autowired
33
    private MailLibrary mailLibrary;
34

    
35
    @Autowired
36
    private CascadingPropertyLoader pLoader;
37

    
38
    @Value("${services.repo-manager.baseUrl}")
39
    private String baseUrl;
40

    
41
    @Value("${services.repo-manager.adminEmail}")
42
    private String adminEmail;
43

    
44
    @Value("${services.repomanager.usagestats.adminEmail}")
45
    private String usageStatsAdminEmail;
46

    
47

    
48
    @PostConstruct
49
    public void init(){
50
        System.out.println("url -> " + this.baseUrl);
51
    }
52

    
53

    
54
    @Override
55
    public void reportException(Exception exception) {
56
        Writer writer = new StringWriter();
57
        PrintWriter printWriter = new PrintWriter(writer);
58
        exception.printStackTrace(printWriter);
59

    
60
        List<String> recipients = new ArrayList<String>();
61

    
62
        try {
63
            recipients.add(this.adminEmail);
64
            String message = "An exception has occurred:\n"+writer.toString();
65
            String subject = "Automatic Bug Report";
66
            this.sendMail(recipients, subject, message, false, null);
67
        } catch (Exception e) {
68
            LOGGER.error("Error sending error report", e);
69
        }
70
    }
71

    
72
    @Override
73
    public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
74

    
75
        try {
76
            String subject = "[OpenAIRE-Usage Statistics] New request to enable usage statistics";
77

    
78
            String message = "Dear administrator,\n" +
79
                    "\n" +
80
                    "we have received a request to enable the OpenAIRE usage statistics for the following repository \n" +
81
                    "\n" +
82
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
83
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
84
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
85
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
86
                    "\n" +
87
                    "For more information about this request, go here: \n" +
88
                    this.baseUrl + "/admin/metrics\n" +
89
                    "\n" +
90
                    "Best,\n" +
91
                    "The OpenAIRE team";
92

    
93
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
94

    
95
        } catch (Exception e) {
96
            LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e);
97
            throw e;
98
        }
99
    }
100

    
101
    @Override
102
    public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
103

    
104
        try {
105
            String subject = "[OpenAIRE-Usage Statistics] Your request to enable usage statistics";
106

    
107
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
108
                    "\n" +
109
                    "we have received your request to enable the OpenAIRE usage statistics for your repository\n" +
110
                    "\n" +
111
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
112
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
113
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
114
                    "\n" +
115
                    "In order to enable the usage statistics, you must install the OpenAIRE's tracking code in your repository software. " +
116
                    "OpenAIRE's usage statistics service tracking code is maintained on Github as a patch for various versions of DSpace " +
117
                    "(https://github.com/openaire/OpenAIRE-Piwik-DSpace) and as an Eprints plugin for version 3 " +
118
                    "(https://github.com/openaire/EPrints-OAPiwik). In case the platform is different from DSpace or EPrints please contact " +
119
                    "the OpenAIRE team in repositoryusagestats@openaire.eu in order to find a solution.\n" +
120
                    "\n" +
121
                    "For more information about your request and configuration details, go here: \n" +
122
                    this.baseUrl + "/getImpact/instructions/" + piwikInfo.getRepositoryId() + "\n" +
123
                    "\n" +
124
                    "Once you have finished configuring your repository or if you have any questions, please notify the OpenAIRE team by sending \n" +
125
                    "an email to repositoryusagestats@openaire.eu\n" +
126
                    "\n" +
127
                    "Best,\n" +
128
                    "The OpenAIRE team";
129

    
130
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
131

    
132
        } catch (Exception e) {
133
            LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e);
134
            throw e;
135
        }
136
    }
137

    
138
    @Override
139
    public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
140

    
141
        try {
142
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
143

    
144
            String message = "Dear administrator,\n" +
145
                    "\n" +
146
                    "The installation and configuration of OpenAIRE's tracking code for the following repository " +
147
                    "has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
148
                    "\n" +
149
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
150
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
151
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
152
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
153
                    "\n" +
154
                    "Best,\n" +
155
                    "The OpenAIRE team";
156

    
157
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
158

    
159
        } catch (Exception e) {
160
            LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e);
161
            throw e;
162
        }
163
    }
164

    
165
    @Override
166
    public void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
167

    
168
        try {
169
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
170

    
171
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
172
                    "\n" +
173
                    "The installation and configuration of OpenAIRE's tracking code for your repository \"" + piwikInfo.getRepositoryName() +
174
                    "\" has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
175
                    "\n" +
176
                    "You can preview the statistics in your repository's dashboard: \n" +
177
                    this.baseUrl + "/getImpact/" + piwikInfo.getRepositoryId() + "\n" +
178
                    "\n" +
179
                    " For more information and questions, you can contact the openaire support team by sending an email to " +
180
                    "repositoryusagestats@openaire.eu\n" +
181
                    "\n" +
182
                    "Best,\n" +
183
                    "The OpenAIRE team";
184

    
185
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
186

    
187
        } catch (Exception e) {
188
            LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e);
189
            throw e;
190
        }
191
    }
192

    
193
    @Override
194
    public void sendUserRegistrationEmail(Repository repository, Authentication authentication) throws Exception{
195
        try {
196
            String subject = "OpenAIRE content provider registration request started for " +
197
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
198

    
199
            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
200
                    "\n" +
201
                    "We received a request to register the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]" +
202
                    " to the OpenAIRE compliant list of content providers.\n " +
203
                    "A validation process against the OpenAIRE guidelines compatibility " +
204
                    "has been started. You will be informed in another message once the process is finished." +
205
                    "\n" +
206
                    "Please do not reply to this message.\n" +
207
                    "This message has been generated automatically.\n" +
208
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
209
                    "Regards,\n" +
210
                    "the OpenAIRE technical team\n";
211

    
212
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
213

    
214
        } catch (Exception e) {
215
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
216
            throw e;
217
        }
218
    }
219

    
220
    @Override
221
    public void sendUserUpdateRepositoryEmail(Repository repository, Authentication authentication) throws Exception {
222
        try {
223
            String subject = "OpenAIRE content provider update request started for " +
224
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
225

    
226
            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
227
                    "\n" +
228
                    "We received a request to update the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n" +
229
                    "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n" +
230
                    "Please do not reply to this message.\n" +
231
                    "This message has been generated automatically.\n" +
232
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
233
                    "Regards,\n" +
234
                    "the OpenAIRE technical team\n";
235

    
236
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
237

    
238
        } catch (Exception e) {
239
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
240
            throw e;
241
        }
242
    }
243

    
244
    @Override
245
    public void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception {
246
        try {
247
            String subject = "OpenAIRE validator - Test submission ";
248

    
249
            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
250
                    "\n" +
251
                    "The validation request you have submitted has started.\n" +
252
                    "Please do not reply to this message.\n" +
253
                    "This message has been generated automatically.\n" +
254
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
255
                    "Regards,\n" +
256
                    "the OpenAIRE technical team\n";
257

    
258
            this.sendMail(jobForValidation.getUserEmail(), subject, message, false, null);
259

    
260
        } catch (Exception e) {
261
            LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e);
262
            throw e;
263
        }
264
    }
265

    
266
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
267
        ArrayList<String> to = new ArrayList<String>();
268
        to.add(email);
269
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
270
    }
271

    
272
    private void sendMail(List<String> recipients, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
273

    
274
        try {
275
            if (sendToSpecial) {
276
                recipients.addAll(this.specialRecipients);
277
            }
278

    
279
            if (repoAdminMails != null)
280
                recipients.addAll(repoAdminMails);
281

    
282
            if (this.override) {
283
                recipients.clear();
284
                recipients.add(overrideEmail);
285
            }
286
            if (!logonly)
287
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
288
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
289
        } catch (Exception e) {
290
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
291
            throw new Exception(e);
292
        }
293
    }
294

    
295
    private String getEmailProperty(String key) {
296
        return pLoader.getProperties().getProperty(key);
297
    }
298

    
299
    public void setSpecialRecipients(String specialRecipients) {
300
        String[] recps = specialRecipients.split(",");
301

    
302
        for (String recp : recps) {
303
            recp = recp.trim();
304

    
305
            this.specialRecipients.add(recp);
306
        }
307
    }
308

    
309

    
310
    public void setOverride(boolean override) {
311
        this.override = override;
312
    }
313

    
314
    public void setOverrideEmail(String overrideEmail) {
315
        this.overrideEmail = overrideEmail;
316
    }
317

    
318
    public String getFrom() {
319
        return from;
320
    }
321

    
322
    public void setFrom(String from) {
323
        this.from = from;
324
    }
325

    
326
    public boolean isLogonly() {
327
        return logonly;
328
    }
329

    
330
    public void setLogonly(boolean logonly) {
331
        this.logonly = logonly;
332
    }
333

    
334

    
335
}
(4-4/19)