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.PropertiesManager;
00034 import org.openmobileis.common.util.exception.SynchroException;
00035 import org.openmobileis.common.util.file.FileUtilities;
00036
00046 public class HttpOpenMSPSynchroConduit implements SynchroConduit {
00047
00048 private HTTPConnection con = null;
00049
00050 private boolean connected = false;
00051
00052 private String userAgent;
00053
00054 private String intallPath;
00055
00056 public HttpOpenMSPSynchroConduit(String synchroDirectoryPath, String userAgent) {
00057 this.userAgent = userAgent;
00058 intallPath = synchroDirectoryPath;
00059 }
00060
00061 public boolean isConnected() {
00062 return connected;
00063 }
00064
00065 public String sendRequest(ConduitParameter[] parameters, String openMSPMessage, String url) throws SynchroException, UserNotFoundException {
00066 intallPath = FileUtilities.convertFileNameToSystem(intallPath);
00067 try {
00068 int index = url.indexOf("http://");
00069 if (index == -1) {
00070 throw new Exception("invalid URL, must begin by http : " + url);
00071 }
00072
00073 String shortURL;
00074 if (index == -1) {
00075 shortURL = url;
00076 } else if (index == 0) {
00077 shortURL = url.substring(7);
00078 } else {
00079 throw new Exception("invalid URL : " + url);
00080 }
00081 index = shortURL.indexOf("/");
00082 String realURL, resp;
00083 int port;
00084 if (index == -1) {
00085 resp = "/";
00086 } else {
00087 resp = shortURL.substring(index);
00088 shortURL = shortURL.substring(0, index);
00089 }
00090 index = shortURL.indexOf(":");
00091 if (index == -1) {
00092 realURL = shortURL;
00093 port = 80;
00094 } else {
00095 realURL = shortURL.substring(0, index);
00096 port = Integer.parseInt(shortURL.substring(index + 1));
00097 }
00098
00099 if (con == null || !con.getHost().equals(url)) {
00100 con = HTTPConnectionFactory.createConnection(realURL, port, this.userAgent);
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 openMSPMessage.getBytes();
00121
00122 NVPair form_data[] = new NVPair[2];
00123 form_data[0] = new NVPair("openMISData", openMSPMessage);
00124 form_data[1] = new NVPair("openMISGroup", parameters[0].value);
00125 HTTPResponse rsp = con.Post(resp, form_data);
00126
00127 if (rsp.getStatusCode() >= 300) {
00128
00129 if ((rsp.getStatusCode() == 403) || (rsp.getStatusCode() == 401)) {
00130 throw new UserNotFoundException("Error during HTTP send request conduit. Server answer :" + rsp.getStatusCode());
00131 } else {
00132 throw new SynchroException("Error during HTTP send request conduit. Server answer :" + rsp.getStatusCode());
00133 }
00134 } else {
00135 byte[] data = rsp.getData();
00136
00137
00138
00139 ByteArrayInputStream inst = new ByteArrayInputStream(data);
00140 try {
00141 org.openmobileis.common.util.file.ZipUtilities.unZipData(inst, intallPath);
00142 } finally {
00143 inst.close();
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 String openMLFileName = intallPath + File.separator + "cyberML.xml";
00160 StringBuffer buffer = new StringBuffer();
00161 java.io.FileInputStream in = new java.io.FileInputStream(openMLFileName);
00162 try {
00163 byte[] bytes = new byte[512];
00164 int len;
00165 while ((len = in.read(bytes)) != -1) {
00166 buffer.append(new String(bytes, 0, len));
00167 }
00168 } finally {
00169 in.close();
00170 }
00171 return buffer.toString();
00172 }
00173 } catch (UserNotFoundException ioe) {
00174 throw ioe;
00175 } catch (Throwable ioe) {
00176 throw new SynchroException(ioe);
00177 }
00178 }
00179
00180
00181
00182
00183
00184
00185 public void openRAS() throws SynchroException {
00186 HTTPClient.CookieModule.setCookiePolicyHandler(null);
00187 connected = true;
00188 }
00189
00190
00191
00192
00193
00194 public void closeRAS() throws SynchroException {
00195 connected = false;
00196 if (con != null) {
00197 con.stop();
00198 HTTPClient.CookieModule.discardAllCookies();
00199 con = null;
00200 }
00201 }
00202
00203 public String getRASConnectionNames() {
00204 return "connection1;connection2";
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 public String getIntallPath() {
00230 return intallPath;
00231 }
00232 }