HTTPConnectionFactory.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 org.openmobileis.common.context.ApplicationContextManager;
00028 import org.openmobileis.common.context.Plateform;
00029 import org.openmobileis.common.intl.IntlResourceManager;
00030 import org.openmobileis.common.util.PropertiesManager;
00031 
00032 import HTTPClient.AuthorizationInfo;
00033 import HTTPClient.CookieModule;
00034 import HTTPClient.HTTPConnection;
00035 import HTTPClient.NVPair;
00036 
00041 public final class HTTPConnectionFactory {
00042         public static HTTPConnection createConnection(String url, int port, String userAgent)   {
00043                 HTTPConnection con = null;
00044                 String socks = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.address");
00045                 //                      LogManager.traceDebug(0, "proxy :"+proxy);
00046                 if (socks != null) {
00047                         String sport = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.port");
00048                         if (sport != null) {
00049                                 int socksport = Integer.parseInt(sport);
00050                                 HTTPConnection.setSocksServer(socks, socksport);
00051                                 String login = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.login");
00052                                 String pass = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.pass");
00053                                 String realm = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.socks.realm");
00054                                 if (login != null && pass != null && realm!=null) {
00055                                         AuthorizationInfo.addBasicAuthorization(socks, socksport, realm, login, pass);
00056                                 }
00057                         }
00058                 }
00059                 String proxy = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.address");
00060                 //                      LogManager.traceDebug(0, "proxy :"+proxy);
00061                 if (proxy != null) {
00062                         String pport = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.port");
00063                         if (pport != null) {
00064                                 int proxyport = Integer.parseInt(pport);
00065                                 HTTPConnection.setProxyServer(proxy, proxyport);
00066                                 String login = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.login");
00067                                 String pass = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.pass");
00068                                 String realm = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.proxy.realm");
00069                                 if (login != null && pass != null && realm!=null) {
00070                                         AuthorizationInfo.addBasicAuthorization(proxy, proxyport, realm, login, pass);
00071                                 }
00072                         }
00073                 }
00074                 con = new HTTPConnection(url, port);
00075                 //manage proxy server
00076 
00077                 CookieModule.setCookiePolicyHandler(null);
00078                 NVPair headerData[] = new NVPair[1];
00079                 headerData[0] = new NVPair("User-Agent", userAgent);
00080                 con.setDefaultHeaders(headerData);
00081                 return con;
00082         }
00083 
00084         public static String getDefaultUserAgent ()     {
00085     String usera = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.useragent");
00086     if (usera == null)  {
00087       Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00088       StringBuffer useragent = new StringBuffer("OpenMSPPlug-");
00089       useragent.append(plateform.getOpenMobileISMajorVersion()).append('.').append(plateform.getOpenMobileISMinorVersion()).append('-');
00090       useragent.append(plateform.getOS()).append('[').append(plateform.getOSVersion()).append("]-");
00091       String language = IntlResourceManager.getManager().getManagerLocaleString();
00092       useragent.append(language);
00093       usera = useragent.toString();
00094     }
00095     return usera;
00096         }
00097 
00098 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4