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 package org.openmobileis.synchro.openmsp.client.conduit;
00026
00027 import java.net.Authenticator;
00028 import java.util.Properties;
00029
00030 import org.apache.commons.httpclient.HostConfiguration;
00031 import org.apache.commons.httpclient.HttpClient;
00032 import org.apache.commons.httpclient.UsernamePasswordCredentials;
00033 import org.apache.commons.httpclient.auth.AuthScope;
00034 import org.apache.commons.httpclient.methods.PostMethod;
00035 import org.apache.commons.httpclient.params.HttpClientParams;
00036 import org.openmobileis.common.context.ApplicationContextManager;
00037 import org.openmobileis.common.context.Plateform;
00038 import org.openmobileis.common.intl.IntlResourceManager;
00039 import org.openmobileis.common.util.PropertiesManager;
00040 import org.openmobileis.common.util.exception.ServiceException;
00041 import org.openmobileis.common.util.log.ApacheSimpleLog;
00042
00047 public final class ApacheHTTPConnectionFactory {
00048
00058 public static HttpClient createConnection(String url, int port, String userAgent) {
00059 if (!ApacheSimpleLog.loggingEnable) {
00060 System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
00061 System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "false");
00062 System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "error");
00063 ApacheSimpleLog.enableLogging();
00064 }
00065
00066 HttpClient client = new HttpClient();
00067 HostConfiguration hostConfig = new HostConfiguration();
00068
00069 String socks = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.address");
00070 boolean hasproxy = false;
00071 if (socks != null) {
00072 String sport = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.port");
00073 if (sport != null) {
00074 Properties props = System.getProperties();
00075
00076 props.put("socksProxyHost", socks);
00077 props.put("socksProxyPort", sport) ;
00078
00079 String login = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.login");
00080 String pass = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.pass");
00081 if (login != null && pass != null) {
00082
00083
00084
00085
00086 SocksAuthenticator newauth = new SocksAuthenticator(login, pass);
00087 Authenticator.setDefault (newauth);
00088 }
00089 System.setProperties(props);
00090 hasproxy = true;
00091 }
00092 }
00093
00094 String proxy = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.address");
00095 if (proxy != null) {
00096 String pport = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.port");
00097 if (pport != null) {
00098 int proxyport = Integer.parseInt(pport);
00099 hostConfig.setProxy(proxy, proxyport);
00100 String login = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.login");
00101 String pass = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.pass");
00102 if (login != null && pass != null) {
00103 client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(login, pass));
00104 }
00105 hasproxy = true;
00106 }
00107 }
00108
00109 hostConfig.setHost(url, port);
00110 client.setHostConfiguration(hostConfig);
00111
00112
00113 if (hasproxy) {
00114 try {
00115
00116
00117 HttpClient testclient = new HttpClient();
00118 HostConfiguration testhostConfig = new HostConfiguration();
00119 testhostConfig.setHost(url, port);
00120 testclient.setHostConfiguration(testhostConfig);
00121 PostMethod rsp = new PostMethod("/");
00122 try {
00123 int st = testclient.executeMethod(rsp);
00124 if (st >= 300) throw new ServiceException();
00125 client = testclient;
00126 } finally {
00127 rsp.releaseConnection();
00128 }
00129 } catch (Throwable ex){
00130
00131 }
00132 }
00133 client.getParams().setCookiePolicy(null);
00134 client.getParams().setParameter(HttpClientParams.USER_AGENT, userAgent);
00135
00136 return client;
00137 }
00138
00139 public static void cleanConnexion() {
00140
00141 String socks = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.address");
00142 if (socks != null) {
00143 Properties props = System.getProperties();
00144 props.remove("socksProxyHost");
00145 props.remove("socksProxyPort") ;
00146 Authenticator.setDefault (null);
00147 }
00148 }
00149
00150 public static String getDefaultUserAgent () {
00151 String usera = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.useragent");
00152 if (usera == null) {
00153 Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00154 StringBuffer useragent = new StringBuffer("OpenMSPPlug-");
00155 useragent.append(plateform.getOpenMobileISMajorVersion()).append('.').append(plateform.getOpenMobileISMinorVersion()).append('-');
00156 useragent.append(plateform.getOS()).append('[').append(plateform.getOSVersion()).append("]-");
00157 String language = IntlResourceManager.getManager().getManagerLocaleString();
00158 useragent.append(language);
00159 usera = useragent.toString();
00160 }
00161 return usera;
00162 }
00163
00164 }