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 | * the entered login |
---|
55 | */ |
---|
56 | private String login; |
---|
57 | /** |
---|
58 | * the entered password |
---|
59 | */ |
---|
60 | private String motDePasse; |
---|
61 | /** |
---|
62 | * the url of the site |
---|
63 | */ |
---|
64 | private String url; |
---|
65 | /** |
---|
66 | * the http client |
---|
67 | */ |
---|
68 | private HttpClient client; |
---|
69 | |
---|
70 | /** |
---|
71 | * Constructor |
---|
72 | * @param login the login |
---|
73 | * @param motDePasse the password |
---|
74 | * @param url the url of the 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 | client = new HttpClient(); |
---|
81 | //Using of a Linux user agent. cause... it's better 8) |
---|
82 | client.getParams().setParameter("http.useragent", |
---|
83 | "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1"); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Connection method |
---|
88 | * @return true if successful |
---|
89 | */ |
---|
90 | public boolean processLogin() { |
---|
91 | Document doc; |
---|
92 | try { |
---|
93 | doc = executerReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", motDePasse); |
---|
94 | return Outil.checkOk(doc); |
---|
95 | } catch (Exception e) { |
---|
96 | LOG.error(Outil.getStackTrace(e)); |
---|
97 | } |
---|
98 | return false; |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Executes a method on the webservice and returns the result as a string |
---|
104 | * @param methode the method to execute |
---|
105 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
106 | * @return the result |
---|
107 | */ |
---|
108 | public String executerReturnString(String methode, String... parametres) { |
---|
109 | if (parametres.length % 2 != 0 && !(parametres == null)) { |
---|
110 | try { |
---|
111 | throw new Exception("Le nombre de parametres doit etre pair"); |
---|
112 | } catch (Exception e) { |
---|
113 | LOG.error(Outil.getStackTrace(e)); |
---|
114 | return null; |
---|
115 | } |
---|
116 | } |
---|
117 | PostMethod method = new PostMethod(url); |
---|
118 | method.addParameter("method", methode); |
---|
119 | for (int i = 0; i < parametres.length; i += 2) { |
---|
120 | method.addParameter(parametres[i], parametres[i + 1]); |
---|
121 | } |
---|
122 | //begin bug:0001833 |
---|
123 | method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1"); |
---|
124 | //end |
---|
125 | |
---|
126 | try { |
---|
127 | client.executeMethod(method); |
---|
128 | InputStream streamResponse = method.getResponseBodyAsStream(); |
---|
129 | // System.out.println(Outil.readInputStreamAsString(streamResponse)); |
---|
130 | // String toReturn = method.getResponseBodyAsString(); |
---|
131 | String toReturn = Outil.readInputStreamAsString(streamResponse); |
---|
132 | LOG.debug(toReturn); |
---|
133 | return toReturn; |
---|
134 | } catch (HttpException e) { |
---|
135 | // TODO Auto-generated catch block |
---|
136 | LOG.error(Outil.getStackTrace(e)); |
---|
137 | } catch (IOException e) { |
---|
138 | // TODO Auto-generated catch block |
---|
139 | LOG.error(Outil.getStackTrace(e)); |
---|
140 | } catch (IllegalArgumentException e) { |
---|
141 | LOG.error(Outil.getStackTrace(e)); |
---|
142 | JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages |
---|
143 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
144 | } finally { |
---|
145 | method.releaseConnection(); |
---|
146 | } |
---|
147 | return null; |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Executes a method on the webservice and returns the result as a Dom document |
---|
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 IOException |
---|
157 | */ |
---|
158 | public Document executerReturnDocument(String methode, String... parametres) throws IOException { |
---|
159 | try { |
---|
160 | return Outil.stringToDocument(executerReturnString(methode, parametres)); |
---|
161 | } catch (JDOMException e) { |
---|
162 | // TODO Auto-generated catch block |
---|
163 | LOG.error(Outil.getStackTrace(e)); |
---|
164 | } |
---|
165 | return null; |
---|
166 | |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Executes a method on the webservice and returns the result as a Dom document |
---|
171 | * @param methode the method to execute |
---|
172 | * @return the result |
---|
173 | */ |
---|
174 | public Document executerReturnDocument(String methode) { |
---|
175 | try { |
---|
176 | return Outil.stringToDocument(executerReturnString(methode)); |
---|
177 | } catch (JDOMException e) { |
---|
178 | // TODO Auto-generated catch block |
---|
179 | LOG.error(Outil.getStackTrace(e)); |
---|
180 | } catch (IOException e) { |
---|
181 | // TODO Auto-generated catch block |
---|
182 | LOG.error(Outil.getStackTrace(e)); |
---|
183 | } |
---|
184 | return null; |
---|
185 | |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * @return the login |
---|
190 | */ |
---|
191 | public String getLogin() { |
---|
192 | return login; |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * @param login the login to set |
---|
197 | */ |
---|
198 | public void setLogin(String login) { |
---|
199 | this.login = login; |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * @return the motDePasse |
---|
204 | */ |
---|
205 | public String getMotDePasse() { |
---|
206 | return motDePasse; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * @param motDePasse the motDePasse to set |
---|
211 | */ |
---|
212 | public void setMotDePasse(String motDePasse) { |
---|
213 | this.motDePasse = motDePasse; |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * @return the url |
---|
218 | */ |
---|
219 | public String getUrl() { |
---|
220 | return url; |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * @param url the url to set |
---|
225 | */ |
---|
226 | public void setUrl(String url) { |
---|
227 | this.url = url; |
---|
228 | } |
---|
229 | |
---|
230 | } |
---|