1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.openr66.context.task;
22
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.CommandLineParser;
25 import org.apache.commons.cli.DefaultParser;
26 import org.apache.commons.cli.HelpFormatter;
27 import org.apache.commons.cli.Option;
28 import org.apache.commons.cli.Options;
29 import org.apache.commons.cli.ParseException;
30 import org.waarp.common.digest.FilesystemBasedDigest.DigestAlgo;
31 import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
32
33 import java.io.File;
34
35
36
37
38 public class FtpArgs {
39 private static final Option FILE_OPTION =
40 Option.builder("file").required(true).hasArg(true)
41 .desc("Specify the file path to operate on").build();
42 private static final Option HOST_OPTION =
43 Option.builder("to").required(true).hasArg(true)
44 .desc("Specify the requested Host").build();
45 private static final Option PORT_OPTION =
46 Option.builder("port").required(true).hasArg(true)
47 .desc("Specify the port on remote host to use").type(Number.class)
48 .build();
49 private static final Option USER_OPTION =
50 Option.builder("user").required(true).hasArg(true)
51 .desc("Specify the user on remote host to use").build();
52 private static final Option PWD_OPTION =
53 Option.builder("pwd").required(true).hasArg(true)
54 .desc("Specify the password on remote host to use").build();
55 private static final Option ACCOUNT_OPTION =
56 Option.builder("account").hasArg(true)
57 .desc("Specify the account on remote host to use").build();
58 private static final Option MODE_OPTION = Option.builder("mode").hasArg(true)
59 .desc(
60 "Specify the mode active of" +
61 " passive to use")
62 .build();
63 private static final Option SSL_OPTION = Option.builder("ssl").hasArg(true)
64 .desc(
65 "Specify the ssl mode to use " +
66 "between no / explicit / implicit")
67 .build();
68 private static final Option CWD_OPTION = Option.builder("cwd").hasArg(true)
69 .desc(
70 "Specify the remote path on " +
71 "remote host to use")
72 .build();
73 private static final Option DIGEST_OPTION =
74 Option.builder("digest").hasArg(true).desc(
75 "Specify the digest to use between crc, md5, " +
76 "sha1, sha256, sha384, sha512").build();
77 private static final Option PRE_OPTION = Option.builder("pre").hasArg(true)
78 .desc(
79 "Specify the extra command as " +
80 "pre operation to use, using ',' as " +
81 "separator of arguments")
82 .build();
83 private static final Option POST_OPTION = Option.builder("post").hasArg(true)
84 .desc(
85 "Specify the extra command " +
86 "as post operation to use, " +
87 "using ',' as" +
88 " separator of arguments")
89 .build();
90 private static final Option CMD_OPTION =
91 Option.builder("command").hasArg(true)
92 .desc("Specify the command as one of get, put, append ").build();
93
94 private static final Options FTP_OPTIONS =
95 new Options().addOption(FILE_OPTION).addOption(HOST_OPTION)
96 .addOption(PORT_OPTION).addOption(USER_OPTION)
97 .addOption(PWD_OPTION).addOption(ACCOUNT_OPTION)
98 .addOption(MODE_OPTION).addOption(SSL_OPTION)
99 .addOption(CWD_OPTION).addOption(DIGEST_OPTION)
100 .addOption(PRE_OPTION).addOption(CMD_OPTION)
101 .addOption(POST_OPTION);
102 private String filepath = null;
103 private String filename = null;
104 private String requested = null;
105 private int port = 21;
106 private String user = null;
107 private String pwd = null;
108 private String acct = null;
109 private boolean isPassive = true;
110 private int ssl = 0;
111 private String cwd = null;
112 private DigestAlgo digest = null;
113
114 private String digestCommand = null;
115 private String command;
116 private int codeCommand = 0;
117 private String preArgs = null;
118 private String postArgs = null;
119
120
121
122
123 public static void printHelp() {
124 final HelpFormatter formatter = new HelpFormatter();
125 formatter.printHelp("FtpArgs", FTP_OPTIONS);
126 }
127
128
129
130
131
132
133
134
135
136
137 public static FtpArgs getFtpArgs(final String[] args)
138 throws OpenR66RunnerErrorException {
139 final FtpArgs ftpArgs = new FtpArgs();
140 final CommandLineParser parser = new DefaultParser();
141 try {
142 final CommandLine cmd = parser.parse(FTP_OPTIONS, args, true);
143
144 ftpArgs.setFilepath(cmd.getOptionValue("file"));
145 ftpArgs.setFilename(new File(ftpArgs.getFilepath()).getName());
146 ftpArgs.setRequested(cmd.getOptionValue("to"));
147 try {
148 ftpArgs.setPort(Integer.parseInt(cmd.getOptionValue("port")));
149 if (ftpArgs.getPort() < 0) {
150 throw new NumberFormatException("Port must be positive");
151 }
152 } catch (final NumberFormatException e) {
153 throw new OpenR66RunnerErrorException(e);
154 }
155 ftpArgs.setUser(cmd.getOptionValue("user"));
156 ftpArgs.setPwd(cmd.getOptionValue("pwd"));
157 if (cmd.hasOption("account")) {
158 ftpArgs.setAcct(cmd.getOptionValue("account"));
159 }
160 if (cmd.hasOption("mode")) {
161 ftpArgs.setPassive(
162 "passive".equalsIgnoreCase(cmd.getOptionValue("mode")));
163 }
164 if (cmd.hasOption("ssl")) {
165 final String ssl = cmd.getOptionValue("ssl");
166 if ("implicit".equalsIgnoreCase(ssl)) {
167 ftpArgs.setSsl(-1);
168 } else if ("explicit".equalsIgnoreCase(ssl)) {
169 ftpArgs.setSsl(1);
170 } else {
171 ftpArgs.setSsl(0);
172 }
173 }
174 if (cmd.hasOption("cwd")) {
175 ftpArgs.setCwd(cmd.getOptionValue("cwd"));
176 }
177 if (cmd.hasOption("digest")) {
178 final String digest = cmd.getOptionValue("digest");
179 if ("crc".equalsIgnoreCase(digest)) {
180 ftpArgs.setDigest(DigestAlgo.CRC32);
181 ftpArgs.setDigestCommand("XCRC " + ftpArgs.getFilename());
182 } else if ("md5".equalsIgnoreCase(digest)) {
183 ftpArgs.setDigest(DigestAlgo.MD5);
184 ftpArgs.setDigestCommand("XMD5 " + ftpArgs.getFilename());
185 } else if ("sha1".equalsIgnoreCase(digest)) {
186 ftpArgs.setDigest(DigestAlgo.SHA1);
187 ftpArgs.setDigestCommand("XSHA1 " + ftpArgs.getFilename());
188 } else if ("sha256".equalsIgnoreCase(digest)) {
189 ftpArgs.setDigest(DigestAlgo.SHA256);
190 ftpArgs.setDigestCommand("XSHA256 " + ftpArgs.getFilename());
191 } else if ("sha384".equalsIgnoreCase(digest)) {
192 ftpArgs.setDigest(DigestAlgo.SHA384);
193 ftpArgs.setDigestCommand("XSHA384 " + ftpArgs.getFilename());
194 } else if ("sha512".equalsIgnoreCase(digest)) {
195 ftpArgs.setDigest(DigestAlgo.SHA512);
196 ftpArgs.setDigestCommand("XSHA512 " + ftpArgs.getFilename());
197 } else {
198 ftpArgs.setDigest(null);
199 }
200 }
201 if (cmd.hasOption("pre")) {
202 ftpArgs.setPreArgs(cmd.getOptionValue("pre").replace(',', ' '));
203 }
204 if (cmd.hasOption("post")) {
205 ftpArgs.setPostArgs(cmd.getOptionValue("post").replace(',', ' '));
206 }
207 if (cmd.hasOption("command")) {
208 ftpArgs.setCommand(cmd.getOptionValue("command"));
209
210
211 if ("get".equalsIgnoreCase(ftpArgs.getCommand())) {
212 ftpArgs.setCodeCommand(-1);
213 } else if ("put".equalsIgnoreCase(ftpArgs.getCommand())) {
214 ftpArgs.setCodeCommand(1);
215 } else if ("append".equalsIgnoreCase(ftpArgs.getCommand())) {
216 ftpArgs.setCodeCommand(2);
217 } else {
218
219 ftpArgs.setCodeCommand(0);
220 throw new OpenR66RunnerErrorException(
221 "Command not known: " + ftpArgs.getCommand());
222 }
223 }
224 } catch (final ParseException e) {
225 throw new OpenR66RunnerErrorException(e);
226 }
227
228 return ftpArgs;
229 }
230
231 public final String getFilepath() {
232 return filepath;
233 }
234
235 public final FtpArgs setFilepath(final String filepath) {
236 this.filepath = filepath;
237 return this;
238 }
239
240 public final String getFilename() {
241 return filename;
242 }
243
244 public final FtpArgs setFilename(final String filename) {
245 this.filename = filename;
246 return this;
247 }
248
249 public final String getRequested() {
250 return requested;
251 }
252
253 public final FtpArgs setRequested(final String requested) {
254 this.requested = requested;
255 return this;
256 }
257
258 public final int getPort() {
259 return port;
260 }
261
262 public final FtpArgs setPort(final int port) {
263 this.port = port;
264 return this;
265 }
266
267 public final String getUser() {
268 return user;
269 }
270
271 public final FtpArgs setUser(final String user) {
272 this.user = user;
273 return this;
274 }
275
276 public final String getPwd() {
277 return pwd;
278 }
279
280 public final FtpArgs setPwd(final String pwd) {
281 this.pwd = pwd;
282 return this;
283 }
284
285 public final String getAcct() {
286 return acct;
287 }
288
289 public final FtpArgs setAcct(final String acct) {
290 this.acct = acct;
291 return this;
292 }
293
294 public final boolean isPassive() {
295 return isPassive;
296 }
297
298 public final FtpArgs setPassive(final boolean passive) {
299 isPassive = passive;
300 return this;
301 }
302
303 public final int getSsl() {
304 return ssl;
305 }
306
307 public final FtpArgs setSsl(final int ssl) {
308 this.ssl = ssl;
309 return this;
310 }
311
312 public final String getCwd() {
313 return cwd;
314 }
315
316 public final FtpArgs setCwd(final String cwd) {
317 this.cwd = cwd;
318 return this;
319 }
320
321 public final DigestAlgo getDigest() {
322 return digest;
323 }
324
325 public final FtpArgs setDigest(final DigestAlgo digest) {
326 this.digest = digest;
327 return this;
328 }
329
330 public final String getDigestCommand() {
331 return digestCommand;
332 }
333
334 public final FtpArgs setDigestCommand(final String digestCommand) {
335 this.digestCommand = digestCommand;
336 return this;
337 }
338
339 public final String getCommand() {
340 return command;
341 }
342
343 public final FtpArgs setCommand(final String command) {
344 this.command = command;
345 return this;
346 }
347
348 public final int getCodeCommand() {
349 return codeCommand;
350 }
351
352 public final FtpArgs setCodeCommand(final int codeCommand) {
353 this.codeCommand = codeCommand;
354 return this;
355 }
356
357 public final String getPreArgs() {
358 return preArgs;
359 }
360
361 public final FtpArgs setPreArgs(final String preArgs) {
362 this.preArgs = preArgs;
363 return this;
364 }
365
366 public final String getPostArgs() {
367 return postArgs;
368 }
369
370 public final FtpArgs setPostArgs(final String postArgs) {
371 this.postArgs = postArgs;
372 return this;
373 }
374 }