View Javadoc

1   /*
2    * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 3.0 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17   * MA 02110-1301  USA
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   * Provides a check on the availability of each published landing page publisher
44   *
45   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
46   */
47  @Requirement(reqId = Requirement.DOI_DISPO_020, reqName = Requirement.DOI_DISPO_020_NAME)
48  public class LandingPageMonitoring implements Runnable {
49  
50      /**
51       * Logger.
52       */
53      private static final Logger LOG = LogManager.getLogger(LandingPageMonitoring.class.getName());
54      
55      /**
56       * Client MDS to get all DOIs.
57       */
58      private final ClientMDS client;
59  
60      public LandingPageMonitoring(final ClientMDS client) {
61          this.client = client;
62      }
63      
64      /**
65       * run.
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                  // build error index by projectID and a global message
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                 // build message by project
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      * Build detail message for a record
124      *
125      * @param record record
126      * @return the message
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      * Extract projectID from the DOI
139      *
140      * @param doi doi
141      * @return project ID
142      */
143     private String extractProjIdFrom(final String doi) {
144         // parse project suffix from doi
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      * Send message by email to project members.
154      *
155      * @param doiSuffix project ID
156      * @param subject email subject
157      * @param body email body
158      * @param email email settings
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 }