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 org.apache.commons.httpclient.Credentials; |
---|
8 | import org.apache.commons.httpclient.HostConfiguration; |
---|
9 | import org.apache.commons.httpclient.HttpClient; |
---|
10 | import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; |
---|
11 | import org.apache.commons.httpclient.UsernamePasswordCredentials; |
---|
12 | import org.apache.commons.httpclient.auth.AuthScope; |
---|
13 | import org.apache.commons.httpclient.methods.PostMethod; |
---|
14 | import org.apache.commons.lang.StringUtils; |
---|
15 | import org.jdom.Document; |
---|
16 | import org.jdom.JDOMException; |
---|
17 | |
---|
18 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
19 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
20 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
21 | |
---|
22 | /* |
---|
23 | * jiwigo-ws-api Piwigo webservice access Api |
---|
24 | * Copyright (c) 2010-2011 Mael mael@le-guevel.com |
---|
25 | * All Rights Reserved |
---|
26 | * |
---|
27 | * This library is free software. It comes without any warranty, to |
---|
28 | * the extent permitted by applicable law. You can redistribute it |
---|
29 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
30 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
31 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
32 | */ |
---|
33 | /** |
---|
34 | |
---|
35 | * @author mael |
---|
36 | * Gestionnaire de connexion |
---|
37 | */ |
---|
38 | public class SessionManager { |
---|
39 | |
---|
40 | /** |
---|
41 | * Logger |
---|
42 | */ |
---|
43 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
44 | .getLog(SessionManager.class); |
---|
45 | /** |
---|
46 | * the entered login |
---|
47 | */ |
---|
48 | private String login; |
---|
49 | /** |
---|
50 | * the entered password |
---|
51 | */ |
---|
52 | private String password; |
---|
53 | /** |
---|
54 | * the url of the site |
---|
55 | */ |
---|
56 | private String url; |
---|
57 | /** |
---|
58 | * the http client |
---|
59 | */ |
---|
60 | private HttpClient client; |
---|
61 | |
---|
62 | /** |
---|
63 | * defines if the user uses a proxy |
---|
64 | */ |
---|
65 | private boolean usesProxy; |
---|
66 | |
---|
67 | /** |
---|
68 | * url of the proxy |
---|
69 | */ |
---|
70 | private String urlProxy; |
---|
71 | |
---|
72 | /** |
---|
73 | * port of the proxy |
---|
74 | */ |
---|
75 | private int portProxy; |
---|
76 | |
---|
77 | /** |
---|
78 | * login for the proxy |
---|
79 | */ |
---|
80 | private String loginProxy; |
---|
81 | |
---|
82 | /** |
---|
83 | * pass for the proxy |
---|
84 | */ |
---|
85 | private String passProxy; |
---|
86 | |
---|
87 | /** |
---|
88 | * true : an error was found for the proxy |
---|
89 | */ |
---|
90 | private boolean proxyError; |
---|
91 | |
---|
92 | /** |
---|
93 | * Constructor |
---|
94 | * @param login the login |
---|
95 | * @param password the password |
---|
96 | * @param url the url of the site |
---|
97 | */ |
---|
98 | public SessionManager(String login, String password, String url) { |
---|
99 | this.login = login; |
---|
100 | this.password = password; |
---|
101 | this.url = url + "/ws.php"; |
---|
102 | MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); |
---|
103 | client = new HttpClient(connectionManager); |
---|
104 | //Using of a Linux user agent. cause... it's better 8) |
---|
105 | client.getParams().setParameter("http.useragent", |
---|
106 | "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1"); |
---|
107 | |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Connection method |
---|
112 | * |
---|
113 | * @return 0 if Ok, 1 if not Ok (reason not specified), 2 if proxy error |
---|
114 | * |
---|
115 | * |
---|
116 | */ |
---|
117 | public int processLogin() { |
---|
118 | Document doc; |
---|
119 | //configures the proxy |
---|
120 | if (usesProxy) { |
---|
121 | HostConfiguration config = client.getHostConfiguration(); |
---|
122 | config.setProxy(urlProxy, portProxy); |
---|
123 | if (!StringUtils.isEmpty(loginProxy) && !StringUtils.isEmpty(passProxy)) { |
---|
124 | Credentials credentials = new UsernamePasswordCredentials(loginProxy, passProxy); |
---|
125 | AuthScope authScope = new AuthScope(urlProxy, portProxy); |
---|
126 | client.getState().setProxyCredentials(authScope, credentials); |
---|
127 | } |
---|
128 | } |
---|
129 | // try { |
---|
130 | try { |
---|
131 | doc = executeReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", password); |
---|
132 | return (Tools.checkOk(doc) ? 0 : 1); |
---|
133 | } catch (IOException e) { |
---|
134 | LOG.debug(Tools.getStackTrace(e)); |
---|
135 | return 1; |
---|
136 | } catch (ProxyAuthenticationException e) { |
---|
137 | LOG.debug(Tools.getStackTrace(e)); |
---|
138 | return 2; |
---|
139 | } catch (Exception e) { |
---|
140 | LOG.debug(Tools.getStackTrace(e)); |
---|
141 | return 1; |
---|
142 | } |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Executes a method on the webservice and returns the result as a string |
---|
148 | * @param methode the method to execute |
---|
149 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
150 | * @return the result |
---|
151 | * @throws UnsupportedEncodingException |
---|
152 | * @throws ProxyAuthenticationException |
---|
153 | */ |
---|
154 | public String executeReturnString(String methode, String... parametres) throws UnsupportedEncodingException, |
---|
155 | ProxyAuthenticationException { |
---|
156 | if (parametres.length % 2 != 0 && !(parametres == null)) { |
---|
157 | try { |
---|
158 | throw new Exception("Le nombre de parametres doit etre pair"); |
---|
159 | } catch (Exception e) { |
---|
160 | LOG.error(Tools.getStackTrace(e)); |
---|
161 | return null; |
---|
162 | } |
---|
163 | } |
---|
164 | PostMethod method = new PostMethod(url); |
---|
165 | method.addParameter("method", methode); |
---|
166 | for (int i = 0; i < parametres.length; i += 2) { |
---|
167 | // System.out.println(parametres[i] + " -> " + parametres[i + 1]); |
---|
168 | method.addParameter(parametres[i], parametres[i + 1]); |
---|
169 | |
---|
170 | } |
---|
171 | //begin bug:0001833 |
---|
172 | method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); |
---|
173 | //end |
---|
174 | try { |
---|
175 | int test = client.executeMethod(method); |
---|
176 | if (test == 407) { |
---|
177 | throw new ProxyAuthenticationException("Proxy configuration exception. Check username and password"); |
---|
178 | } |
---|
179 | InputStream streamResponse = method.getResponseBodyAsStream(); |
---|
180 | // System.out.println(Outil.readInputStreamAsString(streamResponse)); |
---|
181 | // String toReturn = method.getResponseBodyAsString(); |
---|
182 | String toReturn = Tools.readInputStreamAsString(streamResponse); |
---|
183 | LOG.debug(toReturn); |
---|
184 | return toReturn; |
---|
185 | } catch (IOException e) { |
---|
186 | |
---|
187 | } |
---|
188 | return null; |
---|
189 | |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Executes a method on the webservice and returns the result as a Dom document |
---|
194 | * @param methode the method to execute |
---|
195 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
196 | * @return the result |
---|
197 | * @throws IOException |
---|
198 | * @throws ProxyAuthenticationException |
---|
199 | */ |
---|
200 | public Document executeReturnDocument(String methode, String... parametres) throws IOException, |
---|
201 | ProxyAuthenticationException { |
---|
202 | try { |
---|
203 | String returnedString = executeReturnString(methode, parametres); |
---|
204 | return Tools.stringToDocument(returnedString); |
---|
205 | } catch (JDOMException e) { |
---|
206 | // TODO Auto-generated catch block |
---|
207 | LOG.error(Tools.getStackTrace(e)); |
---|
208 | } |
---|
209 | return null; |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * Executes a method on the webservice and returns the result as a Dom document |
---|
215 | * @param methode the method to execute |
---|
216 | * @return the result |
---|
217 | * @throws ProxyAuthenticationException |
---|
218 | */ |
---|
219 | public Document executeReturnDocument(String methode) throws ProxyAuthenticationException { |
---|
220 | try { |
---|
221 | return Tools.stringToDocument(executeReturnString(methode)); |
---|
222 | } catch (JDOMException e) { |
---|
223 | // TODO Auto-generated catch block |
---|
224 | LOG.error(Tools.getStackTrace(e)); |
---|
225 | } catch (IOException e) { |
---|
226 | // TODO Auto-generated catch block |
---|
227 | LOG.error(Tools.getStackTrace(e)); |
---|
228 | } |
---|
229 | return null; |
---|
230 | |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * @return the login |
---|
235 | */ |
---|
236 | public String getLogin() { |
---|
237 | return login; |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * @param login the login to set |
---|
242 | */ |
---|
243 | public void setLogin(String login) { |
---|
244 | this.login = login; |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * @return the url |
---|
249 | */ |
---|
250 | public String getUrl() { |
---|
251 | return url; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * @param url the url to set |
---|
256 | */ |
---|
257 | public void setUrl(String url) { |
---|
258 | this.url = url; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * @param usesProxy the usesProxy to set |
---|
263 | */ |
---|
264 | public void setUsesProxy(boolean usesProxy) { |
---|
265 | this.usesProxy = usesProxy; |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | * @param urlProxy the urlProxy to set |
---|
270 | */ |
---|
271 | public void setUrlProxy(String urlProxy) { |
---|
272 | this.urlProxy = urlProxy; |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * @param portProxy the portProxy to set |
---|
277 | */ |
---|
278 | public void setPortProxy(int portProxy) { |
---|
279 | this.portProxy = portProxy; |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * @param loginProxy the loginProxy to set |
---|
284 | */ |
---|
285 | public void setLoginProxy(String loginProxy) { |
---|
286 | this.loginProxy = loginProxy; |
---|
287 | } |
---|
288 | |
---|
289 | /** |
---|
290 | * @param passProxy the passProxy to set |
---|
291 | */ |
---|
292 | public void setPassProxy(String passProxy) { |
---|
293 | this.passProxy = passProxy; |
---|
294 | } |
---|
295 | |
---|
296 | public String getPassword() { |
---|
297 | return password; |
---|
298 | } |
---|
299 | |
---|
300 | public void setPassword(String password) { |
---|
301 | this.password = password; |
---|
302 | } |
---|
303 | |
---|
304 | public boolean isProxyError() { |
---|
305 | return proxyError; |
---|
306 | } |
---|
307 | |
---|
308 | public void setProxyError(boolean proxyError) { |
---|
309 | this.proxyError = proxyError; |
---|
310 | } |
---|
311 | |
---|
312 | } |
---|