Subversion Repositories eFlore/Applications.coel

Rev

Rev 1860 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
148 gduche 1
package org.tela_botanica.client.util;
2
 
182 gduche 3
import java.util.HashMap;
4
import java.util.Iterator;
148 gduche 5
 
6
import org.tela_botanica.client.RegistreId;
907 jpm 7
import org.tela_botanica.client.configuration.Configuration;
748 jpm 8
import org.tela_botanica.client.http.JsonRestRequestBuilder;
148 gduche 9
 
10
import com.extjs.gxt.ui.client.Registry;
296 gduche 11
import com.google.gwt.http.client.URL;
148 gduche 12
 
748 jpm 13
/**
14
 * @author Gréguoire DUCHÉ <greguoire@tela-botanica.org>
15
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
16
 * @description La classe utilDAO fournit des méthodes communes pour les outils DAO
17
 * */
148 gduche 18
public class UtilDAO {
19
 
754 jpm 20
	private static String baseUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl();
1906 mathias 21
	private static String baseAuthUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getAuthServiceBaseUrl();
754 jpm 22
	public static final String GET = "GET";
23
	public static final String POST = "POST";
24
	public static final String DELETE = "DELETE";
25
	public static final String PUT = "PUT";
26
	private static final String SEPARATEUR_CHEMIN = "/";
27
	private static final String SEPARATEUR_CLE_VALEUR = "=";
28
	private static final String SEPARATEUR_PARAMETRE = "&";
29
	private static final String SEPARATEUR_CHEMIN_PARAMETRE = "?";
30
	private static final String CONTENU_CHEMIN_VIDE = "*";
748 jpm 31
 
32
	public static JsonRestRequestBuilder construireRequetePost(String nomService)	{
754 jpm 33
		return construireRequete(nomService, null, null, POST);
748 jpm 34
	}
754 jpm 35
 
752 jpm 36
	public static JsonRestRequestBuilder construireRequetePost(String nomService, String[] parametres)	{
754 jpm 37
		return construireRequete(nomService, parametres, null, POST);
348 gduche 38
	}
754 jpm 39
 
40
	public static JsonRestRequestBuilder construireRequete(String nomService)	{
41
		return construireRequete(nomService, null, null, GET);
42
	}
348 gduche 43
 
752 jpm 44
	public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres)	{
754 jpm 45
		return construireRequete(nomService, parametres, null, GET);
182 gduche 46
	}
47
 
748 jpm 48
	public static JsonRestRequestBuilder construireRequete(String nomService, HashMap<String, String> restrictions)	{
754 jpm 49
		return construireRequete(nomService, null, restrictions, GET);
348 gduche 50
	}
754 jpm 51
 
52
	public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, HashMap<String, String> restrictions)	{
53
		return construireRequete(nomService, parametres, restrictions, GET);
54
	}
748 jpm 55
 
754 jpm 56
	private static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, HashMap<String, String> restrictions, String typeRequete)	{
748 jpm 57
		String restrictionsUrl = construireUrlParametres(restrictions);
58
		String parametresUrl = construireUrlChemin(parametres);
182 gduche 59
 
748 jpm 60
		String urlComplete = baseUrl + nomService + parametresUrl + restrictionsUrl;
61
		String urlCompleteEncodee = URL.encode(urlComplete);
182 gduche 62
 
748 jpm 63
		JsonRestRequestBuilder jrrb;
754 jpm 64
		if (typeRequete.equals(GET))	{
748 jpm 65
			jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.GET, urlCompleteEncodee);
66
		} else	{
67
			jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.POST, urlCompleteEncodee);
68
		}
69
		return jrrb;
182 gduche 70
	}
71
 
748 jpm 72
	private static String construireUrlParametres(HashMap<String, String> parametres) {
73
		String parametresUrl = "";
74
		if (parametres != null && parametres.size() > 0)	{
754 jpm 75
			parametresUrl = SEPARATEUR_CHEMIN_PARAMETRE;
748 jpm 76
			Iterator<String> iterateur = parametres.keySet().iterator();
77
			while (iterateur.hasNext())	{
78
				String cle = iterateur.next();
754 jpm 79
				parametresUrl += cle + SEPARATEUR_CLE_VALEUR + parametres.get(cle);
182 gduche 80
 
748 jpm 81
				if (iterateur.hasNext())	{
754 jpm 82
					parametresUrl = parametresUrl + SEPARATEUR_PARAMETRE;
182 gduche 83
				}
84
			}
85
		}
748 jpm 86
		return parametresUrl;
87
	}
88
 
89
	private static String construireUrlChemin(String[] morceauxDuChemin) {
90
		String cheminUrl = "";
91
		if (morceauxDuChemin != null && morceauxDuChemin.length > 0)	{
754 jpm 92
			cheminUrl = SEPARATEUR_CHEMIN;
748 jpm 93
			for (int i = 0; i < morceauxDuChemin.length; i++)	{
1020 jpm 94
				cheminUrl += (UtilString.isEmpty(morceauxDuChemin[i]) ? CONTENU_CHEMIN_VIDE : morceauxDuChemin[i]) + SEPARATEUR_CHEMIN;
148 gduche 95
			}
96
		}
748 jpm 97
		return cheminUrl;
148 gduche 98
	}
1906 mathias 99
 
100
	/**
101
	 * Construit un JsonRestRequestBuilder avec le header Authorization
102
	 */
103
	public static JsonRestRequestBuilder construireRequeteAuth(String nomService, HashMap<String, String> parametres) {
104
		return construireRequeteAuth(nomService, parametres, true);
105
	}
106
 
107
	/**
108
	 * Construit un JsonRestRequestBuilder simple; si authorizationHeader vaut true, ajoute le header
109
	 * "Authorization" pour le service d'authentification SSO (annuaire::auth)
110
	 *
111
	 * @param nomService le nom du service (identite, login, logout...)
112
	 * @param parametres tableau de paramètres
113
	 * @param boolean authorizationHeader si true, enverra le jeton (si existant) dans un header Authorization
114
	 * @return JsonRestRequestBuilder
115
	 */
116
	public static JsonRestRequestBuilder construireRequeteAuth(String nomService, HashMap<String, String> parametres, boolean authorizationHeader) {
117
		String parametresUrl = construireUrlParametres(parametres);
118
		String urlComplete = baseAuthUrl + nomService + parametresUrl;
119
		String urlCompleteEncodee = URL.encode(urlComplete);
120
		JsonRestRequestBuilder jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.GET, urlCompleteEncodee, authorizationHeader);
121
		return jrrb;
122
	}
123
 
1860 aurelien 124
	public static String getUrlService(String nomService) {
125
		return baseUrl + nomService;
126
	}
748 jpm 127
}