1 | package fr.mael.jiwigo.transverse.session; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.io.UnsupportedEncodingException; |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.List; |
---|
8 | |
---|
9 | import org.apache.commons.lang.StringUtils; |
---|
10 | import org.apache.http.HttpHost; |
---|
11 | import org.apache.http.HttpResponse; |
---|
12 | import org.apache.http.NameValuePair; |
---|
13 | import org.apache.http.auth.AuthScope; |
---|
14 | import org.apache.http.auth.UsernamePasswordCredentials; |
---|
15 | import org.apache.http.client.entity.UrlEncodedFormEntity; |
---|
16 | import org.apache.http.client.methods.HttpPost; |
---|
17 | import org.apache.http.conn.params.ConnRoutePNames; |
---|
18 | import org.apache.http.impl.client.DefaultHttpClient; |
---|
19 | import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; |
---|
20 | import org.apache.http.message.BasicNameValuePair; |
---|
21 | import org.w3c.dom.Document; |
---|
22 | |
---|
23 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
24 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
25 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
26 | |
---|
27 | /* |
---|
28 | * jiwigo-ws-api Piwigo webservice access Api |
---|
29 | * Copyright (c) 2010-2011 Mael mael@le-guevel.com |
---|
30 | * All Rights Reserved |
---|
31 | * |
---|
32 | * This library is free software. It comes without any warranty, to |
---|
33 | * the extent permitted by applicable law. You can redistribute it |
---|
34 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
35 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
36 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
37 | */ |
---|
38 | /** |
---|
39 | |
---|
40 | * @author mael |
---|
41 | * Gestionnaire de connexion |
---|
42 | */ |
---|
43 | public class SessionManager { |
---|
44 | |
---|
45 | /** |
---|
46 | * Logger |
---|
47 | */ |
---|
48 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
49 | .getLog(SessionManager.class); |
---|
50 | /** |
---|
51 | * the entered login |
---|
52 | */ |
---|
53 | private String login; |
---|
54 | /** |
---|
55 | * the entered password |
---|
56 | */ |
---|
57 | private String password; |
---|
58 | /** |
---|
59 | * the url of the site |
---|
60 | */ |
---|
61 | private String url; |
---|
62 | /** |
---|
63 | * the http client |
---|
64 | */ |
---|
65 | private DefaultHttpClient client; |
---|
66 | |
---|
67 | /** |
---|
68 | * defines if the user uses a proxy |
---|
69 | */ |
---|
70 | private boolean usesProxy; |
---|
71 | |
---|
72 | /** |
---|
73 | * url of the proxy |
---|
74 | */ |
---|
75 | private String urlProxy; |
---|
76 | |
---|
77 | /** |
---|
78 | * port of the proxy |
---|
79 | */ |
---|
80 | private int portProxy; |
---|
81 | |
---|
82 | /** |
---|
83 | * login for the proxy |
---|
84 | */ |
---|
85 | private String loginProxy; |
---|
86 | |
---|
87 | /** |
---|
88 | * pass for the proxy |
---|
89 | */ |
---|
90 | private String passProxy; |
---|
91 | |
---|
92 | /** |
---|
93 | * true : an error was found for the proxy |
---|
94 | */ |
---|
95 | private boolean proxyError; |
---|
96 | |
---|
97 | /** |
---|
98 | * Constructor |
---|
99 | * @param login the login |
---|
100 | * @param password the password |
---|
101 | * @param url the url of the site |
---|
102 | */ |
---|
103 | public SessionManager(String login, String password, String url) { |
---|
104 | this.login = login; |
---|
105 | this.password = password; |
---|
106 | this.url = url + "/ws.php"; |
---|
107 | // MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); |
---|
108 | // client = new HttpClient(connectionManager); |
---|
109 | ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(); |
---|
110 | client = new DefaultHttpClient(connectionManager); |
---|
111 | //Using of a Linux user agent. cause... it's better 8) |
---|
112 | client.getParams().setParameter("http.useragent", |
---|
113 | "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1"); |
---|
114 | |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Connection method |
---|
119 | * |
---|
120 | * @return 0 if Ok, 1 if not Ok (reason not specified), 2 if proxy error |
---|
121 | * |
---|
122 | * |
---|
123 | */ |
---|
124 | public int processLogin() { |
---|
125 | Document doc; |
---|
126 | //configures the proxy |
---|
127 | if (usesProxy) { |
---|
128 | // HostConfiguration config = client.getHostConfiguration(); |
---|
129 | // config.setProxy(urlProxy, portProxy); |
---|
130 | if (!StringUtils.isEmpty(loginProxy) && !StringUtils.isEmpty(passProxy)) { |
---|
131 | // Credentials credentials = new UsernamePasswordCredentials(loginProxy, passProxy); |
---|
132 | // AuthScope authScope = new AuthScope(urlProxy, portProxy); |
---|
133 | // client.getState().setProxyCredentials(authScope, credentials); |
---|
134 | HttpHost proxy = new HttpHost(urlProxy, portProxy); |
---|
135 | client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); |
---|
136 | client.getCredentialsProvider().setCredentials(new AuthScope(urlProxy, portProxy), |
---|
137 | new UsernamePasswordCredentials(loginProxy, passProxy)); |
---|
138 | |
---|
139 | } |
---|
140 | } |
---|
141 | // try { |
---|
142 | try { |
---|
143 | doc = executeReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", password); |
---|
144 | return (Tools.checkOk(doc) ? 0 : 1); |
---|
145 | } catch (IOException e) { |
---|
146 | LOG.debug(Tools.getStackTrace(e)); |
---|
147 | return 1; |
---|
148 | } catch (ProxyAuthenticationException e) { |
---|
149 | LOG.debug(Tools.getStackTrace(e)); |
---|
150 | return 2; |
---|
151 | } catch (Exception e) { |
---|
152 | LOG.debug(Tools.getStackTrace(e)); |
---|
153 | return 1; |
---|
154 | } |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Executes a method on the webservice and returns the result as a string |
---|
160 | * @param methode the method to execute |
---|
161 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
162 | * @return the result |
---|
163 | * @throws UnsupportedEncodingException |
---|
164 | * @throws ProxyAuthenticationException |
---|
165 | */ |
---|
166 | public String executeReturnString(String methode, String... parametres) throws UnsupportedEncodingException, |
---|
167 | ProxyAuthenticationException { |
---|
168 | if (parametres.length % 2 != 0 && !(parametres == null)) { |
---|
169 | try { |
---|
170 | throw new Exception("Le nombre de parametres doit etre pair"); |
---|
171 | } catch (Exception e) { |
---|
172 | LOG.error(Tools.getStackTrace(e)); |
---|
173 | return null; |
---|
174 | } |
---|
175 | } |
---|
176 | HttpPost method = new HttpPost(url); |
---|
177 | List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); |
---|
178 | nameValuePairs.add(new BasicNameValuePair("method", methode)); |
---|
179 | for (int i = 0; i < parametres.length; i += 2) { |
---|
180 | nameValuePairs.add(new BasicNameValuePair(parametres[i], parametres[i + 1])); |
---|
181 | } |
---|
182 | method.setEntity(new UrlEncodedFormEntity(nameValuePairs)); |
---|
183 | //begin bug:0001833 |
---|
184 | method.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); |
---|
185 | //end |
---|
186 | try { |
---|
187 | HttpResponse response = client.execute(method); |
---|
188 | if (response.getStatusLine().getStatusCode() == 407) { |
---|
189 | throw new ProxyAuthenticationException("Error while trying to auth on the proxy"); |
---|
190 | } |
---|
191 | InputStream inputStream = response.getEntity().getContent(); |
---|
192 | StringBuffer buffer = new StringBuffer(); |
---|
193 | byte[] data = new byte[512]; |
---|
194 | int len = 0; |
---|
195 | try { |
---|
196 | while (-1 != (len = inputStream.read(data))) { |
---|
197 | buffer.append(new String(data, 0, len)); |
---|
198 | } |
---|
199 | } catch (IOException e) { |
---|
200 | e.printStackTrace(); |
---|
201 | } |
---|
202 | try { |
---|
203 | inputStream.close(); |
---|
204 | } catch (IOException e) { |
---|
205 | e.printStackTrace(); |
---|
206 | } |
---|
207 | return buffer.toString(); |
---|
208 | } catch (IOException e) { |
---|
209 | |
---|
210 | } |
---|
211 | return null; |
---|
212 | |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Executes a method on the webservice and returns the result as a Dom document |
---|
217 | * @param methode the method to execute |
---|
218 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
219 | * @return the result |
---|
220 | * @throws IOException |
---|
221 | * @throws ProxyAuthenticationException |
---|
222 | */ |
---|
223 | public Document executeReturnDocument(String methode, String... parametres) throws IOException, |
---|
224 | ProxyAuthenticationException { |
---|
225 | try { |
---|
226 | String returnedString = executeReturnString(methode, parametres); |
---|
227 | return Tools.stringToDocument(returnedString); |
---|
228 | } catch (Exception e) { |
---|
229 | // TODO Auto-generated catch block |
---|
230 | LOG.error(Tools.getStackTrace(e)); |
---|
231 | } |
---|
232 | return null; |
---|
233 | |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * Executes a method on the webservice and returns the result as a Dom document |
---|
238 | * @param methode the method to execute |
---|
239 | * @return the result |
---|
240 | * @throws ProxyAuthenticationException |
---|
241 | */ |
---|
242 | public Document executeReturnDocument(String methode) throws ProxyAuthenticationException { |
---|
243 | try { |
---|
244 | return Tools.stringToDocument(executeReturnString(methode)); |
---|
245 | } catch (Exception e) { |
---|
246 | // TODO Auto-generated catch block |
---|
247 | LOG.error(Tools.getStackTrace(e)); |
---|
248 | } |
---|
249 | return null; |
---|
250 | |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * @return the login |
---|
255 | */ |
---|
256 | public String getLogin() { |
---|
257 | return login; |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * @param login the login to set |
---|
262 | */ |
---|
263 | public void setLogin(String login) { |
---|
264 | this.login = login; |
---|
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 | public String getPassword() { |
---|
317 | return password; |
---|
318 | } |
---|
319 | |
---|
320 | public void setPassword(String password) { |
---|
321 | this.password = password; |
---|
322 | } |
---|
323 | |
---|
324 | public boolean isProxyError() { |
---|
325 | return proxyError; |
---|
326 | } |
---|
327 | |
---|
328 | public void setProxyError(boolean proxyError) { |
---|
329 | this.proxyError = proxyError; |
---|
330 | } |
---|
331 | |
---|
332 | } |
---|