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) {
|
|
|
31 |
// Si le code de réponse HTTP ne vaut pas 200 OK, on lance le mécanise d'erreur HTTP
|
|
|
32 |
if (response.getStatusCode() != 200) {
|
|
|
33 |
onErrorHTTP(request, response);
|
|
|
34 |
} else {
|
469 |
jp_milcent |
35 |
if (avoirEnteteDebug(response)) {
|
463 |
jp_milcent |
36 |
final JSONValue reponseEnteteDeboguage = JSONParser.parse(response.getHeader("X-DebugJrest-Data"));
|
|
|
37 |
if (reponseEnteteDeboguage.isArray() != null) {
|
|
|
38 |
GWT.log("DEBOGUAGE:\n"+formaterDeboguages(reponseEnteteDeboguage.isArray()), null);
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
JSONValue responseValue = null;
|
469 |
jp_milcent |
43 |
if (avoirContenu(response)) {
|
|
|
44 |
responseValue = JSONParser.parse(response.getText());
|
463 |
jp_milcent |
45 |
}
|
|
|
46 |
|
|
|
47 |
surReponse(responseValue);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public void surReponse(JSONValue responseValue) {
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public String formaterDeboguages(JSONArray jsonArray) {
|
|
|
55 |
String deboguageFormate = "";
|
|
|
56 |
|
|
|
57 |
for (int i = 0 ; i < jsonArray.size() ; i++) {
|
|
|
58 |
if (jsonArray.get(i).isString() != null) {
|
|
|
59 |
deboguageFormate += jsonArray.get(i).isString().stringValue()+"\n";
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
return deboguageFormate;
|
|
|
64 |
}
|
469 |
jp_milcent |
65 |
|
|
|
66 |
public Boolean avoirEnteteDebug(Response reponse) {
|
|
|
67 |
Boolean retour = false;
|
|
|
68 |
if (reponse.getHeader("X-DebugJrest-Data") != null && reponse.getHeader("X-DebugJrest-Data").length() != 0) {
|
|
|
69 |
retour = true;
|
|
|
70 |
}
|
|
|
71 |
return retour;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public Boolean avoirContenu(Response reponse) {
|
|
|
75 |
Boolean retour = false;
|
|
|
76 |
if (reponse.getText() != null && reponse.getText().length() != 0) {
|
|
|
77 |
retour = true;
|
|
|
78 |
} else if (reponse.getText() == null) {
|
|
|
79 |
GWT.log("La réponse vaul null", null);
|
|
|
80 |
} else if (reponse.getText().length() == 0) {
|
|
|
81 |
GWT.log("La réponse a une taille de 0", null);
|
|
|
82 |
}
|
|
|
83 |
return retour;
|
|
|
84 |
}
|
463 |
jp_milcent |
85 |
}
|