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