Subversion Repositories eFlore/Applications.cel

Rev

Rev 2621 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2621 Rev 2806
Line 11... Line 11...
11
import org.tela_botanica.client.modeles.objets.Configuration;
11
import org.tela_botanica.client.modeles.objets.Configuration;
12
import org.tela_botanica.client.modeles.objets.Utilisateur;
12
import org.tela_botanica.client.modeles.objets.Utilisateur;
Line 13... Line 13...
13
 
13
 
14
import com.google.gwt.http.client.Request;
14
import com.google.gwt.http.client.Request;
-
 
15
import org.tela_botanica.client.util.RequestBuilderWithCredentials;
-
 
16
import org.tela_botanica.client.util.Util;
15
import org.tela_botanica.client.util.RequestBuilderWithCredentials;
17
 
16
import com.google.gwt.http.client.RequestCallback;
18
import com.google.gwt.http.client.RequestCallback;
17
import com.google.gwt.http.client.RequestException;
19
import com.google.gwt.http.client.RequestException;
-
 
20
import com.google.gwt.http.client.Response;
18
import com.google.gwt.http.client.Response;
21
import com.google.gwt.http.client.URL;
19
import com.google.gwt.json.client.JSONObject;
22
import com.google.gwt.json.client.JSONObject;
-
 
23
import com.google.gwt.json.client.JSONParser;
20
import com.google.gwt.json.client.JSONParser;
24
import com.google.gwt.json.client.JSONString;
-
 
25
import com.google.gwt.json.client.JSONValue;
21
import com.google.gwt.json.client.JSONValue;
26
import com.google.gwt.user.client.Timer;
Line 22... Line 27...
22
import com.google.gwt.user.client.Window;
27
import com.google.gwt.user.client.Window;
Line 23... Line 28...
23
 
28
 
24
public class UtilisateurAsynchroneDAO {
29
public class UtilisateurAsynchroneDAO {
25
 
30
 
26
	/**
31
	/**
-
 
32
	 * Le modèle associé au DAO
Line 27... Line 33...
27
	 * Le modèle associé au DAO
33
	 */
28
	 */
34
	private CarnetEnLigneModele carnetEnLigneModele = null ;
29
	private CarnetEnLigneModele carnetEnLigneModele = null ;
35
	private Timer rafraichisseurJeton;
Line 39... Line 45...
39
	 * Methode de classe d'appel du service gestion identification 
45
	 * Methode de classe d'appel du service gestion identification 
40
	 * @param baseURL : URL du service
46
	 * @param baseURL : URL du service
41
	 * @param retour : methode appelle en retour d'appel
47
	 * @param retour : methode appelle en retour d'appel
42
	 */
48
	 */
Line 43... Line 49...
43
	
49
	
44
	public void getEtatUtilisateur(final Rafraichissable r) {
-
 
-
 
50
	public void getEtatUtilisateur(final Rafraichissable r) {		
45
 
51
		String url = Configuration.getIdentificationBaseUrl()+"/identite/"; 			
46
		// Recherche identifiant utilisateur identifie
52
		// Recherche identifiant utilisateur identifie
Line 47... Line 53...
47
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET,Configuration.getServiceBaseUrl() + "/User/") ;
53
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, url) ;
Line 48... Line 54...
48
		
54
		
Line 56... Line 62...
56
				}
62
				}
Line 57... Line 63...
57
 
63
 
58
				@Override
64
				@Override
