package com.expidev.gcmapp.http;

import android.os.AsyncTask;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

/**
 * Created by matthewfrederick on 1/13/15.
 */
public class TokenTask extends AsyncTask<Object, Void, String>
{
    private final String TAG = this.getClass().getSimpleName();
    
    private TokenTaskHandler taskHandler;
    private int statusCode = 0;
    private JSONObject jsonObject;
    private String status;
    
    public static interface TokenTaskHandler
    {
        void taskComplete(JSONObject object);
        void taskFailed(String status);
    }
    
    public TokenTask(TokenTaskHandler listener)
    {
        taskHandler = listener;    
    }
    
    @Override
    protected String doInBackground(Object... params)
    {
        String url = params[0].toString();
        String guid = params[1].toString();
        
        String fullUrl = url + "/token?st=" + guid + "&refresh=true";

        try
        {
            HttpGet request = new HttpGet(fullUrl);
            Log.i(TAG, fullUrl);

            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
            HttpConnectionParams.setSoTimeout(httpParams, 10000);

            HttpClient httpClient = new DefaultHttpClient(httpParams);
            HttpResponse response = httpClient.execute(request);
            statusCode = response.getStatusLine().getStatusCode();

            HttpEntity entity = response.getEntity();
            if (entity != null)
            {
                String jsonString = EntityUtils.toString(entity);
                jsonObject = new JSONObject(jsonString);
                status = jsonObject.getString("status");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s)
    {
        super.onPostExecute(s);
        
        if (statusCode == HttpStatus.SC_OK && status.equalsIgnoreCase("success")) taskHandler.taskComplete(jsonObject);
        else taskHandler.taskFailed(status);
    }
}

@@ -97,344 +97,118 @@
 org.
-apache.http.HttpEntity;%0Aimport org.apache.http.HttpResponse;%0Aimport org.apache.http.HttpStatus;%0Aimport org.apache.http.client.HttpClient;%0Aimport org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.impl.client.DefaultHttpClient;%0Aimport org.apache.http.params.BasicHttpParams;%0Aimport org.apache.http.params.HttpConnectionPar
+json.JSONObject;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStre
 am
-s
 ;%0Aim
@@ -216,110 +216,62 @@
 ort 
-org.apache.http.params.HttpParams;%0Aimport org.apache.http.util.EntityUtils;%0Aimport org.json.JSONObject
+java.net.URL;%0A%0Aimport javax.net.ssl.HttpsURLConnection
 ;%0A%0A/
@@ -936,16 +936,22 @@
 ring url
+String
  = param
@@ -1048,16 +1048,22 @@
 rl = url
+String
  + %22/tok
@@ -1135,82 +1135,126 @@
     
-HttpGet request = new HttpGet(fullUrl);%0A            Log.i(TAG, fullUrl
+URL url = new URL(fullUrl);%0A            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(
 );%0A
-%0A
     
@@ -1265,52 +1265,41 @@
     
-HttpParams httpParams = new BasicHttpParams(
+urlConnection.setReadTimeout(1000
 );%0A 
@@ -1301,36 +1301,35 @@
 0);%0A            
-Http
+url
 ConnectionParams
@@ -1318,30 +1318,24 @@
 rlConnection
-Params
 .setConnecti
@@ -1337,19 +1337,16 @@
 nect
-ion
 Timeout(
 http
@@ -1337,36 +1337,24 @@
 nectTimeout(
-httpParams, 
 10000);%0A    
@@ -1361,20 +1361,19 @@
         
-Http
+url
 Connecti
@@ -1378,49 +1378,34 @@
 tion
-Params.setSoTimeout(httpParams, 10000
+.setRequestMethod(%22GET%22
 );%0A
-%0A
     
@@ -1416,276 +1416,259 @@
     
-HttpClient httpClient = new DefaultHttpClient(httpParams);%0A            HttpResponse response = httpClient.execute(request);%0A            statusCode = response.getStatusLine().getStatusCode();%0A%0A            HttpEntity entity = response.getEntity();%0A            if (entity
+urlConnection.setDoInput(true);%0A            %0A            urlConnection.connect();%0A            statusCode = urlConnection.getResponseCode();%0A%0A            InputStream inputStream = urlConnection.getInputStream();%0A            %0A            if (inputStream
  != 
@@ -1710,24 +1710,26 @@
  String json
+As
 String = Ent
@@ -1729,35 +1729,80 @@
 g = 
-EntityUtils.toString(entity
+readFully(inputStream, %22UTF-8%22);%0A                Log.i(TAG, jsonAsString
 );%0A 
@@ -1848,16 +1848,18 @@
 ect(json
+As
 String);
@@ -2195,48 +2195,17 @@
 if (
-statusCode == HttpStatus.SC_OK && status
+%22success%22
 .equ
@@ -2218,25 +2218,22 @@
 oreCase(
-%22success%22
+status
 )) taskH
@@ -2316,10 +2316,605 @@
 ;%0A    %7D%0A
+    %0A    private String readFully(InputStream inputStream, String encoding) throws IOException%0A    %7B%0A        return new String(readFully(inputStream), encoding);%0A    %7D%0A    %0A    private byte%5B%5D readFully(InputStream inputStream) throws IOException%0A    %7B%0A        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();%0A        byte%5B%5D buffer = new byte%5B1024%5D;%0A        int length = 0;%0A        while ((length = inputStream.read(buffer)) != -1)%0A        %7B%0A            byteArrayOutputStream.write(buffer, 0, length);%0A        %7D%0A        return byteArrayOutputStream.toByteArray();%0A    %7D%0A
 %7D%0A

update http library used to avoid SSL error