[6821] | 1 | package fr.mael.jiwigo.transverse.session; |
---|
| 2 | |
---|
| 3 | import java.io.IOException; |
---|
[6840] | 4 | import java.io.InputStream; |
---|
[7219] | 5 | import java.io.UnsupportedEncodingException; |
---|
[6821] | 6 | import javax.swing.JOptionPane; |
---|
| 7 | import org.apache.commons.httpclient.HttpClient; |
---|
| 8 | import org.apache.commons.httpclient.HttpException; |
---|
| 9 | import org.apache.commons.httpclient.methods.PostMethod; |
---|
| 10 | import org.jdom.Document; |
---|
| 11 | import org.jdom.JDOMException; |
---|
| 12 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
| 13 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
| 14 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
| 15 | |
---|
[7219] | 16 | |
---|
[6821] | 17 | /** |
---|
| 18 | Copyright (c) 2010, Mael |
---|
| 19 | All rights reserved. |
---|
| 20 | |
---|
| 21 | Redistribution and use in source and binary forms, with or without |
---|
| 22 | modification, are permitted provided that the following conditions are met: |
---|
| 23 | * Redistributions of source code must retain the above copyright |
---|
| 24 | notice, this list of conditions and the following disclaimer. |
---|
| 25 | * Redistributions in binary form must reproduce the above copyright |
---|
| 26 | notice, this list of conditions and the following disclaimer in the |
---|
| 27 | documentation and/or other materials provided with the distribution. |
---|
| 28 | * Neither the name of jiwigo nor the |
---|
| 29 | names of its contributors may be used to endorse or promote products |
---|
| 30 | derived from this software without specific prior written permission. |
---|
| 31 | |
---|
| 32 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
| 33 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
| 34 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
| 35 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
| 36 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
| 37 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
| 38 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
| 39 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 40 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 41 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 42 | |
---|
| 43 | * @author mael |
---|
| 44 | * Gestionnaire de connexion |
---|
| 45 | */ |
---|
| 46 | public class SessionManager { |
---|
| 47 | |
---|
[7219] | 48 | /** |
---|
| 49 | * Logger |
---|
| 50 | */ |
---|
| 51 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(SessionManager.class); |
---|
| 52 | /** |
---|
| 53 | * the entered login |
---|
| 54 | */ |
---|
| 55 | private String login; |
---|
| 56 | /** |
---|
| 57 | * the entered password |
---|
| 58 | */ |
---|
| 59 | private String motDePasse; |
---|
| 60 | /** |
---|
| 61 | * the url of the site |
---|
| 62 | */ |
---|
| 63 | private String url; |
---|
| 64 | /** |
---|
| 65 | * the http client |
---|
| 66 | */ |
---|
| 67 | private HttpClient client; |
---|
[6821] | 68 | |
---|
[7219] | 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"); |
---|
[6821] | 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
[7219] | 87 | /** |
---|
| 88 | * Connection method |
---|
| 89 | * @return true if successful |
---|
| 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 | /** |
---|
| 105 | * Executes a method on the webservice and returns the result as a string |
---|
| 106 | * @param methode the method to execute |
---|
| 107 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
| 108 | * @return the result |
---|
| 109 | * @throws UnsupportedEncodingException |
---|
| 110 | */ |
---|
| 111 | public String executerReturnString(String methode, String... parametres) throws UnsupportedEncodingException { |
---|
| 112 | if (parametres.length % 2 != 0 && !(parametres == null)) { |
---|
| 113 | try { |
---|
| 114 | throw new Exception("Le nombre de parametres doit etre pair"); |
---|
| 115 | } catch (Exception e) { |
---|
| 116 | LOG.error(Outil.getStackTrace(e)); |
---|
| 117 | return null; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | PostMethod method = new PostMethod(url); |
---|
| 121 | method.addParameter("method", methode); |
---|
| 122 | for (int i = 0; i < parametres.length; i += 2) { |
---|
| 123 | method.addParameter(parametres[i], parametres[i + 1]); |
---|
| 124 | |
---|
| 125 | } |
---|
| 126 | //begin bug:0001833 |
---|
| 127 | method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); |
---|
| 128 | //end |
---|
| 129 | |
---|
| 130 | try { |
---|
| 131 | client.executeMethod(method); |
---|
| 132 | InputStream streamResponse = method.getResponseBodyAsStream(); |
---|
| 133 | // System.out.println(Outil.readInputStreamAsString(streamResponse)); |
---|
| 134 | // String toReturn = method.getResponseBodyAsString(); |
---|
| 135 | String toReturn = Outil.readInputStreamAsString(streamResponse); |
---|
| 136 | LOG.debug(toReturn); |
---|
| 137 | return toReturn; |
---|
| 138 | } catch (HttpException e) { |
---|
| 139 | // TODO Auto-generated catch block |
---|
| 140 | LOG.error(Outil.getStackTrace(e)); |
---|
| 141 | } catch (IOException e) { |
---|
| 142 | // TODO Auto-generated catch block |
---|
| 143 | LOG.error(Outil.getStackTrace(e)); |
---|
| 144 | } catch (IllegalArgumentException e) { |
---|
| 145 | LOG.error(Outil.getStackTrace(e)); |
---|
| 146 | JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), |
---|
| 147 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 148 | } finally { |
---|
| 149 | method.releaseConnection(); |
---|
| 150 | } |
---|
[6821] | 151 | return null; |
---|
[7219] | 152 | |
---|
[6821] | 153 | } |
---|
[7219] | 154 | |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * Executes a method on the webservice and returns the result as a Dom document |
---|
| 158 | * @param methode the method to execute |
---|
| 159 | * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value |
---|
| 160 | * @return the result |
---|
| 161 | * @throws IOException |
---|
| 162 | */ |
---|
| 163 | public Document executerReturnDocument(String methode, String... parametres) throws IOException { |
---|
| 164 | try { |
---|
| 165 | return Outil.stringToDocument(executerReturnString(methode, parametres)); |
---|
| 166 | } catch (JDOMException e) { |
---|
| 167 | // TODO Auto-generated catch block |
---|
| 168 | LOG.error(Outil.getStackTrace(e)); |
---|
| 169 | } |
---|
| 170 | return null; |
---|
| 171 | |
---|
[6821] | 172 | } |
---|
[6831] | 173 | |
---|
[7219] | 174 | |
---|
| 175 | /** |
---|
| 176 | * Executes a method on the webservice and returns the result as a Dom document |
---|
| 177 | * @param methode the method to execute |
---|
| 178 | * @return the result |
---|
| 179 | */ |
---|
| 180 | public Document executerReturnDocument(String methode) { |
---|
| 181 | try { |
---|
| 182 | return Outil.stringToDocument(executerReturnString(methode)); |
---|
| 183 | } catch (JDOMException e) { |
---|
| 184 | // TODO Auto-generated catch block |
---|
| 185 | LOG.error(Outil.getStackTrace(e)); |
---|
| 186 | } catch (IOException e) { |
---|
| 187 | // TODO Auto-generated catch block |
---|
| 188 | LOG.error(Outil.getStackTrace(e)); |
---|
| 189 | } |
---|
| 190 | return null; |
---|
| 191 | |
---|
[6821] | 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
[7219] | 195 | /** |
---|
| 196 | * @return the login |
---|
| 197 | */ |
---|
| 198 | public String getLogin() { |
---|
| 199 | return login; |
---|
[6821] | 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
[7219] | 203 | /** |
---|
| 204 | * @param login the login to set |
---|
| 205 | */ |
---|
| 206 | public void setLogin(String login) { |
---|
| 207 | this.login = login; |
---|
[6821] | 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
[7219] | 211 | /** |
---|
| 212 | * @return the motDePasse |
---|
| 213 | */ |
---|
| 214 | public String getMotDePasse() { |
---|
| 215 | return motDePasse; |
---|
| 216 | } |
---|
[6821] | 217 | |
---|
| 218 | |
---|
[7219] | 219 | /** |
---|
| 220 | * @param motDePasse the motDePasse to set |
---|
| 221 | */ |
---|
| 222 | public void setMotDePasse(String motDePasse) { |
---|
| 223 | this.motDePasse = motDePasse; |
---|
| 224 | } |
---|
[6821] | 225 | |
---|
| 226 | |
---|
[7219] | 227 | /** |
---|
| 228 | * @return the url |
---|
| 229 | */ |
---|
| 230 | public String getUrl() { |
---|
| 231 | return url; |
---|
| 232 | } |
---|
[6821] | 233 | |
---|
| 234 | |
---|
[7219] | 235 | /** |
---|
| 236 | * @param url the url to set |
---|
| 237 | */ |
---|
| 238 | public void setUrl(String url) { |
---|
| 239 | this.url = url; |
---|
| 240 | } |
---|
| 241 | |
---|
[6821] | 242 | } |
---|