1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.context.task;
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.ftp.client.WaarpFtp4jClient;
26 import org.waarp.openr66.context.ErrorCode;
27 import org.waarp.openr66.context.R66Result;
28 import org.waarp.openr66.context.R66Session;
29 import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
30 import org.waarp.openr66.protocol.configuration.Configuration;
31 import org.waarp.openr66.protocol.exception.OpenR66ProtocolSystemException;
32
33 import java.io.File;
34 import java.io.IOException;
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 public class FtpTransferTask extends AbstractTask {
85
86
87
88 private static final WaarpLogger logger =
89 WaarpLoggerFactory.getLogger(FtpTransferTask.class);
90
91
92
93
94
95
96
97 public FtpTransferTask(final String argRule, final int delay,
98 final String argTransfer, final R66Session session) {
99 super(TaskType.FTP, delay, argRule, argTransfer, session);
100 }
101
102
103
104
105
106
107
108
109 @Override
110 public final void run() {
111 logger.info("FtpTransfer with {}:{} and {}", argRule, argTransfer, session);
112 if (argRule == null) {
113 logger.error(
114 "FtpTransfer cannot be done with " + argRule + ':' + argTransfer +
115 " and " + session);
116 futureCompletion.setFailure(
117 new OpenR66ProtocolSystemException("FtpTransfer cannot be done"));
118 return;
119 }
120 String finalname = argRule;
121 final String[] argFormat = BLANK.split(argTransfer);
122 if (argFormat != null && argFormat.length > 0) {
123 try {
124 finalname = String.format(finalname, (Object[]) argFormat);
125 } catch (final Exception e) {
126
127 logger.error(
128 "Bad format in Rule: {" + finalname + "} " + e.getMessage());
129 }
130 }
131 final String[] args = BLANK.split(finalname);
132 for (int i = 0; i < args.length; i++) {
133 args[i] = getReplacedValue(args[i], null);
134 }
135 final FtpArgs ftpArgs;
136 try {
137 ftpArgs = FtpArgs.getFtpArgs(args);
138 } catch (final OpenR66RunnerErrorException e) {
139 final OpenR66RunnerErrorException exception =
140 new OpenR66RunnerErrorException("Not enough argument in Transfer");
141 final R66Result result =
142 new R66Result(exception, session, false, ErrorCode.CommandNotFound,
143 session.getRunner());
144 logger.error("Not enough arguments: " + e.getMessage());
145 futureCompletion.setResult(result);
146 futureCompletion.setFailure(exception);
147 return;
148 }
149
150 final WaarpFtp4jClient ftpClient =
151 new WaarpFtp4jClient(ftpArgs.getRequested(), ftpArgs.getPort(),
152 ftpArgs.getUser(), ftpArgs.getPwd(),
153 ftpArgs.getAcct(), ftpArgs.isPassive(),
154 ftpArgs.getSsl(), 5000,
155 (int) Configuration.configuration.getTimeoutCon());
156 boolean status = false;
157 for (int i = 0; i < Configuration.RETRYNB; i++) {
158 if (ftpClient.connect()) {
159 status = true;
160 break;
161 }
162 }
163 if (!status) {
164 final OpenR66RunnerErrorException exception =
165 new OpenR66RunnerErrorException("Cannot connect to remote FTP host");
166 final R66Result result = new R66Result(exception, session, false,
167 ErrorCode.ConnectionImpossible,
168 session.getRunner());
169 futureCompletion.setResult(result);
170 futureCompletion.setFailure(exception);
171 logger.error(ftpClient.getResult());
172 return;
173 }
174 try {
175 if (ftpArgs.getCwd() != null && !ftpClient.changeDir(ftpArgs.getCwd())) {
176 ftpClient.makeDir(ftpArgs.getCwd());
177 try {
178 Thread.sleep(Configuration.RETRYINMS);
179 } catch (final InterruptedException e) {
180
181 }
182 if (!ftpClient.changeDir(ftpArgs.getCwd())) {
183 logger.warn("Cannot change od directory: " + ftpArgs.getCwd());
184 }
185 }
186 if (ftpArgs.getPreArgs() != null) {
187 final String[] result = ftpClient.executeCommand(ftpArgs.getPreArgs());
188 for (final String string : result) {
189 logger.debug("PRE: {}", string);
190 }
191 }
192 if (!ftpClient.transferFile(ftpArgs.getFilepath(), ftpArgs.getFilename(),
193 ftpArgs.getCodeCommand())) {
194 final OpenR66RunnerErrorException exception =
195 new OpenR66RunnerErrorException(
196 "Cannot transfert file from/to remote FTP host");
197 final R66Result result =
198 new R66Result(exception, session, false, ErrorCode.TransferError,
199 session.getRunner());
200 futureCompletion.setResult(result);
201 futureCompletion.setFailure(exception);
202 logger.error(ftpClient.getResult());
203 return;
204 }
205 if (ftpArgs.getDigest() != null) {
206 String[] values = ftpClient.executeCommand(ftpArgs.getDigestCommand());
207 String hashresult = null;
208 if (values != null) {
209 values = BLANK.split(values[0]);
210 hashresult = values.length > 3? values[1] : values[0];
211 }
212 if (hashresult == null) {
213 final OpenR66RunnerErrorException exception =
214 new OpenR66RunnerErrorException(
215 "Hash cannot be computed while FTP transfer is done");
216 final R66Result result =
217 new R66Result(exception, session, false, ErrorCode.TransferError,
218 session.getRunner());
219 futureCompletion.setResult(result);
220 futureCompletion.setFailure(exception);
221 logger.error("Hash cannot be computed: " + ftpClient.getResult());
222 return;
223 }
224
225 String hash;
226 try {
227 hash = FilesystemBasedDigest.getHex(
228 FilesystemBasedDigest.getHash(new File(ftpArgs.getFilepath()),
229 false, ftpArgs.getDigest()));
230 } catch (final IOException e) {
231 hash = null;
232 }
233 if (hash == null || !hash.equalsIgnoreCase(hashresult)) {
234 final OpenR66RunnerErrorException exception =
235 new OpenR66RunnerErrorException(
236 "Hash not equal while FTP transfer is done");
237 final R66Result result =
238 new R66Result(exception, session, false, ErrorCode.TransferError,
239 session.getRunner());
240 futureCompletion.setResult(result);
241 futureCompletion.setFailure(exception);
242 logger.error("Hash not equal: " + ftpClient.getResult());
243 return;
244 }
245 }
246 if (ftpArgs.getPostArgs() != null) {
247 final String[] result = ftpClient.executeCommand(ftpArgs.getPostArgs());
248 for (final String string : result) {
249 logger.debug("POST: {}", string);
250 }
251 }
252 } finally {
253 ftpClient.logout();
254 }
255 final R66Result result = new R66Result(session, false, ErrorCode.TransferOk,
256 session.getRunner());
257 futureCompletion.setResult(result);
258 if (logger.isInfoEnabled()) {
259 logger.info("FTP transfer in SUCCESS " +
260 session.getRunner().toShortString() + " <REMOTE>" +
261 ftpArgs.getRequested() + "</REMOTE>");
262 }
263 futureCompletion.setSuccess();
264 }
265
266 }