package com.expidev.gcmapp.http;

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

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by matthewfrederick on 1/13/15.
 * 
 * TokenTask is used to retrieve basic information about the user and the systems 
 * to which he has access. A session ticket (retrieved from TicketTask) is passed
 * to validate that thekey login was successful.
 *
 */
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 urlString = params[0].toString();
        String sessionTicket = params[1].toString();

        String fullUrl = urlString + "?st=" + sessionTicket + "&refresh=true";
        Log.i(TAG, fullUrl);

        try
        {
            URL url = new URL(fullUrl);

            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            urlConnection.setReadTimeout(10000);
            urlConnection.setConnectTimeout(10000);
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoInput(true);

            urlConnection.connect();
            statusCode = urlConnection.getResponseCode();

            InputStream inputStream = urlConnection.getInputStream();

            if (inputStream != null)
            {
                String jsonAsString = readFully(inputStream, "UTF-8");
                Log.i(TAG, jsonAsString);
                jsonObject = new JSONObject(jsonAsString);
                status = jsonObject.getString("status");
            }
        }
        catch (Exception e)
        {
            Log.e(TAG, e.getMessage(), e);
            return null;
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s)
    {
        super.onPostExecute(s);

        if ("success".equalsIgnoreCase(status)) taskHandler.taskComplete(jsonObject);
        else taskHandler.taskFailed(status);
    }
    
    private String readFully(InputStream inputStream, String encoding) throws IOException
    {
        return new String(readFully(inputStream), encoding);
    }
    
    private byte[] readFully(InputStream inputStream) throws IOException
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buffer)) != -1)
        {
            byteArrayOutputStream.write(buffer, 0, length);
        }
        return byteArrayOutputStream.toByteArray();
    }
}

@@ -82,16 +82,51 @@
 l.Log;%0A%0A
+import org.apache.http.HttpStatus;%0A
 import o
@@ -1828,24 +1828,90 @@
 nseCode();%0A%0A
+            if (statusCode == HttpStatus.SC_OK)%0A            %7B%0A    
             
@@ -1977,24 +1977,28 @@
             
+ 
+   
 if (inputStr
@@ -2014,34 +2014,42 @@
 ll)%0A            
-%7B%0A
+    %7B%0A    
                 
@@ -2111,32 +2111,36 @@
                 
+  
+  
 Log.i(TAG, jsonA
@@ -2141,32 +2141,36 @@
  jsonAsString);%0A
+    
                 
@@ -2224,24 +2224,28 @@
             
+ 
+   
 status = jso
@@ -2269,24 +2269,146 @@
 (%22status%22);%0A
+                %7D%0A            %7D%0A            else %0A            %7B%0A                Log.e(TAG, %22Status Code: %22 + statusCode);%0A
             

Add a log for status code