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

Generated on Tue May 22 23:01:10 2007 for OpenMobileIS by  doxygen 1.5.1-p1