1 | package fr.mael.jiwigo.transverse.session; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | |
---|
6 | import javax.swing.JOptionPane; |
---|
7 | |
---|
8 | import org.apache.commons.httpclient.HttpClient; |
---|
9 | import org.apache.commons.httpclient.HttpException; |
---|
10 | import org.apache.commons.httpclient.methods.PostMethod; |
---|
11 | import org.jdom.Document; |
---|
12 | import org.jdom.JDOMException; |
---|
13 | |
---|
14 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
15 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
16 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
17 | |
---|
18 | /** |
---|
19 | Copyright (c) 2010, Mael |
---|
20 | All rights reserved. |
---|
21 | |
---|
22 | Redistribution and use in source and binary forms, with or without |
---|
23 | modification, are permitted provided that the following conditions are met: |
---|
24 | * Redistributions of source code must retain the above copyright |
---|
25 | notice, this list of conditions and the following disclaimer. |
---|
26 | * Redistributions in binary form must reproduce the above copyright |
---|
27 | notice, this list of conditions and the following disclaimer in the |
---|
28 | documentation and/or other materials provided with the distribution. |
---|
29 | * Neither the name of jiwigo nor the |
---|
30 | names of its contributors may be used to endorse or promote products |
---|
31 | derived from this software without specific prior written permission. |
---|
32 | |
---|
33 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
34 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
35 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
36 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
37 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
38 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
39 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
40 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
41 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
42 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
43 | |
---|
44 | * @author mael |
---|
45 | * Gestionnaire de connexion |
---|
46 | */ |
---|
47 | public class SessionManager { |
---|
48 | /** |
---|
49 | * Logger |
---|
50 | */ |
---|
51 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
52 | .getLog(SessionManager.class); |
---|
53 | /** |
---|
54 | * Le login entré |
---|
55 | */ |
---|
56 | private String login; |
---|
57 | /** |
---|
58 | * Le mot de passe entré |
---|
59 | */ |
---|
60 | private String motDePasse; |
---|
61 | /** |
---|
62 | * l'url du site |
---|
63 | */ |
---|
64 | private String url; |
---|
65 | /** |
---|
66 | * le client http |
---|
67 | */ |
---|
68 | private HttpClient client; |
---|
69 | |
---|
70 | /** |
---|
71 | * Constructeur |
---|
72 | * @param login le login |
---|
73 | * @param motDePasse le mot de passe |
---|
74 | * @param url l'url du site |
---|
75 | */ |
---|
76 | public SessionManager(String login, String motDePasse, String url) { |
---|
77 | this.login = login; |
---|
78 | this.motDePasse = motDePasse; |
---|
79 | this.url = url + "/ws.php"; |
---|
80 | System.out.println(this.url); |
---|
81 | client = new HttpClient(); |
---|
82 | //utilisation d'un useragent linux, parce que c'est mieux 8) |
---|
83 | client.getParams().setParameter("http.useragent", |
---|
84 | "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1"); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Methode de connexion |
---|
89 | * @return true si la connexion s'est bien deroulee |
---|
90 | */ |
---|
91 | public boolean processLogin() { |
---|
92 | Document doc; |
---|
93 | try { |
---|
94 | doc = executerReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", motDePasse); |
---|
95 | return Outil.checkOk(doc); |
---|
96 | } catch (Exception e) { |
---|
97 | LOG.error(Outil.getStackTrace(e)); |
---|
98 | } |
---|
99 | return false; |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Execute une methode sur le web service et retourne le resultat sous forme de string |
---|
105 | * @param methode la methode a executer |
---|
106 | * @param parametres les parametres a ajouter a la methode. doit etre un nombre pair : la cle suivie de sa valeur |
---|
107 | * @return le resultat |
---|
108 | */ |
---|
109 | public String executerReturnString(String methode, String... parametres) { |
---|
110 | if (parametres.length % 2 != 0 && !(parametres == null)) { |
---|
111 | try { |
---|
112 | throw new Exception("Le nombre de parametres doit etre pair"); |
---|
113 | } catch (Exception e) { |
---|
114 | LOG.error(Outil.getStackTrace(e)); |
---|
115 | return null; |
---|
116 | } |
---|
117 | } |
---|
118 | PostMethod method = new PostMethod(url); |
---|
119 | method.addParameter("method", methode); |
---|
120 | for (int i = 0; i < parametres.length; i += 2) { |
---|
121 | method.addParameter(parametres[i], parametres[i + 1]); |
---|
122 | } |
---|
123 | //begin bug:0001833 |
---|
124 | method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1"); |
---|
125 | //end |
---|
126 | |
---|
127 | try { |
---|
128 | client.executeMethod(method); |
---|
129 | InputStream streamResponse = method.getResponseBodyAsStream(); |
---|
130 | // System.out.println(Outil.readInputStreamAsString(streamResponse)); |
---|
131 | // String toReturn = method.getResponseBodyAsString(); |
---|
132 | String toReturn = Outil.readInputStreamAsString(streamResponse); |
---|
133 | LOG.debug(toReturn); |
---|
134 | return toReturn; |
---|
135 | } catch (HttpException e) { |
---|
136 | // TODO Auto-generated catch block |
---|
137 | LOG.error(Outil.getStackTrace(e)); |
---|
138 | } catch (IOException e) { |
---|
139 | // TODO Auto-generated catch block |
---|
140 | LOG.error(Outil.getStackTrace(e)); |
---|
141 | } catch (IllegalArgumentException e) { |
---|
142 | LOG.error(Outil.getStackTrace(e)); |
---|
143 | JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages |
---|
144 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
145 | } finally { |
---|
146 | method.releaseConnection(); |
---|
147 | } |
---|
148 | return null; |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Execute une methode sur le webservice et renvoie le resultat sous form de document jdom |
---|
154 | * @param methode la methode a executer |
---|
155 | * @param parametres les parametres a ajouter a la methode. doit etre un nombre pair : la cle suivie de sa valeur |
---|
156 | * @return le resultat |
---|
157 | * @throws IOException |
---|
158 | */ |
---|
159 | public Document executerReturnDocument(String methode, String... parametres) throws IOException { |
---|
160 | try { |
---|
161 | return Outil.stringToDocument(executerReturnString(methode, parametres)); |
---|
162 | } catch (JDOMException e) { |
---|
163 | // TODO Auto-generated catch block |
---|
164 | LOG.error(Outil.getStackTrace(e)); |
---|
165 | } |
---|
166 | return null; |
---|
167 | |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Execute une methode sur le webservice et renvoie le resultat sous form de document jdom |
---|
172 | * @param methode la methode a executer |
---|
173 | * @return le resultat |
---|
174 | */ |
---|
175 | public Document executerReturnDocument(String methode) { |
---|
176 | try { |
---|
177 | return Outil.stringToDocument(executerReturnString(methode)); |
---|
178 | } catch (JDOMException e) { |
---|
179 | // TODO Auto-generated catch block |
---|
180 | LOG.error(Outil.getStackTrace(e)); |
---|
181 | } catch (IOException e) { |
---|
182 | // TODO Auto-generated catch block |
---|
183 | LOG.error(Outil.getStackTrace(e)); |
---|
184 | } |
---|
185 | return null; |
---|
186 | |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * @return the login |
---|
191 | */ |
---|
192 | public String getLogin() { |
---|
193 | return login; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * @param login the login to set |
---|
198 | */ |
---|
199 | public void setLogin(String login) { |
---|
200 | this.login = login; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * @return the motDePasse |
---|
205 | */ |
---|
206 | public String getMotDePasse() { |
---|
207 | return motDePasse; |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * @param motDePasse the motDePasse to set |
---|
212 | */ |
---|
213 | public void setMotDePasse(String motDePasse) { |
---|
214 | this.motDePasse = motDePasse; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * @return the url |
---|
219 | */ |
---|
220 | public String getUrl() { |
---|
221 | return url; |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * @param url the url to set |
---|
226 | */ |
---|
227 | public void setUrl(String url) { |
---|
228 | this.url = url; |
---|
229 | } |
---|
230 | |
---|
231 | } |
---|