-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathEbicsClient.java
More file actions
797 lines (710 loc) · 30.6 KB
/
EbicsClient.java
File metadata and controls
797 lines (710 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
* Copyright (c) 1990-2012 kopiLeft Development SARL, Bizerte, Tunisia
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id$
*/
package org.kopi.ebics.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.kopi.ebics.exception.EbicsException;
import org.kopi.ebics.exception.NoDownloadDataAvailableException;
import org.kopi.ebics.interfaces.Configuration;
import org.kopi.ebics.interfaces.EbicsBank;
import org.kopi.ebics.interfaces.EbicsLogger;
import org.kopi.ebics.interfaces.EbicsOrderType;
import org.kopi.ebics.interfaces.EbicsUser;
import org.kopi.ebics.interfaces.InitLetter;
import org.kopi.ebics.interfaces.LetterManager;
import org.kopi.ebics.interfaces.PasswordCallback;
import org.kopi.ebics.io.IOUtils;
import org.kopi.ebics.messages.Messages;
import org.kopi.ebics.schema.h003.OrderAttributeType;
import org.kopi.ebics.session.CustomOrderType;
import org.kopi.ebics.session.DefaultConfiguration;
import org.kopi.ebics.session.EbicsSession;
import org.kopi.ebics.session.OrderType;
import org.kopi.ebics.session.Product;
import org.kopi.ebics.utils.Constants;
/**
* The ebics client application. Performs necessary tasks to contact the ebics
* bank server like sending the INI, HIA and HPB requests for keys retrieval and
* also performs the files transfer including uploads and downloads.
*
*/
public class EbicsClient {
private final Configuration configuration;
private final Map<String, User> users = new HashMap<>();
private final Map<String, Partner> partners = new HashMap<>();
private final Map<String, Bank> banks = new HashMap<>();
private final ConfigProperties properties;
private final Messages messages;
private Product defaultProduct;
private User defaultUser;
static {
org.apache.xml.security.Init.init();
java.security.Security.addProvider(new BouncyCastleProvider());
}
/**
* Constructs a new ebics client application
*
* @param configuration
* the application configuration
* @param properties
*/
public EbicsClient(Configuration configuration, ConfigProperties properties) {
this.configuration = configuration;
this.properties = properties;
Messages.setLocale(configuration.getLocale());
this.messages = new Messages(Constants.APPLICATION_BUNDLE_NAME, configuration.getLocale());
configuration.getLogger().info(messages.getString("init.configuration"));
configuration.init();
}
private EbicsSession createSession(User user, Product product) {
EbicsSession session = new EbicsSession(user, configuration);
session.setProduct(product);
return session;
}
/**
* Creates the user necessary directories
*
* @param user
* the concerned user
*/
public void createUserDirectories(EbicsUser user) {
configuration.getLogger().info(
messages.getString("user.create.directories", user.getUserId()));
IOUtils.createDirectories(configuration.getUserDirectory(user));
IOUtils.createDirectories(configuration.getTransferTraceDirectory(user));
IOUtils.createDirectories(configuration.getKeystoreDirectory(user));
IOUtils.createDirectories(configuration.getLettersDirectory(user));
}
/**
* Creates a new EBICS bank with the data you should have obtained from the
* bank.
*
* @param url
* the bank URL
* @param name
* the bank name
* @param hostId
* the bank host ID
* @param useCertificate
* does the bank use certificates ?
* @return the created ebics bank
*/
private Bank createBank(URL url, String name, String hostId, boolean useCertificate) {
Bank bank = new Bank(url, name, hostId, useCertificate);
banks.put(hostId, bank);
return bank;
}
/**
* Creates a new ebics partner
*
* @param bank
* the bank
* @param partnerId
* the partner ID
*/
private Partner createPartner(EbicsBank bank, String partnerId) {
Partner partner = new Partner(bank, partnerId);
partners.put(partnerId, partner);
return partner;
}
/**
* Creates a new ebics user and generates its certificates.
*
* @param url
* the bank url
* @param bankName
* the bank name
* @param hostId
* the bank host ID
* @param partnerId
* the partner ID
* @param userId
* UserId as obtained from the bank.
* @param name
* the user name,
* @param email
* the user email
* @param country
* the user country
* @param organization
* the user organization or company
* @param useCertificates
* does the bank use certificates ?
* @param saveCertificates
* save generated certificates?
* @param passwordCallback
* a callback-handler that supplies us with the password. This
* parameter can be null, in this case no password is used.
* @return
* @throws Exception
*/
public User createUser(URL url, String bankName, String hostId, String partnerId,
String userId, String name, String email, String country, String organization,
boolean useCertificates, boolean saveCertificates, PasswordCallback passwordCallback)
throws Exception {
configuration.getLogger().info(messages.getString("user.create.info", userId));
Bank bank = createBank(url, bankName, hostId, useCertificates);
Partner partner = createPartner(bank, partnerId);
try {
User user = new User(partner, userId, name, email, country, organization,
passwordCallback);
createUserDirectories(user);
if (saveCertificates) {
user.saveUserCertificates(configuration.getKeystoreDirectory(user));
}
configuration.getSerializationManager().serialize(bank);
configuration.getSerializationManager().serialize(partner);
configuration.getSerializationManager().serialize(user);
createLetters(user, useCertificates);
users.put(userId, user);
partners.put(partner.getPartnerId(), partner);
banks.put(bank.getHostId(), bank);
configuration.getLogger().info(messages.getString("user.create.success", userId));
return user;
} catch (Exception e) {
configuration.getLogger().error(messages.getString("user.create.error"), e);
throw e;
}
}
private void createLetters(EbicsUser user, boolean useCertificates)
throws GeneralSecurityException, IOException, EbicsException {
user.getPartner().getBank().setUseCertificate(useCertificates);
LetterManager letterManager = configuration.getLetterManager();
List<InitLetter> letters = Arrays.asList(letterManager.createA005Letter(user),
letterManager.createE002Letter(user), letterManager.createX002Letter(user));
File directory = new File(configuration.getLettersDirectory(user));
for (InitLetter letter : letters) {
try (FileOutputStream out = new FileOutputStream(new File(directory, letter.getName()))) {
letter.writeTo(out);
}
}
}
/**
* Loads a user knowing its ID
*
* @throws Exception
*/
public User loadUser(String hostId, String partnerId, String userId,
PasswordCallback passwordCallback) throws Exception {
configuration.getLogger().info(messages.getString("user.load.info", userId));
try {
Bank bank;
Partner partner;
User user;
try (ObjectInputStream input = configuration.getSerializationManager().deserialize(
hostId)) {
bank = (Bank) input.readObject();
}
try (ObjectInputStream input = configuration.getSerializationManager().deserialize(
"partner-" + partnerId)) {
partner = new Partner(bank, input);
}
try (ObjectInputStream input = configuration.getSerializationManager().deserialize(
"user-" + userId)) {
user = new User(partner, input, passwordCallback);
}
users.put(userId, user);
partners.put(partner.getPartnerId(), partner);
banks.put(bank.getHostId(), bank);
configuration.getLogger().info(messages.getString("user.load.success", userId));
return user;
} catch (Exception e) {
configuration.getLogger().error(messages.getString("user.load.error"), e);
throw e;
}
}
/**
* Sends an INI request to the ebics bank server
*
* @param user the user
* @param product the application product
* @throws Exception
*/
public void sendINIRequest(User user, Product product) throws Exception {
String userId = user.getUserId();
configuration.getLogger().info(messages.getString("ini.request.send", userId));
if (user.isInitialized()) {
configuration.getLogger().info(messages.getString("user.already.initialized", userId));
return;
}
EbicsSession session = createSession(user, product);
KeyManagement keyManager = new KeyManagement(session);
configuration.getTraceManager().setTraceDirectory(
configuration.getTransferTraceDirectory(user));
try {
keyManager.sendINI(null);
user.setInitialized(true);
configuration.getLogger().info(messages.getString("ini.send.success", userId));
} catch (Exception e) {
configuration.getLogger().error(messages.getString("ini.send.error", userId), e);
throw e;
}
}
/**
* Sends a HIA request to the ebics server.
*
* @param user
* the user ID.
* @param product
* the application product.
* @throws Exception
*/
public void sendHIARequest(User user, Product product) throws Exception {
String userId = user.getUserId();
configuration.getLogger().info(messages.getString("hia.request.send", userId));
if (user.isInitializedHIA()) {
configuration.getLogger()
.info(messages.getString("user.already.hia.initialized", userId));
return;
}
EbicsSession session = createSession(user, product);
KeyManagement keyManager = new KeyManagement(session);
configuration.getTraceManager().setTraceDirectory(
configuration.getTransferTraceDirectory(user));
try {
keyManager.sendHIA(null);
user.setInitializedHIA(true);
} catch (Exception e) {
configuration.getLogger().error(messages.getString("hia.send.error", userId), e);
throw e;
}
configuration.getLogger().info(messages.getString("hia.send.success", userId));
}
/**
* Sends a HPB request to the ebics server.
*/
public void sendHPBRequest(User user, Product product) throws Exception {
String userId = user.getUserId();
configuration.getLogger().info(messages.getString("hpb.request.send", userId));
EbicsSession session = createSession(user, product);
KeyManagement keyManager = new KeyManagement(session);
configuration.getTraceManager().setTraceDirectory(
configuration.getTransferTraceDirectory(user));
try {
keyManager.sendHPB();
configuration.getLogger().info(messages.getString("hpb.send.success", userId));
} catch (Exception e) {
configuration.getLogger().error(messages.getString("hpb.send.error", userId), e);
throw e;
}
}
/**
* Sends the SPR order to the bank.
*
* @param user
* the user ID
* @param product
* the session product
* @throws Exception
*/
public void revokeSubscriber(User user, Product product) throws Exception {
String userId = user.getUserId();
configuration.getLogger().info(messages.getString("spr.request.send", userId));
EbicsSession session = createSession(user, product);
KeyManagement keyManager = new KeyManagement(session);
configuration.getTraceManager().setTraceDirectory(
configuration.getTransferTraceDirectory(user));
try {
keyManager.lockAccess();
} catch (Exception e) {
configuration.getLogger().error(messages.getString("spr.send.error", userId), e);
throw e;
}
configuration.getLogger().info(messages.getString("spr.send.success", userId));
}
/**
* Sends a file to the ebics bank server
* @throws Exception
*/
public void sendFile(File file, User user, Product product, EbicsOrderType orderType) throws Exception {
EbicsSession session = createSession(user, product);
OrderAttributeType.Enum orderAttribute = OrderAttributeType.OZHNN;
FileTransfer transferManager = new FileTransfer(session);
configuration.getTraceManager().setTraceDirectory(
configuration.getTransferTraceDirectory(user));
try {
configuration.getLogger().info(messages.getString("upload.request.send",
orderType.getCode()));
transferManager.sendFile(IOUtils.getFileContent(file), orderType, orderAttribute);
} catch (IOException | EbicsException e) {
configuration.getLogger()
.error(messages.getString("upload.file.error", file.getAbsolutePath()), e);
throw e;
}
}
public void sendFile(File file, EbicsOrderType orderType) throws Exception {
sendFile(file, defaultUser, defaultProduct, orderType);
}
public void fetchFile(File file, User user, Product product, EbicsOrderType orderType,
boolean isTest, Date start, Date end) throws IOException, EbicsException {
FileTransfer transferManager;
EbicsSession session = createSession(user, product);
session.addSessionParam("FORMAT", "pain.xxx.cfonb160.dct");
if (isTest) {
session.addSessionParam("TEST", "true");
}
transferManager = new FileTransfer(session);
configuration.getTraceManager().setTraceDirectory(
configuration.getTransferTraceDirectory(user));
try {
configuration.getLogger().info(messages.getString("download.request.send",
orderType.getCode()));
transferManager.fetchFile(orderType, start, end, file);
} catch (NoDownloadDataAvailableException e) {
// don't log this exception as an error, caller can decide how to handle
throw e;
} catch (Exception e) {
configuration.getLogger().error(messages.getString("download.file.error"), e);
throw e;
}
}
public void fetchFile(File file, EbicsOrderType orderType, Date start, Date end) throws IOException,
EbicsException {
fetchFile(file, defaultUser, defaultProduct, orderType, false, start, end);
}
/**
* Downloads file for order type without throwing NoDataAvailableException.
*
* Downloads file for the order type and logs success or warns about no data.
*
* @param file output file
* @param user EBICS User
* @param product EBICS Product
* @param orderType EBICS Order Type
* @param start Optional Date Range Start Date
* @param end Optional Date Range End Date
* @return true if data was available, false if not
* @throws java.io.IOException
* @throws org.kopi.ebics.exception.EbicsException
*/
public boolean fetchFileIfAvailable(File file, User user, Product product,
EbicsOrderType orderType, Date start, Date end) throws IOException,
EbicsException {
final EbicsLogger logger = configuration.getLogger();
try {
fetchFile(file, user, product, orderType, false, start, end);
configuration.getLogger().info(messages.getString("download.file.success", orderType.getCode()));
return true;
} catch (NoDownloadDataAvailableException nodataException) {
configuration.getLogger().warn(messages.getString("download.file.nodata", orderType.getCode()));
return false;
}
}
/**
* Performs buffers save before quitting the client application.
*/
public void quit() {
try {
for (User user : users.values()) {
if (user.needsSave()) {
configuration.getLogger()
.info(messages.getString("app.quit.users", user.getUserId()));
configuration.getSerializationManager().serialize(user);
}
}
for (Partner partner : partners.values()) {
if (partner.needsSave()) {
configuration.getLogger()
.info(messages.getString("app.quit.partners", partner.getPartnerId()));
configuration.getSerializationManager().serialize(partner);
}
}
for (Bank bank : banks.values()) {
if (bank.needsSave()) {
configuration.getLogger()
.info(messages.getString("app.quit.banks", bank.getHostId()));
configuration.getSerializationManager().serialize(bank);
}
}
} catch (EbicsException e) {
configuration.getLogger().info(messages.getString("app.quit.error"));
}
clearTraces();
}
public void clearTraces() {
configuration.getLogger().info(messages.getString("app.cache.clear"));
configuration.getTraceManager().clear();
}
public static class ConfigProperties {
Properties properties = new Properties();
public ConfigProperties(File file) throws IOException {
properties.load(new FileInputStream(file));
}
public String get(String key) {
String value = properties.getProperty(key);
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("property not set or empty: " + key);
}
return value.trim();
}
}
private User createUser(ConfigProperties properties, PasswordCallback pwdHandler)
throws Exception {
String userId = properties.get("userId");
String partnerId = properties.get("partnerId");
String bankUrl = properties.get("bank.url");
String bankName = properties.get("bank.name");
String hostId = properties.get("hostId");
String userName = properties.get("user.name");
String userEmail = properties.get("user.email");
String userCountry = properties.get("user.country");
String userOrg = properties.get("user.org");
boolean useCertificates = false;
boolean saveCertificates = true;
return createUser(new URL(bankUrl), bankName, hostId, partnerId, userId, userName, userEmail,
userCountry, userOrg, useCertificates, saveCertificates, pwdHandler);
}
private static CommandLine parseArguments(Options options, String[] args) throws ParseException {
CommandLineParser parser = new DefaultParser();
options.addOption(null, "help", false, "Print this help text");
options.addOption(null, "version", false, "Print the version information and exit");
CommandLine line = parser.parse(options, args);
if (line.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
System.out.println();
formatter.printHelp(EbicsClient.class.getSimpleName(), options);
System.out.println();
System.exit(0);
}
if (line.hasOption("version")) {
InputStream streamOrNull = EbicsClient.class.getClassLoader().getResourceAsStream(
"build.properties");
if (streamOrNull != null) {
try {
Properties properties = new Properties();
properties.load(streamOrNull);
System.out.println(properties.getProperty("build.name") + " " +
properties.getProperty("build.version"));
} catch (IOException ex) {}
}
System.exit(0);
}
return line;
}
public static EbicsClient createEbicsClient(File rootDir, File configFile) throws IOException {
ConfigProperties properties = new ConfigProperties(configFile);
final String country = properties.get("countryCode").toUpperCase();
final String language = properties.get("languageCode").toLowerCase();
final String productName = properties.get("productName");
final Locale locale = new Locale(language, country);
DefaultConfiguration configuration = new DefaultConfiguration(rootDir.getAbsolutePath(),
properties.properties) {
@Override
public Locale getLocale() {
return locale;
}
};
EbicsClient client = new EbicsClient(configuration, properties);
Product product = new Product(productName, language, null);
client.setDefaultProduct(product);
return client;
}
public void createDefaultUser() throws Exception {
defaultUser = createUser(properties, createPasswordCallback());
}
public void loadDefaultUser() throws Exception {
String userId = properties.get("userId");
String hostId = properties.get("hostId");
String partnerId = properties.get("partnerId");
defaultUser = loadUser(hostId, partnerId, userId, createPasswordCallback());
}
private PasswordCallback createPasswordCallback() {
final String password = properties.get("password");
return new PasswordCallback() {
@Override
public char[] getPassword() {
return password.toCharArray();
}
};
}
private void setDefaultProduct(Product product) {
this.defaultProduct = product;
}
public User getDefaultUser() {
return defaultUser;
}
private static void addOption(Options options, EbicsOrderType type, String description) {
options.addOption(null, type.getCode().toLowerCase(), false, description);
}
private static boolean hasOption(CommandLine cmd, EbicsOrderType type) {
return cmd.hasOption(type.getCode().toLowerCase());
}
public static void main(String[] args) throws Exception {
Options options = new Options();
addOption(options, OrderType.INI, "Send INI request");
addOption(options, OrderType.HIA, "Send HIA request");
addOption(options, OrderType.HPB, "Send HPB request");
options.addOption(null, "letters", false, "Create INI Letters");
options.addOption(null, "create", false, "Create and initialize EBICS user");
addOption(options, OrderType.STA,"Fetch STA file (MT940 file)");
addOption(options, OrderType.VMK, "Fetch VMK file (MT942 file)");
addOption(options, OrderType.C52, "Fetch camt.052 file");
addOption(options, OrderType.C53, "Fetch camt.053 file");
addOption(options, OrderType.C54, "Fetch camt.054 file");
addOption(options, OrderType.ZDF, "Fetch ZDF file (zip file with documents)");
addOption(options, OrderType.ZB6, "Fetch ZB6 file");
addOption(options, OrderType.PTK, "Fetch client protocol file (TXT)");
addOption(options, OrderType.HAC, "Fetch client protocol file (XML)");
addOption(options, OrderType.Z01, "Fetch Z01 file");
addOption(options, OrderType.XKD, "Send payment order file (DTA format)");
addOption(options, OrderType.FUL, "Send payment order file (any format)");
addOption(options, OrderType.XCT, "Send XCT file (any format)");
addOption(options, OrderType.XE2, "Send XE2 file (any format)");
addOption(options, OrderType.CCT, "Send CCT file (any format)");
options.addOption(null, "skip_order", true, "Skip a number of order ids");
options.addOption("o", "output", true, "output file");
options.addOption("i", "input", true, "input file");
options.addOption("s", "start", true, "Start date (yyyy-MM-dd)");
options.addOption("e", "end", true, "End date (yyyy-MM-dd)");
options.addOption("d", "download", true, "download custom order type");
options.addOption("u", "upload", true, "upload custom order type");
options.addOption("c", "configuration-directory", true, "Configuration directory");
CommandLine cmd = parseArguments(options, args);
String configurationDirectory = cmd.getOptionValue("configuration-directory",
System.getProperty("user.home") + File.separator + "ebics"
+ File.separator + "client");
File defaultRootDir = new File(configurationDirectory);
File ebicsClientProperties = new File(defaultRootDir, "ebics.txt");
EbicsClient client = createEbicsClient(defaultRootDir, ebicsClientProperties);
if (cmd.hasOption("create")) {
client.createDefaultUser();
} else {
client.loadDefaultUser();
}
if (cmd.hasOption("letters")) {
client.createLetters(client.defaultUser, false);
}
if (hasOption(cmd, OrderType.INI)) {
client.sendINIRequest(client.defaultUser, client.defaultProduct);
}
if (hasOption(cmd, OrderType.HIA)) {
client.sendHIARequest(client.defaultUser, client.defaultProduct);
}
if (hasOption(cmd, OrderType.HPB)) {
client.sendHPBRequest(client.defaultUser, client.defaultProduct);
}
String outputFileValue = cmd.getOptionValue("o");
String inputFileValue = cmd.getOptionValue("i");
// Process start and end dates.
// If the end date is specified, start date is required
// If the start date is specified, the end date defaults
// to the current date.
String start = cmd.getOptionValue("s");
String end = cmd.getOptionValue("e");
Date startDate = null;
Date endDate = null;
if (start != null) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
startDate = format.parse(start);
endDate = end != null
? format.parse(end)
: Date.from(Instant.now());
} else if (end != null) {
throw new EbicsException("Start date required if end date is given");
}
boolean allDataWasAvailable = true;
if (cmd.hasOption("d"))
{
allDataWasAvailable &= client.fetchFileIfAvailable(getOutputFile(outputFileValue),
client.defaultUser,
client.defaultProduct,
new CustomOrderType(cmd.getOptionValue("d")),
startDate, endDate);
}
else
{
List<? extends EbicsOrderType> fetchFileOrders = Arrays.asList(OrderType.STA, OrderType.VMK,
OrderType.C52, OrderType.C53, OrderType.C54,
OrderType.ZDF, OrderType.ZB6, OrderType.PTK, OrderType.HAC, OrderType.Z01);
for (EbicsOrderType type : fetchFileOrders) {
if (hasOption(cmd, type)) {
allDataWasAvailable &= client.fetchFileIfAvailable(getOutputFile(outputFileValue),
client.defaultUser,
client.defaultProduct,
type,
startDate, endDate);
break;
}
}
}
if (cmd.hasOption("u"))
{
client.sendFile(new File(inputFileValue), client.defaultUser,
client.defaultProduct,
new CustomOrderType(cmd.getOptionValue("u")));
}
else
{
List<? extends EbicsOrderType> sendFileOrders = Arrays.asList(OrderType.XKD, OrderType.FUL, OrderType.XCT,
OrderType.XE2, OrderType.CCT);
for (EbicsOrderType type : sendFileOrders) {
if (hasOption(cmd, type)) {
client.sendFile(new File(inputFileValue), client.defaultUser,
client.defaultProduct, type);
break;
}
}
}
if (cmd.hasOption("skip_order")) {
int count = Integer.parseInt(cmd.getOptionValue("skip_order"));
while(count-- > 0) {
client.defaultUser.getPartner().nextOrderId();
}
}
client.quit();
if (!allDataWasAvailable)
{
System.exit(2);
}
}
private static File getOutputFile(String outputFileName) {
if (outputFileName == null || outputFileName.isEmpty()) {
throw new IllegalArgumentException("outputFileName not set");
}
File file = new File(outputFileName);
if (file.exists()) {
throw new IllegalArgumentException("file already exists " + file);
}
return file;
}
}