59
				public void onResponseReceived(Request request,
65
				public void onResponseReceived(Request request,
60
						Response response) {
-
 
61
					
66
						Response response) {
62
					if(response.getText().isEmpty()) {
67
					if(response.getText().isEmpty()) {
63
						Window.alert(Msg.get("erreur-contacter-serveur"));
68
						Window.alert(Msg.get("erreur-contacter-serveur"));
Line 64... Line 69...
64
					} else {
69
					} else {
65
										
70
										
Line 66... Line 71...
66
						JSONValue jsonValue = JSONParser.parse(response.getText());
71
						JSONValue jsonValue = JSONParser.parse(response.getText());
Line -... Line 72...
-
 
72
						JSONObject objetJsonUtilisateur = jsonValue.isObject();
67
						JSONObject objetJsonUtilisateur;
73
						
-
 
74
						if (objetJsonUtilisateur != null && objetJsonUtilisateur.get("session") != null) {
-
 
75
							
-
 
76
							// Drapeau leve si utilisateur deja identifie
68
						
77
							boolean identifie = objetJsonUtilisateur.get("session").isBoolean().booleanValue();
-
 
78
							if(identifie) {
-
 
79
								if(objetJsonUtilisateur.get("token").isString() != null) {
-
 
80
									String token = ((JSONString) objetJsonUtilisateur.get("token")).stringValue();									
-
 
81
									Utilisateur.setJeton(token);
69
						if ((objetJsonUtilisateur = jsonValue.isObject()) != null) {
82
									int dureeJeton = (int)objetJsonUtilisateur.get("duration").isNumber().doubleValue();
-
 
83
									gererTimerRafraichissementJeton(dureeJeton, r);
-
 
84
								}
-
 
85
							}
70
							
86
						}
71
							Utilisateur utilisateur = Utilisateur.InitialiserInstance(objetJsonUtilisateur);
87
						// dans tous les cas même si l'on est pas identifié par le sso, ceci permet d'obtenir
72
							r.rafraichir(utilisateur,true);
88
						// une session temporaire
Line 73... Line 89...
73
						}
89
						obtenirInfosComplementairesUtilisateur(r);
Line 74... Line 90...
74
					}
90
					}
75
				}			
91
				}			
76
			}) ;
-
 
77
					
92
			}) ;
78
		} catch (RequestException e) {
-
 
79
					
93
					
80
		}
-
 
81
	}
-
 
Line -... Line 94...
-
 
94
		} catch (RequestException e) {
-
 
95
					
82
 
96
		}
83
 
-
 
84
 
97
	}
