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 String shortURL;
00085 if (index == -1) {
00086 shortURL = url;
00087 } else if (index == 0) {
00088 shortURL = url.substring(7);
00089 } else {
00090 throw new Exception("invalid URL : " + url);
00091 }
00092 index = shortURL.indexOf("/");
00093 String realURL, resp;
00094 int port;
00095 if (index == -1) {
00096 resp = "/";
00097 } else {
00098 resp = shortURL.substring(index);
00099 shortURL = shortURL.substring(0, index);
00100 }
00101 index = shortURL.indexOf(":");
00102 if (index == -1) {
00103 realURL = shortURL;
00104 port = 80;
00105 } else {
00106 realURL = shortURL.substring(0, index);
00107 port = Integer.parseInt(shortURL.substring(index + 1));
00108 }
00109 if (client == null || !client.getHostConfiguration().getHost().equals(url)) {
00110 client = ApacheHTTPConnectionFactory.createConnection(realURL, port, userAgent);
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 NameValuePair form_data[] = new NameValuePair[2];
00139 form_data[0] = new NameValuePair("openMISData", openMSPMessage);
00140 form_data[1] = new NameValuePair("openMISGroup", parameters[0].value);
00141
00142 PostMethod rsp = new PostMethod(resp);
00143 rsp.addParameters(form_data);
00144
00145
00146
00147 try {
00148 int status = client.executeMethod(rsp);
00149
00150 if (status >= 300) {
00151
00152 if ((status == 403) || (rsp.getStatusCode() == 401)) {
00153 throw new UserNotFoundException("Error during HTTP send request conduit. Server answer :" + status);
00154 } else {
00155 throw new SynchroException("Error during HTTP send request conduit. Server answer :" + status);
00156 }
00157 } else {
00158
00159
00160 InputStream in = rsp.getResponseBodyAsStream();
00161
00162 try {
00163
00164 org.openmobileis.common.util.file.ZipUtilities.unZipData(in, intallPath);
00165
00166 } finally {
00167 in.close();
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 String openMLFileName = intallPath + File.separator + "cyberML.xml";
00186 StringBuffer buffer = new StringBuffer();
00187 java.io.FileInputStream instrem = new java.io.FileInputStream(openMLFileName);
00188 try {
00189 byte[] bytes = new byte[512];
00190 int len;
00191 while ((len = instrem.read(bytes)) != -1) {
00192 buffer.append(new String(bytes, 0, len));
00193 }
00194 } finally {
00195 instrem.close();
00196 }
00197 return buffer.toString();
00198 }
00199
00200
00201 } finally {
00202 rsp.releaseConnection();
00203 rsp.releaseConnection();
00204 }
00205
00206 } catch (UserNotFoundException ioe) {
00207 throw ioe;
00208 } catch (Throwable ioe) {
00209 throw new SynchroException(ioe);
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218 public void openRAS() throws SynchroException {
00219 if(client != null)
00220 client.getParams().setCookiePolicy(null);
00221
00222 connected = true;
00223 }
00224
00225
00226
00227
00228
00229 public void closeRAS() throws SynchroException {
00230 connected = false;
00231 if (client != null) {
00232
00233
00234 ApacheHTTPConnectionFactory.cleanConnexion();
00235 client = null;
00236 }
00237 }
00238
00239 public String getRASConnectionNames() {
00240 return "connection1;connection2";
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 public String getIntallPath() {
00266 return intallPath;
00267 }
00268
00269 }