| 463 | 
           jp_milcent | 
           1 | 
           package org.tela_botanica.client.http;
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
           | 1908 | 
           mathias | 
           3 | 
           import org.tela_botanica.client.Coel;
  | 
        
        
           | 846 | 
           gduche | 
           4 | 
           import org.tela_botanica.client.Mediateur;
  | 
        
        
           | 1225 | 
           cyprien | 
           5 | 
           import org.tela_botanica.client.composants.InfoLogger;
  | 
        
        
           | 463 | 
           jp_milcent | 
           6 | 
           import org.tela_botanica.client.modeles.Information;
  | 
        
        
           | 1563 | 
           jpm | 
           7 | 
           import org.tela_botanica.client.util.Log;
  | 
        
        
           | 463 | 
           jp_milcent | 
           8 | 
              | 
        
        
            | 
            | 
           9 | 
           import com.google.gwt.core.client.GWT;
  | 
        
        
            | 
            | 
           10 | 
           import com.google.gwt.http.client.Request;
  | 
        
        
            | 
            | 
           11 | 
           import com.google.gwt.http.client.RequestCallback;
  | 
        
        
            | 
            | 
           12 | 
           import com.google.gwt.http.client.Response;
  | 
        
        
            | 
            | 
           13 | 
           import com.google.gwt.json.client.JSONArray;
  | 
        
        
            | 
            | 
           14 | 
           import com.google.gwt.json.client.JSONParser;
  | 
        
        
            | 
            | 
           15 | 
           import com.google.gwt.json.client.JSONValue;
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           public class JsonRestRequestCallback implements RequestCallback {
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           	public void onError(Request request, Throwable exception) {
  | 
        
        
            | 
            | 
           20 | 
           		// Gestion des exceptions déclenchées par l'exécution de la requête
  | 
        
        
           | 1563 | 
           jpm | 
           21 | 
           		Log.warn(Mediateur.i18nM.erreurServiceJrest(request.toString()), exception);
  | 
        
        
           | 1225 | 
           cyprien | 
           22 | 
           		InfoLogger.display(Mediateur.i18nM.erreurRequeteTitre(), Mediateur.i18nM.erreurRequete(), true);
  | 
        
        
           | 463 | 
           jp_milcent | 
           23 | 
           	}
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           	public void onErrorHTTP(Request request, Response reponse) {
  | 
        
        
            | 
            | 
           26 | 
           		// Gestion des erreurs HTTP renvoyé par Apache ou JRest
  | 
        
        
           | 1563 | 
           jpm | 
           27 | 
           		Information info = null;
  | 
        
        
            | 
            | 
           28 | 
           		if (avoirContenu(reponse)) {
  | 
        
        
            | 
            | 
           29 | 
           			info = new Information("erreur_jrest", JSONParser.parseStrict(reponse.getText()).isArray());
  | 
        
        
            | 
            | 
           30 | 
           		} else {
  | 
        
        
            | 
            | 
           31 | 
           			info = new Information("erreur_jrest");
  | 
        
        
            | 
            | 
           32 | 
           		}
  | 
        
        
            | 
            | 
           33 | 
           		Log.debug("Erreur JREST - Code "+reponse.getStatusCode()+"\n"+info.getMessages().toString());
  | 
        
        
           | 1225 | 
           cyprien | 
           34 | 
           		InfoLogger.display("Erreur JREST - Code "+reponse.getStatusCode(), info.toString(), true);
  | 
        
        
           | 463 | 
           jp_milcent | 
           35 | 
           	}
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
           	public void onResponseReceived(Request request, Response response) {
  | 
        
        
           | 909 | 
           jpm | 
           38 | 
           		// Affichage du débogage.
  | 
        
        
            | 
            | 
           39 | 
           		afficherDebug(response);
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
           | 1906 | 
           mathias | 
           41 | 
           		// Si le code de réponse HTTP ne vaut pas 200 OK, on lance le mécanisme d'erreur HTTP
  | 
        
        
           | 463 | 
           jp_milcent | 
           42 | 
           		if (response.getStatusCode() != 200) {
  | 
        
        
            | 
            | 
           43 | 
           			onErrorHTTP(request, response);
  | 
        
        
            | 
            | 
           44 | 
           		} else {
  | 
        
        
            | 
            | 
           45 | 
           			JSONValue responseValue = null;
  | 
        
        
           | 469 | 
           jp_milcent | 
           46 | 
           			if (avoirContenu(response)) {
  | 
        
        
           | 1563 | 
           jpm | 
           47 | 
           				responseValue = JSONParser.parseStrict(response.getText());
  | 
        
        
           | 463 | 
           jp_milcent | 
           48 | 
           			}
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
           			surReponse(responseValue);
  | 
        
        
            | 
            | 
           51 | 
           		}
  | 
        
        
            | 
            | 
           52 | 
           	}
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
           	public void surReponse(JSONValue responseValue) {
  | 
        
        
            | 
            | 
           55 | 
           	}
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
           	public String formaterDeboguages(JSONArray jsonArray) {
  | 
        
        
            | 
            | 
           58 | 
           		String deboguageFormate = "";
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
            | 
            | 
           60 | 
           		for (int i = 0 ; i < jsonArray.size() ; i++) {
  | 
        
        
            | 
            | 
           61 | 
           			if (jsonArray.get(i).isString() != null) {
  | 
        
        
            | 
            | 
           62 | 
           				deboguageFormate += jsonArray.get(i).isString().stringValue()+"\n";
  | 
        
        
            | 
            | 
           63 | 
           			}
  | 
        
        
            | 
            | 
           64 | 
           		}
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
           		return deboguageFormate;
  | 
        
        
            | 
            | 
           67 | 
           	}
  | 
        
        
           | 469 | 
           jp_milcent | 
           68 | 
              | 
        
        
           | 909 | 
           jpm | 
           69 | 
           	private void afficherDebug(Response reponse) {
  | 
        
        
            | 
            | 
           70 | 
           		if (avoirEnteteDebug(reponse)) {
  | 
        
        
           | 1563 | 
           jpm | 
           71 | 
           			final JSONValue reponseEnteteDeboguage = JSONParser.parseStrict(reponse.getHeader("X-DebugJrest-Data"));
  | 
        
        
           | 909 | 
           jpm | 
           72 | 
           			if (reponseEnteteDeboguage.isArray() != null) {
  | 
        
        
            | 
            | 
           73 | 
           				GWT.log("DEBOGUAGE:\n"+formaterDeboguages(reponseEnteteDeboguage.isArray()), null);
  | 
        
        
            | 
            | 
           74 | 
           			}
  | 
        
        
            | 
            | 
           75 | 
           		}
  | 
        
        
            | 
            | 
           76 | 
           	}
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
           | 469 | 
           jp_milcent | 
           78 | 
           	public Boolean avoirEnteteDebug(Response reponse) {
  | 
        
        
            | 
            | 
           79 | 
           		Boolean retour = false;
  | 
        
        
            | 
            | 
           80 | 
           		if (reponse.getHeader("X-DebugJrest-Data") != null && reponse.getHeader("X-DebugJrest-Data").length() != 0) {
  | 
        
        
            | 
            | 
           81 | 
           			retour = true;
  | 
        
        
            | 
            | 
           82 | 
           		}
  | 
        
        
            | 
            | 
           83 | 
           		return retour;
  | 
        
        
            | 
            | 
           84 | 
           	}
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
           	public Boolean avoirContenu(Response reponse) {
  | 
        
        
            | 
            | 
           87 | 
           		Boolean retour = false;
  | 
        
        
            | 
            | 
           88 | 
           		if (reponse.getText() != null && reponse.getText().length() != 0) {
  | 
        
        
            | 
            | 
           89 | 
           			retour = true;
  | 
        
        
            | 
            | 
           90 | 
           		} else if (reponse.getText() == null) {
  | 
        
        
           | 1563 | 
           jpm | 
           91 | 
           			Log.debug("La réponse vaul null");
  | 
        
        
           | 469 | 
           jp_milcent | 
           92 | 
           		} else if (reponse.getText().length() == 0) {
  | 
        
        
           | 1563 | 
           jpm | 
           93 | 
           			Log.debug("La réponse a une taille de 0");
  | 
        
        
           | 469 | 
           jp_milcent | 
           94 | 
           		}
  | 
        
        
            | 
            | 
           95 | 
           		return retour;
  | 
        
        
            | 
            | 
           96 | 
           	}
  | 
        
        
           | 463 | 
           jp_milcent | 
           97 | 
           }
  |