Ignore:
Timestamp:
Jan 21, 2011, 7:20:05 PM (13 years ago)
Author:
mlg
Message:

Adds proxy support for the connection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/session/SessionManager.java

    r7219 r8830  
    44import java.io.InputStream;
    55import java.io.UnsupportedEncodingException;
     6
    67import javax.swing.JOptionPane;
     8
     9import org.apache.commons.httpclient.Credentials;
     10import org.apache.commons.httpclient.HostConfiguration;
    711import org.apache.commons.httpclient.HttpClient;
    812import org.apache.commons.httpclient.HttpException;
     13import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
     14import org.apache.commons.httpclient.UsernamePasswordCredentials;
     15import org.apache.commons.httpclient.auth.AuthScope;
    916import org.apache.commons.httpclient.methods.PostMethod;
     17import org.apache.commons.lang.StringUtils;
    1018import org.jdom.Document;
    1119import org.jdom.JDOMException;
     20
    1221import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
    1322import fr.mael.jiwigo.transverse.util.Messages;
    1423import fr.mael.jiwigo.transverse.util.Outil;
    15 
    1624
    1725/**
     
    4654public class SessionManager {
    4755
    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;
    68 
    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         /**
    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                 }
     56    /**
     57     * Logger
     58     */
     59    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
     60            .getLog(SessionManager.class);
     61    /**
     62     * the entered login
     63     */
     64    private String login;
     65    /**
     66     * the entered password
     67     */
     68    private String motDePasse;
     69    /**
     70     * the url of the site
     71     */
     72    private String url;
     73    /**
     74     * the http client
     75     */
     76    private HttpClient client;
     77
     78    /**
     79     * defines if the user uses a proxy
     80     */
     81    private boolean usesProxy;
     82
     83    /**
     84     * url of the proxy
     85     */
     86    private String urlProxy;
     87
     88    /**
     89     * port of the proxy
     90     */
     91    private int portProxy;
     92
     93    /**
     94     * login for the proxy
     95     */
     96    private String loginProxy;
     97
     98    /**
     99     * pass for the proxy
     100     */
     101    private String passProxy;
     102
     103    /**
     104     * true : an error was found for the proxy
     105     */
     106    private boolean erreurProxy;
     107
     108    /**
     109     * Constructor
     110     * @param login the login
     111     * @param motDePasse the password
     112     * @param url the url of the site
     113     */
     114    public SessionManager(String login, String motDePasse, String url) {
     115        this.login = login;
     116        this.motDePasse = motDePasse;
     117        this.url = url + "/ws.php";
     118        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
     119        client = new HttpClient(connectionManager);
     120        //Using of a Linux user agent. cause... it's better 8)
     121        client.getParams().setParameter("http.useragent",
     122                "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1");
     123
     124    }
     125
     126    /**
     127     * Connection method
     128     * @return true if successful
     129     */
     130    public boolean processLogin() {
     131        Document doc;
     132        //configures the proxy
     133        if (usesProxy) {
     134            HostConfiguration config = client.getHostConfiguration();
     135            config.setProxy(urlProxy, portProxy);
     136            if (!StringUtils.isEmpty(loginProxy) && !StringUtils.isEmpty(passProxy)) {
     137                Credentials credentials = new UsernamePasswordCredentials(loginProxy, passProxy);
     138                AuthScope authScope = new AuthScope(urlProxy, portProxy);
     139                client.getState().setProxyCredentials(authScope, credentials);
     140            }
     141        }
     142        try {
     143            doc = executerReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", motDePasse);
     144            return Outil.checkOk(doc);
     145        } catch (Exception e) {
     146            LOG.error(Outil.getStackTrace(e));
     147        }
     148        return false;
     149    }
     150
     151    /**
     152     * Executes a method on the webservice and returns the result as a string
     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 UnsupportedEncodingException
     157     */
     158    public String executerReturnString(String methode, String... parametres) throws UnsupportedEncodingException {
     159        if (parametres.length % 2 != 0 && !(parametres == null)) {
     160            try {
     161                throw new Exception("Le nombre de parametres doit etre pair");
     162            } catch (Exception e) {
     163                LOG.error(Outil.getStackTrace(e));
    151164                return null;
    152 
    153         }
    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 
    172         }
    173 
    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 
    192         }
    193 
    194 
    195         /**
    196          * @return the login
    197          */
    198         public String getLogin() {
    199                 return login;
    200         }
    201 
    202 
    203         /**
    204          * @param login the login to set
    205          */
    206         public void setLogin(String login) {
    207                 this.login = login;
    208         }
    209 
    210 
    211         /**
    212          * @return the motDePasse
    213          */
    214         public String getMotDePasse() {
    215                 return motDePasse;
    216         }
    217 
    218 
    219         /**
    220          * @param motDePasse the motDePasse to set
    221          */
    222         public void setMotDePasse(String motDePasse) {
    223                 this.motDePasse = motDePasse;
    224         }
    225 
    226 
    227         /**
    228          * @return the url
    229          */
    230         public String getUrl() {
    231                 return url;
    232         }
    233 
    234 
    235         /**
    236          * @param url the url to set
    237          */
    238         public void setUrl(String url) {
    239                 this.url = url;
    240         }
     165            }
     166        }
     167        PostMethod method = new PostMethod(url);
     168        method.addParameter("method", methode);
     169        for (int i = 0; i < parametres.length; i += 2) {
     170            method.addParameter(parametres[i], parametres[i + 1]);
     171
     172        }
     173        //begin bug:0001833
     174        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
     175        //end
     176
     177        try {
     178            client.executeMethod(method);
     179            InputStream streamResponse = method.getResponseBodyAsStream();
     180            //      System.out.println(Outil.readInputStreamAsString(streamResponse));
     181            //      String toReturn = method.getResponseBodyAsString();
     182            String toReturn = Outil.readInputStreamAsString(streamResponse);
     183            LOG.debug(toReturn);
     184            return toReturn;
     185        } catch (HttpException e) {
     186            // TODO Auto-generated catch block
     187            LOG.error(Outil.getStackTrace(e));
     188        } catch (IOException e) {
     189            // TODO Auto-generated catch block
     190            LOG.error(Outil.getStackTrace(e));
     191        } catch (IllegalArgumentException e) {
     192            LOG.error(Outil.getStackTrace(e));
     193            JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages
     194                    .getMessage("error"), JOptionPane.ERROR_MESSAGE);
     195        } finally {
     196            method.releaseConnection();
     197        }
     198        return null;
     199
     200    }
     201
     202    /**
     203     * Executes a method on the webservice and returns the result as a Dom document
     204     * @param methode the method to execute
     205     * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value
     206     * @return the result
     207     * @throws IOException
     208     */
     209    public Document executerReturnDocument(String methode, String... parametres) throws IOException {
     210        try {
     211            return Outil.stringToDocument(executerReturnString(methode, parametres));
     212        } catch (JDOMException e) {
     213            // TODO Auto-generated catch block
     214            LOG.error(Outil.getStackTrace(e));
     215        }
     216        return null;
     217
     218    }
     219
     220    /**
     221     * Executes a method on the webservice and returns the result as a Dom document
     222     * @param methode the method to execute
     223     * @return the result
     224     */
     225    public Document executerReturnDocument(String methode) {
     226        try {
     227            return Outil.stringToDocument(executerReturnString(methode));
     228        } catch (JDOMException e) {
     229            // TODO Auto-generated catch block
     230            LOG.error(Outil.getStackTrace(e));
     231        } catch (IOException e) {
     232            // TODO Auto-generated catch block
     233            LOG.error(Outil.getStackTrace(e));
     234        }
     235        return null;
     236
     237    }
     238
     239    /**
     240     * @return the login
     241     */
     242    public String getLogin() {
     243        return login;
     244    }
     245
     246    /**
     247     * @param login the login to set
     248     */
     249    public void setLogin(String login) {
     250        this.login = login;
     251    }
     252
     253    /**
     254     * @return the motDePasse
     255     */
     256    public String getMotDePasse() {
     257        return motDePasse;
     258    }
     259
     260    /**
     261     * @param motDePasse the motDePasse to set
     262     */
     263    public void setMotDePasse(String motDePasse) {
     264        this.motDePasse = motDePasse;
     265    }
     266
     267    /**
     268     * @return the url
     269     */
     270    public String getUrl() {
     271        return url;
     272    }
     273
     274    /**
     275     * @param url the url to set
     276     */
     277    public void setUrl(String url) {
     278        this.url = url;
     279    }
     280
     281    /**
     282     * @param usesProxy the usesProxy to set
     283     */
     284    public void setUsesProxy(boolean usesProxy) {
     285        this.usesProxy = usesProxy;
     286    }
     287
     288    /**
     289     * @param urlProxy the urlProxy to set
     290     */
     291    public void setUrlProxy(String urlProxy) {
     292        this.urlProxy = urlProxy;
     293    }
     294
     295    /**
     296     * @param portProxy the portProxy to set
     297     */
     298    public void setPortProxy(int portProxy) {
     299        this.portProxy = portProxy;
     300    }
     301
     302    /**
     303     * @param loginProxy the loginProxy to set
     304     */
     305    public void setLoginProxy(String loginProxy) {
     306        this.loginProxy = loginProxy;
     307    }
     308
     309    /**
     310     * @param passProxy the passProxy to set
     311     */
     312    public void setPassProxy(String passProxy) {
     313        this.passProxy = passProxy;
     314    }
     315
     316    /**
     317     * @return the erreurProxy
     318     */
     319    public boolean isErreurProxy() {
     320        return erreurProxy;
     321    }
    241322
    242323}
Note: See TracChangeset for help on using the changeset viewer.