List of usage examples for java.net Proxy Proxy
public Proxy(Type type, SocketAddress sa)
From source file:base.BasePlayer.Main.java
public static String checkFile(URL url, ArrayList<String> others) throws IOException { URLConnection httpCon = null; String fileURL = url.getPath(); Proxy proxy = null;/* ww w .j a va 2 s . c o m*/ if (ProxySettings.useProxy.isSelected()) { String res = setProxyVariables(); if (res.equals("")) { InetSocketAddress proxyInet = new InetSocketAddress(address, port); proxy = new Proxy(type, proxyInet); httpCon = (URLConnection) url.openConnection(proxy); } } else { httpCon = (URLConnection) url.openConnection(); } try { httpCon.connect(); } catch (Exception e) { if (ProxySettings.useProxy.isSelected()) { Main.showError("Could not connect to internet to download genomes.\n" + "Proxy settings are not correct. Go to Tools -> Settings -> Proxy.", "Error"); } else { Main.showError("Could not connect to internet to download genomes.\n" + "If you are behind proxy server, Go to Tools -> Settings -> Proxy.", "Error"); } return null; } String fileName = ""; String disposition = httpCon.getHeaderField("Content-Disposition"); if (disposition != null) { // extracts file name from header field int index = disposition.indexOf("filename="); if (index > 0) { fileName = disposition.substring(index + 10, disposition.length() - 1); } } else { // extracts file name from URL fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1); } InputStream inputStream = null; // opens input stream from the HTTP connection try { inputStream = httpCon.getInputStream(); } catch (Exception e) { if (fileName.endsWith(".gff3.gz")) { String urldir = fileURL.substring(0, fileURL.lastIndexOf("/") + 1); fileName = getNewFile(url.getHost(), urldir, fileName); if (!others.contains(fileName.replace(".gff3.gz", ""))) { return fileName; } else { return ""; } } } if (inputStream == null) { return fileName; } else { inputStream.close(); if (!others.contains(fileName.replace(".gff3.gz", ""))) { return fileName; } return ""; } }
From source file:base.BasePlayer.Main.java
public static String downloadFile(URL url, String saveDir, int size) throws IOException { URLConnection httpCon = null; Proxy proxy = null;/*from w ww . j ava 2 s.c om*/ if (ProxySettings.useProxy.isSelected()) { String res = setProxyVariables(); if (res.equals("")) { InetSocketAddress proxyInet = new InetSocketAddress(address, port); proxy = new Proxy(type, proxyInet); httpCon = (URLConnection) url.openConnection(proxy); } } else { httpCon = (URLConnection) url.openConnection(); } try { httpCon.connect(); } catch (Exception e) { if (ProxySettings.useProxy.isSelected()) { Main.showError("Could not connect to internet to download genomes.\n" + "Proxy settings are not correct. Go to Tools -> Settings -> Proxy.\n" + "You can download genome files manually from Ensembl by selecting the genome and pressing \"Get file links.\" button below.\n" + "Download files with web browser and add them manually. See online manual for more instructions.", "Error"); } else { Main.showError("Could not connect to internet to download genomes.\n" + "If you are behind proxy server, Go to Tools -> Settings -> Proxy.\n" + "You can download genome files manually from Ensembl by selecting the genome and pressing \"Get file links.\" button below.\n" + "Download files with web browser and add them manually. See online manual for more instructions.", "Error"); } return null; } String fileURL = url.getPath(); int BUFFER_SIZE = 4096; // always check HTTP response code first String fileName = ""; String disposition = httpCon.getHeaderField("Content-Disposition"); if (disposition != null) { // extracts file name from header field int index = disposition.indexOf("filename="); if (index > 0) { fileName = disposition.substring(index + 10, disposition.length() - 1); } } else { // extracts file name from URL fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1); } InputStream inputStream = null; // opens input stream from the HTTP connection try { inputStream = httpCon.getInputStream(); } catch (Exception e) { if (fileName.endsWith(".gff3.gz")) { String urldir = fileURL.substring(0, fileURL.lastIndexOf("/") + 1); fileName = getNewFile(url.getHost(), urldir, fileName); url = new URL(url.getProtocol() + "://" + url.getHost() + "/" + urldir + "/" + fileName); httpCon = (URLConnection) url.openConnection(); inputStream = httpCon.getInputStream(); } } if (inputStream == null) { return ""; } if (fileName.endsWith(".gff3.gz")) { saveDir = saveDir += "/" + fileName + "/"; new File(saveDir).mkdirs(); } String saveFilePath = saveDir + File.separator + fileName; // opens an output stream to save into file FileOutputStream outputStream = new FileOutputStream(saveFilePath); long downloaded = 0; int bytesRead = -1, counter = 0; String loading = ""; byte[] buffer = new byte[BUFFER_SIZE]; if (Main.drawCanvas != null) { loading = drawCanvas.loadingtext; Main.drawCanvas.loadingtext = loading + " 0MB"; } while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); downloaded += buffer.length; if (Main.drawCanvas != null) { counter++; if (counter == 100) { Main.drawCanvas.loadingtext = loading + " " + (downloaded / 1048576) + "/~" + (size / 1048576) + "MB"; Main.drawCanvas.loadBarSample = (int) (downloaded / (double) size * 100); Main.drawCanvas.loadbarAll = (int) (downloaded / (double) size * 100); if (Main.drawCanvas.loadBarSample > 100) { Main.drawCanvas.loadBarSample = 100; Main.drawCanvas.loadbarAll = 100; } counter = 0; } } } if (Main.drawCanvas != null) { Main.drawCanvas.loadBarSample = 0; Main.drawCanvas.loadbarAll = 0; } outputStream.close(); inputStream.close(); return fileName; }
From source file:fr.cls.atoll.motu.library.misc.intfce.Organizer.java
/** * Gets the url connection opts.// w w w . j av a 2 s.co m * * @param scheme the scheme * @param host the host * @return the url connection opts * @throws MotuException the motu exception */ public static Proxy getUrlConnectionProxy(String scheme, String host) throws MotuException { if (LOG.isDebugEnabled()) { LOG.debug("getUrlConnectionProxy(String, String) - start - scheme=" + scheme + ", host=" + host); } Proxy proxy = null; String proxyHost = null; String proxyPort = null; String proxyLogin = null; String proxyPwd = null; Authenticator.setDefault(null); if (Organizer.isNullOrEmpty(scheme)) { if (LOG.isDebugEnabled()) { LOG.debug("getUrlConnectionProxy(String, String) - end - scheme is null or empty"); } return proxy; } MotuConfigFileSystemWrapper<Boolean> wrapperBoolean = new MotuConfigFileSystemWrapper<Boolean>(); MotuConfigFileSystemWrapper<Period> wrapperPeriod = new MotuConfigFileSystemWrapper<Period>(); MotuConfigFileSystemWrapper<String> wrapperString = new MotuConfigFileSystemWrapper<String>(); if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) { Boolean isUseProxy = wrapperBoolean.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_USEHTTPPROXY); if ((isUseProxy != null) && (isUseProxy)) { proxyHost = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_HTTPPROXYHOST); proxyPort = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_HTTPPROXYPORT); proxyLogin = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_HTTPPROXYLOGIN); proxyPwd = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_HTTPPROXYPWD); } } else if (scheme.equalsIgnoreCase("ftp")) { Boolean isUseProxy = wrapperBoolean.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_USEFTPPROXY); if ((isUseProxy != null) && (isUseProxy)) { proxyHost = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_FTPPROXYHOST); proxyPort = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_FTPPROXYPORT); proxyLogin = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_FTPPROXYLOGIN); proxyPwd = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_FTPPROXYPWD); } } else if (scheme.equalsIgnoreCase("sftp")) { Boolean isUseProxy = wrapperBoolean.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_USESFTPPROXY); if ((isUseProxy != null) && (isUseProxy)) { proxyHost = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_SFTPPROXYHOST); proxyPort = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_SFTPPROXYPORT); proxyLogin = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_SFTPPROXYLOGIN); proxyPwd = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_SFTPPROXYPWD); } } if ((proxyHost != null) && (proxyPort != null)) { if (LOG.isDebugEnabled()) { LOG.debug("getUrlConnectionProxy(String, String) - Create Proxy - proxyHost=" + proxyHost + ", proxyPort=" + proxyPort); } proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort))); if ((proxyLogin != null) && (proxyPwd != null)) { if (LOG.isDebugEnabled()) { LOG.debug("getUrlConnectionProxy(String, String) - set authentication - proxyLogin=" + proxyLogin + ", proxyPwd=" + proxyPwd); } Authenticator.setDefault(new SimpleAuthenticator(proxyLogin, proxyPwd)); } } if (LOG.isDebugEnabled()) { LOG.debug("getUrlConnectionProxy(String, String) - end"); } return proxy; }