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.util.logging.Level;
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.restlet.Client;
26  import org.restlet.Context;
27  import org.restlet.data.Parameter;
28  import org.restlet.data.Protocol;
29  import org.restlet.resource.ClientResource;
30  import org.restlet.util.Series;
31  
32  /**
33   * Base client
34   *
35   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
36   */
37  public class BaseClient {
38  
39      /**
40       * Port of the Datacite mockserver
41       */
42      public static final int DATACITE_MOCKSERVER_PORT = 1080;
43  
44      /**
45       * Logger.
46       */
47      private static final Logger LOG = LogManager.getLogger(BaseClient.class.getName());
48  
49      /**
50       * Number of retry when an error happens.
51       */
52      private static final int NB_RETRY = 10;
53  
54      /**
55       * Delay between two {@link #NB_RETRY} in ms.
56       */
57      private static final int NB_DELAY = 1000;
58  
59      /**
60       * Client, which executes request.
61       */
62      private volatile ClientResource client;
63  
64      /**
65       * Constructor.
66       *
67       * @param uri URI of the client's end point
68       */
69      public BaseClient(final String uri) {
70          this.client = new ClientResource(uri);
71          this.client.getLogger().setLevel(Level.OFF);
72          this.client.setLoggable(false);
73          this.client.setRetryOnError(true);
74          this.client.setRetryAttempts(NB_RETRY);
75          this.client.setRetryDelay(NB_DELAY);
76          final Client configureClient = new Client(new Context(), Protocol.HTTPS);
77          final Series<Parameter> parameters = configureClient.getContext().getParameters();
78          parameters.add(HttpClient.MAX_RETRY, String.valueOf(this.client.getRetryAttempts()));
79          parameters.add(HttpClient.RETRY_DELAY, String.valueOf(this.client.getRetryDelay()));
80          this.client.setNext(configureClient);
81      }
82  
83      /**
84       * Returns the client.
85       *
86       * @return the client
87       */
88      public final ClientResource getClient() {
89          return client;
90      }
91  
92      /**
93       * Returns the logger.
94       *
95       * @return the logger
96       */
97      public Logger getLog() {
98          return LOG;
99      }
100 
101 }