1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.services;
20
21 import fr.cnes.doi.client.ClientLandingPage;
22 import fr.cnes.doi.client.ClientMDS;
23 import fr.cnes.doi.db.AbstractProjectSuffixDBHelper;
24 import fr.cnes.doi.db.model.DOIUser;
25 import fr.cnes.doi.exception.DOIDbException;
26 import fr.cnes.doi.plugin.PluginFactory;
27 import fr.cnes.doi.settings.EmailSettings;
28 import fr.cnes.doi.utils.spec.Requirement;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37
38 import org.apache.logging.log4j.LogManager;
39 import org.apache.logging.log4j.Logger;
40 import org.restlet.data.Status;
41
42
43
44
45
46
47 @Requirement(reqId = Requirement.DOI_DISPO_020, reqName = Requirement.DOI_DISPO_020_NAME)
48 public class LandingPageMonitoring implements Runnable {
49
50
51
52
53 private static final Logger LOG = LogManager.getLogger(LandingPageMonitoring.class.getName());
54
55
56
57
58 private final ClientMDS client;
59
60 public LandingPageMonitoring(final ClientMDS client) {
61 this.client = client;
62 }
63
64
65
66
67 @Override
68 public void run() {
69 LOG.traceEntry();
70 final EmailSettings email = EmailSettings.getInstance();
71 final StringBuffer msg = new StringBuffer();
72 try {
73 final String subject;
74 final Map<String, Map<String, Status>> doiErrors = new ConcurrentHashMap<>();
75 final List<String> response = client.getDois();
76 final ClientLandingPage clientLandingPage = new ClientLandingPage(response);
77
78 if (clientLandingPage.isSuccess()) {
79 subject = "Landing pages checked with success";
80 msg.append("All landing pages (").append(response.size()).append(") are on-line");
81 } else {
82 subject = "Landing pages checked with errors";
83
84 final StringBuffer header = new StringBuffer();
85 header.append("List of off-line landing pages:\n");
86 header.append("-------------------------------\n");
87
88 final Map<String, Status> errors = clientLandingPage.getErrors();
89 msg.append(errors.size()).append(" are off-line !!!\n");
90 msg.append(header);
91
92
93 for (final Entry<String, Status> entry : errors.entrySet()) {
94 msg.append(buildDetailMessage(entry));
95
96 final String projectID = this.extractProjIdFrom(entry.getKey());
97 if (doiErrors.containsKey(projectID)) {
98 doiErrors.get(projectID).put(entry.getKey(), entry.getValue());
99 } else {
100 doiErrors.put(projectID, new HashMap<>());
101 doiErrors.get(projectID).put(entry.getKey(), entry.getValue());
102 }
103 }
104
105 for (final Entry<String, Map<String, Status>> project : doiErrors.entrySet()) {
106 final StringBuffer body = new StringBuffer();
107 body.append(header);
108 final Map<String, Status> errorsDoi = project.getValue();
109 for (final Entry<String, Status> errorDoi : errorsDoi.entrySet()) {
110 body.append(buildDetailMessage(errorDoi));
111 }
112 sendMessageToMembers(project.getKey(), subject, body.toString(), email);
113 }
114 }
115 email.sendMessage(subject, msg.toString());
116 } catch (Exception ex) {
117 email.sendMessage("Unrecoverable errors when checking landing pages", ex.toString(),
118 null);
119 }
120 }
121
122
123
124
125
126
127
128 private StringBuffer buildDetailMessage(final Entry<String, Status> record) {
129 final StringBuffer msg = new StringBuffer();
130 msg.append("- ").append(record.getKey())
131 .append(" (").append(record.getValue().getCode())
132 .append(" - ").append(record.getValue().getDescription()).append(" )")
133 .append("\n");
134 return msg;
135 }
136
137
138
139
140
141
142
143 private String extractProjIdFrom(final String doi) {
144
145 final String doiRegex = "^(.+)\\/(.+)\\/(.+)$";
146 final Pattern doiPattern = Pattern.compile(doiRegex);
147 final Matcher doiMatcher = doiPattern.matcher(doi);
148 doiMatcher.matches();
149 return doiMatcher.group(2);
150 }
151
152
153
154
155
156
157
158
159
160 private void sendMessageToMembers(final String doiSuffix, final String subject,
161 final String body, final EmailSettings email) {
162 final AbstractProjectSuffixDBHelper manageProjects = PluginFactory.getProjectSuffix();
163 List<DOIUser> members;
164 try {
165 members = manageProjects.getAllDOIUsersForProject(Integer.parseInt(doiSuffix));
166 } catch (DOIDbException ex) {
167 members = new ArrayList<>();
168 }
169 for (final DOIUser member : members) {
170 email.sendMessage(subject, body, member.getEmail());
171 }
172 }
173 }