1 | package fr.mael.jiwigo.transverse.session; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.io.UnsupportedEncodingException; |
---|
6 | |
---|
7 | import javax.swing.JOptionPane; |
---|
8 | |
---|
9 | import org.apache.commons.httpclient.Credentials; |
---|
10 | import org.apache.commons.httpclient.HostConfiguration; |
---|
11 | import org.apache.commons.httpclient.HttpClient; |
---|
12 | import org.apache.commons.httpclient.HttpException; |
---|
13 | import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; |
---|
14 | import org.apache.commons.httpclient.UsernamePasswordCredentials; |
---|
15 | import org.apache.commons.httpclient.auth.AuthScope; |
---|
16 | import org.apache.commons.httpclient.methods.PostMethod; |
---|
17 | import org.apache.commons.lang.StringUtils; |
---|
18 | import org.jdom.Document; |
---|
19 | import org.jdom.JDOMException; |
---|
20 | |
---|
21 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
22 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
23 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
24 | |
---|
25 | /** |
---|
26 | Copyright (c) 2010, Mael |
---|
27 | All rights reserved. |
---|
28 | |
---|
29 | Redistribution and use in source and binary forms, with or without |
---|
30 | modification, are permitted provided that the following conditions are met: |
---|
31 | * Redistributions of source code must retain the above copyright |
---|
32 | notice, this list of conditions and the following disclaimer. |
---|
33 | * Redistributions in binary form must reproduce the above copyright |
---|
34 | notice, this list of conditions and the following disclaimer in the |
---|
35 | documentation and/or other materials provided with the distribution. |
---|
36 | * Neither the name of jiwigo nor the |
---|
37 | names of its contributors may be used to endorse or promote products |
---|
38 | derived from this software without specific prior written permission. |
---|
39 | |
---|
40 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
41 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
42 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
43 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
44 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
45 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
46 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
47 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
49 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
50 | |
---|
51 | * @author mael |
---|
52 | * Gestionnaire de connexion |
---|
53 | */ |
---|
54 | public class SessionManager { |
---|
55 | |
---|
56 | /** |
---|
57 | * Logger |
---|
58 | */ |
---|
59 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
60 | .getLog(SessionManager.class); |
---|
61 | /** |
---|
62 | * the entered login |
---|
63 | */ |
---|
64 | private String login; |
---|
65 | /** |
---|
66 | * the entered password |
---|
67 | */ |
---|
68 | private String motDePasse; |
---|
69 | /** |
---|
70 | * the url of the site |
---|
71 | */ |
---|
72 | private String url; |
---|
73 | /** |
---|
74 | * the http client |
---|
75 | */ |
---|
76 | private HttpClient client; |
---|
77 | |
---|
78 | /** |
---|
79 | * defines if the user uses a proxy |
---|
80 | */ |
---|
81 | private boolean usesProxy; |
---|
82 | |
---|
83 | /** |
---|
84 | * url of the proxy |
---|
85 | */ |
---|
86 | private String urlProxy; |
---|
87 | |
---|
88 | /** |
---|
89 | * port of the proxy |
---|
90 | */ |
---|
91 | private int portProxy; |
---|
92 | |
---|
93 | /** |
---|
94 | * login for the proxy |
---|
95 | */ |
---|
96 | private String loginProxy; |
---|
97 | |
---|
98 | /** |
---|
99 | * pass for the proxy |
---|
100 | */ |
---|
101 | private String passProxy; |
---|
102 | |
---|
103 | /** |
---|
104 | * true : an error was found for the proxy |
---|
105 | */ |
---|
106 | private boolean erreurProxy; |
---|
107 | |
---|
108 | /** |
---|
109 | * Constructor |
---|
110 | * @param login the login |
---|
111 | * @param motDePasse the password |
---|
112 | * @param url the url of the site |
---|
113 | */ |
---|
114 | public SessionManager(String login, String motDePasse, String url) { |
---|
115 | this.login = login; |
---|
116 | this.motDePasse = motDePasse; |
---|
117 | this.url = url + "/ws.php"; |
---|
118 | MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); |
---|
119 | client = new HttpClient(connectionManager); |
---|
120 | //Using of a Linux user agent. cause... it's better 8) |
---|
121 | client.getParams().setParameter("http.useragent", |
---|
122 | "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1"); |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Connection method |
---|
128 | * @return true if successful |
---|
129 | */ |
---|
130 | public boolean processLogin() { |
---|
131 | Document doc; |
---|
132 | //configures the proxy |
---|
133 | if (usesProxy) { |
---|
134 | HostConfiguration config = client.getHostConfiguration(); |
---|
135 | config.setProxy(urlProxy, portProxy); |
---|
136 | if (!StringUtils.isEmpty(loginProxy) && !StringUtils.isEmpty(passProxy)) { |
---|
137 | Credentials credentials = new UsernamePasswordCredentials(loginProxy, passProxy); |
---|
138 | AuthScope authScope = new AuthScope(urlProxy, portProxy); |
---|
139 | client.getState().setProxyCredentials(authScope, credentials); |
---|
140 | } |
---|
141 | } |
---|
142 | try { |
---|
143 | doc = executerReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", motDePasse); |
---|
144 | return Outil.checkOk(doc); |
---|
145 | } catch (Exception e) { |
---|
146 | LOG.error(Outil.getStackTrace(e)); |
---|
147 | } |
---|
148 | return false; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Executes a method on the webservice and returns the result as a string |
---|
153 | * @param methode the method to execute |
---|
154 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
155 | * @return the result |
---|
156 | * @throws UnsupportedEncodingException |
---|
157 | */ |
---|
158 | public String executerReturnString(String methode, String... parametres) throws UnsupportedEncodingException { |
---|
159 | if (parametres.length % 2 != 0 && !(parametres == null)) { |
---|
160 | try { |
---|
161 | throw new Exception("Le nombre de parametres doit etre pair"); |
---|
162 | } catch (Exception e) { |
---|
163 | LOG.error(Outil.getStackTrace(e)); |
---|
164 | return null; |
---|
165 | } |
---|
166 | } |
---|
167 | PostMethod method = new PostMethod(url); |
---|
168 | method.addParameter("method", methode); |
---|
169 | for (int i = 0; i < parametres.length; i += 2) { |
---|
170 | method.addParameter(parametres[i], parametres[i + 1]); |
---|
171 | |
---|
172 | } |
---|
173 | //begin bug:0001833 |
---|
174 | method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); |
---|
175 | //end |
---|
176 | |
---|
177 | try { |
---|
178 | client.executeMethod(method); |
---|
179 | InputStream streamResponse = method.getResponseBodyAsStream(); |
---|
180 | // System.out.println(Outil.readInputStreamAsString(streamResponse)); |
---|
181 | // String toReturn = method.getResponseBodyAsString(); |
---|
182 | String toReturn = Outil.readInputStreamAsString(streamResponse); |
---|
183 | LOG.debug(toReturn); |
---|
184 | return toReturn; |
---|
185 | } catch (HttpException e) { |
---|
186 | // TODO Auto-generated catch block |
---|
187 | LOG.error(Outil.getStackTrace(e)); |
---|
188 | } catch (IOException e) { |
---|
189 | // TODO Auto-generated catch block |
---|
190 | LOG.error(Outil.getStackTrace(e)); |
---|
191 | } catch (IllegalArgumentException e) { |
---|
192 | LOG.error(Outil.getStackTrace(e)); |
---|
193 | JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages |
---|
194 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
195 | } finally { |
---|
196 | method.releaseConnection(); |
---|
197 | } |
---|
198 | return null; |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Executes a method on the webservice and returns the result as a Dom document |
---|
204 | * @param methode the method to execute |
---|
205 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
206 | * @return the result |
---|
207 | * @throws IOException |
---|
208 | */ |
---|
209 | public Document executerReturnDocument(String methode, String... parametres) throws IOException { |
---|
210 | try { |
---|
211 | return Outil.stringToDocument(executerReturnString(methode, parametres)); |
---|
212 | } catch (JDOMException e) { |
---|
213 | // TODO Auto-generated catch block |
---|
214 | LOG.error(Outil.getStackTrace(e)); |
---|
215 | } |
---|
216 | return null; |
---|
217 | |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * Executes a method on the webservice and returns the result as a Dom document |
---|
222 | * @param methode the method to execute |
---|
223 | * @return the result |
---|
224 | */ |
---|
225 | public Document executerReturnDocument(String methode) { |
---|
226 | try { |
---|
227 | return Outil.stringToDocument(executerReturnString(methode)); |
---|
228 | } catch (JDOMException e) { |
---|
229 | // TODO Auto-generated catch block |
---|
230 | LOG.error(Outil.getStackTrace(e)); |
---|
231 | } catch (IOException e) { |
---|
232 | // TODO Auto-generated catch block |
---|
233 | LOG.error(Outil.getStackTrace(e)); |
---|
234 | } |
---|
235 | return null; |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * @return the login |
---|
241 | */ |
---|
242 | public String getLogin() { |
---|
243 | return login; |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * @param login the login to set |
---|
248 | */ |
---|
249 | public void setLogin(String login) { |
---|
250 | this.login = login; |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * @return the motDePasse |
---|
255 | */ |
---|
256 | public String getMotDePasse() { |
---|
257 | return motDePasse; |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * @param motDePasse the motDePasse to set |
---|
262 | */ |
---|
263 | public void setMotDePasse(String motDePasse) { |
---|
264 | this.motDePasse = motDePasse; |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * @return the url |
---|
269 | */ |
---|
270 | public String getUrl() { |
---|
271 | return url; |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * @param url the url to set |
---|
276 | */ |
---|
277 | public void setUrl(String url) { |
---|
278 | this.url = url; |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * @param usesProxy the usesProxy to set |
---|
283 | */ |
---|
284 | public void setUsesProxy(boolean usesProxy) { |
---|
285 | this.usesProxy = usesProxy; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * @param urlProxy the urlProxy to set |
---|
290 | */ |
---|
291 | public void setUrlProxy(String urlProxy) { |
---|
292 | this.urlProxy = urlProxy; |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * @param portProxy the portProxy to set |
---|
297 | */ |
---|
298 | public void setPortProxy(int portProxy) { |
---|
299 | this.portProxy = portProxy; |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * @param loginProxy the loginProxy to set |
---|
304 | */ |
---|
305 | public void setLoginProxy(String loginProxy) { |
---|
306 | this.loginProxy = loginProxy; |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * @param passProxy the passProxy to set |
---|
311 | */ |
---|
312 | public void setPassProxy(String passProxy) { |
---|
313 | this.passProxy = passProxy; |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | * @return the erreurProxy |
---|
318 | */ |
---|
319 | public boolean isErreurProxy() { |
---|
320 | return erreurProxy; |
---|
321 | } |
---|
322 | |
---|
323 | } |
---|