Skip to content

Commit

Permalink
setTimeout method Added
Browse files Browse the repository at this point in the history
  • Loading branch information
mourice committed Sep 8, 2017
1 parent fac464b commit c047dcc
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public abstract class CordovaHttp {
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
private static AtomicBoolean cacheResults = new AtomicBoolean(false);
private static AtomicBoolean validateDomainName = new AtomicBoolean(true);
private static AtomicInteger connectionTimeout = new AtomicInteger(0);

private String urlString;
private Map<?, ?> params;
Expand Down Expand Up @@ -83,6 +84,10 @@ public static void validateDomainName(boolean accept) {
validateDomainName.set(accept);
}

public static void setTimeout(int cTimeout) {
connectionTimeout.set(cTimeout);
}

protected String getUrlString() {
return this.urlString;
}
Expand Down Expand Up @@ -115,6 +120,11 @@ protected HttpRequest setupSecurity(HttpRequest request) {
}
return request;
}

protected HttpRequest setupTimeouts(HttpRequest request) {
request.connectTimeout(connectionTimeout.get());
return request;
}

protected void respondWithError(int status, String msg) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void run() {
try {
HttpRequest request = HttpRequest.delete(this.getUrlString(), this.getParams(), false);
this.setupSecurity(request);
this.setupTimeouts(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
int code = request.code();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void run() {
try {
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true);
this.setupSecurity(request);
this.setupTimeouts(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
int code = request.code();
Expand Down
1 change: 1 addition & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void run() {
try {
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), false);
this.setupSecurity(request);
this.setupTimeouts(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
request.useCaches(this.getCacheResults());
Expand Down
1 change: 1 addition & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void run() {
try {
HttpRequest request = HttpRequest.head(this.getUrlString(), this.getParams(), true);
this.setupSecurity(request);
this.setupTimeouts(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
int code = request.code();
Expand Down
4 changes: 4 additions & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
boolean accept = args.getBoolean(0);
CordovaHttp.validateDomainName(accept);
callbackContext.success();
} else if (action.equals("setTimeout")) {
int connectionTimeout = args.getInt(0);
CordovaHttp.setTimeout(connectionTimeout);
callbackContext.success();
} else if (action.equals("uploadFile")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Expand Down
1 change: 1 addition & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void run() {
try {
HttpRequest request = HttpRequest.post(this.getUrlString());
this.setupSecurity(request);
this.setupTimeouts(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
request.form(this.getParams());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void run() {
try {
HttpRequest request = HttpRequest.post(this.getUrlString());
this.setupSecurity(request);

this.setupTimeouts(request);
request.headers(this.getHeaders());
request.acceptJson();
request.contentType(HttpRequest.CONTENT_TYPE_JSON);
Expand Down
1 change: 1 addition & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpPut.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void run() {
try {
HttpRequest request = HttpRequest.put(this.getUrlString());
this.setupSecurity(request);
this.setupTimeouts(request);
request.headers(this.getHeaders());
request.acceptJson();
request.contentType(HttpRequest.CONTENT_TYPE_JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void run() {
try {
HttpRequest request = HttpRequest.post(this.getUrlString());
this.setupSecurity(request);
this.setupTimeouts(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
URI uri = new URI(filePath);
Expand Down
2 changes: 2 additions & 0 deletions src/ios/CordovaHttpPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
- (void)downloadFile:(CDVInvokedUrlCommand*)command;
- (void)put:(CDVInvokedUrlCommand*)command;
- (void)delete:(CDVInvokedUrlCommand*)command;
- (void)setTimeout:(CDVInvokedUrlCommand*)command;

@end
12 changes: 11 additions & 1 deletion src/ios/CordovaHttpPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ - (void)validateDomainName:(CDVInvokedUrlCommand*)command {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)setTimeout:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;

int timeoutValue = [[command.arguments objectAtIndex:0] intValue];

[manager.requestSerializer setTimeoutInterval:timeoutValue];

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)post:(CDVInvokedUrlCommand*)command {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy = securityPolicy;
Expand Down Expand Up @@ -161,7 +172,6 @@ - (void)put:(CDVInvokedUrlCommand*)command {

}


- (void)get:(CDVInvokedUrlCommand*)command {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy = securityPolicy;
Expand Down
6 changes: 6 additions & 0 deletions www/cordovaHTTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ var http = {
acceptAllCerts: function(allow, success, failure) {
return exec(success, failure, "CordovaHttpPlugin", "acceptAllCerts", [allow]);
},
setTimeout: function(connectionTimeout, success, failure) {
return exec(success, failure, "CordovaHttpPlugin", "setTimeout", [connectionTimeout]);
},
validateDomainName: function(validate, success, failure) {
return exec(success, failure, "CordovaHttpPlugin", "validateDomainName", [validate]);
},
Expand Down Expand Up @@ -167,6 +170,9 @@ if (typeof angular !== "undefined") {
acceptAllCerts: function(allow) {
return makePromise(http.acceptAllCerts, [allow]);
},
setTimeout: function(connectionTimeout) {
return makePromise(http.setTimeout, [connectionTimeout]);
},
validateDomainName: function(validate) {
return makePromise(http.validateDomainName, [validate]);
},
Expand Down

0 comments on commit c047dcc

Please sign in to comment.