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

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