<commit_before>package io.rover.network;

import android.support.annotation.Nullable;
import android.util.JsonReader;
import android.util.JsonWriter;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import io.rover.Rover;

/**
 * Created by ata_n on 2016-03-23.
 */
public class NetworkTask {

//    public interface JsonPayloadProvider {
//        void onProvidePayload(JsonWriter writer) throws IOException;
//        void onPrepareConnection(HttpURLConnection connection);
//    }
//
//    public interface JsonResponseHandler {
//        void onHandleResponse(JsonReader reader) throws IOException;
//    }

    public interface PayloadProvider {
        void onPrepareConnection(HttpURLConnection connection);
        void onProvidePayload(OutputStreamWriter writer) throws IOException;
    }

    public interface NetworkTaskConnectionManager {
        void onPrepareConnection(HttpURLConnection connection);
    }

    private URL mURL;
    private String mMethod;

    //private JsonPayloadProvider mPayloadProvider;
    //private JsonResponseHandler mResponseHandler;

    private PayloadProvider mPayloadProvider;
    private NetworkTaskConnectionManager mConnectionManager;

    public NetworkTask(String method, URL url) {
        mMethod = method;
        mURL = url;
    }

    public void setPayloadProvider(PayloadProvider payloadProvider) {
        mPayloadProvider = payloadProvider;
    }

    public void setConnectionManager(NetworkTaskConnectionManager manager) {
        mConnectionManager = manager;
    }

    @Nullable
    public HttpResponse run() {

        InputStream is = null;
        HttpResponse response = null;

        HttpURLConnection connection = null;

        try {

            Log.i("NetworkTask", "Connection to: " + mMethod + " " + mURL.toString());

            connection = (HttpURLConnection)mURL.openConnection();
            connection.setUseCaches(true);
            connection.setRequestMethod(mMethod);
            //connection.setRequestProperty("Content-Type", "application/json");

            if (mConnectionManager != null) {
                mConnectionManager.onPrepareConnection(connection);
            }

            connection.setDoInput(true);

            if (mPayloadProvider != null) {

                mPayloadProvider.onPrepareConnection(connection);

                connection.setDoOutput(true);


                OutputStream outputStream = connection.getOutputStream();
                mPayloadProvider.onProvidePayload(new OutputStreamWriter(outputStream));


//                JsonWriter writer = new JsonWriter(new OutputStreamWriter(connection.getOutputStream()));
//                writer.setIndent("  ");
//
//                mPayloadProvider.onProvidePayload(writer);

                //writer.close();
            }

            connection.connect();

            response = new HttpResponse();
            response.setStatus(connection.getResponseCode());

            try {
                is = connection.getInputStream();
                response.setBody(new InputStreamReader(is, "UTF-8"));
            } catch (Exception e) {
                Log.w("NetworkTask", "Failed to get input stream");
            }

            Log.i("NetworkTask", "HTTP Status: " + response.getStatus());

        } catch (IOException e) {

            if (connection != null) {
                is = connection.getErrorStream();
            }

            String error = getStringFromInputStream(is);

            Log.e("NetworkTask", error);
            Log.e("NetworkTask", "Error making HTTP connection: ");
            e.printStackTrace();

            try {
                is.close();
            } catch (IOException ignored) {

            }
        }

        return response;
    }

    private static String getStringFromInputStream(InputStream is) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        String line;
        try {

            if (is == null) {
                return " - No stream -";
            }

            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);<commit_msg>Guard against failed network tasks<commit_after>package io.rover.network;

import android.support.annotation.Nullable;
import android.util.JsonReader;
import android.util.JsonWriter;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import io.rover.Rover;

/**
 * Created by ata_n on 2016-03-23.
 */
public class NetworkTask {

//    public interface JsonPayloadProvider {
//        void onProvidePayload(JsonWriter writer) throws IOException;
//        void onPrepareConnection(HttpURLConnection connection);
//    }
//
//    public interface JsonResponseHandler {
//        void onHandleResponse(JsonReader reader) throws IOException;
//    }

    public interface PayloadProvider {
        void onPrepareConnection(HttpURLConnection connection);
        void onProvidePayload(OutputStreamWriter writer) throws IOException;
    }

    public interface NetworkTaskConnectionManager {
        void onPrepareConnection(HttpURLConnection connection);
    }

    private URL mURL;
    private String mMethod;

    //private JsonPayloadProvider mPayloadProvider;
    //private JsonResponseHandler mResponseHandler;

    private PayloadProvider mPayloadProvider;
    private NetworkTaskConnectionManager mConnectionManager;

    public NetworkTask(String method, URL url) {
        mMethod = method;
        mURL = url;
    }

    public void setPayloadProvider(PayloadProvider payloadProvider) {
        mPayloadProvider = payloadProvider;
    }

    public void setConnectionManager(NetworkTaskConnectionManager manager) {
        mConnectionManager = manager;
    }

    @Nullable
    public HttpResponse run() {

        InputStream is = null;
        HttpResponse response = null;

        HttpURLConnection connection = null;

        try {

            Log.i("NetworkTask", "Connection to: " + mMethod + " " + mURL.toString());

            connection = (HttpURLConnection)mURL.openConnection();
            connection.setUseCaches(true);
            connection.setRequestMethod(mMethod);
            //connection.setRequestProperty("Content-Type", "application/json");

            if (mConnectionManager != null) {
                mConnectionManager.onPrepareConnection(connection);
            }

            connection.setDoInput(true);

            if (mPayloadProvider != null) {

                mPayloadProvider.onPrepareConnection(connection);

                connection.setDoOutput(true);


                OutputStream outputStream = connection.getOutputStream();
                mPayloadProvider.onProvidePayload(new OutputStreamWriter(outputStream));


//                JsonWriter writer = new JsonWriter(new OutputStreamWriter(connection.getOutputStream()));
//                writer.setIndent("  ");
//
//                mPayloadProvider.onProvidePayload(writer);

                //writer.close();
            }

            connection.connect();

            response = new HttpResponse();
            response.setStatus(connection.getResponseCode());

            try {
                is = connection.getInputStream();
                response.setBody(new InputStreamReader(is, "UTF-8"));
            } catch (Exception e) {
                Log.w("NetworkTask", "Failed to get input stream");
            }

            Log.i("NetworkTask", "HTTP Status: " + response.getStatus());

        } catch (IOException e) {

            if (connection != null) {
                is = connection.getErrorStream();
            }

            String error = getStringFromInputStream(is);

            Log.e("NetworkTask", error);
            Log.e("NetworkTask", "Error making HTTP connection: ");
            e.printStackTrace();

            try {
                if(is != null)
                    is.close();
            } catch (IOException ignored) {

            }
        }

        return response;
    }

    private static String getStringFromInputStream(InputStream is) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        String line;
        try {

            if (is == null) {
                return " - No stream -";
            }

            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
{'fraction_non_alphanumeric': 0.09330357142857143, 'fraction_numerical': 0.0020089285714285712, 'mean_word_length': 2.277615215801024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6956077', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2104, 'n_words': 487}