package ca.etsmtl.applets.etsmobile.http;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

import com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import ca.etsmtl.applets.etsmobile.model.MonETSNotificationList;
import ca.etsmtl.applets.etsmobile.util.Constants;
import ca.etsmtl.applets.etsmobile2.R;

/**
 * Created by gnut3ll4 on 15/12/15.
 */
public class MonETSNotificationsRequest extends SpringAndroidSpiceRequest<MonETSNotificationList> {

    private final Context context;
    private final boolean onlyNewNotifs;
    private AccountManager accountManager;
    private String authToken = "";

    public MonETSNotificationsRequest(Context context, boolean onlyNewNotifs) {
        super(MonETSNotificationList.class);

        this.context = context;
        this.onlyNewNotifs = onlyNewNotifs;

    }

    @Override
    public MonETSNotificationList loadDataFromNetwork() throws Exception {

        accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
        if (accounts.length > 0) {
            authToken = accountManager.peekAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE);
        }

        String url = context.getString(R.string.portail_api_base_url);
        if (onlyNewNotifs) {
            url += "/api/notification/dossier/1";
        } else {
            url += "/api/notification";
        }

        ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
            @Override
            public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
                HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
                requestWrapper.getHeaders().set("Cookie", authToken);
                return execution.execute(requestWrapper, body);
            }
        };

        RestTemplate restTemplate = getRestTemplate();
        List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
        list.add(interceptor);
        restTemplate.setInterceptors(list);

        return restTemplate.getForObject(url, MonETSNotificationList.class);
    }
}

@@ -529,24 +529,88 @@
 estWrapper;%0A
+import org.springframework.web.client.HttpClientErrorException;%0A
 import org.s
@@ -2708,17 +2708,34 @@
 (list);%0A
-%0A
+        try %7B%0A    
         
@@ -2803,16 +2803,1204 @@
 class);%0A
+        %7D catch (HttpClientErrorException e) %7B%0A            if (e.getStatusCode().value() == 401) %7B%0A                if (accounts.length %3E 0) %7B%0A                    accountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);%0A                    authToken = accountManager.blockingGetAuthToken(accounts%5B0%5D, Constants.AUTH_TOKEN_TYPE, true);%0A%0A                    interceptor = new ClientHttpRequestInterceptor() %7B%0A                        @Override%0A                        public ClientHttpResponse intercept(HttpRequest request, byte%5B%5D body, ClientHttpRequestExecution execution) throws IOException %7B%0A                            HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);%0A                            requestWrapper.getHeaders().set(%22Cookie%22, authToken);%0A                            return execution.execute(requestWrapper, body);%0A                        %7D%0A                    %7D;%0A                    list.clear();%0A                    list.add(interceptor);%0A                    restTemplate.setInterceptors(list);%0A%0A                %7D%0A            %7D%0A        %7D finally %7B%0A            return restTemplate.getForObject(url, MonETSNotificationList.class);%0A        %7D%0A
     %7D%0A%7D%0A

Fix auth refresh