View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.protocol.http.restv2;
22  
23  import io.netty.util.AsciiString;
24  import org.waarp.common.database.ConnectionFactory;
25  import org.waarp.common.database.exception.WaarpDatabaseException;
26  import org.waarp.common.logging.SysErrLogger;
27  import org.waarp.common.utility.WaarpStringUtils;
28  import org.waarp.openr66.dao.DAOFactory;
29  import org.waarp.openr66.protocol.configuration.Configuration;
30  import org.waarp.openr66.protocol.http.restv2.dbhandlers.ServerHandler;
31  import org.waarp.openr66.protocol.http.restv2.dbhandlers.TransferIdHandler;
32  
33  import java.nio.charset.Charset;
34  
35  /**
36   * A list of all constants of the RESTv2 API.
37   * <p>
38   * This includes the URI of all the entry points, the name of HTTP headers
39   * specific to the API, the name of
40   * the JSON objects' fields, and a {@link DAOFactory} to create DAOs for access
41   * to the database.
42   */
43  public final class RestConstants {
44  
45    static {
46      try {
47        DAOFactory.initialize(ConnectionFactory.getInstance());
48      } catch (final Throwable ignored) {//NOSONAR
49        SysErrLogger.FAKE_LOGGER //NOSONAR
50                                 .syserr("Error during static execution",//NOSONAR
51                                         ignored);//NOSONAR
52      }
53      DAO_FACTORY = DAOFactory.getInstance();
54    }
55  
56    /**
57     * Makes the default constructor of this utility class inaccessible.
58     */
59    private RestConstants() throws InstantiationException {
60      throw new InstantiationException(
61          getClass().getName() + " cannot be instantiated.");
62    }
63  
64    // ########################## SERVER CONSTANTS ##############################
65  
66    /**
67     * The name of this R66 server instance.
68     *
69     * @return The name of this R66 Server
70     */
71    public static String serverName() {
72      return Configuration.configuration.getHostId();
73    }
74  
75    /**
76     * The name of this R66 server instance according to SSL or not of remote one.
77     *
78     * @param requested
79     *
80     * @return the name of this R66 server according to SSL
81     */
82    public static String serverName(final String requested) {
83      String requester = serverName();
84      try {
85        requester = Configuration.configuration.getHostId(requested);
86      } catch (final WaarpDatabaseException e) {
87        // Ignore !!
88      }
89      return requester;
90    }
91  
92    /**
93     * The DAO_FACTORY to generate connections to the underlying database.
94     */
95    public static final DAOFactory DAO_FACTORY;
96  
97    /**
98     * The UTF-8 {@link Charset} constant.
99     */
100   public static final Charset UTF8_CHARSET = WaarpStringUtils.UTF8;
101 
102   // ######################### HTTP HEADER NAMES ##############################
103 
104   /**
105    * Name of the HTTP header used to store the user who made a request.
106    */
107   public static final AsciiString AUTH_USER = AsciiString.cached("X-Auth-User");
108 
109   /**
110    * Name of the HTTP header used to store the timestamp of the request.
111    */
112   public static final AsciiString AUTH_TIMESTAMP =
113       AsciiString.cached("X-Auth-Timestamp");
114 
115   /**
116    * Name of the HTTP header used to store the signature key of a request.
117    */
118   public static final AsciiString AUTH_SIGNATURE =
119       AsciiString.cached("X-Auth-Signature");
120 
121   // ########################## ENTRY POINTS URI ##############################
122 
123   /**
124    * Root directory of the API.
125    */
126   public static final String VERSION_PREFIX = "/v2/";
127 
128   /**
129    * Name of the URI parameter containing the id of an entry in a collection.
130    */
131   public static final String URI_ID = "id";
132 
133   /**
134    * Regex corresponding to the id URI parameter of an entry in a collection.
135    */
136   private static final String ID_PARAMETER = "/{" + URI_ID + '}';
137 
138   /**
139    * Access point of the transfers collection.
140    */
141   public static final String FILE_MONITOR_HANDLER_URI =
142       VERSION_PREFIX + "filemonitors";
143 
144   /**
145    * Access point of the transfers collection.
146    */
147   public static final String TRANSFERS_HANDLER_URI =
148       VERSION_PREFIX + "transfers/";
149 
150   /**
151    * Access point of a single transfer entry.
152    */
153   public static final String TRANSFER_ID_HANDLER_URI =
154       TRANSFERS_HANDLER_URI + ID_PARAMETER;
155 
156   /**
157    * Access point of the server commands.
158    */
159   public static final String SERVER_HANDLER_URI = VERSION_PREFIX + "server/";
160 
161   /**
162    * Access point of the transfer rules collection.
163    */
164   public static final String RULES_HANDLER_URI = VERSION_PREFIX + "rules/";
165 
166   /**
167    * Access point of a single transfer rules entry.
168    */
169   public static final String RULE_ID_HANDLER_URI =
170       RULES_HANDLER_URI + ID_PARAMETER;
171 
172   /**
173    * Access point of the bandwidth limits.
174    */
175   public static final String LIMITS_HANDLER_URI = VERSION_PREFIX + "limits/";
176 
177   /**
178    * Access point of the hosts collection.
179    */
180   public static final String HOSTS_HANDLER_URI = VERSION_PREFIX + "hosts/";
181 
182   /**
183    * Access point of a single host entry.
184    */
185   public static final String HOST_ID_HANDLER_URI =
186       HOSTS_HANDLER_URI + ID_PARAMETER;
187 
188   /**
189    * Access point of the host configuration.
190    */
191   public static final String CONFIG_HANDLER_URI =
192       VERSION_PREFIX + "hostconfig/";
193 
194   /**
195    * The names of all the sub-paths of the {@link ServerHandler} corresponding
196    * to the server commands.
197    */
198   public static final class ServerCommandsURI {
199     public static final String STATUS_URI = "status";
200     public static final String DEACTIVATE_URI = "deactivate";
201     public static final String SHUTDOWN_URI = "shutdown";
202     public static final String RESTART_URI = "restart";
203     public static final String LOGS_URI = "logs";
204     public static final String CONFIG_URI = "config";
205 
206     private ServerCommandsURI() {
207     }
208   }
209 
210   /**
211    * The names of the sub-paths of the {@link TransferIdHandler} corresponding
212    * to the transfer commands.
213    */
214   public static final class TransferCommandsURI {
215     public static final String RESTART_URI = "restart";
216     public static final String STOP_URI = "stop";
217     public static final String CANCEL_URI = "cancel";
218 
219     private TransferCommandsURI() {
220     }
221   }
222 
223   // ######################### JSON FIELDS NAMES ##############################
224 
225   /**
226    * The names of the fields of a HostConfig JSON object.
227    */
228   public static final class HostConfigFields {
229     public static final String BUSINESS = "business";
230     public static final String ROLES = "roles";
231     public static final String ALIASES = "aliases";
232     public static final String OTHERS = "others";
233     public static final String HOST_NAME = "hostName";
234     public static final String ROLE_LIST = "roleList";
235     public static final String ALIAS_LIST = "aliasList";
236 
237     private HostConfigFields() {
238     }
239   }
240 
241   /**
242    * The names of the fields of a Host JSON object.
243    */
244   public static final class HostFields {
245     public static final String HOST_NAME = "name";
246     public static final String ADDRESS = "address";
247     public static final String PORT = "port";
248     public static final String PASSWORD = "password";//NOSONAR
249     public static final String IS_SSL = "isSSL";
250     public static final String IS_CLIENT = "isClient";
251     public static final String IS_ADMIN = "isAdmin";
252     public static final String IS_ACTIVE = "isActive";
253     public static final String IS_PROXY = "isProxy";
254 
255     private HostFields() {
256     }
257   }
258 
259   /**
260    * The names of the fields of a Limits JSON object.
261    */
262   public static final class LimitsFields {
263     public static final String WRITE_GLOBAL_LIMIT = "upGlobalLimit";
264     public static final String READ_GLOBAL_LIMIT = "downGlobalLimit";
265     public static final String WRITE_SESSION_LIMIT = "upSessionLimit";
266     public static final String READ_SESSION_LIMIT = "downSessionLimit";
267     public static final String DELAY_LIMIT = "delayLimit";
268 
269     private LimitsFields() {
270     }
271   }
272 
273   /**
274    * The names of the fields of a Rule JSON object.
275    */
276   public static final class RuleFields {
277     public static final String RULE_NAME = "name";
278     public static final String HOST_IDS = "hostIds";
279     public static final String MODE_TRANS = "mode";
280     public static final String RECV_PATH = "recvPath";
281     public static final String SEND_PATH = "sendPath";
282     public static final String ARCHIVE_PATH = "archivePath";
283     public static final String WORK_PATH = "workPath";
284     public static final String R_PRE_TASKS = "rPreTasks";
285     public static final String R_POST_TASKS = "rPostTasks";
286     public static final String R_ERROR_TASKS = "sPreTasks";
287     public static final String S_PRE_TASKS = "rPreTasks";
288     public static final String S_POST_TASKS = "rPostTasks";
289     public static final String S_ERROR_TASKS = "sErrorTasks";
290     public static final String TASK_TYPE = "type";
291     public static final String TASK_ARGUMENTS = "arguments";
292     public static final String TASK_DELAY = "delay";
293 
294     private RuleFields() {
295     }
296   }
297 
298   /**
299    * The names of the fields of a Transfer JSON object.
300    */
301   public static final class TransferFields {
302     public static final String TRANSFER_ID = "id";
303     public static final String GLOBAL_STEP = "globalStep";
304     public static final String GLOBAL_LAST_STEP = "globalLastStep";
305     public static final String STEP = "step";
306     public static final String RANK = "rank";
307     public static final String UPDATED_INFO = "status";
308     public static final String STEP_STATUS = "stepStatus";
309     public static final String ORIGINAL_FILENAME = "originalFilename";
310     public static final String FILENAME = "filename";
311     public static final String RULE = "ruleName";
312     public static final String BLOCK_SIZE = "blockSize";
313     public static final String FILE_INFO = "fileInfo";
314     public static final String TRANSFER_INFO = "transferInfo";
315     public static final String START = "start";
316     public static final String STOP = "stop";
317     public static final String REQUESTED = "requested";
318     public static final String REQUESTER = "requester";
319     public static final String RETRIEVE = "retrieve";
320     public static final String ERROR_CODE = "errorCode";
321     public static final String ERROR_MESSAGE = "errorMessage";
322 
323     private TransferFields() {
324     }
325   }
326 
327   // ######################### QUERY PARAM NAMES ##############################
328 
329   /**
330    * The names of the query parameters of the {@code GET} method on the host
331    * collection entry point.
332    */
333   public static final class GetHostsParams {
334     public static final String LIMIT = "limit";
335     public static final String OFFSET = "offset";
336     public static final String ORDER = "order";
337     public static final String ADDRESS = "address";
338     public static final String IS_SSL = "isSSL";
339     public static final String IS_ACTIVE = "isActive";
340     public static final String COUNT_ORDER = "countOrder";
341 
342     private GetHostsParams() {
343     }
344   }
345 
346   /**
347    * The names of the query parameters of the {@code GET} method on the rule
348    * collection entry point.
349    */
350   public static final class GetRulesParams {
351     public static final String LIMIT = "limit";
352     public static final String OFFSET = "offset";
353     public static final String ORDER = "order";
354     public static final String MODE_TRANS = "modeTrans";
355     public static final String COUNT_ORDER = "countOrder";
356 
357     private GetRulesParams() {
358     }
359   }
360 
361   /**
362    * The names of the query parameters of the {@code GET} method on the server
363    * status entry point.
364    */
365   public static final class GetStatusParams {
366     public static final String PERIOD = "period";
367 
368     private GetStatusParams() {
369     }
370   }
371 
372   /**
373    * The names of the query parameters of the {@code GET} method on the
374    * transfer logs entry point.
375    */
376   public static final class GetLogsParams {
377     public static final String PURGE = "purge";
378     public static final String CLEAN = "clean";
379     public static final String STATUS = "status";
380     public static final String RULE_NAME = "ruleName";
381     public static final String START = "start";
382     public static final String STOP = "stop";
383     public static final String START_ID = "startID";
384     public static final String STOP_ID = "stopID";
385     public static final String REQUESTED = "requester";
386 
387     private GetLogsParams() {
388     }
389   }
390 
391   /**
392    * The names of the query parameters of the {@code GET} method on the server
393    * configuration entry point.
394    */
395   public static final class ExportConfigParams {
396     public static final String EXPORT_HOSTS = "exportHosts";
397     public static final String EXPORT_RULES = "exportRules";
398     public static final String EXPORT_BUSINESS = "exportBusiness";
399     public static final String EXPORT_ALIASES = "exportAliases";
400     public static final String EXPORT_ROLES = "exportRoles";
401 
402     private ExportConfigParams() {
403     }
404   }
405 
406   /**
407    * The names of the query parameters of the {@code PUT} method on the server
408    * configuration entry point.
409    */
410   public static final class ImportConfigParams {
411     public static final String PURGE_HOST = "purgeHosts";
412     public static final String PURGE_RULE = "purgeRules";
413     public static final String PURGE_BUSINESS = "purgeBusiness";
414     public static final String PURGE_ALIASES = "purgeAliases";
415     public static final String PURGE_ROLES = "purgeRoles";
416     public static final String HOST_FILE = "hostsFile";
417     public static final String RULE_FILE = "rulesFile";
418     public static final String BUSINESS_FILE = "businessFile";
419     public static final String ALIAS_FILE = "aliasesFile";
420     public static final String ROLE_FILE = "rolesFile";
421 
422     private ImportConfigParams() {
423     }
424   }
425 
426   /**
427    * The names of the query parameters of the {@code GET} method on the
428    * transfer collection entry point.
429    */
430   public static final class GetTransfersParams {
431     public static final String LIMIT = "limit";
432     public static final String OFFSET = "offset";
433     public static final String ORDER = "order";
434     public static final String RULE_ID = "ruleID";
435     public static final String PARTNER = "partner";
436     public static final String STATUS = "status";
437     public static final String FILENAME = "filename";
438     public static final String START_TRANS = "startTrans";
439     public static final String STOP_TRANS = "stopTrans";
440     public static final String FOLLOW_ID = "followId";
441     public static final String COUNT_ORDER = "countOrder";
442 
443     private GetTransfersParams() {
444     }
445   }
446 }