00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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;
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
00132
00133
00134
00135 public void openRAS() throws SynchroException {
00136 }
00137
00138
00139
00140
00141
00142 public void closeRAS() throws SynchroException {
00143 }
00144
00145 public String getRASConnectionNames() {
00146 return "connection1;connection2";
00147 }
00148
00149 public String getIntallPath() {
00150 return intallPath;
00151 }
00152 }