HttpOpenMSPSynchroConduit.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  */
00025 
00026 package org.openmobileis.synchro.openmsp.client.conduit;
00027 
00028 import java.io.*;
00029 
00030 import HTTPClient.*;
00031 
00032 import org.openmobileis.common.user.UserNotFoundException;
00033 import org.openmobileis.common.util.exception.SynchroException;
00034 import org.openmobileis.common.util.file.FileUtilities;
00035 
00045 public class HttpOpenMSPSynchroConduit implements SynchroConduit {
00046 
00047         private HTTPConnection con = null;
00048 
00049         private boolean connected = false;
00050 
00051         private String userAgent;
00052 
00053         private String intallPath;
00054 
00055         public HttpOpenMSPSynchroConduit() {
00056         }
00057 
00062         public void setSynchroPath(String path) {
00063                 intallPath = path;
00064         }
00065 
00070         public void setSynchroUserAgent(String userAgent)       {
00071                 this.userAgent = userAgent; // "OpenMSPPlug-WI-V1.0-WINCE[DR-FR-WINCE/04-02-00-00]"
00072         }
00073 
00074         public boolean isConnected() {
00075                 return connected;
00076         }
00077 
00078         public String sendRequest(ConduitParameter[] parameters, String openMSPMessage, String url) throws SynchroException, UserNotFoundException {
00079                 intallPath = FileUtilities.convertFileNameToSystem(intallPath);
00080                 try {
00081                         int index = url.indexOf("http://");
00082                         if (index == -1) { //test for ssl
00083                                 throw new Exception("invalid URL, must begin by http : " + url);
00084                         }
00085 
00086                         String shortURL;
00087                         if (index == -1) {
00088                                 shortURL = url;
00089                         } else if (index == 0) {
00090                                 shortURL = url.substring(7);
00091                         } else {
00092                                 throw new Exception("invalid URL : " + url);
00093                         }
00094                         index = shortURL.indexOf("/");
00095                         String realURL, resp;
00096                         int port;
00097                         if (index == -1) {
00098                                 resp = "/";
00099                         } else {
00100                                 resp = shortURL.substring(index);
00101                                 shortURL = shortURL.substring(0, index);
00102                         }
00103                         index = shortURL.indexOf(":");
00104                         if (index == -1) {
00105                                 realURL = shortURL;
00106                                 port = 80;
00107                         } else {
00108                                 realURL = shortURL.substring(0, index);
00109                                 port = Integer.parseInt(shortURL.substring(index + 1));
00110                         }
00111 
00112                         if (con == null || !con.getHost().equals(url)) {
00113                                 con = HTTPConnectionFactory.createConnection(realURL, port, this.userAgent);
00114                         }
00115 
00116                         /***************** DEBUGGING ************************/
00117                         //save send request
00118                         /*       byte[] openmsmessage = this.uncompressAndDecodeRequest(openMSPMessage.getBytes());
00119                          // save send request
00120                          String sendFileName = intallPath + "cn" + File.separator + "synchro" + File.separator + "sendCyberML.xml";
00121                          FileOutputStream sendFileStream = new FileOutputStream(sendFileName);
00122                          try {
00123                          sendFileStream.write(openmsmessage);
00124                          } finally {
00125                          sendFileStream.flush();
00126                          sendFileStream.close();
00127                          }
00128                          System.out.println(new String(openmsmessage));
00129                          */
00130                         /***************** END DEBUGGING ********************/
00131 
00132                         //send request
00133                         openMSPMessage.getBytes(); //put here to correct PC2003 PDA bug
00134                         // no parameters is send for the fisrt request if not set.
00135                         NVPair form_data[] = new NVPair[2];
00136                         form_data[0] = new NVPair("openMISData", openMSPMessage);
00137                         form_data[1] = new NVPair("openMISGroup", parameters[0].value);
00138                         HTTPResponse rsp = con.Post(resp, form_data);
00139 
00140                         if (rsp.getStatusCode() >= 300) {
00141                                 //  System.out.println("TestSynchroService Error in HTTP Request :" + rsp.getStatusCode());
00142                                 if ((rsp.getStatusCode() == 403) || (rsp.getStatusCode() == 401)) {
00143                                         throw new UserNotFoundException("Error during HTTP send request conduit. Server answer :" + rsp.getStatusCode());
00144                                 } else {
00145                                         throw new SynchroException("Error during HTTP send request conduit. Server answer :" + rsp.getStatusCode());
00146                                 }
00147                         } else {
00148                                 byte[] data = rsp.getData();
00149                                 //         System.out.println("sendRequest HTTP request OK");
00150                                 //         System.out.println("SyncNum :" + rsp.getHeader("SyncNum"));
00151                                 //         System.out.println("data length :" + data.length);
00152                                 ByteArrayInputStream inst = new ByteArrayInputStream(data);
00153                                 try {
00154                                         org.openmobileis.common.util.file.ZipUtilities.unZipData(inst, intallPath);
00155                                 } finally {
00156                                         inst.close();
00157                                 }
00158 
00159                                 /************* DEBUGING ********************/
00160                                 /*       try {
00161                                  java.io.FileOutputStream writer = new java.io.FileOutputStream(intallPath + "cn" + File.separator + "synchro.zip");
00162                                  writer.write(data);
00163                                  writer.flush();
00164                                  writer.close();
00165                                  System.out.println("write synchro.zip");
00166                                  } catch (Exception e) {
00167                                  e.printStackTrace();
00168                                  }
00169                                  System.out.println("HTTP request answer saved in " + intallPath + "cn");
00170                                  */
00171                                 /************** END DEBUGGING ***********************/
00172                                 String openMLFileName = intallPath + File.separator + "cyberML.xml";
00173                                 StringBuffer buffer = new StringBuffer();
00174                                 java.io.FileInputStream in = new java.io.FileInputStream(openMLFileName);
00175                                 try {
00176                                         byte[] bytes = new byte[512];
00177                                         int len;
00178                                         while ((len = in.read(bytes)) != -1) {
00179                                                 buffer.append(new String(bytes, 0, len));
00180                                         }
00181                                 } finally {
00182                                         in.close();
00183                                 }
00184                                 return buffer.toString();
00185                         }
00186                 } catch (UserNotFoundException ioe) {
00187                         throw ioe;
00188                 } catch (Throwable ioe) {
00189                         throw new SynchroException(ioe);
00190                 }
00191         }
00192 
00193         /*
00194          * Open RAS connection
00195          * @param : isSSLRequired : boolean indicates wether or not is a SSL connection
00196          * @return: boolean (true : connection OK, false : connection failed
00197          */
00198         public void openRAS() throws SynchroException { //do nothing
00199                 HTTPClient.CookieModule.setCookiePolicyHandler(null);
00200                 connected = true;
00201         }
00202 
00203         /*
00204          * Close RAS connection
00205          * @return boolean (status OK, not OK)
00206          */
00207         public void closeRAS() throws SynchroException { // do nothing
00208                 connected = false;
00209                 if (con != null)        {
00210                         con.stop();
00211                         HTTPClient.CookieModule.discardAllCookies();//remove cookies at the end of the synchro to create a new session on the server side for the next synchro.
00212                         con = null;
00213                 }
00214         }
00215 
00216         public String getRASConnectionNames() {
00217                 return "connection1;connection2";
00218         }
00219 
00220         //use for debug purpose.
00221 /*      private byte[] uncompressAndDecodeRequest(byte[] request) throws IOException {
00222                 // decode compressed data
00223                 byte[] decodedData = GeneralCoder.decodeBase64(request);
00224 
00225                 // uncompresse data
00226                 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
00227                 ByteArrayInputStream inStream = new ByteArrayInputStream(decodedData);
00228                 GZIPInputStream zipStream = new GZIPInputStream(inStream);
00229                 try {
00230                         byte[] buff = new byte[1024];
00231                         int length;
00232                         while ((length = zipStream.read(buff)) != -1) {
00233                                 byteStream.write(buff, 0, length);
00234                         }
00235                 } finally {
00236                         zipStream.close();
00237                         byteStream.close();
00238                 }
00239                 return byteStream.toByteArray();
00240         } */
00241 
00242         public String getIntallPath() {
00243                 return intallPath;
00244         }
00245 }

Generated on Mon Jan 14 17:29:46 2008 for OpenMobileIS by  doxygen 1.5.4