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

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

Two minor improvements :

  • I now get the responses of piwigo's webservices as stream, and not as string any more.
  • The information panel now displays which file is being processed.
File size: 7.0 KB
Line 
1package fr.mael.jiwigo.transverse.session;
2
3import java.io.IOException;
4import java.io.InputStream;
5
6import javax.swing.JOptionPane;
7
8import org.apache.commons.httpclient.HttpClient;
9import org.apache.commons.httpclient.HttpException;
10import org.apache.commons.httpclient.methods.PostMethod;
11import org.jdom.Document;
12import org.jdom.JDOMException;
13
14import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
15import fr.mael.jiwigo.transverse.util.Messages;
16import 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 */
47public 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     * Le login entré
55     */
56    private String login;
57    /**
58     * Le mot de passe entré
59     */
60    private String motDePasse;
61    /**
62     * l'url du site
63     */
64    private String url;
65    /**
66     * le client http
67     */
68    private HttpClient client;
69
70    /**
71     * Constructeur
72     * @param login le login
73     * @param motDePasse le mot de passe
74     * @param url l'url du 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        //utilisation d'un useragent linux, parce que c'est mieux 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     * Methode de connexion
88     * @return true si la connexion s'est bien deroulee
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     * Execute une methode sur le web service et retourne le resultat sous forme de string
104     * @param methode la methode a executer
105     * @param parametres les parametres a  ajouter a la methode. doit etre un nombre pair : la cle suivie de sa valeur
106     * @return le resultat
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=utf-8");
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     * Execute une methode sur le webservice et renvoie le resultat sous form de document jdom
153     * @param methode la methode a executer
154     * @param parametres les parametres a  ajouter a la methode. doit etre un nombre pair : la cle suivie de sa valeur
155     * @return le resultat
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     * Execute une methode sur le webservice et renvoie le resultat sous form de document jdom
171     * @param methode la methode a executer
172     * @return le resultat
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}
Note: See TracBrowser for help on using the repository browser.