00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package org.openmobileis.synchro.openmsp.client.conduit;
00025
00026 import java.io.File;
00027 import java.io.InputStream;
00028
00029 import org.apache.commons.httpclient.NameValuePair;
00030 import org.apache.commons.httpclient.methods.PostMethod;
00031 import org.openmobileis.common.user.UserNotFoundException;
00032 import org.openmobileis.common.util.exception.SynchroException;
00033 import org.openmobileis.common.util.file.FileUtilities;
00034
00035
00047 public class ApacheHTTPClientSynchroConduit implements SynchroConduit {
00048
00049 private org.apache.commons.httpclient.HttpClient client = null;
00050
00051 private boolean connected = false;
00052
00053 private String userAgent;
00054
00055 private String intallPath;
00056
00057 public ApacheHTTPClientSynchroConduit() {
00058 }
00059
00064 public void setSynchroPath(String path) {
00065 intallPath = path;
00066 }
00067
00072 public void setSynchroUserAgent(String userAgent) {
00073 this.userAgent = userAgent;
00074 }
00075
00076 public boolean isConnected() {
00077 return connected;
00078 }
00079
00080 public String sendRequest(ConduitParameter[] parameters, String openMSPMessage, String url) throws SynchroException, UserNotFoundException {
00081 intallPath = FileUtilities.convertFileNameToSystem(intallPath);
00082 try {
00083 int index = url.indexOf("http://");
00084 if (index == -1) {
00085 throw new Exception("invalid URL, must begin by http : " + url);
00086 }
00087
00088 String shortURL;
00089 if (index == -1) {
00090 shortURL = url;
00091 } else if (index == 0) {
00092 shortURL = url.substring(7);
00093 } else {
00094 throw new Exception("invalid URL : " + url);
00095 }
00096 index = shortURL.indexOf("/");
00097 String realURL, resp;
00098 int port;
00099 if (index == -1) {
00100 resp = "/";
00101 } else {
00102 resp = shortURL.substring(index);
00103 shortURL = shortURL.substring(0, index);
00104 }
00105 index = shortURL.indexOf(":");
00106 if (index == -1) {
00107 realURL = shortURL;
00108 port = 80;
00109 } else {
00110 realURL = shortURL.substring(0, index);
00111 port = Integer.parseInt(shortURL.substring(index + 1));
00112 }
00113 if (client == null || !client.getHostConfiguration().getHost().equals(url)) {
00114 client = ApacheHTTPConnectionFactory.createConnection(realURL, port, userAgent);
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 openMSPMessage.getBytes();
00136
00137
00138
00139
00140
00141
00142 NameValuePair form_data[] = new NameValuePair[2];
00143 form_data[0] = new NameValuePair("openMISData", openMSPMessage);
00144 form_data[1] = new NameValuePair("openMISGroup", parameters[0].value);
00145
00146 PostMethod rsp = new PostMethod(resp);
00147 rsp.addParameters(form_data);
00148
00149
00150
00151 try {
00152 int status = client.executeMethod(rsp);
00153
00154 if (status >= 300) {
00155
00156 if ((status == 403) || (rsp.getStatusCode() == 401)) {
00157 throw new UserNotFoundException("Error during HTTP send request conduit. Server answer :" + status);
00158 } else {
00159 throw new SynchroException("Error during HTTP send request conduit. Server answer :" + status);
00160 }
00161 } else {
00162
00163
00164 InputStream in = rsp.getResponseBodyAsStream();
00165
00166 try {
00167
00168 org.openmobileis.common.util.file.ZipUtilities.unZipData(in, intallPath);
00169
00170 } finally {
00171 in.close();
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 String openMLFileName = intallPath + File.separator + "cyberML.xml";
00190 StringBuffer buffer = new StringBuffer();
00191 java.io.FileInputStream instrem = new java.io.FileInputStream(openMLFileName);
00192 try {
00193 byte[] bytes = new byte[512];
00194 int len;
00195 while ((len = instrem.read(bytes)) != -1) {
00196 buffer.append(new String(bytes, 0, len));
00197 }
00198 } finally {
00199 instrem.close();
00200 }
00201 return buffer.toString();
00202 }
00203
00204
00205 } finally {
00206 rsp.releaseConnection();
00207 rsp.releaseConnection();
00208 }
00209
00210 } catch (UserNotFoundException ioe) {
00211 throw ioe;
00212 } catch (Throwable ioe) {
00213 throw new SynchroException(ioe);
00214 }
00215 }
00216
00217
00218
00219
00220
00221
00222 public void openRAS() throws SynchroException {
00223 if(client != null)
00224 client.getParams().setCookiePolicy(null);
00225
00226 connected = true;
00227 }
00228
00229
00230
00231
00232
00233 public void closeRAS() throws SynchroException {
00234 connected = false;
00235 if (client != null) {
00236
00237
00238 ApacheHTTPConnectionFactory.cleanConnexion();
00239 client = null;
00240 }
00241 }
00242
00243 public String getRASConnectionNames() {
00244 return "connection1;connection2";
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 public String getIntallPath() {
00270 return intallPath;
00271 }
00272
00273 }