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

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

Bug correction :
bug:0001837 : there is now a default translation file (in english), so the application won't crash when a translation is not found
bug:0001833 : the accents are manage when creating a new category
bug:0001832 : on a right click on the categories list, the selection is now visible
bug:0001830 : there is no bug on refreshing the categories tree

Features :
feature:001828 : exif and iptc tags are kept after resizing an image
feature:001827 : pwg.images.addChunk is now fully used : images are split into chunks before being sent

Other features :

  • The user can manage his preferences :

-The web images size
-The chunks size

  • The user can save the login informations (url, login, password) /!\ in a plain text file !
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            return (method.getResponseBodyAsString());
128        } catch (HttpException e) {
129            // TODO Auto-generated catch block
130            LOG.error(Outil.getStackTrace(e));
131        } catch (IOException e) {
132            // TODO Auto-generated catch block
133            LOG.error(Outil.getStackTrace(e));
134        } catch (IllegalArgumentException e) {
135            LOG.error(Outil.getStackTrace(e));
136            JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages
137                    .getMessage("error"), JOptionPane.ERROR_MESSAGE);
138        } finally {
139            method.releaseConnection();
140        }
141        return null;
142
143    }
144
145    /**
146     * Execute une methode sur le webservice et renvoie le resultat sous form de document jdom
147     * @param methode la methode a executer
148     * @param parametres les parametres a  ajouter a la methode. doit etre un nombre pair : la cle suivie de sa valeur
149     * @return le resultat
150     * @throws IOException
151     */
152    public Document executerReturnDocument(String methode, String... parametres) throws IOException {
153        try {
154            return Outil.stringToDocument(executerReturnString(methode, parametres));
155        } catch (JDOMException e) {
156            // TODO Auto-generated catch block
157            LOG.error(Outil.getStackTrace(e));
158        }
159        return null;
160
161    }
162
163    /**
164     * Execute une methode sur le webservice et renvoie le resultat sous form de document jdom
165     * @param methode la methode a executer
166     * @return le resultat
167     */
168    public Document executerReturnDocument(String methode) {
169        try {
170            return Outil.stringToDocument(executerReturnString(methode));
171        } catch (JDOMException e) {
172            // TODO Auto-generated catch block
173            LOG.error(Outil.getStackTrace(e));
174        } catch (IOException e) {
175            // TODO Auto-generated catch block
176            LOG.error(Outil.getStackTrace(e));
177        }
178        return null;
179
180    }
181
182    /**
183     * @return the login
184     */
185    public String getLogin() {
186        return login;
187    }
188
189    /**
190     * @param login the login to set
191     */
192    public void setLogin(String login) {
193        this.login = login;
194    }
195
196    /**
197     * @return the motDePasse
198     */
199    public String getMotDePasse() {
200        return motDePasse;
201    }
202
203    /**
204     * @param motDePasse the motDePasse to set
205     */
206    public void setMotDePasse(String motDePasse) {
207        this.motDePasse = motDePasse;
208    }
209
210    /**
211     * @return the url
212     */
213    public String getUrl() {
214        return url;
215    }
216
217    /**
218     * @param url the url to set
219     */
220    public void setUrl(String url) {
221        this.url = url;
222    }
223
224}
Note: See TracBrowser for help on using the repository browser.