ApacheHTTPClientSynchroConduit.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  */
00024 package org.openmobileis.synchro.openmsp.client.conduit;
00025 
00026 import java.io.File;
00027 import java.io.InputStream;
00028 
00029 import org.apache.commons.httpclient.NameValuePair;
00030 import org.apache.commons.httpclient.methods.PostMethod;
00031 import org.openmobileis.common.user.UserNotFoundException;
00032 import org.openmobileis.common.util.exception.SynchroException;
00033 import org.openmobileis.common.util.file.FileUtilities;
00034 
00035 
00047 public class ApacheHTTPClientSynchroConduit implements SynchroConduit {
00048 
00049         private org.apache.commons.httpclient.HttpClient client = null;
00050 
00051         private boolean connected = false;
00052 
00053         private String userAgent;
00054 
00055         private String intallPath;
00056 
00057         public ApacheHTTPClientSynchroConduit() {
00058         }
00059 
00064         public void setSynchroPath(String path) {
00065                 intallPath = path;
00066         }
00067 
00072         public void setSynchroUserAgent(String userAgent)       {
00073                 this.userAgent = userAgent; // "OpenMSPPlug-WI-V1.0-WINCE[DR-FR-WINCE/04-02-00-00]"
00074         }
00075 
00076         public boolean isConnected() {
00077                 return connected;
00078         }
00079 
00080         public String sendRequest(ConduitParameter[] parameters, String openMSPMessage, String url) throws SynchroException, UserNotFoundException {
00081                 intallPath = FileUtilities.convertFileNameToSystem(intallPath);
00082                 try {
00083                         int index = url.indexOf("http://");
00084                         String shortURL;
00085                         if (index == -1) {
00086                                 shortURL = url;
00087                         } else if (index == 0) {
00088                                 shortURL = url.substring(7);
00089                         } else {
00090                                 throw new Exception("invalid URL : " + url);
00091                         }
00092                         index = shortURL.indexOf("/");
00093                         String realURL, resp;
00094                         int port;
00095                         if (index == -1) {
00096                                 resp = "/";
00097                         } else {
00098                                 resp = shortURL.substring(index);
00099                                 shortURL = shortURL.substring(0, index);
00100                         }
00101                         index = shortURL.indexOf(":");
00102                         if (index == -1) {
00103                                 realURL = shortURL;
00104                                 port = 80;
00105                         } else {
00106                                 realURL = shortURL.substring(0, index);
00107                                 port = Integer.parseInt(shortURL.substring(index + 1));
00108                         }
00109                         if (client == null || !client.getHostConfiguration().getHost().equals(url)) {
00110                          client = ApacheHTTPConnectionFactory.createConnection(realURL, port, userAgent);
00111                         }
00112 
00113 
00114                         /***************** DEBUGGING ************************/
00115                         //save send request
00116                         /*       byte[] openmsmessage = this.uncompressAndDecodeRequest(openMSPMessage.getBytes());
00117                          // save send request
00118                          String sendFileName = intallPath + "cn" + File.separator + "synchro" + File.separator + "sendCyberML.xml";
00119                          FileOutputStream sendFileStream = new FileOutputStream(sendFileName);
00120                          try {
00121                          sendFileStream.write(openmsmessage);
00122                          } finally {
00123                          sendFileStream.flush();
00124                          sendFileStream.close();
00125                          }
00126                          System.out.println(new String(openmsmessage));
00127                          */
00128                         /***************** END DEBUGGING ********************/
00129 
00130                         //send request
00131 //                      openMSPMessage.getBytes(); //put here to correct PC2003 PDA bug
00132 
00133                         //byte[] encodedChain = URLCodec.encodeUrl(null,openMSPMessage.getBytes());     //necessary for the Http transfert
00134                         //String openMISData = new String(encodedChain);
00135                         //GetMethod rsp = new GetMethod(resp);
00136                         //rsp.setQueryString(form_data);
00137 
00138                         NameValuePair form_data[] = new NameValuePair[2];
00139                         form_data[0] = new NameValuePair("openMISData", openMSPMessage);
00140                         form_data[1] = new NameValuePair("openMISGroup", parameters[0].value);
00141 
00142                         PostMethod rsp = new PostMethod(resp);
00143                         rsp.addParameters(form_data);
00144 
00145 
00146 
00147                         try {
00148                                 int status = client.executeMethod(rsp);
00149 
00150                                 if (status >= 300) {
00151                                         //  System.out.println("TestSynchroService Error in HTTP Request :" + rsp.getStatusCode());
00152                                         if ((status == 403) || (rsp.getStatusCode() == 401)) {
00153                                                 throw new UserNotFoundException("Error during HTTP send request conduit. Server answer :" + status);
00154                                         } else {
00155                                                 throw new SynchroException("Error during HTTP send request conduit. Server answer :" + status);
00156                                         }
00157                                 } else {
00158 
00159                                         //LogManager.traceDebug(0, "DEBUG TIME in ApacheHTTPCientSynchroConduit avant rsp.getResponseBodyAsStream() : "+new Date(System.currentTimeMillis()));
00160                                         InputStream in = rsp.getResponseBodyAsStream();
00161                                         //LogManager.traceDebug(0, "DEBUG TIME in ApacheHTTPCientSynchroConduit apres rsp.getResponseBodyAsStream() : "+new Date(System.currentTimeMillis()));
00162                                         try {
00163                                                 //LogManager.traceDebug(0, "DEBUG TIME in ApacheHTTPCientSynchroConduit avant unZipData() : "+new Date(System.currentTimeMillis()));
00164                                                 org.openmobileis.common.util.file.ZipUtilities.unZipData(in, intallPath);
00165                                                 //LogManager.traceDebug(0, "DEBUG TIME in ApacheHTTPCientSynchroConduit apres unZipData() : "+new Date(System.currentTimeMillis()));
00166                                         } finally {
00167                                                 in.close();
00168                                         }
00169 
00170 
00171 
00172                                         /************* DEBUGING ********************/
00173                                         /*       try {
00174                                          java.io.FileOutputStream writer = new java.io.FileOutputStream(intallPath + "cn" + File.separator + "synchro.zip");
00175                                          writer.write(data);
00176                                          writer.flush();
00177                                          writer.close();
00178                                          System.out.println("write synchro.zip");
00179                                          } catch (Exception e) {
00180                                          e.printStackTrace();
00181                                          }
00182                                          System.out.println("HTTP request answer saved in " + intallPath + "cn");
00183                                          */
00184                                         /************** END DEBUGGING ***********************/
00185                                         String openMLFileName = intallPath + File.separator + "cyberML.xml";
00186                                         StringBuffer buffer = new StringBuffer();
00187                                         java.io.FileInputStream instrem = new java.io.FileInputStream(openMLFileName);
00188                                         try {
00189                                                 byte[] bytes = new byte[512];
00190                                                 int len;
00191                                                 while ((len = instrem.read(bytes)) != -1) {
00192                                                         buffer.append(new String(bytes, 0, len));
00193                                                 }
00194                                         } finally {
00195                                                 instrem.close();
00196                                         }
00197                                         return buffer.toString();
00198                                 }
00199 
00200                     // consume the response entity
00201                         } finally {
00202                                 rsp.releaseConnection();
00203                                 rsp.releaseConnection();
00204                         }
00205 
00206                 } catch (UserNotFoundException ioe) {
00207                         throw ioe;
00208                 } catch (Throwable ioe) {
00209                         throw new SynchroException(ioe);
00210                 }
00211         }
00212 
00213         /*
00214          * Open RAS connection
00215          * @param : isSSLRequired : boolean indicates wether or not is a SSL connection
00216          * @return: boolean (true : connection OK, false : connection failed
00217          */
00218         public void openRAS() throws SynchroException { //do nothing
00219                 if(client != null)
00220                         client.getParams().setCookiePolicy(null);
00221                 //HTTPClient.CookieModule.setCookiePolicyHandler(null);
00222                 connected = true;
00223         }
00224 
00225         /*
00226          * Close RAS connection
00227          * @return boolean (status OK, not OK)
00228          */
00229         public void closeRAS() throws SynchroException { // do nothing
00230                 connected = false;
00231                 if (client != null)     {
00232                         //client.getParams().setCookiePolicy(null);
00233                         //HTTPClient.CookieModule.discardAllCookies();//remove cookies at the end of the synchro to create a new session on the server side for the next synchro.
00234                         ApacheHTTPConnectionFactory.cleanConnexion();
00235                         client = null;
00236                 }
00237         }
00238 
00239         public String getRASConnectionNames() {
00240                 return "connection1;connection2";
00241         }
00242 
00243         //use for debug purpose.
00244 /*      private byte[] uncompressAndDecodeRequest(byte[] request) throws IOException {
00245                 // decode compressed data
00246                 byte[] decodedData = GeneralCoder.decodeBase64(request);
00247 
00248                 // uncompresse data
00249                 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
00250                 ByteArrayInputStream inStream = new ByteArrayInputStream(decodedData);
00251                 GZIPInputStream zipStream = new GZIPInputStream(inStream);
00252                 try {
00253                         byte[] buff = new byte[1024];
00254                         int length;
00255                         while ((length = zipStream.read(buff)) != -1) {
00256                                 byteStream.write(buff, 0, length);
00257                         }
00258                 } finally {
00259                         zipStream.close();
00260                         byteStream.close();
00261                 }
00262                 return byteStream.toByteArray();
00263         } */
00264 
00265         public String getIntallPath() {
00266                 return intallPath;
00267         }
00268 
00269 }

Generated on Mon Jan 11 21:19:13 2010 for OpenMobileIS by  doxygen 1.5.4