tharak-android 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -0
- package/build.gradle +45 -0
- package/consumer-rules.pro +0 -0
- package/libs/vital-sdk.aar +0 -0
- package/package.json +22 -0
- package/proguard-rules.pro +21 -0
- package/src/androidTest/androidTest.iml +11 -0
- package/src/androidTest/java/org/tharak/core/ExampleInstrumentedTest.java +27 -0
- package/src/androidTest/java/org/thiragatisoft/core/ExampleInstrumentedTest.java +26 -0
- package/src/main/AndroidManifest.xml +5 -0
- package/src/main/java/org/thiragatisoft/core/AppUtils.java +31 -0
- package/src/main/java/org/thiragatisoft/core/FileSelector.java +162 -0
- package/src/main/java/org/thiragatisoft/core/MainActivity.java +27 -0
- package/src/main/java/org/thiragatisoft/core/RestClient.java +298 -0
- package/src/main/java/org/thiragatisoft/core/VitalActivity.java +612 -0
- package/src/main/java/org/thiragatisoft/core/VitalsPlugin.java +15 -0
- package/src/main/java/org/thiragatisoft/core/VolleyMultipartRequest.java +218 -0
- package/src/main/java/org/thiragatisoft/core/http/CapacitorCookieManager.java +175 -0
- package/src/main/java/org/thiragatisoft/core/http/CapacitorHttpUrlConnection.java +408 -0
- package/src/main/java/org/thiragatisoft/core/http/FilesystemUtils.java +65 -0
- package/src/main/java/org/thiragatisoft/core/http/FormUploader.java +219 -0
- package/src/main/java/org/thiragatisoft/core/http/Http.java +275 -0
- package/src/main/java/org/thiragatisoft/core/http/HttpRequestHandler.java +515 -0
- package/src/main/java/org/thiragatisoft/core/http/ICapacitorHttpUrlConnection.java +15 -0
- package/src/main/java/org/thiragatisoft/core/http/JSValue.java +68 -0
- package/src/main/java/org/thiragatisoft/core/http/MimeType.java +17 -0
- package/src/main/main.iml +12 -0
- package/src/test/java/org/tharak/ExampleUnitTest.java +17 -0
- package/src/test/java/org/tharak/JSObjectTest.java +209 -0
- package/src/test/java/org/tharak/PluginMethodHandleTest.java +44 -0
- package/src/test/java/org/tharak/util/HostMaskTest.java +70 -0
- package/src/test/java/org/thiragatisoft/core/ExampleUnitTest.java +17 -0
- package/src/test/test.iml +11 -0
@@ -0,0 +1,298 @@
|
|
1
|
+
package org.thiragatisoft.core;
|
2
|
+
|
3
|
+
import android.app.Activity;
|
4
|
+
import android.net.Uri;
|
5
|
+
|
6
|
+
import com.android.volley.AuthFailureError;
|
7
|
+
import com.android.volley.NetworkResponse;
|
8
|
+
import com.android.volley.Request;
|
9
|
+
import com.android.volley.RequestQueue;
|
10
|
+
import com.android.volley.Response;
|
11
|
+
import com.android.volley.ServerError;
|
12
|
+
import com.android.volley.VolleyError;
|
13
|
+
import com.android.volley.VolleyLog;
|
14
|
+
import com.android.volley.toolbox.HttpHeaderParser;
|
15
|
+
import com.android.volley.toolbox.JsonObjectRequest;
|
16
|
+
import com.android.volley.toolbox.StringRequest;
|
17
|
+
import com.android.volley.toolbox.Volley;
|
18
|
+
import com.getcapacitor.CapConfig;
|
19
|
+
import com.getcapacitor.JSObject;
|
20
|
+
import com.getcapacitor.NativePlugin;
|
21
|
+
import com.getcapacitor.Plugin;
|
22
|
+
import com.getcapacitor.PluginCall;
|
23
|
+
import com.getcapacitor.PluginMethod;
|
24
|
+
import com.getcapacitor.PluginResult;
|
25
|
+
|
26
|
+
import org.json.JSONObject;
|
27
|
+
|
28
|
+
import java.io.ByteArrayOutputStream;
|
29
|
+
import java.io.FileNotFoundException;
|
30
|
+
import java.io.InputStream;
|
31
|
+
import java.io.UnsupportedEncodingException;
|
32
|
+
import java.util.HashMap;
|
33
|
+
import java.util.Map;
|
34
|
+
|
35
|
+
@NativePlugin
|
36
|
+
public class RestClient extends Plugin {
|
37
|
+
|
38
|
+
@PluginMethod
|
39
|
+
public void invoke(PluginCall call) {
|
40
|
+
JSObject request = call.getObject("request");
|
41
|
+
String method = request.getString("method");
|
42
|
+
switch (method){
|
43
|
+
case "POST":
|
44
|
+
performBodyCall(call, Request.Method.POST);
|
45
|
+
break;
|
46
|
+
case "PUT":
|
47
|
+
performBodyCall(call, Request.Method.PUT);
|
48
|
+
break;
|
49
|
+
case "PATCH":
|
50
|
+
performBodyCall(call, Request.Method.PATCH);
|
51
|
+
break;
|
52
|
+
case "GET":
|
53
|
+
performUrlCall(call, Request.Method.GET);
|
54
|
+
break;
|
55
|
+
case "DELETE":
|
56
|
+
performUrlCall(call, Request.Method.DELETE);
|
57
|
+
break;
|
58
|
+
default:
|
59
|
+
performUrlCall(call, Request.Method.GET);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
public void performUrlCall(PluginCall call, int method) {
|
63
|
+
JSObject request = call.getObject("request");
|
64
|
+
String requestURL = request.getString("url");
|
65
|
+
String responseType = request.getString("responseType");
|
66
|
+
JSONObject options = call.getObject("headers");
|
67
|
+
RequestQueue queue = Volley.newRequestQueue(bridge.getContext());
|
68
|
+
JsonObjectRequest stringRequest = new JsonObjectRequest(method, requestURL, null, new Response.Listener<JSONObject>() {
|
69
|
+
@Override
|
70
|
+
public void onResponse(JSONObject response) {
|
71
|
+
// Display the first 500 characters of the response string.
|
72
|
+
try {
|
73
|
+
call.success(new JSObject(response.toString()));
|
74
|
+
}catch (Exception ex){
|
75
|
+
ex.printStackTrace();
|
76
|
+
}
|
77
|
+
}
|
78
|
+
}, new Response.ErrorListener() {
|
79
|
+
@Override
|
80
|
+
public void onErrorResponse(VolleyError error) {
|
81
|
+
RestClient.addError(call, error);
|
82
|
+
}
|
83
|
+
}) {
|
84
|
+
//
|
85
|
+
@Override
|
86
|
+
public Map<String, String> getHeaders() {
|
87
|
+
return RestClient.getHeaders(options);
|
88
|
+
}
|
89
|
+
};
|
90
|
+
// Add the request to the RequestQueue.
|
91
|
+
queue.add(stringRequest);
|
92
|
+
}
|
93
|
+
public void performBodyCall(PluginCall call, int method) {
|
94
|
+
JSObject request = call.getObject("request");
|
95
|
+
String requestURL = request.getString("url");
|
96
|
+
String responseType = request.getString("responseType");
|
97
|
+
RequestQueue queue = Volley.newRequestQueue(bridge.getContext());
|
98
|
+
Request volyRequest = findRequest(call, method, requestURL, this.getActivity());
|
99
|
+
// Add the request to the RequestQueue.
|
100
|
+
queue.add(volyRequest);
|
101
|
+
}
|
102
|
+
private static Request findRequest(PluginCall call, int method, String requestURL, Activity activity) {
|
103
|
+
try {
|
104
|
+
JSONObject request = call.getObject("request");
|
105
|
+
JSONObject options = call.getObject("headers");
|
106
|
+
String ignoreType = options.getString("Ignore-Type");
|
107
|
+
String contentType = options.getString("Content-Type");
|
108
|
+
if("Y".equalsIgnoreCase(ignoreType)){
|
109
|
+
contentType = "multipart/formdata";
|
110
|
+
}
|
111
|
+
if(contentType == null)
|
112
|
+
contentType = "application/json";
|
113
|
+
if(contentType.contains("application/json")){
|
114
|
+
JSONObject body = null;
|
115
|
+
if(method != Request.Method.GET && method != Request.Method.DELETE){
|
116
|
+
body = request.getJSONObject("body");
|
117
|
+
}
|
118
|
+
return new JsonObjectRequest(method, requestURL, body, new Response.Listener<JSONObject>() {
|
119
|
+
@Override
|
120
|
+
public void onResponse(JSONObject response) {
|
121
|
+
// Display the first 500 characters of the response string.
|
122
|
+
try {
|
123
|
+
call.success(new JSObject(response.toString()));
|
124
|
+
}catch (Exception ex){
|
125
|
+
ex.printStackTrace();
|
126
|
+
}
|
127
|
+
}
|
128
|
+
}, new Response.ErrorListener() {
|
129
|
+
@Override
|
130
|
+
public void onErrorResponse(VolleyError error) {
|
131
|
+
RestClient.addError(call, error);
|
132
|
+
}
|
133
|
+
}){
|
134
|
+
//
|
135
|
+
@Override
|
136
|
+
public Map<String, String> getHeaders() {
|
137
|
+
return RestClient.getHeaders(options);
|
138
|
+
}
|
139
|
+
};
|
140
|
+
}else if(contentType.contains("multipart/")){
|
141
|
+
JSONObject body = null;
|
142
|
+
body = request.getJSONObject("body");
|
143
|
+
String fileName = body.getString("fileName");
|
144
|
+
|
145
|
+
JSONObject finalBody = body;
|
146
|
+
return new VolleyMultipartRequest(method, requestURL, new Response.Listener<NetworkResponse>() {
|
147
|
+
@Override
|
148
|
+
public void onResponse(NetworkResponse response) {
|
149
|
+
String resultResponse = new String(response.data);
|
150
|
+
try {
|
151
|
+
call.success(new JSObject(resultResponse));
|
152
|
+
} catch (Exception e) {
|
153
|
+
e.printStackTrace();
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}, new Response.ErrorListener() {
|
157
|
+
@Override
|
158
|
+
public void onErrorResponse(VolleyError error) {
|
159
|
+
RestClient.addError(call, error);
|
160
|
+
}
|
161
|
+
}) {
|
162
|
+
@Override
|
163
|
+
protected Map<String, String> getParams() {
|
164
|
+
Map<String, String> params = new HashMap<>();
|
165
|
+
try {
|
166
|
+
if (finalBody != null) {
|
167
|
+
for (int idx = 0; idx < finalBody.names().length(); ++idx) {
|
168
|
+
String key = finalBody.names().getString(idx);
|
169
|
+
if("fileBase64".equalsIgnoreCase(key))
|
170
|
+
continue;
|
171
|
+
params.put(key, finalBody.getString(key));
|
172
|
+
}
|
173
|
+
}
|
174
|
+
}catch (Exception ex){
|
175
|
+
ex.printStackTrace();
|
176
|
+
}
|
177
|
+
// params.put("file", new DataPart(fileName, RestClient.getFileData(fileName)));
|
178
|
+
return params;
|
179
|
+
}
|
180
|
+
|
181
|
+
@Override
|
182
|
+
protected Map<String, DataPart> getByteData() {
|
183
|
+
Map<String, DataPart> params = new HashMap<>();
|
184
|
+
try {
|
185
|
+
String fileData = finalBody.getString("fileBase64");
|
186
|
+
params.put("file", new DataPart(fileName, fileData.getBytes()));
|
187
|
+
}catch (Exception ex){
|
188
|
+
ex.printStackTrace();
|
189
|
+
}
|
190
|
+
// file name could found file base or direct access from real path
|
191
|
+
// for now just get bitmap data from ImageView
|
192
|
+
//params.put("file", new DataPart(fileName, RestClient.getFileData(fileName, activity)));
|
193
|
+
return params;
|
194
|
+
}
|
195
|
+
@Override
|
196
|
+
public Map<String, String> getHeaders() {
|
197
|
+
return RestClient.getHeaders(options);
|
198
|
+
}
|
199
|
+
};
|
200
|
+
}else {
|
201
|
+
return new StringRequest(method, requestURL, new Response.Listener<String>() {
|
202
|
+
@Override
|
203
|
+
public void onResponse(String response) {
|
204
|
+
// Display the first 500 characters of the response string.
|
205
|
+
try {
|
206
|
+
call.success(new JSObject(response));
|
207
|
+
} catch (Exception ex) {
|
208
|
+
ex.printStackTrace();
|
209
|
+
}
|
210
|
+
}
|
211
|
+
}, new Response.ErrorListener() {
|
212
|
+
@Override
|
213
|
+
public void onErrorResponse(VolleyError error) {
|
214
|
+
RestClient.addError(call, error);
|
215
|
+
}
|
216
|
+
}){
|
217
|
+
//
|
218
|
+
@Override
|
219
|
+
public Map<String, String> getHeaders() {
|
220
|
+
return RestClient.getHeaders(options);
|
221
|
+
}
|
222
|
+
@Override
|
223
|
+
public String getBodyContentType() {
|
224
|
+
String bodyType = "";
|
225
|
+
try {
|
226
|
+
bodyType = options.getString("content-type") + "; charset=utf-8";
|
227
|
+
}catch (Exception ex){
|
228
|
+
ex.printStackTrace();
|
229
|
+
}
|
230
|
+
return bodyType;
|
231
|
+
}
|
232
|
+
@Override
|
233
|
+
public byte[] getBody() throws AuthFailureError {
|
234
|
+
String mRequestBody = "";
|
235
|
+
try {
|
236
|
+
mRequestBody = request.getString("body");
|
237
|
+
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
|
238
|
+
} catch (Exception uee) {
|
239
|
+
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
|
240
|
+
return null;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
};
|
244
|
+
}
|
245
|
+
}catch (Exception ex){
|
246
|
+
ex.printStackTrace();
|
247
|
+
}
|
248
|
+
return null;
|
249
|
+
}
|
250
|
+
private static void addError(PluginCall call, VolleyError error) {
|
251
|
+
try {
|
252
|
+
// As of f605da3 the following should work
|
253
|
+
NetworkResponse response = error.networkResponse;
|
254
|
+
if (error instanceof ServerError && response != null) {
|
255
|
+
try {
|
256
|
+
String res = new String(response.data,
|
257
|
+
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
|
258
|
+
// Now you can use any deserializer to make sense of data
|
259
|
+
System.out.println(res);
|
260
|
+
error = new VolleyError(res);
|
261
|
+
} catch (UnsupportedEncodingException e1) {
|
262
|
+
// Couldn't properly decode data to string
|
263
|
+
e1.printStackTrace();
|
264
|
+
}
|
265
|
+
}
|
266
|
+
int errorCode = 500;
|
267
|
+
if(response != null){
|
268
|
+
errorCode = response.statusCode;
|
269
|
+
}
|
270
|
+
/*PluginResult errorResult = new PluginResult();
|
271
|
+
errorResult.put("message", "Error");
|
272
|
+
errorResult.put("code", errorCode);
|
273
|
+
errorResult.put("status", errorCode);
|
274
|
+
call.successCallback(errorResult);*/
|
275
|
+
call.error("Error", String.valueOf(errorCode), error);
|
276
|
+
}catch (Exception ex){
|
277
|
+
ex.printStackTrace();
|
278
|
+
}
|
279
|
+
}
|
280
|
+
|
281
|
+
private static Map<String, String> getHeaders(JSONObject options) {
|
282
|
+
HashMap<String, String> headers = new HashMap<>();
|
283
|
+
String ignoreType = null;
|
284
|
+
try {
|
285
|
+
ignoreType = options.getString("Ignore-Type");
|
286
|
+
for (int idx = 0; idx < options.names().length(); ++idx) {
|
287
|
+
String key = options.names().getString(idx);
|
288
|
+
headers.put(key, options.getString(key));
|
289
|
+
}
|
290
|
+
}catch (Exception ex){
|
291
|
+
ex.printStackTrace();
|
292
|
+
}
|
293
|
+
if(!headers.containsKey("Content-Type") && !headers.containsKey("content-type") && !"Y".equalsIgnoreCase(ignoreType)){
|
294
|
+
headers.put("Content-Type", "application/json; charset=utf-8");
|
295
|
+
}
|
296
|
+
return headers;
|
297
|
+
}
|
298
|
+
}
|