Line 85... Line 98...
85
	public void deconnecterUtilisateur(final Rafraichissable r, String user) {
98
	
86
 
99
	public void obtenirInfosComplementairesUtilisateur(final Rafraichissable r) {
87
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl()+ "/User/" + user) ;
-
 
88
		
100
		
89
		try {
101
		String url = Configuration.getServiceBaseUrl()+"/User/"; 	
Line 90... Line 102...
90
			
102
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, url) ;	
91
			rb.sendRequest(null, new RequestCallback() {
103
		try {	
92
 
104
			rb.sendRequest(null, new RequestCallback() {
Line 93... Line 105...
93
				@Override
105
 
94
				public void onError(Request request, Throwable exception) {
106
				@Override
-
 
107
				public void onError(Request request, Throwable exception) {
95
					// TODO Auto-generated method stub
108
				
96
					
109
				}
-
 
110
 
Line -... Line 111...
-
 
111
				@Override
97
				}
112
				public void onResponseReceived(Request request,
-
 
113
						Response response) {
98
 
114
					
-
 
115
					if(response.getText().isEmpty()) {
-
 
116
						Window.alert(Msg.get("erreur-contacter-serveur"));
-
 
117
					} else {
-
 
118
						
99
				@Override
119
						JSONValue jsonValue = JSONParser.parse(response.getText());
100
				public void onResponseReceived(Request request,
120
						JSONObject objetJsonUtilisateur;
101
						Response response) {
121
						
102
					
-
 
103
					JSONValue jsonValue = JSONParser.parse(response.getText());
122
						if ((objetJsonUtilisateur = jsonValue.isObject()) != null) {
Line 104... Line 123...
104
					JSONObject objetJsonUtilisateur;
123
								Utilisateur utilisateur = Utilisateur.InitialiserInstance(objetJsonUtilisateur);
105
					
124
								if(r != null) { 
Line 106... Line 125...
106
					if ((objetJsonUtilisateur = jsonValue.isObject()) != null) {
125
									r.rafraichir(utilisateur,true);
107
						
-
 
108
						Utilisateur utilisateur = Utilisateur.InitialiserInstance(objetJsonUtilisateur);
126
								}
Line 109... Line 127...
109
						r.rafraichir(utilisateur,true);
127
						} else {
Line 110... Line 128...
110
					}		
128
							
Line 130... Line 148...
130
					
148
					
Line 131... Line 149...
131
				}
149
				}
132
 
150
 
133
				@Override
151
				@Override
134
				public void onResponseReceived(Request request,
-
 
135
						Response response) {
-
 
136
					
152
				public void onResponseReceived(Request request,
137
					JSONValue jsonValue = JSONParser.parse(response.getText());
-
 
138
					JSONObject objetJsonUtilisateur;
153
						Response response) {	
139
					
-
 
140
					if ((objetJsonUtilisateur = jsonValue.isObject()) != null) {
-
 
141
						
154
					Utilisateur.setJeton("");
142
						Utilisateur utilisateur = Utilisateur.InitialiserInstance(objetJsonUtilisateur);
-
 
143
						r.rafraichir(utilisateur,true);
155
					gererTimerRafraichissementJeton(0, r);
144
					}		
-
 
145
				}
-
 
146
				
156
					obtenirInfosComplementairesUtilisateur(r);
Line 147... Line 157...
147
				
157
				}			
-
 
158
			}) ;
-
 
159
			
-
 
160
		} catch (RequestException e) {
-
 
161
					
-
 
162
		}
-
 
163
	}
-
 
164
 
-
 
165
	public void connecterUtilisateur (final Rafraichissable r, String login, String password) {
-
 
166
 
-
 
167
		//connexion?login=aurelien@tela-botanica.org&password=Canard123$
Line -... Line 168...
-
 
168
		if(!login.isEmpty() && !password.isEmpty()) {
-
 
169
			String url = Configuration.getIdentificationBaseUrl() + "/connexion?login=" + URL.encodeComponent(login) + "&password=" + URL.encodeComponent(password);
-
 
170
			RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, url) ;
-
 
171
			
-
 
172
			try {
-
 
173
				
-
 
174
				rb.sendRequest(null, new RequestCallback() {
-
 
175
	
-
 
176
					@Override
-
 
177
					public void onError(Request request, Throwable exception) {
-
 
178
						// TODO Auto-generated method stub
-
 
179
						
-
 
180
					}
-
 
181
	
-
 
182
					@Override
-
 
183
					public void onResponseReceived(Request request,
-
 
184
							Response response) {
-
 
185
												
-
 
186
						JSONValue jsonValue = JSONParser.parse(response.getText());
-
 
187
						JSONObject objetJsonUtilisateur = jsonValue.isObject();
-
 
188
						
-
 
189
						if (objetJsonUtilisateur != null && objetJsonUtilisateur.get("session") != null) {
-
 
190
							if(objetJsonUtilisateur.get("token").isString() != null) {
-
 
191
								String token = ((JSONString) objetJsonUtilisateur.get("token")).stringValue();									
-
 
192
								Utilisateur.setJeton(token);
-
 
193
								int dureeJeton = (int)objetJsonUtilisateur.get("duration").isNumber().doubleValue();
-
 
194
								gererTimerRafraichissementJeton(dureeJeton, r);
-
 
195
							}
-
 
196
							obtenirInfosComplementairesUtilisateur(r);
-
 
197
						}		
-
 
198
					}	
148
			}) ;
199
				}) ;
-
 
200
				
-
 
201
			} catch (RequestException e) {
-
 
202
				
-
 
203
				e.printStackTrace() ;
-
 
204
			}
-
 
205
		}
-
 
206
	}
-
 
207
	
-
 
208
	private void gererTimerRafraichissementJeton(int duree, final Rafraichissable r) {
-
 
209
		// Rafraîchissement automatique du jeton - s'annule lorsque le jeton devient nul
-
 
210
		if (!Utilisateur.getJeton().isEmpty() && duree > 0) {
-
 
211
			
-
 
212
			if (rafraichisseurJeton == null) { // on sait jamais
-
 
213
				// 3/4 de la durée de vie du jeton, en millisecondes (ça laisse de la marge)
-
 
214
				int delaiRepetition = (duree * 1000) * 3 / 4;
-
 
215
				rafraichisseurJeton = new Timer() {
-
 
216
					@Override
-
 
217
					public void run() {
-
 
218
						// Appel de l'annuaire pour rafraîchir le jeton (cb nul pour l'instant)
-
 
219
						// @TODO gérer le possible changement de profil / coordonnées de l'utilisateur
-
 
220
						// en cours de connexion
-
 
221
						getEtatUtilisateur(null);
-
 
222
					}
-
 
223
				};
-
 
224
				rafraichisseurJeton.scheduleRepeating(delaiRepetition);
-
 
225
			}
-
 
226
		} else {
149
			
227
			if (rafraichisseurJeton != null) { // on sait jamais non plus
150
		} catch (RequestException e) {
228
				rafraichisseurJeton.cancel();
Line 151... Line 229...
151
			
229
				rafraichisseurJeton = null;