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.file.AbstractDir;
23 import org.waarp.common.logging.WaarpLogger;
24 import org.waarp.common.logging.WaarpLoggerFactory;
25 import org.waarp.openr66.context.R66Session;
26 import org.waarp.openr66.context.task.exception.OpenR66RunnerException;
27
28 import java.io.File;
29
30 /**
31 * This task validate the File Path according to the following argument:<br>
32 * - the full path is get from the current file<br>
33 * - the arg path is transformed as usual (static and dynamic from information
34 * transfer) and should be the
35 * beginning of the correct valid path<br>
36 * - the full path should begin with one of the result of the transformation
37 * (blank separated)<br>
38 * <br>
39 * For instance "#OUTPATH# #INPATH# #WORKPATH# #ARHCPATH#" will test that the
40 * current file is in one of the
41 * standard path.
42 */
43 public class ValidFilePathTask extends AbstractTask {
44 /**
45 * Internal Logger
46 */
47 private static final WaarpLogger logger =
48 WaarpLoggerFactory.getLogger(ValidFilePathTask.class);
49
50 /**
51 * @param argRule
52 * @param delay
53 * @param argTransfer
54 * @param session
55 */
56 public ValidFilePathTask(final String argRule, final int delay,
57 final String argTransfer, final R66Session session) {
58 super(TaskType.VALIDFILEPATH, delay, argRule, argTransfer, session);
59 }
60
61 @Override
62 public final void run() {
63 String finalname = argRule;
64 finalname = AbstractDir.normalizePath(
65 getReplacedValue(finalname, BLANK.split(argTransfer)));
66 logger.info("Test Valid Path with {} from {}", finalname, session);
67 final File from = session.getFile().getTrueFile();
68 final String curpath = AbstractDir.normalizePath(from.getAbsolutePath());
69 final String[] paths = BLANK.split(finalname);
70 for (final String base : paths) {
71 if (curpath.startsWith(base)) {
72 if (delay > 0) {
73 logger.info("Validate File {} from {} and {}", curpath, base,
74 session);
75 }
76 futureCompletion.setSuccess();
77 return;
78 }
79 }
80 if (delay > 0) {
81 logger.error("Unvalidate File: " + curpath + " " + session);
82 }
83 futureCompletion.setFailure(
84 new OpenR66RunnerException("File not Validated"));
85 }
86
87 }