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
|
package org.fox.ttrss;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;
public class ApiCommon {
public static final String TAG = "ApiCommon";
private static final int API_STATUS_OK = 0;
private static final int API_STATUS_ERR = 1;
private static final MediaType TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
public interface ApiCaller {
void setStatusCode(int statusCode);
void setLastError(ApiError lastError);
void setLastErrorMessage(String message);
void notifyProgress(int progress);
}
public enum ApiError {
SUCCESS, UNKNOWN_ERROR, HTTP_UNAUTHORIZED, HTTP_FORBIDDEN, HTTP_NOT_FOUND, HTTP_BAD_REQUEST,
HTTP_SERVER_ERROR, HTTP_OTHER_ERROR, SSL_REJECTED, SSL_HOSTNAME_REJECTED, PARSE_ERROR, IO_ERROR, OTHER_ERROR, API_DISABLED,
API_UNKNOWN, LOGIN_FAILED, INVALID_URL, API_INCORRECT_USAGE, NETWORK_UNAVAILABLE, API_UNKNOWN_METHOD
}
public static int getErrorMessage(ApiError error) {
switch (error) {
case SUCCESS:
return R.string.error_success;
case UNKNOWN_ERROR:
return R.string.error_unknown;
case HTTP_UNAUTHORIZED:
return R.string.error_http_unauthorized;
case HTTP_BAD_REQUEST:
return R.string.error_bad_request;
case HTTP_FORBIDDEN:
return R.string.error_http_forbidden;
case HTTP_NOT_FOUND:
return R.string.error_http_not_found;
case HTTP_SERVER_ERROR:
return R.string.error_http_server_error;
case HTTP_OTHER_ERROR:
return R.string.error_http_other_error;
case SSL_REJECTED:
return R.string.error_ssl_rejected;
case SSL_HOSTNAME_REJECTED:
return R.string.error_ssl_hostname_rejected;
case PARSE_ERROR:
return R.string.error_parse_error;
case IO_ERROR:
return R.string.error_io_error;
case OTHER_ERROR:
return R.string.error_other_error;
case API_DISABLED:
return R.string.error_api_disabled;
case API_UNKNOWN:
return R.string.error_api_unknown;
case API_UNKNOWN_METHOD:
return R.string.error_api_unknown_method;
case LOGIN_FAILED:
return R.string.error_login_failed;
case INVALID_URL:
return R.string.error_invalid_api_url;
case API_INCORRECT_USAGE:
return R.string.error_api_incorrect_usage;
case NETWORK_UNAVAILABLE:
return R.string.error_network_unavailable;
default:
Log.d(TAG, "getErrorMessage: unknown error code=" + error);
return R.string.error_unknown;
}
}
static boolean isNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
return networkInfo != null && networkInfo.isConnected();
}
static JsonElement performRequest(Context context, @NonNull HashMap<String, String> m_params,
@NonNull ApiCommon.ApiCaller caller) {
try {
if (!ApiCommon.isNetworkAvailable(context)) {
caller.setLastError(ApiError.NETWORK_UNAVAILABLE);
return null;
}
SharedPreferences m_prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean m_transportDebugging = m_prefs.getBoolean("transport_debugging", false);
Gson gson = new Gson();
String payload = gson.toJson(new HashMap<>(m_params));
String apiUrl = m_prefs.getString("ttrss_url", "").trim() + "/api/";
if (m_transportDebugging) Log.d(TAG, ">>> " + payload + " -> " + apiUrl);
Request.Builder requestBuilder = new Request.Builder()
.url(apiUrl)
.header("User-Agent", getUserAgent(context))
.post(RequestBody.create(TYPE_JSON, payload));
String httpLogin = m_prefs.getString("http_login", "").trim();
String httpPassword = m_prefs.getString("http_password", "").trim();
if (!httpLogin.isEmpty()) {
if (m_transportDebugging) Log.d(TAG, "Using HTTP Basic authentication.");
requestBuilder.addHeader("Authorization", Credentials.basic(httpLogin, httpPassword));
}
Request request = requestBuilder.build();
ResponseProgressListener listener = new ResponseProgressListener() {
@Override
public void update(HttpUrl url, long bytesRead, long contentLength) {
// Log.d(TAG, "[progress] " + url + " " + bytesRead + " of " + contentLength);
if (contentLength > 0)
caller.notifyProgress((int) (bytesRead * 100f / contentLength));
}
};
/* lets shamelessly hijack OkHttpProgressGlideModule */
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addNetworkInterceptor(createInterceptor(listener))
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String payloadReceived = response.body().string();
if (m_transportDebugging) Log.d(TAG, "<<< " + payloadReceived);
JsonParser parser = new JsonParser();
JsonElement result = parser.parse(payloadReceived);
JsonObject resultObj = result.getAsJsonObject();
int statusCode = resultObj.get("status").getAsInt();
caller.setStatusCode(statusCode);
switch (statusCode) {
case API_STATUS_OK:
caller.setLastError(ApiError.SUCCESS);
return result.getAsJsonObject().get("content");
case API_STATUS_ERR:
JsonObject contentObj = resultObj.get("content").getAsJsonObject();
String error = contentObj.get("error").getAsString();
switch (error) {
case "LOGIN_ERROR":
case "NOT_LOGGED_IN":
caller.setLastError(ApiError.LOGIN_FAILED);
break;
case "API_DISABLED":
caller.setLastError(ApiError.API_DISABLED);
break;
case "INCORRECT_USAGE":
caller.setLastError(ApiError.API_INCORRECT_USAGE);
break;
case "UNKNOWN_METHOD":
caller.setLastError(ApiError.API_UNKNOWN_METHOD);
break;
default:
Log.d(TAG, "Unknown API error: " + error);
caller.setLastErrorMessage(error);
caller.setLastError(ApiError.API_UNKNOWN);
break;
}
}
} else {
switch (response.code()) {
case 400:
caller.setLastError(ApiError.HTTP_BAD_REQUEST);
break;
case 401:
caller.setLastError(ApiError.HTTP_UNAUTHORIZED);
break;
case 403:
caller.setLastError(ApiError.HTTP_FORBIDDEN);
break;
case 404:
caller.setLastError(ApiError.HTTP_NOT_FOUND);
break;
case 500:
case 501:
caller.setLastError(ApiError.HTTP_SERVER_ERROR);
break;
default:
Log.d(TAG, "HTTP response code: " + response.code());
caller.setLastErrorMessage("HTTP response code: " + response.code());
caller.setLastError(ApiError.HTTP_OTHER_ERROR);
break;
}
}
return null;
} catch (javax.net.ssl.SSLPeerUnverifiedException e) {
caller.setLastError(ApiError.SSL_REJECTED);
caller.setLastErrorMessage(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
caller.setLastError(ApiError.IO_ERROR);
caller.setLastErrorMessage(e.getMessage());
if (e.getMessage() != null) {
if (e.getMessage().matches("Hostname [^ ]+ was not verified")) {
caller.setLastError(ApiError.SSL_HOSTNAME_REJECTED);
}
}
e.printStackTrace();
} catch (com.google.gson.JsonSyntaxException e) {
caller.setLastError(ApiError.PARSE_ERROR);
caller.setLastErrorMessage(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
caller.setLastError(ApiError.OTHER_ERROR);
caller.setLastErrorMessage(e.getMessage());
e.printStackTrace();
}
return null;
}
private interface ResponseProgressListener {
void update(HttpUrl url, long bytesRead, long contentLength);
}
private static Interceptor createInterceptor(final ResponseProgressListener listener) {
return chain -> {
Request request = chain.request();
Response response = chain.proceed(request);
return response.newBuilder()
.body(new ProgressResponseBody(request.url(), response.body(), listener))
.build();
};
}
private static class ProgressResponseBody extends ResponseBody {
private final HttpUrl url;
private final ResponseBody responseBody;
private final ResponseProgressListener progressListener;
private BufferedSource bufferedSource;
public ProgressResponseBody(HttpUrl url, ResponseBody responseBody,
ResponseProgressListener progressListener) {
this.url = url;
this.responseBody = responseBody;
this.progressListener = progressListener;
}
@Override
public MediaType contentType() {
return responseBody.contentType();
}
@Override
public long contentLength() {
return responseBody.contentLength();
}
@Override
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(responseBody.source()));
}
return bufferedSource;
}
private Source source(Source source) {
return new ForwardingSource(source) {
long totalBytesRead = 0L;
@Override
public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead = super.read(sink, byteCount);
long fullLength = responseBody.contentLength();
if (bytesRead == -1) { // this source is exhausted
totalBytesRead = fullLength;
} else {
totalBytesRead += bytesRead;
}
progressListener.update(url, totalBytesRead, fullLength);
return bytesRead;
}
};
}
}
private static String getUserAgent(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager().
getPackageInfo(context.getPackageName(), 0);
return String.format(Locale.ENGLISH,
"Tiny Tiny RSS (Android) %1$s (%2$d) %3$s",
packageInfo.versionName,
packageInfo.versionCode,
System.getProperty("http.agent"));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return String.format(Locale.ENGLISH,
"Tiny Tiny RSS (Android) Unknown %1$s",
System.getProperty("http.agent"));
}
}
}
|