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(String synchroDirectoryPath, String userAgent) {
00054 this.userAgent = userAgent;
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
00118
00119
00120
00121 public void openRAS() throws SynchroException {
00122 }
00123
00124
00125
00126
00127
00128 public void closeRAS() throws SynchroException {
00129 }
00130
00131 public String getRASConnectionNames() {
00132 return "connection1;connection2";
00133 }
00134
00135 public String getIntallPath() {
00136 return intallPath;
00137 }
00138 }