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.vitam.common;
22  
23  import com.fasterxml.jackson.annotation.JsonGetter;
24  import com.fasterxml.jackson.annotation.JsonIgnore;
25  import com.fasterxml.jackson.annotation.JsonProperty;
26  import com.fasterxml.jackson.annotation.JsonSetter;
27  import fr.gouv.vitam.common.StringUtils;
28  import fr.gouv.vitam.common.client.VitamContext;
29  import fr.gouv.vitam.common.exception.InvalidParseOperationException;
30  import fr.gouv.vitam.common.model.ProcessState;
31  import fr.gouv.vitam.common.model.StatusCode;
32  import org.waarp.common.logging.WaarpLogger;
33  import org.waarp.common.logging.WaarpLoggerFactory;
34  import org.waarp.common.utility.ParametersChecker;
35  import org.waarp.vitam.common.WaarpCommon.TaskOption;
36  
37  /**
38   * Common part for Vitam Request
39   */
40  public abstract class AbstractVitamRequest {
41    /**
42     * Internal Logger
43     */
44    private static final WaarpLogger logger =
45        WaarpLoggerFactory.getLogger(AbstractVitamRequest.class);
46    private static final String CHECK_MESSAGE = "Check within ";
47    @JsonProperty("status")
48    protected int status;
49    @JsonProperty("path")
50    private String path;
51    @JsonProperty("tenantId")
52    private int tenantId;
53    @JsonProperty("applicationSessionId")
54    private String applicationSessionId;
55    @JsonProperty("personalCertificate")
56    private String personalCertificate;
57    @JsonProperty("accessContract")
58    private String accessContract;
59    @JsonProperty("requestId")
60    private String requestId;
61    @JsonProperty("globalExecutionState")
62    private String globalExecutionState;
63    @JsonProperty("globalExecutionStatus")
64    private String globalExecutionStatus;
65    @JsonProperty("lastTryTime")
66    private long lastTryTime;
67    @JsonProperty("jsonPath")
68    private String jsonPath;
69    @JsonProperty("waarpPartner")
70    private String waarpPartner;
71    @JsonProperty("waarpRule")
72    private String waarpRule;
73    @JsonProperty("waarpId")
74    private long waarpId;
75    @JsonProperty("waarpModel")
76    private String waarpModel;
77  
78    public AbstractVitamRequest() {
79      // Empty constructor for Json
80    }
81  
82    /**
83     * Standard constructor
84     *
85     * @param taskOption
86     *
87     * @throws InvalidParseOperationException
88     */
89    public AbstractVitamRequest(final TaskOption taskOption) {
90      this.path = taskOption.getPath();
91      this.tenantId = taskOption.getTenantId();
92      this.applicationSessionId = taskOption.getApplicationSessionId();
93      this.personalCertificate = taskOption.getPersonalCertificate();
94      this.accessContract = taskOption.getAccessContract();
95      this.waarpPartner = taskOption.getWaarpPartner();
96      this.waarpRule = taskOption.getWaarpRule();
97      this.waarpModel = taskOption.getWaarpModel();
98    }
99  
100   protected static String getCheckMessage() {
101     return CHECK_MESSAGE +
102            Thread.currentThread().getStackTrace()[2].getMethodName();
103   }
104 
105   @JsonGetter("path")
106   public String getPath() {
107     return path;
108   }
109 
110   @JsonSetter("path")
111   public AbstractVitamRequest setPath(final String path) {
112     try {
113       ParametersChecker.checkParameterDefault(getCheckMessage(), path);
114       StringUtils.checkSanityString(path);
115     } catch (InvalidParseOperationException | IllegalArgumentException e) {
116       logger.error(e);
117       throw new IllegalArgumentException(e.getMessage(), e);
118     }
119     this.path = path;
120     return this;
121   }
122 
123   @JsonGetter("tenantId")
124   public int getTenantId() {
125     return tenantId;
126   }
127 
128   @JsonSetter("tenantId")
129   public AbstractVitamRequest setTenantId(final int tenantId) {
130     if (tenantId < 0) {
131       logger.error("TenantId is negative");
132       throw new IllegalArgumentException("TenantId is negative");
133     }
134     this.tenantId = tenantId;
135     return this;
136   }
137 
138   @JsonGetter("applicationSessionId")
139   public String getApplicationSessionId() {
140     return applicationSessionId;
141   }
142 
143   @JsonSetter("applicationSessionId")
144   public AbstractVitamRequest setApplicationSessionId(
145       final String applicationSessionId) {
146     try {
147       ParametersChecker
148           .checkParameterDefault(getCheckMessage(), applicationSessionId);
149       StringUtils.checkSanityString(applicationSessionId);
150     } catch (InvalidParseOperationException | IllegalArgumentException e) {
151       logger.error(e);
152       throw new IllegalArgumentException(e.getMessage(), e);
153     }
154     this.applicationSessionId = applicationSessionId;
155     return this;
156   }
157 
158   @JsonGetter("personalCertificate")
159   public String getPersonalCertificate() {
160     return personalCertificate;
161   }
162 
163   @JsonSetter("personalCertificate")
164   public AbstractVitamRequest setPersonalCertificate(
165       final String personalCertificate) {
166     try {
167       ParametersChecker
168           .checkParameterDefault(getCheckMessage(), personalCertificate);
169       StringUtils.checkSanityString(personalCertificate);
170     } catch (InvalidParseOperationException | IllegalArgumentException e) {
171       logger.error(e);
172       throw new IllegalArgumentException(e.getMessage(), e);
173     }
174     this.personalCertificate = personalCertificate;
175     return this;
176   }
177 
178   @JsonGetter("accessContract")
179   public String getAccessContract() {
180     return accessContract;
181   }
182 
183   @JsonSetter("accessContract")
184   public AbstractVitamRequest setAccessContract(final String accessContract) {
185     try {
186       ParametersChecker
187           .checkParameterDefault(getCheckMessage(), accessContract);
188       StringUtils.checkSanityString(accessContract);
189     } catch (InvalidParseOperationException | IllegalArgumentException e) {
190       logger.error(e);
191       throw new IllegalArgumentException(e.getMessage(), e);
192     }
193     this.accessContract = accessContract;
194     return this;
195   }
196 
197   @JsonGetter("waarpPartner")
198   public String getWaarpPartner() {
199     return waarpPartner;
200   }
201 
202   @JsonSetter("waarpPartner")
203   public AbstractVitamRequest setWaarpPartner(final String waarpPartner) {
204     try {
205       ParametersChecker.checkParameterDefault(getCheckMessage(), waarpPartner);
206       StringUtils.checkSanityString(waarpPartner);
207     } catch (InvalidParseOperationException | IllegalArgumentException e) {
208       logger.error(e);
209       throw new IllegalArgumentException(e.getMessage(), e);
210     }
211     this.waarpPartner = waarpPartner;
212     return this;
213   }
214 
215   @JsonGetter("waarpRule")
216   public String getWaarpRule() {
217     return waarpRule;
218   }
219 
220   @JsonSetter("waarpRule")
221   public AbstractVitamRequest setWaarpRule(final String waarpRule) {
222     try {
223       ParametersChecker.checkParameterDefault(getCheckMessage(), waarpRule);
224       StringUtils.checkSanityString(waarpRule);
225     } catch (InvalidParseOperationException | IllegalArgumentException e) {
226       logger.error(e);
227       throw new IllegalArgumentException(e.getMessage(), e);
228     }
229     this.waarpRule = waarpRule;
230     return this;
231   }
232 
233   @JsonGetter("waarpId")
234   public long getWaarpId() {
235     return waarpId;
236   }
237 
238   @JsonSetter("waarpId")
239   public AbstractVitamRequest setWaarpId(final long waarpId) {
240     this.waarpId = waarpId;
241     return this;
242   }
243 
244   @JsonGetter("waarpModel")
245   public String getWaarpModel() {
246     return waarpModel;
247   }
248 
249   @JsonSetter("waarpModel")
250   public AbstractVitamRequest setWaarpModel(final String waarpModel) {
251     this.waarpModel = waarpModel;
252     return this;
253   }
254 
255   @JsonGetter("requestId")
256   public String getRequestId() {
257     return requestId;
258   }
259 
260   @JsonSetter("requestId")
261   public AbstractVitamRequest setRequestId(final String requestId) {
262     try {
263       ParametersChecker.checkParameterDefault(getCheckMessage(), requestId);
264       StringUtils.checkSanityString(requestId);
265     } catch (InvalidParseOperationException | IllegalArgumentException e) {
266       logger.error(e);
267       throw new IllegalArgumentException(e.getMessage(), e);
268     }
269     this.requestId = requestId;
270     return this;
271   }
272 
273   @JsonGetter("globalExecutionState")
274   public String getGlobalExecutionState() {
275     return globalExecutionState;
276   }
277 
278   @JsonSetter("globalExecutionState")
279   public AbstractVitamRequest setGlobalExecutionState(
280       final String globalExecutionState) {
281     try {
282       ParametersChecker
283           .checkParameterDefault(getCheckMessage(), globalExecutionState);
284       StringUtils.checkSanityString(globalExecutionState);
285     } catch (InvalidParseOperationException | IllegalArgumentException e) {
286       logger.error(e);
287       throw new IllegalArgumentException(e.getMessage(), e);
288     }
289     this.globalExecutionState = globalExecutionState;
290     return this;
291   }
292 
293   @JsonGetter("globalExecutionStatus")
294   public String getGlobalExecutionStatus() {
295     return globalExecutionStatus;
296   }
297 
298   @JsonSetter("globalExecutionStatus")
299   public AbstractVitamRequest setGlobalExecutionStatus(
300       final String globalExecutionStatus) {
301     try {
302       ParametersChecker
303           .checkParameterDefault(getCheckMessage(), globalExecutionStatus);
304       StringUtils.checkSanityString(globalExecutionStatus);
305     } catch (InvalidParseOperationException | IllegalArgumentException e) {
306       logger.error(e);
307       throw new IllegalArgumentException(e.getMessage(), e);
308     }
309     this.globalExecutionStatus = globalExecutionStatus;
310     return this;
311   }
312 
313   @JsonGetter("lastTryTime")
314   public long getLastTryTime() {
315     return lastTryTime;
316   }
317 
318   @JsonSetter("lastTryTime")
319   public AbstractVitamRequest setLastTryTime(final long lastTryTime) {
320     this.lastTryTime = lastTryTime;
321     return this;
322   }
323 
324   @JsonGetter("status")
325   public int getStatus() {
326     return status;
327   }
328 
329   /**
330    * Set the status AND the step according to the value of the status (if
331    * less than 0, it is a step value, not a final status), but in dry mode
332    * (no check, used by Json deserialization)
333    *
334    * @param status
335    *
336    * @return this
337    */
338   @JsonSetter("status")
339   public abstract AbstractVitamRequest setStatus(final int status);
340 
341   @JsonGetter("jsonPath")
342   public String getJsonPath() {
343     return jsonPath;
344   }
345 
346   @JsonSetter("jsonPath")
347   public AbstractVitamRequest setJsonPath(final String jsonPath) {
348     try {
349       ParametersChecker.checkParameterDefault(getCheckMessage(), jsonPath);
350       StringUtils.checkSanityString(jsonPath);
351     } catch (InvalidParseOperationException | IllegalArgumentException e) {
352       logger.error(e);
353       throw new IllegalArgumentException(e.getMessage(), e);
354     }
355     this.jsonPath = jsonPath;
356     return this;
357   }
358 
359   /**
360    * @return the VitamContext according to this
361    */
362   @JsonIgnore
363   public VitamContext getVitamContext() {
364     return new VitamContext(tenantId)
365         .setApplicationSessionId(applicationSessionId)
366         .setPersonalCertificate(personalCertificate)
367         .setAccessContract(accessContract);
368   }
369 
370   @JsonIgnore
371   public ProcessState getProcessState() {
372     try {
373       ParametersChecker
374           .checkParameterDefault(getCheckMessage(), globalExecutionState);
375       StringUtils.checkSanityString(globalExecutionState);
376     } catch (InvalidParseOperationException | IllegalArgumentException e) {
377       logger.error(e);
378       return null;
379     }
380     try {
381       final ProcessState processState =
382           ProcessState.valueOf(globalExecutionState);
383       if (processState != ProcessState.PAUSE &&
384           processState != ProcessState.COMPLETED) {
385         // Error
386         logger.warn("Not PAUSE not COMPLETED: {}", globalExecutionState);
387       }
388       return processState;
389     } catch (IllegalArgumentException ignored) {
390       // Error
391       logger.error("Not PAUSE: {}", globalExecutionState);
392     }
393     return null;
394   }
395 
396   @JsonIgnore
397   public StatusCode getStatusCode() {
398     try {
399       ParametersChecker
400           .checkParameterDefault(getCheckMessage(), globalExecutionStatus);
401       StringUtils.checkSanityString(globalExecutionStatus);
402     } catch (InvalidParseOperationException | IllegalArgumentException e) {
403       logger.error(e);
404       return null;
405     }
406     try {
407       return StatusCode.valueOf(globalExecutionStatus);
408     } catch (IllegalArgumentException ignored) {
409       // Error
410       logger.error("Not UNKNOWN: {}", globalExecutionStatus);
411     }
412     return null;
413   }
414 
415 }