ApacheHTTPConnectionFactory.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2007 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
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                                         //props.put("java.net.socks.username", login);
00083                                         //props.put("java.net.socks.password", pass) ;
00084                                         //props.put("user.name", login);
00085                                         //props.put("java.net.socks.password", pass) ;
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                 //------------------- Construct HttpClient connection -----------------------
00109                 hostConfig.setHost(url, port);
00110                 client.setHostConfiguration(hostConfig);
00111 
00112                 //test connexion  if proxy.
00113                 if (hasproxy)   {
00114                         try     {
00115                                 //try with out proxy
00116                                 //create a new connexion without proxy test if it work
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                                 //do nothing keep proxy
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                 //clean socks info to allow next connection to work.
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 }

Generated on Mon Jan 14 17:29:44 2008 for OpenMobileIS by  doxygen 1.5.4