HTTPFileClientSynchroConduit.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.UniqueIdGenerator;
00033 import org.openmobileis.common.util.exception.SynchroException;
00034 import org.openmobileis.common.util.file.FileUtilities;
00035 import org.openmobileis.common.util.file.HTTPFileConduit;
00036 import org.openmobileis.common.util.log.LogManager;
00037 
00049 public class HTTPFileClientSynchroConduit implements SynchroConduit {
00050 
00051         private org.apache.commons.httpclient.HttpClient client = null;
00052 
00053         private boolean connected = false;
00054 
00055         private String userAgent;
00056 
00057         private String intallPath;
00058         
00059         private String httpFileDirectory;
00060 
00061         public HTTPFileClientSynchroConduit() {
00062                 this.httpFileDirectory = System.getProperty("user.dir") + File.separator + "temp";
00063                 File dir = new File(this.httpFileDirectory);
00064                 if(!dir.exists()) dir.mkdirs();
00065         }
00066 
00072         public void setSynchroPath(String path) {
00073                 intallPath = path;
00074         }
00075 
00081         public void setSynchroUserAgent(String userAgent) {
00082                 this.userAgent = userAgent; // "OpenMSPPlug-1.0-LINUX[1.0]-FR-LINUX"
00083         }
00084 
00085         public boolean isConnected() {
00086                 return connected;
00087         }
00088 
00089         public String sendRequest(ConduitParameter[] parameters, String openMSPMessage, String url) throws SynchroException, UserNotFoundException {
00090                 intallPath = FileUtilities.convertFileNameToSystem(intallPath);
00091                 try {
00092                         int index = url.indexOf("http://");
00093                         if (index == -1) { // test for ssl
00094                                 throw new Exception("invalid URL, must begin by http : " + url);
00095                         }
00096 
00097                         String shortURL;
00098                         if (index == -1) {
00099                                 shortURL = url;
00100                         } else if (index == 0) {
00101                                 shortURL = url.substring(7);
00102                         } else {
00103                                 throw new Exception("invalid URL : " + url);
00104                         }
00105                         index = shortURL.indexOf("/");
00106                         String host, resp;
00107                         int port;
00108                         if (index == -1) {
00109                                 resp = "/";
00110                         } else {
00111                                 resp = shortURL.substring(index);
00112                                 shortURL = shortURL.substring(0, index);
00113                         }
00114                         index = shortURL.indexOf(":");
00115                         if (index == -1) {
00116                                 host = shortURL;
00117                                 port = 80;
00118                         } else {
00119                                 host = shortURL.substring(0, index);
00120                                 port = Integer.parseInt(shortURL.substring(index + 1));
00121                         }
00122 
00123                         // prepare request parameters
00124                         NameValuePair form_data[] = new NameValuePair[2];
00125                         form_data[0] = new NameValuePair("openMISData", openMSPMessage);
00126                         form_data[1] = new NameValuePair("openMISGroup", parameters[0].value);
00127 
00128                         PostMethod rsp = new PostMethod(resp);
00129                         // PostMethod rsp = new PostMethod("/services/index");
00130                         rsp.addParameters(form_data);
00131 
00132                         try {
00133                                 // test if the network is connected. If ok use network, otherwise use
00134                                 // file transfert.
00135                                 int status = 500;
00136                                 try {
00137                                         if (client == null || !client.getHostConfiguration().getHost().equals(url)) {
00138                                                 client = ApacheHTTPConnectionFactory.createConnection(host, port, userAgent);
00139                                         }                       
00140   // throw new Exception(); //FOR TEST
00141                                         status = client.executeMethod(rsp);
00142                                 //      LogManager.traceDebug(0, "END Send HTTPFileConduit Synchro (WIFI)");
00143                                 } catch (Throwable ex) {
00144                                         
00145                                         ex.printStackTrace();
00146                                         
00147                                         
00148                                         ApacheHTTPConnectionFactory.cleanConnexion();
00149                                         //No network use HTTPFilestansfert.
00150                                         int conduitPort = 7867;
00151                                         client = ApacheHTTPConnectionFactory.createConnection("localhost", conduitPort, userAgent);
00152 
00153                                         // create HTTPFile manager
00154                                 //      LogManager.traceDebug(0, "START Send HTTPFileConduit Synchro (GPRS)");
00155                                         HTTPFileConduit httpfileconduit = new HTTPFileConduit(conduitPort, this.httpFileDirectory + File.separator + "httpfile" + Long.toString(UniqueIdGenerator.getManager().getNewID()));
00156                                         httpfileconduit.startListening(host + ":" + Integer.toString(port));
00157                                         status = client.executeMethod(rsp);
00158                                 //      LogManager.traceDebug(0, "END Send HTTPFileConduit Synchro (GPRS)");
00159                           }
00160 
00161                                 if (status >= 300) {
00162                                         // System.out.println("TestSynchroService Error in HTTP Request :" +
00163                                         // rsp.getStatusCode());
00164                                         if ((status == 403) || (rsp.getStatusCode() == 401)) {
00165                                                 throw new UserNotFoundException("Error during HTTP send request conduit. Server answer :" + status);
00166                                         } else {
00167                                                 throw new SynchroException("Error during HTTP send request conduit. Server answer :" + status);
00168                                         }
00169                                 } else {
00170 
00171                                         // LogManager.traceDebug(0, "DEBUG TIME in
00172                                         // ApacheHTTPCientSynchroConduit avant rsp.getResponseBodyAsStream() :
00173                                         // "+new Date(System.currentTimeMillis()));
00174                 //                      long length = rsp.getResponseContentLength();
00175                 //                      byte[] bb = rsp.getResponseBody();
00176                                         InputStream in = rsp.getResponseBodyAsStream();
00177                                         // LogManager.traceDebug(0, "DEBUG TIME in
00178                                         // ApacheHTTPCientSynchroConduit apres rsp.getResponseBodyAsStream() :
00179                                         // "+new Date(System.currentTimeMillis()));
00180                                         try {
00181                                                 // LogManager.traceDebug(0, "DEBUG TIME in
00182                                                 // ApacheHTTPCientSynchroConduit avant unZipData() : "+new
00183                                                 // Date(System.currentTimeMillis()));
00184                                                 org.openmobileis.common.util.file.ZipUtilities.unZipData(in, intallPath);
00185                                                 // LogManager.traceDebug(0, "DEBUG TIME in
00186                                                 // ApacheHTTPCientSynchroConduit apres unZipData() : "+new
00187                                                 // Date(System.currentTimeMillis()));
00188                                         } finally {
00189                                                 in.close();
00190                                         }
00191 
00192                                         String openMLFileName = intallPath + File.separator + "cyberML.xml";
00193                                         StringBuffer buffer = new StringBuffer();
00194                                         java.io.FileInputStream instrem = new java.io.FileInputStream(openMLFileName);
00195                                         try {
00196                                                 byte[] bytes = new byte[512];
00197                                                 int len;
00198                                                 while ((len = instrem.read(bytes)) != -1) {
00199                                                         buffer.append(new String(bytes, 0, len));
00200                                                 }
00201                                         } finally {
00202                                                 instrem.close();
00203                                         }
00204                                         return buffer.toString();
00205                                 }
00206 
00207                                 // consume the response entity
00208                         } finally {
00209                                 rsp.releaseConnection();
00210                                 rsp.releaseConnection();
00211                         }
00212 
00213                 } catch (UserNotFoundException ioe) {
00214                         throw ioe;
00215                 } catch (Throwable ioe) {
00216                         LogManager.traceDebug(0, "END ERROR Synchro :"+ioe.toString());
00217                         throw new SynchroException(ioe);
00218                 }
00219         }
00220 
00221         /*
00222          * Open RAS connection @param : isSSLRequired : boolean indicates wether or
00223          * not is a SSL connection @return: boolean (true : connection OK, false :
00224          * connection failed
00225          */
00226         public void openRAS() throws SynchroException { // do nothing
00227                 if (client != null)
00228                         client.getParams().setCookiePolicy(null);
00229                 connected = true;
00230         }
00231 
00232         /*
00233          * Close RAS connection @return boolean (status OK, not OK)
00234          */
00235         public void closeRAS() throws SynchroException { // do nothing
00236                 connected = false;
00237                 if (client != null) {
00238                         ApacheHTTPConnectionFactory.cleanConnexion();
00239                         client = null;
00240                 }
00241         }
00242 
00243         public String getRASConnectionNames() {
00244                 return "connection1;connection2";
00245         }
00246 
00247         public String getIntallPath() {
00248                 return intallPath;
00249         }
00250 
00251 }

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