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.UniqueIdGenerator;
00033 import org.openmobileis.common.util.exception.SynchroException;
00034 import org.openmobileis.common.util.file.FileUtilities;
00035 import org.openmobileis.common.util.file.HTTPFileConduit;
00036 import org.openmobileis.common.util.log.LogManager;
00037
00049 public class HTTPFileClientSynchroConduit implements SynchroConduit {
00050
00051 private org.apache.commons.httpclient.HttpClient client = null;
00052
00053 private boolean connected = false;
00054
00055 private String userAgent;
00056
00057 private String intallPath;
00058
00059 private String httpFileDirectory;
00060
00061 public HTTPFileClientSynchroConduit() {
00062 this.httpFileDirectory = System.getProperty("user.dir") + File.separator + "temp";
00063 File dir = new File(this.httpFileDirectory);
00064 if(!dir.exists()) dir.mkdirs();
00065 }
00066
00072 public void setSynchroPath(String path) {
00073 intallPath = path;
00074 }
00075
00081 public void setSynchroUserAgent(String userAgent) {
00082 this.userAgent = userAgent;
00083 }
00084
00085 public boolean isConnected() {
00086 return connected;
00087 }
00088
00089 public String sendRequest(ConduitParameter[] parameters, String openMSPMessage, String url) throws SynchroException, UserNotFoundException {
00090 intallPath = FileUtilities.convertFileNameToSystem(intallPath);
00091 try {
00092 int index = url.indexOf("http://");
00093 if (index == -1) {
00094 throw new Exception("invalid URL, must begin by http : " + url);
00095 }
00096
00097 String shortURL;
00098 if (index == -1) {
00099 shortURL = url;
00100 } else if (index == 0) {
00101 shortURL = url.substring(7);
00102 } else {
00103 throw new Exception("invalid URL : " + url);
00104 }
00105 index = shortURL.indexOf("/");
00106 String host, resp;
00107 int port;
00108 if (index == -1) {
00109 resp = "/";
00110 } else {
00111 resp = shortURL.substring(index);
00112 shortURL = shortURL.substring(0, index);
00113 }
00114 index = shortURL.indexOf(":");
00115 if (index == -1) {
00116 host = shortURL;
00117 port = 80;
00118 } else {
00119 host = shortURL.substring(0, index);
00120 port = Integer.parseInt(shortURL.substring(index + 1));
00121 }
00122
00123
00124 NameValuePair form_data[] = new NameValuePair[2];
00125 form_data[0] = new NameValuePair("openMISData", openMSPMessage);
00126 form_data[1] = new NameValuePair("openMISGroup", parameters[0].value);
00127
00128 PostMethod rsp = new PostMethod(resp);
00129
00130 rsp.addParameters(form_data);
00131
00132 try {
00133
00134
00135 int status = 500;
00136 try {
00137 if (client == null || !client.getHostConfiguration().getHost().equals(url)) {
00138 client = ApacheHTTPConnectionFactory.createConnection(host, port, userAgent);
00139 }
00140
00141 status = client.executeMethod(rsp);
00142
00143 } catch (Throwable ex) {
00144
00145 ex.printStackTrace();
00146
00147
00148 ApacheHTTPConnectionFactory.cleanConnexion();
00149
00150 int conduitPort = 7867;
00151 client = ApacheHTTPConnectionFactory.createConnection("localhost", conduitPort, userAgent);
00152
00153
00154
00155 HTTPFileConduit httpfileconduit = new HTTPFileConduit(conduitPort, this.httpFileDirectory + File.separator + "httpfile" + Long.toString(UniqueIdGenerator.getManager().getNewID()));
00156 httpfileconduit.startListening(host + ":" + Integer.toString(port));
00157 status = client.executeMethod(rsp);
00158
00159 }
00160
00161 if (status >= 300) {
00162
00163
00164 if ((status == 403) || (rsp.getStatusCode() == 401)) {
00165 throw new UserNotFoundException("Error during HTTP send request conduit. Server answer :" + status);
00166 } else {
00167 throw new SynchroException("Error during HTTP send request conduit. Server answer :" + status);
00168 }
00169 } else {
00170
00171
00172
00173
00174
00175
00176 InputStream in = rsp.getResponseBodyAsStream();
00177
00178
00179
00180 try {
00181
00182
00183
00184 org.openmobileis.common.util.file.ZipUtilities.unZipData(in, intallPath);
00185
00186
00187
00188 } finally {
00189 in.close();
00190 }
00191
00192 String openMLFileName = intallPath + File.separator + "cyberML.xml";
00193 StringBuffer buffer = new StringBuffer();
00194 java.io.FileInputStream instrem = new java.io.FileInputStream(openMLFileName);
00195 try {
00196 byte[] bytes = new byte[512];
00197 int len;
00198 while ((len = instrem.read(bytes)) != -1) {
00199 buffer.append(new String(bytes, 0, len));
00200 }
00201 } finally {
00202 instrem.close();
00203 }
00204 return buffer.toString();
00205 }
00206
00207
00208 } finally {
00209 rsp.releaseConnection();
00210 rsp.releaseConnection();
00211 }
00212
00213 } catch (UserNotFoundException ioe) {
00214 throw ioe;
00215 } catch (Throwable ioe) {
00216 LogManager.traceDebug(0, "END ERROR Synchro :"+ioe.toString());
00217 throw new SynchroException(ioe);
00218 }
00219 }
00220
00221
00222
00223
00224
00225
00226 public void openRAS() throws SynchroException {
00227 if (client != null)
00228 client.getParams().setCookiePolicy(null);
00229 connected = true;
00230 }
00231
00232
00233
00234
00235 public void closeRAS() throws SynchroException {
00236 connected = false;
00237 if (client != null) {
00238 ApacheHTTPConnectionFactory.cleanConnexion();
00239 client = null;
00240 }
00241 }
00242
00243 public String getRASConnectionNames() {
00244 return "connection1;connection2";
00245 }
00246
00247 public String getIntallPath() {
00248 return intallPath;
00249 }
00250
00251 }