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