Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options for any custom order type and for date ranges #27

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down Expand Up @@ -139,6 +145,21 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>de.ntcomputer</groupId>
<artifactId>executable-packer-maven-plugin</artifactId>
<version>1.0.1</version>
<configuration>
<mainClass>org.kopi.ebics.client.EbicsClient</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>pack-executable-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
Expand Down
143 changes: 127 additions & 16 deletions src/main/java/org/kopi/ebics/client/EbicsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
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;
Expand All @@ -45,6 +50,7 @@
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;
Expand All @@ -53,6 +59,7 @@
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;
Expand Down Expand Up @@ -390,6 +397,8 @@ public void sendFile(File file, User user, Product product, EbicsOrderType order
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()
Expand All @@ -416,6 +425,8 @@ public void fetchFile(File file, User user, Product product, EbicsOrderType orde
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
Expand All @@ -431,6 +442,35 @@ public void fetchFile(File file, EbicsOrderType orderType, Date start, Date end)
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.
*/
Expand Down Expand Up @@ -507,6 +547,7 @@ private User createUser(ConfigProperties properties, PasswordCallback 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();
Expand All @@ -515,6 +556,20 @@ private static CommandLine parseArguments(Options options, String[] args) throws
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;
}

Expand Down Expand Up @@ -611,11 +666,20 @@ public static void main(String[] args) throws Exception {
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);

File defaultRootDir = new File(System.getProperty("user.home") + File.separator + "ebics"
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);

Expand All @@ -642,25 +706,68 @@ public static void main(String[] args) throws Exception {
String outputFileValue = cmd.getOptionValue("o");
String inputFileValue = cmd.getOptionValue("i");

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);
// 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;

for (EbicsOrderType type : fetchFileOrders) {
if (hasOption(cmd, type)) {
client.fetchFile(getOutputFile(outputFileValue), client.defaultUser,
client.defaultProduct, type, false, null, null);
break;
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;
}
}
}

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("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;
}
}
}

Expand All @@ -671,6 +778,10 @@ public static void main(String[] args) throws Exception {
}
}
client.quit();
if (!allDataWasAvailable)
{
System.exit(2);
}
}

private static File getOutputFile(String outputFileName) {
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/org/kopi/ebics/session/CustomOrderType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2021, Digital Financial, LLC.
*
* 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
*/

package org.kopi.ebics.session;

import java.util.Objects;
import org.kopi.ebics.interfaces.EbicsOrderType;

/**
*
* @author nsushkin
*/
public class CustomOrderType implements EbicsOrderType {

private final String code;

public CustomOrderType(String code) {
Objects.requireNonNull(code, "CustomOrderType code must not be null");
this.code = code.toUpperCase();
}

/**
* Get the value of code
*
* @return the value of code
*/
@Override
public String getCode() {
return code;
}

}
Loading