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.pojo;
22  
23  import com.fasterxml.jackson.annotation.JsonIgnore;
24  import com.fasterxml.jackson.annotation.JsonProperty;
25  import com.fasterxml.jackson.annotation.JsonPropertyOrder;
26  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
27  import org.waarp.common.utility.ParametersChecker;
28  import org.waarp.openr66.configuration.BadConfigurationException;
29  import org.waarp.openr66.configuration.FileBasedConfiguration;
30  import org.waarp.openr66.protocol.configuration.Configuration;
31  import org.waarp.openr66.protocol.http.restv2.utils.XmlSerializable.Rules.Tasks;
32  
33  import javax.xml.bind.annotation.XmlAccessType;
34  import javax.xml.bind.annotation.XmlAccessorType;
35  import javax.xml.bind.annotation.XmlElement;
36  import javax.xml.bind.annotation.XmlElementDecl;
37  import javax.xml.bind.annotation.XmlElementWrapper;
38  import javax.xml.bind.annotation.XmlTransient;
39  import javax.xml.bind.annotation.XmlType;
40  import java.sql.Types;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  import static org.waarp.common.database.data.AbstractDbData.*;
45  import static org.waarp.openr66.configuration.RuleFileBasedConfiguration.*;
46  
47  /**
48   * Rule data object
49   */
50  @XmlType(name = ROOT)
51  @XmlAccessorType(XmlAccessType.FIELD)
52  @JsonPropertyOrder({
53      "IDRULE", "MODETRANS", "RECVPATH", "SENDPATH", "ARCHIVEPATH", "WORKPATH",
54      "UPDATEDINFO", "HOSTIDS", "SPRETASKS", "SPOSTASKS", "SERRORTASKS",
55      "RPRETASKS", "RPOSTTASKS", "RERRORTASKS"
56  })
57  public class Rule {
58  
59    @XmlElement(name = XIDRULE)
60    @JsonProperty("IDRULE")
61    private String name;
62  
63    @XmlElement(name = XMODE)
64    @JsonProperty("MODETRANS")
65    private int mode;
66  
67    @XmlElementWrapper(name = XHOSTIDS)
68    @XmlElement(name = XHOSTID)
69    @JsonIgnore
70    private List<String> hostids;
71  
72    @XmlElement(name = XRECVPATH, required = true)
73    @JsonProperty("RECVPATH")
74    private String recvPath;
75  
76    @XmlElement(name = XSENDPATH, required = true)
77    @JsonProperty("SENDPATH")
78    private String sendPath;
79  
80    @XmlElement(name = XARCHIVEPATH, required = true)
81    @JsonProperty("ARCHIVEPATH")
82    private String archivePath;
83  
84    @XmlElement(name = XWORKPATH, required = true)
85    @JsonProperty("WORKPATH")
86    private String workPath;
87  
88    @XmlTransient
89    @JsonIgnore
90    private List<RuleTask> rPreTasks;
91  
92    @XmlTransient
93    @JsonIgnore
94    private List<RuleTask> rPostTasks;
95  
96    @XmlTransient
97    @JsonIgnore
98    private List<RuleTask> rErrorTasks;
99  
100   @XmlTransient
101   @JsonIgnore
102   private List<RuleTask> sPreTasks;
103 
104   @XmlTransient
105   @JsonIgnore
106   private List<RuleTask> sPostTasks;
107 
108   @XmlTransient
109   @JsonIgnore
110   private List<RuleTask> sErrorTasks;
111 
112   @JsonProperty("UPDATEDINFO")
113   private UpdatedInfo updatedInfo = UpdatedInfo.UNKNOWN;
114 
115   /**
116    * Empty constructor
117    */
118   public Rule() {
119     hostids = new ArrayList<String>();
120     rPreTasks = new ArrayList<RuleTask>();
121     rPostTasks = new ArrayList<RuleTask>();
122     rErrorTasks = new ArrayList<RuleTask>();
123     sPreTasks = new ArrayList<RuleTask>();
124     sPostTasks = new ArrayList<RuleTask>();
125     sErrorTasks = new ArrayList<RuleTask>();
126   }
127 
128   @XmlElementDecl(name = XRPRETASKS)
129   @XmlElement(name = XRPRETASKS)
130   @JsonIgnore
131   public final Tasks get_rPreTasks() {
132     return new Tasks(rPreTasks);
133   }
134 
135   @XmlElementDecl(name = XRPOSTTASKS)
136   @XmlElement(name = XRPOSTTASKS)
137   @JsonIgnore
138   public final Tasks get_rPostTasks() {
139     return new Tasks(rPostTasks);
140   }
141 
142   @XmlElementDecl(name = XRERRORTASKS)
143   @XmlElement(name = XRERRORTASKS)
144   @JsonIgnore
145   public final Tasks get_rErrorTasks() {
146     return new Tasks(rErrorTasks);
147   }
148 
149   @XmlElementDecl(name = XSPRETASKS)
150   @XmlElement(name = XSPRETASKS)
151   @JsonIgnore
152   public final Tasks get_sPreTasks() {
153     return new Tasks(sPreTasks);
154   }
155 
156   @XmlElementDecl(name = XSPOSTTASKS)
157   @XmlElement(name = XSPOSTTASKS)
158   @JsonIgnore
159   public final Tasks get_sPostTasks() {
160     return new Tasks(sPostTasks);
161   }
162 
163   @XmlElementDecl(name = XSERRORTASKS)
164   @XmlElement(name = XSERRORTASKS)
165   @JsonIgnore
166   public final Tasks get_sErrorTasks() {
167     return new Tasks(sErrorTasks);
168   }
169 
170   public Rule(final String name, final int mode, final List<String> hostids,
171               final String recvPath, final String sendPath,
172               final String archivePath, final String workPath,
173               final List<RuleTask> rPre, final List<RuleTask> rPost,
174               final List<RuleTask> rError, final List<RuleTask> sPre,
175               final List<RuleTask> sPost, final List<RuleTask> sError,
176               final UpdatedInfo updatedInfo) throws WaarpDatabaseSqlException {
177     this(name, mode, hostids, recvPath, sendPath, archivePath, workPath, rPre,
178          rPost, rError, sPre, sPost, sError);
179     this.updatedInfo = updatedInfo;
180     checkValues();
181   }
182 
183   public Rule(final String name, final int mode, final List<String> hostids,
184               final String recvPath, final String sendPath,
185               final String archivePath, final String workPath,
186               final List<RuleTask> rPre, final List<RuleTask> rPost,
187               final List<RuleTask> rError, final List<RuleTask> sPre,
188               final List<RuleTask> sPost, final List<RuleTask> sError)
189       throws WaarpDatabaseSqlException {
190     this.name = name;
191     this.mode = mode;
192     this.hostids = hostids;
193     this.recvPath = checkPath(recvPath);
194     this.sendPath = checkPath(sendPath);
195     this.archivePath = checkPath(archivePath);
196     this.workPath = checkPath(workPath);
197     rPreTasks = rPre;
198     rPostTasks = rPost;
199     rErrorTasks = rError;
200     sPreTasks = sPre;
201     sPostTasks = sPost;
202     sErrorTasks = sError;
203     checkValues();
204   }
205 
206   public Rule(final String name, final int mode, final List<String> hostids,
207               final String recvPath, final String sendPath,
208               final String archivePath, final String workPath)
209       throws WaarpDatabaseSqlException {
210     this(name, mode, hostids, recvPath, sendPath, archivePath, workPath,
211          new ArrayList<RuleTask>(), new ArrayList<RuleTask>(),
212          new ArrayList<RuleTask>(), new ArrayList<RuleTask>(),
213          new ArrayList<RuleTask>(), new ArrayList<RuleTask>());
214   }
215 
216   public Rule(final String name, final int mode, final List<String> hostids)
217       throws WaarpDatabaseSqlException {
218     this(name, mode, hostids, "", "", "", "");
219   }
220 
221   public Rule(final String name, final int mode)
222       throws WaarpDatabaseSqlException {
223     this(name, mode, new ArrayList<String>());
224   }
225 
226   @JsonIgnore
227   public final void checkValues() throws WaarpDatabaseSqlException {
228     validateLength(Types.NVARCHAR, name);
229     validateLength(Types.VARCHAR, recvPath, sendPath, archivePath, workPath);
230     validateLength(Types.LONGVARCHAR, getXMLHostids(), getXMLRErrorTasks(),
231                    getXMLRPreTasks(), getXMLRPostTasks(), getXMLSErrorTasks(),
232                    getXMLSPreTasks(), getXMLSPostTasks());
233   }
234 
235   public final boolean isAuthorized(final String hostid) {
236     return hostids.contains(hostid);
237   }
238 
239   @JsonProperty("HOSTIDS")
240   public final String getXMLHostids() {
241     final StringBuilder res = new StringBuilder("<hostids>");
242     for (final String hostid : hostids) {
243       res.append("<hostid>").append(hostid).append("</hostid>");
244     }
245     return res.append("</hostids>").toString();
246   }
247 
248   @JsonProperty("RPRETASKS")
249   public final String getXMLRPreTasks() {
250     return getXMLTasks(rPreTasks);
251   }
252 
253   @JsonProperty("RPOSTTASKS")
254   public final String getXMLRPostTasks() {
255     return getXMLTasks(rPostTasks);
256   }
257 
258   @JsonProperty("RERRORTASKS")
259   public final String getXMLRErrorTasks() {
260     return getXMLTasks(rErrorTasks);
261   }
262 
263   @JsonProperty("SPRETASKS")
264   public final String getXMLSPreTasks() {
265     return getXMLTasks(sPreTasks);
266   }
267 
268   @JsonProperty("SPOSTTASKS")
269   public final String getXMLSPostTasks() {
270     return getXMLTasks(sPostTasks);
271   }
272 
273   @JsonProperty("SERRORTASKS")
274   public final String getXMLSErrorTasks() {
275     return getXMLTasks(sErrorTasks);
276   }
277 
278   private String getXMLTasks(final List<RuleTask> tasks) {
279     final StringBuilder res = new StringBuilder("<tasks>");
280     for (final RuleTask task : tasks) {
281       res.append(task.getXML());
282     }
283     return res.append("</tasks>").toString();
284   }
285 
286   public final String getName() {
287     return name;
288   }
289 
290   public final void setName(final String name) {
291     this.name = name;
292   }
293 
294   public final int getMode() {
295     return mode;
296   }
297 
298   public final void setMode(final int mode) {
299     this.mode = mode;
300   }
301 
302   public final List<String> getHostids() {
303     return hostids;
304   }
305 
306   public final void setHostids(final List<String> hostids) {
307     this.hostids = hostids;
308   }
309 
310   private String checkPath(final String path) throws WaarpDatabaseSqlException {
311     if (ParametersChecker.isEmpty(path)) {
312       return "";
313     }
314     final String path2 = path.replace("//", "/").replaceAll("[\\\\]+", "\\\\");
315     try {
316       return FileBasedConfiguration.checkNotAbsolutePathNotUnderBase(
317           Configuration.configuration, path2);
318     } catch (final BadConfigurationException e) {
319       throw new WaarpDatabaseSqlException(e);
320     }
321   }
322 
323   public final String getRecvPath() {
324     return recvPath;
325   }
326 
327   public final void setRecvPath(final String recvPath)
328       throws WaarpDatabaseSqlException {
329     this.recvPath = checkPath(recvPath);
330   }
331 
332   public final String getSendPath() {
333     return sendPath;
334   }
335 
336   public final void setSendPath(final String sendPath)
337       throws WaarpDatabaseSqlException {
338     this.sendPath = checkPath(sendPath);
339   }
340 
341   public final String getArchivePath() {
342     return archivePath;
343   }
344 
345   public final void setArchivePath(final String archivePath)
346       throws WaarpDatabaseSqlException {
347     this.archivePath = checkPath(archivePath);
348   }
349 
350   public final String getWorkPath() {
351     return workPath;
352   }
353 
354   public final void setWorkPath(final String workPath)
355       throws WaarpDatabaseSqlException {
356     this.workPath = checkPath(workPath);
357   }
358 
359   @JsonIgnore
360   public final List<RuleTask> getRPreTasks() {
361     return rPreTasks;
362   }
363 
364   public final void setRPreTasks(final List<RuleTask> rPreTasks) {
365     this.rPreTasks = rPreTasks;
366   }
367 
368   @JsonIgnore
369   public final List<RuleTask> getRPostTasks() {
370     return rPostTasks;
371   }
372 
373   public final void setRPostTasks(final List<RuleTask> rPostTasks) {
374     this.rPostTasks = rPostTasks;
375   }
376 
377   @JsonIgnore
378   public final List<RuleTask> getRErrorTasks() {
379     return rErrorTasks;
380   }
381 
382   public final void setRErrorTasks(final List<RuleTask> rErrorTasks) {
383     this.rErrorTasks = rErrorTasks;
384   }
385 
386   @JsonIgnore
387   public final List<RuleTask> getSPreTasks() {
388     return sPreTasks;
389   }
390 
391   public final void setSPreTasks(final List<RuleTask> sPreTasks) {
392     this.sPreTasks = sPreTasks;
393   }
394 
395   @JsonIgnore
396   public final List<RuleTask> getSPostTasks() {
397     return sPostTasks;
398   }
399 
400   public final void setSPostTasks(final List<RuleTask> sPostTasks) {
401     this.sPostTasks = sPostTasks;
402   }
403 
404   @JsonIgnore
405   public final List<RuleTask> getSErrorTasks() {
406     return sErrorTasks;
407   }
408 
409   public final void setSErrorTasks(final List<RuleTask> sErrorTasks) {
410     this.sErrorTasks = sErrorTasks;
411   }
412 
413   public final UpdatedInfo getUpdatedInfo() {
414     return updatedInfo;
415   }
416 
417   public final void setUpdatedInfo(final UpdatedInfo info) {
418     updatedInfo = info;
419   }
420 }