<commit_before>package fr.insapp.insapp.http.interceptors;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.preference.PreferenceManager;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

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

import java.io.IOException;

import auto.parcelgson.gson.AutoParcelGsonTypeAdapterFactory;
import fr.insapp.insapp.App;
import fr.insapp.insapp.models.User;
import fr.insapp.insapp.notifications.FirebaseService;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by thomas on 11/07/2017.
 */

public class JsonInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        SharedPreferences credentialsPreferences = App.getAppContext().getSharedPreferences("Credentials", Context.MODE_PRIVATE);
        SharedPreferences userPreferences = App.getAppContext().getSharedPreferences("User", Context.MODE_PRIVATE);

        Response response = chain.proceed(request);
        String json = response.body().string();

        if (request.url().toString().contains("signin/user")) {
            credentialsPreferences.edit().putString("login", json).apply();
        }
        else if (request.url().toString().contains("login/user")) {
            credentialsPreferences.edit().putString("session", json).apply();

            // retrieve user data from server

            try {
                final JSONObject userJson = new JSONObject(json).getJSONObject("user");

                userPreferences.edit().putString("user", userJson.toString()).apply();

                SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.getAppContext());
                SharedPreferences.Editor editor = defaultSharedPreferences.edit();

                editor.putString("name", userJson.getString("name"));
                editor.putString("description", userJson.getString("description"));
                editor.putString("email", userJson.getString("email"));
                editor.putString("class", userJson.getString("promotion"));
                editor.putString("sex", userJson.getString("gender"));

                editor.apply();
            }
            catch (JSONException ex) {
                ex.printStackTrace();
            }
        }

        // save user data from server to local shared preferences

        else if (request.url().toString().contains("user") && request.method().equals("PUT")) {
            userPreferences.edit().putString("user", json).apply();
        }

        else if (request.url().toString().contains("participant") && request.method().equals("POST") ||
                 request.url().toString().contains("like") && (request.method().equals("POST") || request.method().equals("DELETE"))) {
            try {
                userPreferences.edit().putString("user", new JSONObject(json).getJSONObject("user").toString()).apply();
            }
            catch (JSONException ex) {
                ex.printStackTrace();
            }
        }

        return response.newBuilder().body(ResponseBody.create(response.body().contentType(), json)).build();
    }
}
<commit_msg>Credentials are saved in preferences only if returned json is valid<commit_after>package fr.insapp.insapp.http.interceptors;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.preference.PreferenceManager;

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

import java.io.IOException;

import fr.insapp.insapp.App;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by thomas on 11/07/2017.
 */

public class JsonInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        SharedPreferences credentialsPreferences = App.getAppContext().getSharedPreferences("Credentials", Context.MODE_PRIVATE);
        SharedPreferences userPreferences = App.getAppContext().getSharedPreferences("User", Context.MODE_PRIVATE);

        Response response = chain.proceed(request);
        String json = response.body().string();

        if (request.url().toString().contains("signin/user")) {
            if (isJsonValid(json)) {
                credentialsPreferences.edit().putString("login", json).apply();
            }
        }
        else if (request.url().toString().contains("login/user")) {
            if (isJsonValid(json)) {
                credentialsPreferences.edit().putString("session", json).apply();
            }

            // retrieve user data from server

            try {
                final JSONObject userJson = new JSONObject(json).getJSONObject("user");

                userPreferences.edit().putString("user", userJson.toString()).apply();

                SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.getAppContext());
                SharedPreferences.Editor editor = defaultSharedPreferences.edit();

                editor.putString("name", userJson.getString("name"));
                editor.putString("description", userJson.getString("description"));
                editor.putString("email", userJson.getString("email"));
                editor.putString("class", userJson.getString("promotion"));
                editor.putString("sex", userJson.getString("gender"));

                editor.apply();
            }
            catch (JSONException ex) {
                ex.printStackTrace();
            }
        }

        // save user data from server to local shared preferences

        else if (request.url().toString().contains("user") && request.method().equals("PUT")) {
            userPreferences.edit().putString("user", json).apply();
        }

        else if (request.url().toString().contains("participant") && request.method().equals("POST") ||
                 request.url().toString().contains("like") && (request.method().equals("POST") || request.method().equals("DELETE"))) {
            try {
                userPreferences.edit().putString("user", new JSONObject(json).getJSONObject("user").toString()).apply();
            }
            catch (JSONException ex) {
                ex.printStackTrace();
            }
        }

        return response.newBuilder().body(ResponseBody.create(response.body().contentType(), json)).build();
    }

    private boolean isJsonValid(String json) {
        try {
            new JSONObject(json);
        }
        catch (JSONException ex) {
            ex.printStackTrace();

            return false;
        }

        return true;
    }
}

{'fraction_non_alphanumeric': 0.13095065837071335, 'fraction_numerical': 0.003762118362031544, 'mean_word_length': 2.779114270092947, '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': '5565170', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1659, 'n_words': 309}