Ignore:
Timestamp:
Mar 29, 2011, 8:21:25 PM (13 years ago)
Author:
mlg
Message:

Complete rewriting of the api
The api now uses interfaces everywhere (for the dao, services and session manager). This allows the developer to use his own implementations if he wants.
Deletion of the singletons. There are now getters and setters. It allows to make dependency injection.
Use of sl4j in the api, instead of using log4j.

File:
1 edited

Legend:

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

    r9902 r9919  
    22
    33import java.io.IOException;
    4 import java.io.InputStream;
    54import java.io.UnsupportedEncodingException;
    6 import java.util.ArrayList;
    7 import java.util.List;
    85
    9 import org.apache.commons.lang.StringUtils;
    10 import org.apache.http.HttpHost;
    11 import org.apache.http.HttpResponse;
    12 import org.apache.http.NameValuePair;
    13 import org.apache.http.auth.AuthScope;
    14 import org.apache.http.auth.UsernamePasswordCredentials;
    15 import org.apache.http.client.entity.UrlEncodedFormEntity;
    16 import org.apache.http.client.methods.HttpPost;
    17 import org.apache.http.conn.params.ConnRoutePNames;
    18 import org.apache.http.impl.client.DefaultHttpClient;
    19 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    20 import org.apache.http.message.BasicNameValuePair;
    216import org.w3c.dom.Document;
    227
    23 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
    248import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    25 import fr.mael.jiwigo.transverse.util.Tools;
    269
    27 /*
    28  *  jiwigo-ws-api Piwigo webservice access Api
    29  *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
    30  *                All Rights Reserved
    31  *
    32  *  This library is free software. It comes without any warranty, to
    33  *  the extent permitted by applicable law. You can redistribute it
    34  *  and/or modify it under the terms of the Do What The Fuck You Want
    35  *  To Public License, Version 2, as published by Sam Hocevar. See
    36  *  http://sam.zoy.org/wtfpl/COPYING for more details.
    37  */
    38 /**
    39 
    40  * @author mael
    41  * Gestionnaire de connexion
    42  */
    43 public class SessionManager {
    44 
    45     /**
    46      * Logger
    47      */
    48     public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
    49             .getLog(SessionManager.class);
    50     /**
    51      * the entered login
    52      */
    53     private String login;
    54     /**
    55      * the entered password
    56      */
    57     private String password;
    58     /**
    59      * the url of the site
    60      */
    61     private String url;
    62     /**
    63      * the http client
    64      */
    65     private DefaultHttpClient client;
    66 
    67     /**
    68      * defines if the user uses a proxy
    69      */
    70     private boolean usesProxy;
    71 
    72     /**
    73      * url of the proxy
    74      */
    75     private String urlProxy;
    76 
    77     /**
    78      * port of the proxy
    79      */
    80     private int portProxy;
    81 
    82     /**
    83      * login for the proxy
    84      */
    85     private String loginProxy;
    86 
    87     /**
    88      * pass for the proxy
    89      */
    90     private String passProxy;
    91 
    92     /**
    93      * true : an error was found for the proxy
    94      */
    95     private boolean proxyError;
    96 
    97     /**
    98      * Constructor
    99      * @param login the login
    100      * @param password the password
    101      * @param url the url of the site
    102      */
    103     public SessionManager(String login, String password, String url) {
    104         this.login = login;
    105         this.password = password;
    106         this.url = url + "/ws.php";
    107         //      MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    108         //      client = new HttpClient(connectionManager);
    109         ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();
    110         client = new DefaultHttpClient(connectionManager);
    111         //Using of a Linux user agent. cause... it's better 8)
    112         client.getParams().setParameter("http.useragent",
    113                 "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1");
    114 
    115     }
    116 
     10public interface SessionManager {
    11711    /**
    11812     * Connection method
     
    12216     *
    12317     */
    124     public int processLogin() {
    125         Document doc;
    126         //configures the proxy
    127         if (usesProxy) {
    128             //      HostConfiguration config = client.getHostConfiguration();
    129             //      config.setProxy(urlProxy, portProxy);
    130             if (!StringUtils.isEmpty(loginProxy) && !StringUtils.isEmpty(passProxy)) {
    131                 //              Credentials credentials = new UsernamePasswordCredentials(loginProxy, passProxy);
    132                 //              AuthScope authScope = new AuthScope(urlProxy, portProxy);
    133                 //              client.getState().setProxyCredentials(authScope, credentials);
    134                 HttpHost proxy = new HttpHost(urlProxy, portProxy);
    135                 client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    136                 client.getCredentialsProvider().setCredentials(new AuthScope(urlProxy, portProxy),
    137                         new UsernamePasswordCredentials(loginProxy, passProxy));
    138 
    139             }
    140         }
    141         //      try {
    142         try {
    143             doc = executeReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", password);
    144             return (Tools.checkOk(doc) ? 0 : 1);
    145         } catch (IOException e) {
    146             LOG.debug(Tools.getStackTrace(e));
    147             return 1;
    148         } catch (ProxyAuthenticationException e) {
    149             LOG.debug(Tools.getStackTrace(e));
    150             return 2;
    151         } catch (Exception e) {
    152             LOG.debug(Tools.getStackTrace(e));
    153             return 1;
    154         }
    155 
    156     }
     18    public int processLogin();
    15719
    15820    /**
     
    16527     */
    16628    public String executeReturnString(String methode, String... parametres) throws UnsupportedEncodingException,
    167             ProxyAuthenticationException {
    168         if (parametres.length % 2 != 0 && !(parametres == null)) {
    169             try {
    170                 throw new Exception("Le nombre de parametres doit etre pair");
    171             } catch (Exception e) {
    172                 LOG.error(Tools.getStackTrace(e));
    173                 return null;
    174             }
    175         }
    176         HttpPost method = new HttpPost(url);
    177         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    178         nameValuePairs.add(new BasicNameValuePair("method", methode));
    179         for (int i = 0; i < parametres.length; i += 2) {
    180             nameValuePairs.add(new BasicNameValuePair(parametres[i], parametres[i + 1]));
    181         }
    182         method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    183         //begin bug:0001833
    184         method.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    185         //end
    186         try {
    187             HttpResponse response = client.execute(method);
    188             if (response.getStatusLine().getStatusCode() == 407) {
    189                 throw new ProxyAuthenticationException("Error while trying to auth on the proxy");
    190             }
    191             InputStream inputStream = response.getEntity().getContent();
    192             StringBuffer buffer = new StringBuffer();
    193             byte[] data = new byte[512];
    194             int len = 0;
    195             try {
    196                 while (-1 != (len = inputStream.read(data))) {
    197                     buffer.append(new String(data, 0, len));
    198                 }
    199             } catch (IOException e) {
    200                 e.printStackTrace();
    201             }
    202             try {
    203                 inputStream.close();
    204             } catch (IOException e) {
    205                 e.printStackTrace();
    206             }
    207             return buffer.toString();
    208         } catch (IOException e) {
    209 
    210         }
    211         return null;
    212 
    213     }
     29            ProxyAuthenticationException;
    21430
    21531    /**
     
    22238     */
    22339    public Document executeReturnDocument(String methode, String... parametres) throws IOException,
    224             ProxyAuthenticationException {
    225         try {
    226             String returnedString = executeReturnString(methode, parametres);
    227             return Tools.stringToDocument(returnedString);
    228         } catch (Exception e) {
    229             // TODO Auto-generated catch block
    230             LOG.error(Tools.getStackTrace(e));
    231         }
    232         return null;
    233 
    234     }
     40            ProxyAuthenticationException;
    23541
    23642    /**
     
    24046     * @throws ProxyAuthenticationException
    24147     */
    242     public Document executeReturnDocument(String methode) throws ProxyAuthenticationException {
    243         try {
    244             return Tools.stringToDocument(executeReturnString(methode));
    245         } catch (Exception e) {
    246             // TODO Auto-generated catch block
    247             LOG.error(Tools.getStackTrace(e));
    248         }
    249         return null;
    250 
    251     }
     48    public Document executeReturnDocument(String methode) throws ProxyAuthenticationException;
    25249
    25350    /**
     51     * Getter of the login
    25452     * @return the login
    25553     */
    256     public String getLogin() {
    257         return login;
    258     }
     54    public String getLogin();
    25955
    26056    /**
    261      * @param login the login to set
     57     * Setter of the login
     58     * @param login
    26259     */
    263     public void setLogin(String login) {
    264         this.login = login;
    265     }
     60    public void setLogin(String login);
    26661
    26762    /**
    268      * @return the url
     63     * Setter of the user agent
     64     * @param userAgent
    26965     */
    270     public String getUrl() {
    271         return url;
    272     }
     66    public void setUserAgent(String userAgent);
    27367
    27468    /**
    275      * @param url the url to set
     69     * Setter of the password to access piwigo
     70     * @param password
    27671     */
    277     public void setUrl(String url) {
    278         this.url = url;
    279     }
     72    public void setPassword(String password);
    28073
    28174    /**
    282      * @param usesProxy the usesProxy to set
     75     * Setter of the proxy login
     76     * @param loginProxy
    28377     */
    284     public void setUsesProxy(boolean usesProxy) {
    285         this.usesProxy = usesProxy;
    286     }
     78    public void setLoginProxy(String loginProxy);
    28779
    28880    /**
    289      * @param urlProxy the urlProxy to set
     81     * Setter of the proxy port
     82     * @param port
    29083     */
    291     public void setUrlProxy(String urlProxy) {
    292         this.urlProxy = urlProxy;
    293     }
     84    public void setPortProxy(int port);
    29485
    29586    /**
    296      * @param portProxy the portProxy to set
     87     * Setter of the proxy url
     88     * @param urlProxy
    29789     */
    298     public void setPortProxy(int portProxy) {
    299         this.portProxy = portProxy;
    300     }
     90    public void setUrlProxy(String urlProxy);
    30191
    30292    /**
    303      * @param loginProxy the loginProxy to set
     93     * Setter of the proxy pass
     94     * @param proxyPass
    30495     */
    305     public void setLoginProxy(String loginProxy) {
    306         this.loginProxy = loginProxy;
    307     }
     96    public void setPassProxy(String proxyPass);
    30897
    30998    /**
    310      * @param passProxy the passProxy to set
     99     * Setter of the proxy url
     100     * @param url
    311101     */
    312     public void setPassProxy(String passProxy) {
    313         this.passProxy = passProxy;
    314     }
     102    public void setUrl(String url);
    315103
    316     public String getPassword() {
    317         return password;
    318     }
     104    /**
     105     * Setter of the boolean that tells to use a proxy or not
     106     * @param usesProxy
     107     */
     108    public void setUsesProxy(boolean usesProxy);
    319109
    320     public void setPassword(String password) {
    321         this.password = password;
    322     }
    323 
    324     public boolean isProxyError() {
    325         return proxyError;
    326     }
    327 
    328     public void setProxyError(boolean proxyError) {
    329         this.proxyError = proxyError;
    330     }
    331 
     110    /**
     111     * Function that returns true if there is a proxy error
     112     * @return
     113     */
     114    public boolean isProxyError();
    332115}
Note: See TracChangeset for help on using the changeset viewer.