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  package org.waarp.openr66.context.task;
21  
22  import org.waarp.common.logging.WaarpLogger;
23  import org.waarp.common.logging.WaarpLoggerFactory;
24  import org.waarp.openr66.context.ErrorCode;
25  import org.waarp.openr66.context.R66Result;
26  import org.waarp.openr66.context.R66Session;
27  import org.waarp.openr66.context.task.exception.OpenR66RunnerException;
28  import org.waarp.openr66.database.data.DbTaskRunner;
29  
30  /**
31   * This task checks some properties relative to the File according to the
32   * following argument:<br>
33   * - SIZE LT/GT/LTE/GTE/EQ number : to check the file size according to a limit
34   * (less than, greater than, less
35   * than or equal, greater than or equal, equal)<br>
36   * - DFCHECK : to check if the future received file size is compatible with the
37   * space left on both working and
38   * received directories (from general configuration or rule configuration)<br>
39   * <br>
40   * For instance "SIZE LT 10000000 SIZE GT 1000 DFCHECK" will test that the
41   * current file size is less than 10
42   * MB (base 10), and greater than 1000 bytes, and that the working and received
43   * directories have enough space
44   * to receive the file.
45   */
46  public class FileCheckTask extends AbstractTask {
47    /**
48     * Internal Logger
49     */
50    private static final WaarpLogger logger =
51        WaarpLoggerFactory.getLogger(FileCheckTask.class);
52  
53    private enum FC_COMMAND {
54      SIZE, DFCHECK
55    }
56  
57    private enum FC_ARGSSIZE {
58      LT, GT, LTE, GTE, EQ
59    }
60  
61    /**
62     * @param argRule
63     * @param delay
64     * @param argTransfer
65     * @param session
66     */
67    public FileCheckTask(final String argRule, final int delay,
68                         final String argTransfer, final R66Session session) {
69      super(TaskType.CHKFILE, delay, argRule, argTransfer, session);
70    }
71  
72    @Override
73    public void run() {
74      final DbTaskRunner runner = session.getRunner();
75      if (runner == null) {
76        // no information so in error
77        logger.error("No Runner task so cannot check file: " + session);
78        futureCompletion.setFailure(
79            new OpenR66RunnerException("No Runner task so cannot check File"));
80        return;
81      }
82      final long supposelength = runner.getOriginalSize();
83      if (supposelength <= 0) {
84        // no information but could be not an error so ignore
85        logger.warn("No file size known: " + session);
86        futureCompletion.setSuccess();
87        return;
88      }
89      final String[] commands = BLANK.split(argRule);
90      int current = 0;
91      while (current < commands.length) {
92        if (commands[current].equalsIgnoreCase(FC_COMMAND.SIZE.name())) {
93          current++;
94          FC_ARGSSIZE arg;
95          try {
96            arg = FC_ARGSSIZE.valueOf(commands[current]);
97          } catch (final IllegalArgumentException e) {
98            arg = null;
99          }
100         if (arg != null) {
101           current++;
102           try {
103             final long tocompare = Long.parseLong(commands[current]);
104             current++;
105             final boolean result;
106             switch (arg) {
107               case EQ:
108                 result = supposelength == tocompare;
109                 break;
110               case GT:
111                 result = supposelength > tocompare;
112                 break;
113               case GTE:
114                 result = supposelength >= tocompare;
115                 break;
116               case LT:
117                 result = supposelength < tocompare;
118                 break;
119               case LTE:
120                 result = supposelength <= tocompare;
121                 break;
122               default:
123                 result = true; // ??
124                 break;
125             }
126             logger.debug("DEBUG: {} {} {}", supposelength, arg.name(),
127                          tocompare);
128             if (!result) {
129               // error so stop
130               logger.error(
131                   "File length is incompatible with specified SIZE comparizon: " +
132                   supposelength + " NOT " + arg.name() + ' ' + tocompare);
133               futureCompletion.setResult(
134                   new R66Result(session, false, ErrorCode.SizeNotAllowed,
135                                 runner));
136               futureCompletion.setFailure(
137                   new OpenR66RunnerException("File size incompatible"));
138               return;
139             }
140           } catch (final NumberFormatException e) {
141             // ignore and continue
142           }
143         }
144       } else if (commands[current].equalsIgnoreCase(
145           FC_COMMAND.DFCHECK.name())) {
146         current++;
147         long freesize = runner.freespace(session, true);
148         if (freesize > 0 && supposelength > freesize) {
149           // error so stop
150           logger.error(
151               "File length is incompatible with available space in Working directory: " +
152               supposelength + " > " + freesize);
153           futureCompletion.setResult(
154               new R66Result(session, false, ErrorCode.SizeNotAllowed, runner));
155           futureCompletion.setFailure(new OpenR66RunnerException(
156               "File size incompatible with Working directory"));
157           return;
158         }
159         logger.debug("DEBUG: {} < {}", supposelength, freesize);
160         freesize = runner.freespace(session, false);
161         if (freesize > 0 && supposelength > freesize) {
162           // error so stop
163           logger.error(
164               "File length is incompatible with available space in Recv directory: " +
165               supposelength + " > " + freesize);
166           futureCompletion.setResult(
167               new R66Result(session, false, ErrorCode.SizeNotAllowed, runner));
168           futureCompletion.setFailure(new OpenR66RunnerException(
169               "File size incompatible with Recv directory"));
170           return;
171         }
172         logger.debug("DEBUG: {} < {}", supposelength, freesize);
173       } else {
174         // ignore and continue
175         current++;
176       }
177     }
178     logger.debug("DEBUG: End of check {}", supposelength);
179     futureCompletion.setSuccess();
180   }
181 
182 }