source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/session/SessionManager.java @ 6832

Last change on this file since 6832 was 6832, checked in by mlg, 14 years ago

Bug correction :
The scrollpane on the thumbnails viewer did not work. Now it works and each row of the viewer contains 6 thumbnails.
The scrollpane is displayed "as needed".

File size: 6.7 KB
Line 
1package fr.mael.jiwigo.transverse.session;
2
3import java.io.IOException;
4
5import javax.swing.JOptionPane;
6
7import org.apache.commons.httpclient.HttpClient;
8import org.apache.commons.httpclient.HttpException;
9import org.apache.commons.httpclient.methods.PostMethod;
10import org.jdom.Document;
11import org.jdom.JDOMException;
12
13import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
14import fr.mael.jiwigo.transverse.util.Messages;
15import fr.mael.jiwigo.transverse.util.Outil;
16
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 */
46public class SessionManager {
47    /**
48     * Logger
49     */
50    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
51            .getLog(SessionManager.class);
52    /**
53     * Le login entré
54     */
55    private String login;
56    /**
57     * Le mot de passe entré
58     */
59    private String motDePasse;
60    /**
61     * l'url du site
62     */
63    private String url;
64    /**
65     * le client http
66     */
67    private HttpClient client;
68
69    /**
70     * Constructeur
71     * @param login le login
72     * @param motDePasse le mot de passe
73     * @param url l'url du site
74     */
75    public SessionManager(String login, String motDePasse, String url) {
76        this.login = login;
77        this.motDePasse = motDePasse;
78        this.url = url + "/ws.php";
79        client = new HttpClient();
80        //utilisation d'un useragent linux, parce que c'est mieux 8)
81        client.getParams().setParameter("http.useragent",
82                "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1");
83    }
84
85    /**
86     * Methode de connexion
87     * @return true si la connexion s'est bien deroulee
88     */
89    public boolean processLogin() {
90        Document doc;
91        try {
92            doc = executerReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", motDePasse);
93            return Outil.checkOk(doc);
94        } catch (Exception e) {
95            LOG.error(Outil.getStackTrace(e));
96        }
97        return false;
98
99    }
100
101    /**
102     * Execute une methode sur le web service et retourne le resultat sous forme de string
103     * @param methode la methode a executer
104     * @param parametres les parametres a  ajouter a la methode. doit etre un nombre pair : la cle suivie de sa valeur
105     * @return le resultat
106     */
107    public String executerReturnString(String methode, String... parametres) {
108        if (parametres.length % 2 != 0 && !(parametres == null)) {
109            try {
110                throw new Exception("Le nombre de parametres doit etre pair");
111            } catch (Exception e) {
112                LOG.error(Outil.getStackTrace(e));
113                return null;
114            }
115        }
116        PostMethod method = new PostMethod(url);
117        method.addParameter("method", methode);
118        for (int i = 0; i < parametres.length; i += 2) {
119            method.addParameter(parametres[i], parametres[i + 1]);
120        }
121        //begin bug:0001833
122        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
123        //end
124
125        try {
126            client.executeMethod(method);
127            String toReturn = method.getResponseBodyAsString();
128            LOG.debug(toReturn);
129            return toReturn;
130        } catch (HttpException e) {
131            // TODO Auto-generated catch block
132            LOG.error(Outil.getStackTrace(e));
133        } catch (IOException e) {
134            // TODO Auto-generated catch block
135            LOG.error(Outil.getStackTrace(e));
136        } catch (IllegalArgumentException e) {
137            LOG.error(Outil.getStackTrace(e));
138            JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages
139                    .getMessage("error"), JOptionPane.ERROR_MESSAGE);
140        } finally {
141            method.releaseConnection();
142        }
143        return null;
144
145    }
146
147    /**
148     * Execute une methode sur le webservice et renvoie le resultat sous form de document jdom
149     * @param methode la methode a executer
150     * @param parametres les parametres a  ajouter a la methode. doit etre un nombre pair : la cle suivie de sa valeur
151     * @return le resultat
152     * @throws IOException
153     */
154    public Document executerReturnDocument(String methode, String... parametres) throws IOException {
155        try {
156            return Outil.stringToDocument(executerReturnString(methode, parametres));
157        } catch (JDOMException e) {
158            // TODO Auto-generated catch block
159            LOG.error(Outil.getStackTrace(e));
160        }
161        return null;
162
163    }
164
165    /**
166     * Execute une methode sur le webservice et renvoie le resultat sous form de document jdom
167     * @param methode la methode a executer
168     * @return le resultat
169     */
170    public Document executerReturnDocument(String methode) {
171        try {
172            return Outil.stringToDocument(executerReturnString(methode));
173        } catch (JDOMException e) {
174            // TODO Auto-generated catch block
175            LOG.error(Outil.getStackTrace(e));
176        } catch (IOException e) {
177            // TODO Auto-generated catch block
178            LOG.error(Outil.getStackTrace(e));
179        }
180        return null;
181
182    }
183
184    /**
185     * @return the login
186     */
187    public String getLogin() {
188        return login;
189    }
190
191    /**
192     * @param login the login to set
193     */
194    public void setLogin(String login) {
195        this.login = login;
196    }
197
198    /**
199     * @return the motDePasse
200     */
201    public String getMotDePasse() {
202        return motDePasse;
203    }
204
205    /**
206     * @param motDePasse the motDePasse to set
207     */
208    public void setMotDePasse(String motDePasse) {
209        this.motDePasse = motDePasse;
210    }
211
212    /**
213     * @return the url
214     */
215    public String getUrl() {
216        return url;
217    }
218
219    /**
220     * @param url the url to set
221     */
222    public void setUrl(String url) {
223        this.url = url;
224    }
225
226}
Note: See TracBrowser for help on using the repository browser.