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