1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.gateway.ftp.exec;
21
22 import org.waarp.common.digest.FilesystemBasedDigest;
23 import org.waarp.common.logging.WaarpLogger;
24 import org.waarp.common.logging.WaarpLoggerFactory;
25 import org.waarp.common.utility.WaarpStringUtils;
26 import org.waarp.ftp.client.WaarpFtp4jClient;
27 import org.waarp.ftp.core.config.FtpInternalConfiguration;
28 import org.waarp.openr66.context.task.FtpArgs;
29 import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.regex.Pattern;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public class JavaExecutorWaarpFtp4jClient implements GatewayRunnable {
87
88
89
90 private static final WaarpLogger logger =
91 WaarpLoggerFactory.getLogger(JavaExecutorWaarpFtp4jClient.class);
92 private static final Pattern BLANK = WaarpStringUtils.BLANK;
93
94 boolean waitForValidation;
95 boolean useLocalExec;
96 int delay;
97 String[] args;
98 int status;
99
100 public JavaExecutorWaarpFtp4jClient() {
101
102 }
103
104
105
106
107
108
109
110 @Override
111 public final void run() {
112 logger.info("FtpTransfer with {} arguments", args.length);
113 final FtpArgs ftpArgs;
114 try {
115 ftpArgs = FtpArgs.getFtpArgs(args);
116 } catch (final OpenR66RunnerErrorException e) {
117 status = -2;
118 logger.error("Not enough arguments: {}", e.getMessage());
119 return;
120 }
121 int timeout = 30000;
122 if (delay > 10000) {
123 timeout = delay;
124 }
125 final WaarpFtp4jClient ftpClient =
126 new WaarpFtp4jClient(ftpArgs.getRequested(), ftpArgs.getPort(),
127 ftpArgs.getUser(), ftpArgs.getPwd(),
128 ftpArgs.getAcct(), ftpArgs.isPassive(),
129 ftpArgs.getSsl(), 5000, timeout);
130 boolean connected = false;
131 for (int i = 0; i < 3; i++) {
132 if (ftpClient.connect()) {
133 connected = true;
134 break;
135 }
136 }
137 if (!connected) {
138 status = -3;
139 logger.error(ftpClient.getResult());
140 return;
141 }
142 try {
143 if (ftpArgs.getCwd() != null && !ftpClient.changeDir(ftpArgs.getCwd())) {
144 ftpClient.makeDir(ftpArgs.getCwd());
145 try {
146 Thread.sleep(FtpInternalConfiguration.RETRYINMS);
147 } catch (final InterruptedException e) {
148
149 }
150 if (!ftpClient.changeDir(ftpArgs.getCwd())) {
151 logger.warn("Cannot change od directory: " + ftpArgs.getCwd());
152 }
153 }
154 if (ftpArgs.getPreArgs() != null) {
155 final String[] result = ftpClient.executeCommand(ftpArgs.getPreArgs());
156 for (final String string : result) {
157 logger.debug("PRE: {}", string);
158 }
159 }
160 if (!ftpClient.transferFile(ftpArgs.getFilepath(), ftpArgs.getFilename(),
161 ftpArgs.getCodeCommand())) {
162 status = -4;
163 logger.error(ftpClient.getResult());
164 return;
165 }
166 if (ftpArgs.getDigest() != null) {
167 String[] values = ftpClient.executeCommand(ftpArgs.getDigestCommand());
168 String hashresult = null;
169 if (values != null) {
170 values = BLANK.split(values[0]);
171 hashresult = values.length > 3? values[1] : values[0];
172 }
173 if (hashresult == null) {
174 status = -5;
175 logger.error("Hash cannot be computed: " + ftpClient.getResult());
176 return;
177 }
178
179 String hash;
180 try {
181 hash = FilesystemBasedDigest.getHex(
182 FilesystemBasedDigest.getHash(new File(ftpArgs.getFilepath()),
183 false, ftpArgs.getDigest()));
184 } catch (final IOException e) {
185 hash = null;
186 }
187 if (hash == null || !hash.equalsIgnoreCase(hashresult)) {
188 status = -6;
189 logger.error("Hash not equal: " + ftpClient.getResult());
190 return;
191 }
192 }
193 if (ftpArgs.getPostArgs() != null) {
194 final String[] result = ftpClient.executeCommand(ftpArgs.getPostArgs());
195 for (final String string : result) {
196 logger.debug("POST: {}", string);
197 }
198 }
199 } finally {
200 ftpClient.logout();
201 }
202 logger.info("FTP transfer in\n SUCCESS\n {}\n <REMOTE>{}</REMOTE>",
203 ftpArgs.getFilepath(), ftpArgs.getRequested());
204 status = 0;
205 }
206
207 @Override
208 public final void setArgs(final boolean waitForValidation,
209 final boolean useLocalExec, final int delay,
210 final String[] args) {
211 this.waitForValidation = waitForValidation;
212 this.useLocalExec = useLocalExec;
213 this.delay = delay;
214 this.args = args;
215 }
216
217 @Override
218 public final int getFinalStatus() {
219 return status;
220 }
221
222 }