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