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.client;
20  
21  import fr.cnes.httpclient.HttpClient;
22  import java.io.IOException;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  import org.restlet.Client;
28  import org.restlet.data.Parameter;
29  import org.restlet.data.Status;
30  import org.restlet.representation.Representation;
31  import org.restlet.resource.ResourceException;
32  import org.restlet.util.Series;
33  
34  /**
35   * Checks the status of the landing page.
36   *
37   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
38   */
39  public class ClientLandingPage extends BaseClient {
40  
41      /**
42       * DOI name resolver {@value #BASE_URI}.
43       */
44      private static final String BASE_URI = "http://doi.org";
45  
46      /**
47       * List of offline landing pages.
48       */
49      private final Map<String, Status> errors = new ConcurrentHashMap<>();
50  
51      /**
52       * Constructor
53       *
54       * @param dois List of DOIs to check
55       */
56      public ClientLandingPage(final List<String> dois) {
57          super(BASE_URI);
58          checkDoi(dois);
59      }
60  
61      /**
62       * Tests for each DOI if the landing page is online.
63       *
64       * @param dois dois to check
65       */
66      //TODO : check with Head before. If not implemented, check with get
67      private void checkDoi(final List<String> dois) {
68          this.getLog().traceEntry("Parameters\n\tdois : {}", dois);
69          this.getClient().setMaxRedirects(5);
70          this.getClient().setLoggable(true);
71          final Client configurationClient = (Client) this.getClient().getNext();
72          final Series<Parameter> parameters = configurationClient.getContext().getParameters();
73          parameters.add(HttpClient.MAX_REDIRECTION, String.
74                  valueOf(this.getClient().getMaxRedirects()));
75          this.getLog().info("{} landing pages to check.", dois.size());
76          for (final String doi : dois) {
77              this.getClient().setReference(BASE_URI);
78              this.getClient().addSegment(doi);
79              this.getLog().info("Checking landing page {}", doi);
80              try {
81                  final Representation rep = this.getClient().get();
82                  rep.exhaust();
83                  final Status status = this.getClient().getStatus();
84                  if (status.isError()) {
85                      this.errors.put(doi, status);
86                      this.getLog().error("Error for landing page {}", doi);
87                  } else {
88                      this.getLog().info("OK");
89                  }
90              } catch (ResourceException ex) {
91                  this.getLog().error("Checking landing pages", ex);
92                  this.errors.put(doi, ex.getStatus());
93              } catch (IOException ex) {
94                  this.getLog().error("Checking landing pages", ex);
95                  this.errors.put(doi, new Status(Status.SERVER_ERROR_INTERNAL, ex));
96              } finally {
97                  this.getClient().release();
98              }
99          }
100         this.getLog().traceExit();
101     }
102 
103     /**
104      * Returns true when there is no error otherwise False.
105      *
106      * @return true when there is no error otherwise False
107      */
108     public boolean isSuccess() {
109         this.getLog().traceEntry();
110         return this.getLog().traceExit(this.errors.isEmpty());
111     }
112 
113     /**
114      * Returns True when there is more than zero error otherwise False
115      *
116      * @return True when there is more than zero error otherwise False
117      */
118     public boolean isError() {
119         return !isSuccess();
120     }
121 
122     /**
123      * Returns the errors.
124      *
125      * @return the error
126      */
127     public Map<String, Status> getErrors() {
128         this.getLog().traceEntry();
129         return this.getLog().traceExit(Collections.unmodifiableMap(this.errors));
130     }
131 
132 }