WIP: reduce duplicate code & cleanup

This commit is contained in:
Sefa Ilkimen
2019-03-22 02:06:39 +01:00
parent e8e1c4273f
commit ee30160921
15 changed files with 221 additions and 4296 deletions
@@ -0,0 +1,171 @@
package com.silkimen.cordovahttp;
import java.io.ByteArrayOutputStream;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import javax.net.ssl.SSLHandshakeException;
import com.silkimen.http.HttpBodyDecoder;
import com.silkimen.http.HttpRequest;
import com.silkimen.http.HttpRequest.HttpRequestException;
import com.silkimen.http.JsonUtils;
import org.apache.cordova.CallbackContext;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
abstract class CordovaHttpBase implements Runnable {
protected static final String TAG = "Cordova-Plugin-HTTP";
protected String method;
protected String url;
protected String serializer = "none";
protected Object data;
protected JSONObject params;
protected JSONObject headers;
protected int timeout;
protected CallbackContext callbackContext;
public CordovaHttpBase(String method, String url, String serializer, Object data, JSONObject headers, int timeout,
CallbackContext callbackContext) {
this.method = method;
this.url = url;
this.serializer = serializer;
this.data = data;
this.headers = headers;
this.timeout = timeout;
this.callbackContext = callbackContext;
}
public CordovaHttpBase(String method, String url, JSONObject params, JSONObject headers, int timeout,
CallbackContext callbackContext) {
this.method = method;
this.url = url;
this.params = params;
this.headers = headers;
this.timeout = timeout;
this.callbackContext = callbackContext;
}
@Override
public void run() {
CordovaHttpResponse response = new CordovaHttpResponse();
try {
HttpRequest request = this.createRequest();
this.prepareRequest(request);
this.sendBody(request);
this.processResponse(request, response);
} catch (HttpRequestException e) {
if (e.getCause() instanceof SSLHandshakeException) {
response.setStatus(-2);
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
Log.w(TAG, "SSL handshake failed", e);
} else if (e.getCause() instanceof UnknownHostException) {
response.setStatus(-3);
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
Log.w(TAG, "Host could not be resolved", e);
} else if (e.getCause() instanceof SocketTimeoutException) {
response.setStatus(-4);
response.setErrorMessage("Request timed out: " + e.getMessage());
Log.w(TAG, "Request timed out", e);
} else {
response.setStatus(-1);
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
Log.w(TAG, "Generic request error", e);
}
} catch (Exception e) {
response.setStatus(-1);
response.setErrorMessage(e.getMessage());
Log.e(TAG, "An unexpected error occured", e);
}
try {
if (response.hasFailed()) {
this.callbackContext.error(response.toJSON());
} else {
this.callbackContext.success(response.toJSON());
}
} catch (JSONException e) {
Log.e(TAG, "An unexpected error occured while creating HTTP response object", e);
}
}
protected HttpRequest createRequest() throws JSONException {
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
HttpRequest request = new HttpRequest(processedUrl, this.method);
return request;
}
protected void prepareRequest(HttpRequest request) throws JSONException {
request.followRedirects(true /* @TODO */);
request.readTimeout(this.timeout);
request.acceptCharset("UTF-8");
request.uncompress(true);
// setup content type before applying headers, so user can override it
this.setContentType(request);
request.headers(JsonUtils.getStringMap(this.headers));
}
protected void setContentType(HttpRequest request) {
switch (this.serializer) {
case "json":
request.contentType("application/json", "UTF-8");
break;
case "utf8":
request.contentType("text/plain", "UTF-8");
break;
case "urlencoded":
// intentionally left blank, because content type is set in HttpRequest.form()
break;
}
}
protected void sendBody(HttpRequest request) throws Exception {
if (this.data == null) {
return;
}
switch (this.serializer) {
case "json":
request.send(this.data.toString());
break;
case "utf8":
request.send(((JSONObject) this.data).getString("text"));
break;
case "urlencoded":
request.form(JsonUtils.getObjectMap((JSONObject) this.data));
break;
}
}
protected void processResponse(HttpRequest request, CordovaHttpResponse response) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
request.receive(outputStream);
ByteBuffer rawOutput = ByteBuffer.wrap(outputStream.toByteArray());
String decodedBody = HttpBodyDecoder.decodeBody(rawOutput, request.charset());
response.setStatus(request.code());
response.setUrl(request.url().toString());
response.setHeaders(request.headers());
if (request.code() >= 200 && request.code() < 300) {
response.setBody(decodedBody);
} else {
response.setErrorMessage(decodedBody);
}
}
}
@@ -1,105 +1,38 @@
package com.silkimen.cordovahttp;
import java.io.File;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.net.ssl.SSLHandshakeException;
import com.silkimen.http.HttpBodyDecoder;
import com.silkimen.http.HttpRequest;
import com.silkimen.http.HttpRequest.HttpRequestException;
import com.silkimen.http.JsonUtils;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.file.FileUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
class CordovaHttpDownload implements Runnable {
private static final String TAG = "Cordova-Plugin-HTTP";
private String url;
private JSONObject params;
private JSONObject headers;
class CordovaHttpDownload extends CordovaHttpBase {
private String filePath;
private int timeout;
private CallbackContext callbackContext;
public CordovaHttpDownload(String url, JSONObject params, JSONObject headers, String filePath, int timeout,
CallbackContext callbackContext) {
this.url = url;
this.params = params;
this.headers = headers;
super("GET", url, params, headers, timeout, callbackContext);
this.filePath = filePath;
this.timeout = timeout;
this.callbackContext = callbackContext;
}
@Override
public void run() {
CordovaHttpResponse response = new CordovaHttpResponse();
protected void processResponse(HttpRequest request, CordovaHttpResponse response) throws Exception {
response.setStatus(request.code());
response.setUrl(request.url().toString());
response.setHeaders(request.headers());
try {
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
if (request.code() >= 200 && request.code() < 300) {
File file = new File(new URI(this.filePath));
JSONObject fileEntry = FileUtils.getFilePlugin().getEntryForFile(file);
HttpRequest request = new HttpRequest(processedUrl, "GET")
.followRedirects(true /* @TODO */)
.readTimeout(this.timeout)
.acceptCharset("UTF-8")
.uncompress(true)
.headers(JsonUtils.getStringMap(this.headers));
response.setStatus(request.code());
response.setUrl(request.url().toString());
response.setHeaders(request.headers());
if (request.code() >= 200 && request.code() < 300) {
File file = new File(new URI(filePath));
request.receive(file);
response.setFileEntry(FileUtils.getFilePlugin().getEntryForFile(file));
} else {
response.setErrorMessage("There was an error downloading the file");
}
} catch (HttpRequestException e) {
if (e.getCause() instanceof SSLHandshakeException) {
response.setStatus(-2);
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
Log.w(TAG, "SSL handshake failed", e);
} else if (e.getCause() instanceof UnknownHostException) {
response.setStatus(-3);
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
Log.w(TAG, "Host could not be resolved", e);
} else if (e.getCause() instanceof SocketTimeoutException) {
response.setStatus(-4);
response.setErrorMessage("Request timed out: " + e.getMessage());
Log.w(TAG, "Request timed out", e);
} else {
response.setStatus(-1);
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
Log.w(TAG, "Generic request error", e);
}
} catch (Exception e) {
response.setStatus(-1);
response.setErrorMessage(e.getMessage());
Log.e(TAG, "An unexpected error occured", e);
}
try {
if (response.hasFailed()) {
this.callbackContext.error(response.toJSON());
} else {
this.callbackContext.success(response.toJSON());
}
} catch (JSONException e) {
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
request.receive(file);
response.setFileEntry(fileEntry);
} else {
response.setErrorMessage("There was an error downloading the file");
}
}
}
@@ -0,0 +1,18 @@
package com.silkimen.cordovahttp;
import org.apache.cordova.CallbackContext;
import org.json.JSONObject;
class CordovaHttpOperation extends CordovaHttpBase {
public CordovaHttpOperation(String method, String url, String serializer, Object data, JSONObject headers,
int timeout, CallbackContext callbackContext) {
super(method, url, serializer, data, headers, timeout, callbackContext);
}
public CordovaHttpOperation(String method, String url, JSONObject params, JSONObject headers, int timeout,
CallbackContext callbackContext) {
super(method, url, params, headers, timeout, callbackContext);
}
}
@@ -83,10 +83,10 @@ public class CordovaHttpPlugin extends CordovaPlugin {
JSONObject headers = args.getJSONObject(2);
int timeout = args.getInt(3) * 1000;
CordovaHttpRequest get = new CordovaHttpRequest(method.toUpperCase(), url, params, headers, timeout,
CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, params, headers, timeout,
callbackContext);
cordova.getThreadPool().execute(get);
cordova.getThreadPool().execute(request);
return true;
}
@@ -100,7 +100,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
JSONObject headers = args.getJSONObject(3);
int timeout = args.getInt(4) * 1000;
CordovaHttpRequest request = new CordovaHttpRequest(method.toUpperCase(), url, serializer, data, headers, timeout,
CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, serializer, data, headers, timeout,
callbackContext);
cordova.getThreadPool().execute(request);
@@ -1,156 +0,0 @@
package com.silkimen.cordovahttp;
import java.io.ByteArrayOutputStream;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import javax.net.ssl.SSLHandshakeException;
import com.silkimen.http.HttpBodyDecoder;
import com.silkimen.http.HttpRequest;
import com.silkimen.http.HttpRequest.HttpRequestException;
import com.silkimen.http.JsonUtils;
import org.apache.cordova.CallbackContext;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class CordovaHttpRequest implements Runnable {
private static final String TAG = "Cordova-Plugin-HTTP";
private String method;
private String url;
private String serializer = "none";
private Object data;
private JSONObject params;
private JSONObject headers;
private int timeout;
private CallbackContext callbackContext;
public CordovaHttpRequest(String method, String url, String serializer, Object data, JSONObject headers,
int timeout, CallbackContext callbackContext) {
this.method = method;
this.url = url;
this.serializer = serializer;
this.data = data;
this.headers = headers;
this.timeout = timeout;
this.callbackContext = callbackContext;
}
public CordovaHttpRequest(String method, String url, JSONObject params, JSONObject headers, int timeout,
CallbackContext callbackContext) {
this.method = method;
this.url = url;
this.params = params;
this.headers = headers;
this.timeout = timeout;
this.callbackContext = callbackContext;
}
@Override
public void run() {
CordovaHttpResponse response = new CordovaHttpResponse();
try {
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HttpRequest request = new HttpRequest(processedUrl, this.method)
.followRedirects(true /* @TODO */)
.readTimeout(this.timeout)
.acceptCharset("UTF-8")
.uncompress(true);
// setup content type before applying headers, so user can override it
this.setContentType(request)
.headers(JsonUtils.getStringMap(this.headers));
this.sendBody(request)
.receive(outputStream);
ByteBuffer rawOutput = ByteBuffer.wrap(outputStream.toByteArray());
String decodedBody = HttpBodyDecoder.decodeBody(rawOutput, request.charset());
response.setStatus(request.code());
response.setUrl(request.url().toString());
response.setHeaders(request.headers());
if (request.code() >= 200 && request.code() < 300) {
response.setBody(decodedBody);
} else {
response.setErrorMessage(decodedBody);
}
} catch (HttpRequestException e) {
if (e.getCause() instanceof SSLHandshakeException) {
response.setStatus(-2);
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
Log.w(TAG, "SSL handshake failed", e);
} else if (e.getCause() instanceof UnknownHostException) {
response.setStatus(-3);
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
Log.w(TAG, "Host could not be resolved", e);
} else if (e.getCause() instanceof SocketTimeoutException) {
response.setStatus(-4);
response.setErrorMessage("Request timed out: " + e.getMessage());
Log.w(TAG, "Request timed out", e);
} else {
response.setStatus(-1);
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
Log.w(TAG, "Generic request error", e);
}
} catch (Exception e) {
response.setStatus(-1);
response.setErrorMessage(e.getMessage());
Log.e(TAG, "An unexpected error occured", e);
}
try {
if (response.hasFailed()) {
this.callbackContext.error(response.toJSON());
} else {
this.callbackContext.success(response.toJSON());
}
} catch (JSONException e) {
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
}
}
private HttpRequest setContentType(HttpRequest request) {
switch(this.serializer) {
case "json":
return request.contentType("application/json", "UTF-8");
case "utf8":
return request.contentType("text/plain", "UTF-8");
case "urlencoded":
// intentionally left blank, because content type is set in HttpRequest.form()
}
return request;
}
private HttpRequest sendBody(HttpRequest request) throws JSONException {
if (this.data == null) {
return request;
}
switch (this.serializer) {
case "json":
return request.send(this.data.toString());
case "utf8":
return request.send(((JSONObject) this.data).getString("text"));
case "urlencoded":
return request.form(JsonUtils.getObjectMap((JSONObject) this.data));
}
return request;
}
}
@@ -1,128 +1,39 @@
package com.silkimen.cordovahttp;
import java.io.ByteArrayOutputStream;
import java.io.File;
import android.webkit.MimeTypeMap;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import javax.net.ssl.SSLHandshakeException;
import com.silkimen.http.HttpBodyDecoder;
import com.silkimen.http.HttpRequest;
import com.silkimen.http.HttpRequest.HttpRequestException;
import com.silkimen.http.JsonUtils;
import java.io.File;
import java.net.URI;
import org.apache.cordova.CallbackContext;
import org.json.JSONException;
import org.json.JSONObject;
import android.webkit.MimeTypeMap;
import android.util.Log;
class CordovaHttpUpload implements Runnable {
private static final String TAG = "Cordova-Plugin-HTTP";
private String url;
private JSONObject params;
private JSONObject headers;
class CordovaHttpUpload extends CordovaHttpBase {
private String filePath;
private String uploadName;
private int timeout;
private CallbackContext callbackContext;
public CordovaHttpUpload(String url, JSONObject params, JSONObject headers, String filePath, String uploadName,
int timeout, CallbackContext callbackContext) {
this.url = url;
this.params = params;
this.headers = headers;
super("POST", url, params, headers, timeout, callbackContext);
this.filePath = filePath;
this.uploadName = uploadName;
this.timeout = timeout;
this.callbackContext = callbackContext;
}
@Override
public void run() {
CordovaHttpResponse response = new CordovaHttpResponse();
protected void sendBody(HttpRequest request) throws Exception {
int filenameIndex = this.filePath.lastIndexOf('/');
String filename = this.filePath.substring(filenameIndex + 1);
try {
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int extIndex = this.filePath.lastIndexOf('.');
String ext = this.filePath.substring(extIndex + 1);
HttpRequest request = new HttpRequest(processedUrl, "POST")
.followRedirects(true /* @TODO */)
.readTimeout(this.timeout)
.acceptCharset("UTF-8")
.uncompress(true)
.headers(JsonUtils.getStringMap(this.headers));
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
int filenameIndex = filePath.lastIndexOf('/');
String filename = filePath.substring(filenameIndex + 1);
int extIndex = filePath.lastIndexOf('.');
String ext = filePath.substring(extIndex + 1);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
request.part(this.uploadName, filename, mimeType, new File(new URI(this.filePath)))
.receive(outputStream);
ByteBuffer rawOutput = ByteBuffer.wrap(outputStream.toByteArray());
String decodedBody = HttpBodyDecoder.decodeBody(rawOutput, request.charset());
response.setStatus(request.code());
response.setUrl(request.url().toString());
response.setHeaders(request.headers());
if (request.code() >= 200 && request.code() < 300) {
response.setBody(decodedBody);
} else {
response.setErrorMessage(decodedBody);
}
} catch (HttpRequestException e) {
if (e.getCause() instanceof SSLHandshakeException) {
response.setStatus(-2);
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
Log.w(TAG, "SSL handshake failed", e);
} else if (e.getCause() instanceof UnknownHostException) {
response.setStatus(-3);
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
Log.w(TAG, "Host could not be resolved", e);
} else if (e.getCause() instanceof SocketTimeoutException) {
response.setStatus(-4);
response.setErrorMessage("Request timed out: " + e.getMessage());
Log.w(TAG, "Request timed out", e);
} else {
response.setStatus(-1);
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
Log.w(TAG, "Generic request error", e);
}
} catch (URISyntaxException e) {
response.setStatus(-1);
response.setErrorMessage("An error occured while loading file");
Log.e(TAG, "An error occured while loading file", e);
} catch (Exception e) {
response.setStatus(-1);
response.setErrorMessage(e.getMessage());
Log.e(TAG, "An unexpected error occured", e);
}
try {
if (response.hasFailed()) {
this.callbackContext.error(response.toJSON());
} else {
this.callbackContext.success(response.toJSON());
}
} catch (JSONException e) {
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
}
request.part(this.uploadName, filename, mimeType, new File(new URI(this.filePath)));
}
}