Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

HttpOpenMSPSynchroConduit.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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       boolean usessl = false;
00067       int index = url.indexOf("http://");
00068       if (index == -1) { //test for ssl
00069         index = url.indexOf("https://");
00070         usessl = true;
00071         //TO BE DONE SSL IMPLEMENTATION
00072       }
00073 
00074       String shortURL;
00075       if (index == -1) {
00076         shortURL = url;
00077       } else if (index == 0) {
00078         shortURL = url.substring(7);
00079       } else {
00080         throw new Exception("invalid URL : " + url);
00081       }
00082       index = shortURL.indexOf("/");
00083       String realURL, resp;
00084       int port;
00085       if (index == -1) {
00086         resp = "/";
00087       } else {
00088         resp = shortURL.substring(index);
00089         shortURL = shortURL.substring(0, index);
00090       }
00091       index = shortURL.indexOf(":");
00092       if (index == -1) {
00093         realURL = shortURL;
00094         port = 80;
00095       } else {
00096         realURL = shortURL.substring(0, index);
00097         port = Integer.parseInt(shortURL.substring(index + 1));
00098       }
00099 
00100       if (con == null || !con.getHost().equals(url)) {
00101         con = new HTTPConnection(realURL, port);
00102                                 CookieModule.setCookiePolicyHandler(null);
00103         NVPair headerData[] = new NVPair[1];
00104         headerData[0] = new NVPair("User-Agent", userAgent);
00105         con.setDefaultHeaders(headerData);
00106       }
00107  
00108       /***************** DEBUGGING ************************/
00109         //save send request
00110  /*       byte[] openmsmessage = this.uncompressAndDecodeRequest(openMSPMessage.getBytes());
00111         // save send request
00112         String sendFileName = intallPath + "cn" + File.separator + "synchro" + File.separator + "sendCyberML.xml";
00113         FileOutputStream sendFileStream = new FileOutputStream(sendFileName);
00114         try {
00115           sendFileStream.write(openmsmessage);
00116         } finally {
00117           sendFileStream.flush();
00118           sendFileStream.close();
00119         }
00120         System.out.println(new String(openmsmessage)); 
00121         */
00122       /***************** END DEBUGGING ********************/
00123 
00124       //send request
00125                         openMSPMessage.getBytes(); //put here to correct PC2003 PDA bug
00126                         // no parameters is send for the fisrt request if not set.
00127       NVPair form_data[] = new NVPair[2];
00128       form_data[0] = new NVPair("openMISData", openMSPMessage);
00129       form_data[1] = new NVPair("openMISGroup", parameters[0].value);
00130       HTTPResponse rsp = con.Post(resp, form_data);
00131 
00132       if (rsp.getStatusCode() >= 300) {
00133         //  System.out.println("TestSynchroService Error in HTTP Request :" + rsp.getStatusCode());
00134         if ((rsp.getStatusCode() == 403) || (rsp.getStatusCode() == 401)) {
00135           throw  new UserNotFoundException("Error during HTTP send request conduit. Server answer :"+rsp.getStatusCode());
00136         } else  {
00137           throw  new SynchroException("Error during HTTP send request conduit. Server answer :"+rsp.getStatusCode());
00138         }
00139       } else {
00140         byte[] data = rsp.getData();
00141  //         System.out.println("sendRequest HTTP request OK");
00142  //         System.out.println("SyncNum :" + rsp.getHeader("SyncNum"));
00143  //         System.out.println("data length :" + data.length);
00144         ByteArrayInputStream inst  = new ByteArrayInputStream(data);
00145         try {
00146           org.openmobileis.common.util.file.ZipUtilities.unZipData(inst, intallPath);
00147         } finally {
00148           inst.close();
00149         }
00150 
00151         /************* DEBUGING ********************/
00152   /*       try {
00153             java.io.FileOutputStream writer = new java.io.FileOutputStream(intallPath + "cn" + File.separator + "synchro.zip");
00154             writer.write(data);
00155             writer.flush();
00156             writer.close();
00157             System.out.println("write synchro.zip");
00158           } catch (Exception e) {
00159             e.printStackTrace();
00160           }
00161           System.out.println("HTTP request answer saved in " + intallPath + "cn");
00162           */
00163         /************** END DEBUGGING ***********************/
00164         String openMLFileName = intallPath+File.separator+"cyberML.xml";
00165         StringBuffer buffer = new StringBuffer();
00166         java.io.FileInputStream in = new java.io.FileInputStream(openMLFileName);
00167         try {
00168           byte[] bytes = new byte[512];
00169             int len;
00170             while ( ( len = in.read( bytes ) ) != -1 ) {
00171                 buffer.append(new String(bytes, 0, len));
00172             }
00173         } finally {
00174           in.close();
00175         }
00176         return buffer.toString();
00177       }
00178     } catch (UserNotFoundException ioe) {
00179       throw ioe;
00180     } catch (Throwable ioe) {
00181       throw  new SynchroException(ioe);
00182     }
00183   }
00184 
00185   /*
00186   * Open RAS connection
00187   * @param : isSSLRequired : boolean indicates wether or not is a SSL connection
00188   * @return: boolean (true : connection OK, false : connection failed
00189   */
00190   public void openRAS() throws SynchroException { //do nothing
00191     HTTPClient.CookieModule.setCookiePolicyHandler(null);
00192     connected = true;
00193    }
00194 
00195   /*
00196   * Close RAS connection
00197   * @return boolean (status OK, not OK)
00198   */
00199   public void closeRAS() throws SynchroException { // do nothing
00200     connected = false;
00201     if (con != null) con.stop();
00202    }
00203 
00204   public String getRASConnectionNames() {
00205     return "connection1;connection2";
00206   }
00207 
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 Wed Dec 14 21:05:33 2005 for OpenMobileIS by  doxygen 1.4.4