2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.async;
20 import android.os.AsyncTask;
21 import android.util.Log;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.ObjectWriter;
26 import net.ktnx.mobileledger.json.ParsedLedgerTransaction;
27 import net.ktnx.mobileledger.model.LedgerTransaction;
28 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
29 import net.ktnx.mobileledger.model.MobileLedgerProfile;
30 import net.ktnx.mobileledger.utils.Globals;
31 import net.ktnx.mobileledger.utils.NetworkUtil;
32 import net.ktnx.mobileledger.utils.UrlEncodedFormData;
34 import java.io.BufferedReader;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.io.OutputStream;
39 import java.net.HttpURLConnection;
40 import java.nio.charset.StandardCharsets;
41 import java.util.List;
42 import java.util.Locale;
44 import java.util.regex.Matcher;
45 import java.util.regex.Pattern;
47 import static android.os.SystemClock.sleep;
48 import static net.ktnx.mobileledger.utils.Logger.debug;
50 public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void> {
51 private final TaskCallback taskCallback;
52 protected String error;
54 private String session;
55 private LedgerTransaction ltr;
56 private MobileLedgerProfile mProfile;
58 public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile) {
59 taskCallback = callback;
62 private boolean sendOK() throws IOException {
63 HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
64 http.setRequestMethod("PUT");
65 http.setRequestProperty("Content-Type", "application/json");
66 http.setRequestProperty("Accept", "*/*");
68 ParsedLedgerTransaction jsonTransaction;
69 jsonTransaction = ltr.toParsedLedgerTransaction();
70 ObjectMapper mapper = new ObjectMapper();
71 ObjectWriter writer = mapper.writerFor(ParsedLedgerTransaction.class);
72 String body = writer.writeValueAsString(jsonTransaction);
74 byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8);
75 http.setDoOutput(true);
76 http.setDoInput(true);
77 http.addRequestProperty("Content-Length", String.valueOf(bodyBytes.length));
79 debug("network", "request header: " + http.getRequestProperties()
82 try (OutputStream req = http.getOutputStream()) {
83 debug("network", "Request body: " + body);
86 final int responseCode = http.getResponseCode();
88 String.format("Response: %d %s", responseCode, http.getResponseMessage()));
90 try (InputStream resp = http.getErrorStream()) {
92 switch (responseCode) {
97 return false; // will cause a retry with the legacy method
99 BufferedReader reader = new BufferedReader(new InputStreamReader(resp));
100 String line = reader.readLine();
101 debug("network", "Response content: " + line);
102 throw new IOException(
103 String.format("Error response code %d", responseCode));
110 private boolean legacySendOK() throws IOException {
111 HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
112 http.setRequestMethod("POST");
113 http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
114 http.setRequestProperty("Accept", "*/*");
115 if ((session != null) && !session.isEmpty()) {
116 http.setRequestProperty("Cookie", String.format("_SESSION=%s", session));
118 http.setDoOutput(true);
119 http.setDoInput(true);
121 UrlEncodedFormData params = new UrlEncodedFormData();
122 params.addPair("_formid", "identify-add");
124 params.addPair("_token", token);
125 params.addPair("date", Globals.formatLedgerDate(ltr.getDate()));
126 params.addPair("description", ltr.getDescription());
127 for (LedgerTransactionAccount acc : ltr.getAccounts()) {
128 params.addPair("account", acc.getAccountName());
129 if (acc.isAmountSet())
130 params.addPair("amount", String.format(Locale.US, "%1.2f", acc.getAmount()));
132 params.addPair("amount", "");
135 String body = params.toString();
136 http.addRequestProperty("Content-Length", String.valueOf(body.length()));
138 debug("network", "request header: " + http.getRequestProperties()
141 try (OutputStream req = http.getOutputStream()) {
142 debug("network", "Request body: " + body);
143 req.write(body.getBytes(StandardCharsets.US_ASCII));
145 try (InputStream resp = http.getInputStream()) {
146 debug("update_accounts", String.valueOf(http.getResponseCode()));
147 if (http.getResponseCode() == 303) {
148 // everything is fine
151 else if (http.getResponseCode() == 200) {
152 // get the new cookie
154 Pattern reSessionCookie = Pattern.compile("_SESSION=([^;]+);.*");
156 Map<String, List<String>> header = http.getHeaderFields();
157 List<String> cookieHeader = header.get("Set-Cookie");
158 if (cookieHeader != null) {
159 String cookie = cookieHeader.get(0);
160 Matcher m = reSessionCookie.matcher(cookie);
162 session = m.group(1);
163 debug("network", "new session is " + session);
166 debug("network", "set-cookie: " + cookie);
168 "Response Set-Cookie headers is not a _SESSION one");
172 Log.w("network", "Response has no Set-Cookie header");
175 // the token needs to be updated
176 BufferedReader reader = new BufferedReader(new InputStreamReader(resp));
177 Pattern re = Pattern.compile(
178 "<input type=\"hidden\" name=\"_token\" value=\"([^\"]+)\">");
180 while ((line = reader.readLine()) != null) {
181 //debug("dump", line);
182 Matcher m = re.matcher(line);
185 debug("save-transaction", line);
186 debug("save-transaction", "Token=" + token);
187 return false; // retry
190 throw new IOException("Can't find _token string");
193 throw new IOException(
194 String.format("Error response code %d", http.getResponseCode()));
201 protected Void doInBackground(LedgerTransaction... ledgerTransactions) {
204 ltr = ledgerTransactions[0];
208 while (!legacySendOK()) {
211 throw new IOException(String.format("aborting after %d tries", tried));
216 catch (Exception e) {
218 error = e.getMessage();
225 protected void onPostExecute(Void aVoid) {
226 super.onPostExecute(aVoid);
227 taskCallback.done(error);