/**
 * A HTTP plugin for Cordova / Phonegap
 */
package com.synconset;

import org.apache.cordova.CallbackContext;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Map;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.HostnameVerifier;

import java.util.Iterator;

import android.util.Log;

import com.github.kevinsawicki.http.HttpRequest;
 
public abstract class CordovaHttp {
    protected static final String TAG = "CordovaHTTP";
    protected static final String CHARSET = "UTF-8";
    
    private static boolean sslPinning;
    private static boolean acceptAllCerts;
    
    private String urlString;
    private Map<?, ?> params;
    private Map<String, String> headers;
    private CallbackContext callbackContext;
    
    public CordovaHttp(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
        this.urlString = urlString;
        this.params = params;
        this.headers = headers;
        this.sslPinning = sslPinning;
        this.acceptAllCerts = acceptAllCerts;
        this.callbackContext = callbackContext;
    }
    
    public static void enableSSLPinning(boolean enable) {
        sslPinning = enable;
        if (sslPinning) {
            acceptAllCerts = false;
        }
    }
    
    public static void acceptAllCerts(boolean accept) {
        acceptAllCerts = accept;
        if (acceptAllCerts) {
            sslPinning = false;
        }
    }
    
    protected String getUrlString() {
        return this.urlString;
    }
    
    protected void setUrlString(String urlString) {
        this.urlString = urlString;
    }
    
    protected Map<?, ?> getParams() {
        return this.params;
    }
    
    protected void setParams(Map<?, ?> params) {
        this.params = params;
    }
    
    protected Map<String, String> getHeaders() {
        return this.headers;
    }
    
    protected void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }
    
    protected CallbackContext getCallbackContext() {
        return this.callbackContext;
    }
    
    protected HttpRequest setupSecurity(HttpRequest request) {
        if (acceptAllCerts) {
            request.trustAllCerts();
            request.trustAllHosts();
        }
        if (sslPinning) {
            request.pinToCerts();
        }
        return request;
    }
    
    protected void respondWithError(String msg) {
        try {
            JSONObject response = new JSONObject();
            response.put("status", 500);
            response.put("error", msg);
            this.callbackContext.error(response);
        } catch (JSONException e) {
            this.callbackContext.error(msg);
        }
    }
}

@@ -311,16 +311,66 @@
 til.Map;
+%0Aimport java.util.concurrent.atomic.AtomicBoolean;
 %0A%0Aimport
@@ -838,25 +838,31 @@
 vate static 
-b
+AtomicB
 oolean sslPi
@@ -862,24 +862,51 @@
 n sslPinning
+ = new AtomicBoolean(false)
 ;%0A    privat
@@ -914,17 +914,23 @@
  static 
-b
+AtomicB
 oolean a
@@ -938,24 +938,51 @@
 ceptAllCerts
+ = new AtomicBoolean(false)
 ;%0A    %0A    p
@@ -1352,92 +1352,8 @@
 rs;%0A
-        this.sslPinning = sslPinning;%0A        this.acceptAllCerts = acceptAllCerts;%0A
     
@@ -1487,17 +1487,20 @@
 ning
- = 
+.set(
 enable
+)
 ;%0A  
@@ -1505,34 +1505,30 @@
         if (
-sslPinning
+enable
 ) %7B%0A        
@@ -1545,24 +1545,27 @@
 AllCerts
- = 
+.set(
 false
+)
 ;%0A      
@@ -1661,17 +1661,20 @@
 erts
- = 
+.set(
 accept
+)
 ;%0A  
@@ -1685,32 +1685,24 @@
   if (accept
-AllCerts
 ) %7B%0A        
@@ -1719,16 +1719,19 @@
 ning
- = 
+.set(
 false
+)
 ;%0A  
@@ -1847,433 +1847,140 @@
 ted 
-void setUrlString(String urlString) %7B%0A        this.urlString = urlString;%0A    %7D%0A    %0A    protected Map%3C?, ?%3E getParams() %7B%0A        return this.params;%0A    %7D%0A    %0A    protected void setParams(Map%3C?, ?%3E params) %7B%0A        this.params = params;%0A    %7D%0A    %0A    protected Map%3CString, String%3E getHeaders() %7B%0A        return this.headers;%0A    %7D%0A    %0A    protected void setHeaders(Map%3CString, String%3E headers) %7B%0A        this.headers = 
+Map%3C?, ?%3E getParams() %7B%0A        return this.params;%0A    %7D%0A    %0A    protected Map%3CString, String%3E getHeaders() %7B%0A        return this.
 head
@@ -2185,16 +2185,22 @@
 AllCerts
+.get()
 ) %7B%0A    
@@ -2301,16 +2301,22 @@
 lPinning
+.get()
 ) %7B%0A    

changed booleans to AtomicBooleans