/*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
import net.ktnx.mobileledger.utils.Globals;
import net.ktnx.mobileledger.utils.Logger;
import net.ktnx.mobileledger.utils.NetworkUtil;
+import net.ktnx.mobileledger.utils.SimpleDate;
import net.ktnx.mobileledger.utils.UrlEncodedFormData;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
-import java.util.Date;
-import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static android.os.SystemClock.sleep;
import static net.ktnx.mobileledger.utils.Logger.debug;
+/* TODO: get rid of the custom session/cookie and auth code?
+ * (the last problem with the POST was the missing content-length header)
+ * This will resolve itself when hledger-web 1.14+ is released with Debian/stable,
+ * at which point the HTML form emulation can be dropped entirely
+ */
+
public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void> {
private final TaskCallback taskCallback;
+ private final MobileLedgerProfile mProfile;
+ private final boolean simulate;
protected String error;
private String token;
private String session;
- private LedgerTransaction ltr;
- private MobileLedgerProfile mProfile;
- private boolean simulate = false;
+ private LedgerTransaction transaction;
public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile,
boolean simulate) {
http.setRequestProperty("Accept", "*/*");
net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction jsonTransaction =
- net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.fromLedgerTransaction(ltr);
+ net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.fromLedgerTransaction(
+ transaction);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer =
mapper.writerFor(net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.class);
http.setRequestProperty("Accept", "*/*");
net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction jsonTransaction =
- net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.fromLedgerTransaction(ltr);
+ net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.fromLedgerTransaction(
+ transaction);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer =
mapper.writerFor(net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.class);
req.write(bodyBytes);
final int responseCode = http.getResponseCode();
- debug("network",
- String.format("Response: %d %s", responseCode, http.getResponseMessage()));
+ debug("network", String.format(Locale.US, "Response: %d %s", responseCode,
+ http.getResponseMessage()));
try (InputStream resp = http.getErrorStream()) {
case 201:
break;
case 400:
- case 405:
+ case 405: {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(resp));
+ String line;
+ int count = 0;
+ while (count <= 5) {
+ line = reader.readLine();
+ if (line == null)
+ break;
+ Logger.debug("network", line);
+ count++;
+ }
return false; // will cause a retry with the legacy method
+ }
default:
BufferedReader reader = new BufferedReader(new InputStreamReader(resp));
String line = reader.readLine();
if (token != null)
params.addPair("_token", token);
- Date transactionDate = ltr.getDate();
- if (transactionDate == null) {
- transactionDate = new GregorianCalendar().getTime();
- }
+ SimpleDate transactionDate = transaction.getDateIfAny();
+ if (transactionDate == null)
+ transactionDate = SimpleDate.today();
params.addPair("date", Globals.formatLedgerDate(transactionDate));
- params.addPair("description", ltr.getDescription());
- for (LedgerTransactionAccount acc : ltr.getAccounts()) {
+ params.addPair("description", transaction.getDescription());
+ for (LedgerTransactionAccount acc : transaction.getAccounts()) {
params.addPair("account", acc.getAccountName());
if (acc.isAmountSet())
params.addPair("amount", String.format(Locale.US, "%1.2f", acc.getAmount()));
protected Void doInBackground(LedgerTransaction... ledgerTransactions) {
error = null;
try {
- ltr = ledgerTransactions[0];
+ transaction = ledgerTransactions[0];
switch (mProfile.getApiVersion()) {
case auto:
if (!send_1_15_OK()) {
Logger.debug("network", "Version 1.5 request failed. Trying with 1.14");
if (!send_1_14_OK()) {
- Logger.debug("network", "Version 1.14 failed too. Trying HTML form emulation");
+ Logger.debug("network",
+ "Version 1.14 failed too. Trying HTML form emulation");
legacySendOkWithRetry();
}
else {
while (!legacySendOK()) {
tried++;
if (tried >= 2)
- throw new IOException(
- String.format("aborting after %d tries", tried));
+ throw new IOException(String.format("aborting after %d tries", tried));
sleep(100);
}
}
public enum API {
auto(0), html(-1), pre_1_15(-2), post_1_14(-3);
- private static SparseArray<API> map = new SparseArray<>();
+ private static final SparseArray<API> map = new SparseArray<>();
static {
for (API item : API.values()) {