LocalOpenMSPSynchroConduit.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 import org.openmobileis.synchro.openmsp.server.OpenMSPService;
00036 import org.openmobileis.test.mock.embedded.MockHttpServletRequest;
00037 import org.openmobileis.test.mock.embedded.MockHttpServletResponse;
00038 
00048 public class LocalOpenMSPSynchroConduit implements SynchroConduit {
00049 
00050   private String userAgent;
00051   private String intallPath;
00052   
00053   public LocalOpenMSPSynchroConduit()  {
00054   }
00055         
00060         public void setSynchroPath(String path) {
00061                 intallPath = path;
00062         }
00063         
00068         public void setSynchroUserAgent(String userAgent)       {
00069                 this.userAgent = userAgent; // "OpenMSPPlug-WI-V1.0-WINCE[DR-FR-WINCE/04-02-00-00]"
00070         }
00071 
00072   public boolean isConnected() {
00073     return true;
00074   }
00075 
00076   public String sendRequest(ConduitParameter[]parameters, String openMSPMessage, String url) throws SynchroException, UserNotFoundException{
00077     intallPath = FileUtilities.convertFileNameToSystem(intallPath);
00078     try {
00079       MockHttpServletRequest req = new MockHttpServletRequest();
00080       MockHttpServletResponse res = new MockHttpServletResponse();
00081       
00082       req.addParameter("openMISData", openMSPMessage);
00083       req.addParameter("openMISGroup", parameters[0].value);
00084       req.headerNames.add("user-agent");
00085       req.headerValues.add(this.userAgent);
00086          
00087       NVPair form_data[] = new NVPair[2];
00088       form_data[0] = new NVPair("openMISData", openMSPMessage);
00089       form_data[1] = new NVPair("openMISGroup", parameters[0].value);
00090       
00091       OpenMSPService syncservice = new OpenMSPService();
00092       syncservice.run(req, res);
00093 
00094       if (res.status >= 300) {
00095        if ((res.status == 403) || (res.status == 401)) {
00096           throw  new UserNotFoundException("Error during HTTP send request conduit. Server answer :"+res.status);
00097         } else  {
00098           throw  new SynchroException("Error during HTTP send request conduit. Server answer :"+res.status);
00099         }
00100       } else {
00101         byte[] data = res.outStream.toByteArray();
00102          ByteArrayInputStream inst  = new ByteArrayInputStream(data);
00103         try {
00104           org.openmobileis.common.util.file.ZipUtilities.unZipData(inst, intallPath);
00105         } finally {
00106           inst.close();
00107         }
00108 
00109         String openMLFileName = intallPath+File.separator+"cyberML.xml";
00110         StringBuffer buffer = new StringBuffer();
00111         java.io.FileInputStream in = new java.io.FileInputStream(openMLFileName);
00112         try {
00113           byte[] bytes = new byte[512];
00114             int len;
00115             while ( ( len = in.read( bytes ) ) != -1 ) {
00116                 buffer.append(new String(bytes, 0, len));
00117             }
00118         } finally {
00119           in.close();
00120         }
00121         return buffer.toString();
00122       }
00123     } catch (UserNotFoundException ioe) {
00124       throw ioe;
00125     } catch (Throwable ioe) {
00126       throw  new SynchroException(ioe);
00127     }
00128   }
00129 
00130   /*
00131   * Open RAS connection
00132   * @param : isSSLRequired : boolean indicates wether or not is a SSL connection
00133   * @return: boolean (true : connection OK, false : connection failed
00134   */
00135   public void openRAS() throws SynchroException { //do nothing
00136    }
00137 
00138   /*
00139   * Close RAS connection
00140   * @return boolean (status OK, not OK)
00141   */
00142   public void closeRAS() throws SynchroException { // do nothing
00143    }
00144 
00145   public String getRASConnectionNames() {
00146     return "connection1;connection2";
00147   }
00148 
00149   public String getIntallPath() {
00150     return intallPath;
00151   }
00152 }

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