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
    @Value("${services.provide.adminEmail}")
48
    private String provideAdminEmail;
49

    
50

    
51
    @PostConstruct
52
    public void init(){
53
        System.out.println("url -> " + this.baseUrl);
54
    }
55

    
56

    
57
    @Override
58
    public void reportException(Exception exception) {
59
        Writer writer = new StringWriter();
60
        PrintWriter printWriter = new PrintWriter(writer);
61
        exception.printStackTrace(printWriter);
62

    
63
        List<String> recipients = new ArrayList<String>();
64

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

    
75
    @Override
76
    public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
77

    
78
        try {
79
            String subject = "[OpenAIRE-Usage Statistics] New request to enable usage statistics";
80

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

    
96
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
97

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

    
104
    @Override
105
    public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
106

    
107
        try {
108
            String subject = "[OpenAIRE-Usage Statistics] Your request to enable usage statistics";
109

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

    
133
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
134

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

    
141
    @Override
142
    public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
143

    
144
        try {
145
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
146

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

    
160
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
161

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

    
168
    @Override
169
    public void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
170

    
171
        try {
172
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
173

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

    
188
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
189

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

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

    
202
            String message = "Dear administrator" + ",\n" +
203
                    "\n" +
204
                    "We received a request to register the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]" +
205
                    " to the OpenAIRE compliant list of content providers. " +
206
                    "A validation process against the OpenAIRE guidelines compatibility " +
207
                    "has been started. You will be informed in another message once the process is finished." +
208
                    "\n\n" +
209
                    "Please do not reply to this message\n" +
210
                    "This message has been generated automatically.\n\n" +
211
                    "Regards,\n" +
212
                    "the OpenAIRE technical team\n";
213

    
214
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
215

    
216
        } catch (Exception e) {
217
            LOGGER.error("Error while sending registration notification email to the administrator", e);
218
            throw e;
219
        }
220
    }
221

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

    
228
            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
229
                    "\n" +
230
                    "We received a request to register the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]" +
231
                    " to the OpenAIRE compliant list of content providers. " +
232
                    "A validation process against the OpenAIRE guidelines compatibility " +
233
                    "has been started. You will be informed in another message once the process is finished." +
234
                    "\n\n" +
235
                    "Please do not reply to this message\n" +
236
                    "This message has been generated automatically.\n\n" +
237
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
238
                    "Regards,\n" +
239
                    "the OpenAIRE technical team\n";
240

    
241
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
242

    
243
        } catch (Exception e) {
244
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
245
            throw e;
246
        }
247
    }
248

    
249
    @Override
250
    public void sendAdminUpdateRepositoryEmail(Repository repository, Authentication authentication) throws Exception {
251
        try {
252
            String subject = "OpenAIRE content provider update request started for " +
253
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
254

    
255
            String message = "Dear administrator" + ",\n" +
256
                    "\n" +
257
                    "We received a request to update the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]." +
258
                    "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
259
                    "Please do not reply to this message\n" +
260
                    "This message has been generated automatically.\n\n" +
261
                    "Regards,\n" +
262
                    "the OpenAIRE technical team\n";
263

    
264
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
265

    
266
        } catch (Exception e) {
267
            LOGGER.error("Error while sending registration notification email to the administrator", e);
268
            throw e;
269
        }
270
    }
271

    
272
    @Override
273
    public void sendUserUpdateRepositoryEmail(Repository repository, Authentication authentication) throws Exception {
274
        try {
275
            String subject = "OpenAIRE content provider update request started for " +
276
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
277

    
278
            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
279
                    "\n" +
280
                    "We received a request to update the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]." +
281
                    "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
282
                    "Please do not reply to this message\n" +
283
                    "This message has been generated automatically.\n\n" +
284
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
285
                    "Regards,\n" +
286
                    "the OpenAIRE technical team\n";
287

    
288
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
289

    
290
        } catch (Exception e) {
291
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
292
            throw e;
293
        }
294
    }
295

    
296
    @Override
297
    public void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception {
298
        try {
299
            String subject = "OpenAIRE validator - Test submission ";
300

    
301
            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
302
                    "\n" +
303
                    "The validation request you have submitted has started.\n" +
304
                    "Please do not reply to this message.\n" +
305
                    "This message has been generated automatically.\n" +
306
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
307
                    "Regards,\n" +
308
                    "the OpenAIRE technical team\n";
309

    
310
            this.sendMail(jobForValidation.getUserEmail(), subject, message, false, null);
311

    
312
        } catch (Exception e) {
313
            LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e);
314
            throw e;
315
        }
316
    }
317

    
318
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
319
        ArrayList<String> to = new ArrayList<String>();
320
        to.add(email);
321
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
322
    }
323

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

    
326
        try {
327
            if (sendToSpecial) {
328
                recipients.addAll(this.specialRecipients);
329
            }
330

    
331
            if (repoAdminMails != null)
332
                recipients.addAll(repoAdminMails);
333

    
334
            if (this.override) {
335
                recipients.clear();
336
                recipients.add(overrideEmail);
337
            }
338
            if (!logonly)
339
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
340
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
341
        } catch (Exception e) {
342
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
343
            throw new Exception(e);
344
        }
345
    }
346

    
347
    private String getEmailProperty(String key) {
348
        return pLoader.getProperties().getProperty(key);
349
    }
350

    
351
    public void setSpecialRecipients(String specialRecipients) {
352
        String[] recps = specialRecipients.split(",");
353

    
354
        for (String recp : recps) {
355
            recp = recp.trim();
356

    
357
            this.specialRecipients.add(recp);
358
        }
359
    }
360

    
361

    
362
    public void setOverride(boolean override) {
363
        this.override = override;
364
    }
365

    
366
    public void setOverrideEmail(String overrideEmail) {
367
        this.overrideEmail = overrideEmail;
368
    }
369

    
370
    public String getFrom() {
371
        return from;
372
    }
373

    
374
    public void setFrom(String from) {
375
        this.from = from;
376
    }
377

    
378
    public boolean isLogonly() {
379
        return logonly;
380
    }
381

    
382
    public void setLogonly(boolean logonly) {
383
        this.logonly = logonly;
384
    }
385

    
386

    
387
}
(6-6/20)