Changeset 7219 for extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/session/SessionManager.java
- Timestamp:
- Oct 16, 2010, 12:04:23 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/session/SessionManager.java
r7071 r7219 3 3 import java.io.IOException; 4 4 import java.io.InputStream; 5 5 import java.io.UnsupportedEncodingException; 6 6 import javax.swing.JOptionPane; 7 8 7 import org.apache.commons.httpclient.HttpClient; 9 8 import org.apache.commons.httpclient.HttpException; … … 11 10 import org.jdom.Document; 12 11 import org.jdom.JDOMException; 13 14 12 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; 15 13 import fr.mael.jiwigo.transverse.util.Messages; 16 14 import fr.mael.jiwigo.transverse.util.Outil; 15 17 16 18 17 /** … … 46 45 */ 47 46 public 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 * the entered login 55 */ 56 private String login; 57 /** 58 * the entered password 59 */ 60 private String motDePasse; 61 /** 62 * the url of the site 63 */ 64 private String url; 65 /** 66 * the http client 67 */ 68 private HttpClient client; 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 * Connection method 88 * @return true if successful 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 * Executes a method on the webservice and returns the result as a string 104 * @param methode the method to execute 105 * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value 106 * @return the result 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)); 47 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 } 114 151 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 * Executes a method on the webservice and returns the result as a Dom document 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 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 * Executes a method on the webservice and returns the result as a Dom document 171 * @param methode the method to execute 172 * @return the result 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 } 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 } 229 241 230 242 }
Note: See TracChangeset
for help on using the changeset viewer.