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

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1