/** Set default proxy settings, used by cobra for ie */ public static synchronized void setDefaultProxySettings() { String sProxyHost = ConfigurationManager.getProperty(CONF_NETWORK_PROXY_HOSTNAME); int iProxyPort = ConfigurationManager.getInt(CONF_NETWORK_PROXY_PORT); String sProxyLogin = ConfigurationManager.getProperty(CONF_NETWORK_PROXY_LOGIN); String sProxyPwd = ConfigurationManager.getProperty(CONF_NETWORK_PROXY_PWD); Type proxyType = Type.DIRECT; if (ConfigurationManager.getBoolean(CONF_NETWORK_USE_PROXY)) { // Set default proxy value if (PROXY_TYPE_HTTP.equals(ConfigurationManager.getProperty(CONF_NETWORK_PROXY_TYPE))) { proxyType = Type.HTTP; } else if (PROXY_TYPE_SOCKS.equals( ConfigurationManager.getProperty(CONF_NETWORK_PROXY_TYPE))) { proxyType = Type.SOCKS; } try { proxy = new Proxy(proxyType, sProxyHost, iProxyPort, sProxyLogin, sProxyPwd); } catch (Exception e) { Log.error(e); return; } } // Set system defaults proxy values, if we don't use DownloadManager // methods // see http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html if (ConfigurationManager.getBoolean(CONF_NETWORK_USE_PROXY)) { System.getProperties().put("proxySet", "true"); if (PROXY_TYPE_HTTP.equals(ConfigurationManager.getProperty(CONF_NETWORK_PROXY_TYPE))) { System.setProperty("http.proxyHost", sProxyHost); System.setProperty("http.proxyPort", Integer.toString(iProxyPort)); } else if (PROXY_TYPE_SOCKS.equals( ConfigurationManager.getProperty(CONF_NETWORK_PROXY_TYPE))) { System.setProperty("socksProxyHost", sProxyHost); System.setProperty("socksProxyPort ", Integer.toString(iProxyPort)); } Authenticator.setDefault( new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { String user = ConfigurationManager.getProperty(CONF_NETWORK_PROXY_LOGIN); char[] pwd = Util.rot13(ConfigurationManager.getProperty(CONF_NETWORK_PROXY_PWD)) .toCharArray(); return new PasswordAuthentication(user, pwd); } }); } }
/** * Download the resource at the given url * * @param url to download * @param Use cache : store file in image cache * @throws Exception * @return result as an array of bytes, null if a problem occured */ public static byte[] download(URL url, boolean bUseCache) throws Exception { byte[] bOut = null; // check if file is not already downloaded or being downloaded if (bUseCache) { if (new File(Util.getCachePath(url)).exists()) { return bOut; } } GetMethod get = null; HttpClient client = null; int iConTO = 1000 * ConfigurationManager.getInt(CONF_NETWORK_CONNECTION_TO); int iTraTO = 1000 * ConfigurationManager.getInt(CONF_NETWORK_TRANSFERT_TO); if (ConfigurationManager.getBoolean(CONF_NETWORK_USE_PROXY)) { client = getHTTPClient( ConfigurationManager.getProperty(CONF_NETWORK_PROXY_LOGIN), DownloadManager.getProxyPwd(), iConTO, iTraTO); } else { client = getHTTPClient(iConTO, iTraTO); } get = new GetMethod(url.toString()); get.addRequestHeader( "Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"); //$NON-NLS-1$ //$NON-NLS-2$ get.addRequestHeader("Accept-Language", "en-us"); // $NON-NLS-1$ //$NON-NLS-2$ get.addRequestHeader( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); //$NON-NLS-1$ //$NON-NLS-2$ get.addRequestHeader("Connection", "Keep-Alive"); // $NON-NLS-1$ //$NON-NLS-2$ int status = client.executeMethod(get); if (bUseCache) { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(Util.getCachePath(url))); BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream()); int i; while ((i = bis.read()) != -1) { bos.write(i); } bos.close(); bis.close(); } else { bOut = get.getResponseBody(); } if (get != null && get.isRequestSent()) { get.releaseConnection(); } return bOut; }