List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:org.jab.docsearch.DocSearch.java
private boolean downloadURLToFile(String urlString, String fileToSaveAs) { int numBytes = 0; int curI = 0; FileOutputStream dos = null;/* w ww.j a v a 2 s .c om*/ InputStream urlStream = null; int lastPercent = 0; int curPercent = 0; try { URL url = new URL(urlString); File saveFile = new File(fileToSaveAs); dos = new FileOutputStream(saveFile); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setUseCaches(false); conn.connect(); urlStream = conn.getInputStream(); int totalSize = conn.getContentLength(); while (curI != -1) { curI = urlStream.read(); byte curBint = (byte) curI; if (curI == -1) { break; } dos.write(curBint); numBytes++; if (totalSize > 0) { curPercent = (numBytes * 100) / totalSize; if (curPercent != lastPercent) { setStatus(curPercent + " " + I18n.getString("percent_downloaded") + " " + I18n.getString("of_file") + " " + urlString + " " + I18n.getString("total_bytes") + " " + totalSize); lastPercent = curPercent; } } // end if total size not zero } urlStream.close(); dos.close(); } catch (IOException ioe) { logger.fatal("downloadURLToFile() failed", ioe); showMessage(I18n.getString("error_download_file"), ioe.toString()); return false; } finally { IOUtils.closeQuietly(dos); IOUtils.closeQuietly(urlStream); } return true; }
From source file:org.openqa.selenium.server.ProxyHandler.java
protected long proxyPlainTextRequest(URL url, String pathInContext, String pathParams, HttpRequest request, HttpResponse response) throws IOException { CaptureNetworkTrafficCommand.Entry entry = new CaptureNetworkTrafficCommand.Entry(request.getMethod(), url.toString());//from ww w .j av a 2s . c o m entry.addRequestHeaders(request); if (log.isDebugEnabled()) log.debug("PROXY URL=" + url); URLConnection connection = url.openConnection(); connection.setAllowUserInteraction(false); if (proxyInjectionMode) { adjustRequestForProxyInjection(request, connection); } // Set method HttpURLConnection http = null; if (connection instanceof HttpURLConnection) { http = (HttpURLConnection) connection; http.setRequestMethod(request.getMethod()); http.setInstanceFollowRedirects(false); if (trustAllSSLCertificates && connection instanceof HttpsURLConnection) { TrustEverythingSSLTrustManager.trustAllSSLCertificates((HttpsURLConnection) connection); } } // check connection header String connectionHdr = request.getField(HttpFields.__Connection); if (connectionHdr != null && (connectionHdr.equalsIgnoreCase(HttpFields.__KeepAlive) || connectionHdr.equalsIgnoreCase(HttpFields.__Close))) connectionHdr = null; // copy headers boolean xForwardedFor = false; boolean isGet = "GET".equals(request.getMethod()); boolean hasContent = false; Enumeration enm = request.getFieldNames(); while (enm.hasMoreElements()) { // TODO could be better than this! String hdr = (String) enm.nextElement(); if (_DontProxyHeaders.containsKey(hdr) || !_chained && _ProxyAuthHeaders.containsKey(hdr)) continue; if (connectionHdr != null && connectionHdr.indexOf(hdr) >= 0) continue; if (!isGet && HttpFields.__ContentType.equals(hdr)) hasContent = true; Enumeration vals = request.getFieldValues(hdr); while (vals.hasMoreElements()) { String val = (String) vals.nextElement(); if (val != null) { // don't proxy Referer headers if the referer is Selenium! if ("Referer".equals(hdr) && (-1 != val.indexOf("/selenium-server/"))) { continue; } if (!isGet && HttpFields.__ContentLength.equals(hdr) && Integer.parseInt(val) > 0) { hasContent = true; } connection.addRequestProperty(hdr, val); xForwardedFor |= HttpFields.__XForwardedFor.equalsIgnoreCase(hdr); } } } // add any custom request headers that the user asked for Map<String, String> customRequestHeaders = AddCustomRequestHeaderCommand.getHeaders(); for (Map.Entry<String, String> e : customRequestHeaders.entrySet()) { connection.addRequestProperty(e.getKey(), e.getValue()); entry.addRequestHeader(e.getKey(), e.getValue()); } // Proxy headers if (!_anonymous) connection.setRequestProperty("Via", "1.1 (jetty)"); if (!xForwardedFor) connection.addRequestProperty(HttpFields.__XForwardedFor, request.getRemoteAddr()); // a little bit of cache control String cache_control = request.getField(HttpFields.__CacheControl); if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0)) connection.setUseCaches(false); // customize Connection customizeConnection(pathInContext, pathParams, request, connection); try { connection.setDoInput(true); // do input thang! InputStream in = request.getInputStream(); if (hasContent) { connection.setDoOutput(true); IO.copy(in, connection.getOutputStream()); } // Connect connection.connect(); } catch (Exception e) { LogSupport.ignore(log, e); } InputStream proxy_in = null; // handler status codes etc. int code = -1; if (http != null) { proxy_in = http.getErrorStream(); try { code = http.getResponseCode(); } catch (SSLHandshakeException e) { throw new RuntimeException("Couldn't establish SSL handshake. Try using trustAllSSLCertificates.\n" + e.getLocalizedMessage(), e); } response.setStatus(code); response.setReason(http.getResponseMessage()); String contentType = http.getContentType(); if (log.isDebugEnabled()) { log.debug("Content-Type is: " + contentType); } } if (proxy_in == null) { try { proxy_in = connection.getInputStream(); } catch (Exception e) { LogSupport.ignore(log, e); proxy_in = http.getErrorStream(); } } // clear response defaults. response.removeField(HttpFields.__Date); response.removeField(HttpFields.__Server); // set response headers int h = 0; String hdr = connection.getHeaderFieldKey(h); String val = connection.getHeaderField(h); while (hdr != null || val != null) { if (hdr != null && val != null && !_DontProxyHeaders.containsKey(hdr) && (_chained || !_ProxyAuthHeaders.containsKey(hdr))) response.addField(hdr, val); h++; hdr = connection.getHeaderFieldKey(h); val = connection.getHeaderField(h); } if (!_anonymous) response.setField("Via", "1.1 (jetty)"); response.removeField(HttpFields.__ETag); // possible cksum? Stop caching... response.removeField(HttpFields.__LastModified); // Stop caching... // Handled long bytesCopied = -1; request.setHandled(true); if (proxy_in != null) { boolean injectableResponse = http.getResponseCode() == HttpURLConnection.HTTP_OK || (http.getResponseCode() >= 400 && http.getResponseCode() < 600); if (proxyInjectionMode && injectableResponse) { // check if we should proxy this path based on the dontProxyRegex that can be user-specified if (shouldInject(request.getPath())) { bytesCopied = InjectionHelper.injectJavaScript(request, response, proxy_in, response.getOutputStream(), debugURL); } else { bytesCopied = ModifiedIO.copy(proxy_in, response.getOutputStream()); } } else { bytesCopied = ModifiedIO.copy(proxy_in, response.getOutputStream()); } } entry.finish(code, bytesCopied); entry.addResponseHeader(response); CaptureNetworkTrafficCommand.capture(entry); return bytesCopied; }
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 . ja v a2 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.\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: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;//from w w w. ja v a 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:org.orbeon.oxf.util.Connection.java
/** * Open the connection. This sends request headers, request body, and reads status and response headers. * * @return connection result *//*from w ww.ja v a 2s.co m*/ public ConnectionResult connect() { final boolean isDebugEnabled = indentedLogger.isDebugEnabled(); // Perform connection final String scheme = connectionURL.getProtocol(); if (isHTTPOrHTTPS(scheme) || (httpMethod.equals("GET") && (scheme.equals("file") || scheme.equals("oxf")))) { // http MUST be supported // https SHOULD be supported // file SHOULD be supported try { // Create URL connection object final URLConnection urlConnection = connectionURL.openConnection(); final HTTPURLConnection httpURLConnection = (urlConnection instanceof HTTPURLConnection) ? (HTTPURLConnection) urlConnection : null; // Whether a message body must be sent final boolean hasRequestBody = httpMethod.equals("POST") || httpMethod.equals("PUT"); urlConnection.setDoInput(true); urlConnection.setDoOutput(hasRequestBody); // Configure HTTPURLConnection if (httpURLConnection != null) { // Set state if possible httpURLConnection.setCookieStore(this.cookieStore); // Set method httpURLConnection.setRequestMethod(httpMethod); // Set credentials if (credentials != null) { httpURLConnection.setUsername(credentials.username); if (credentials.password != null) httpURLConnection.setPassword(credentials.password); if (credentials.preemptiveAuthentication != null) httpURLConnection.setPreemptiveAuthentication(credentials.preemptiveAuthentication); if (credentials.domain != null) httpURLConnection.setDomain(credentials.domain); } } // Update request headers { // Handle SOAP // Set request Content-Type, SOAPAction or Accept header if needed final boolean didSOAP = handleSOAP(indentedLogger, httpMethod, headersMap, contentType, hasRequestBody); // Set request content type if (!didSOAP && hasRequestBody) { final String actualContentType = (contentType != null) ? contentType : "application/xml"; headersMap.put("Content-Type", new String[] { actualContentType }); indentedLogger.logDebug(LOG_TYPE, "setting header", "Content-Type", actualContentType); } } // Set headers on connection final List<String> headersToLog; if (headersMap != null && headersMap.size() > 0) { headersToLog = isDebugEnabled ? new ArrayList<String>() : null; for (Map.Entry<String, String[]> currentEntry : headersMap.entrySet()) { final String currentHeaderName = currentEntry.getKey(); final String[] currentHeaderValues = currentEntry.getValue(); if (currentHeaderValues != null) { // Add all header values as "request properties" for (String currentHeaderValue : currentHeaderValues) { urlConnection.addRequestProperty(currentHeaderName, currentHeaderValue); if (headersToLog != null) { headersToLog.add(currentHeaderName); headersToLog.add(currentHeaderValue); } } } } } else { headersToLog = null; } // Log request details except body if (isDebugEnabled) { // Basic connection information final URI connectionURI; try { String userInfo = connectionURL.getUserInfo(); if (userInfo != null) { final int colonIndex = userInfo.indexOf(':'); if (colonIndex != -1) userInfo = userInfo.substring(0, colonIndex + 1) + "xxxxxxxx";// hide password in logs } connectionURI = new URI(connectionURL.getProtocol(), userInfo, connectionURL.getHost(), connectionURL.getPort(), connectionURL.getPath(), connectionURL.getQuery(), connectionURL.getRef()); } catch (URISyntaxException e) { throw new OXFException(e); } indentedLogger.logDebug(LOG_TYPE, "opening URL connection", "method", httpMethod, "URL", connectionURI.toString(), "request Content-Type", contentType); // Log all headers if (headersToLog != null) { final String[] strings = new String[headersToLog.size()]; indentedLogger.logDebug(LOG_TYPE, "request headers", headersToLog.toArray(strings)); } } // Write request body if needed if (hasRequestBody) { // Case of empty body if (messageBody == null) messageBody = new byte[0]; // Log message body for debugging purposes if (logBody) logRequestBody(indentedLogger, contentType, messageBody); // Set request body on connection httpURLConnection.setRequestBody(messageBody); } // Connect urlConnection.connect(); if (httpURLConnection != null) { // Get state if possible // This is either the state we set above before calling connect(), or a new state if we didn't provide any this.cookieStore = httpURLConnection.getCookieStore(); } // Create result final ConnectionResult connectionResult = new ConnectionResult(connectionURL.toExternalForm()) { @Override public void close() { if (getResponseInputStream() != null) { try { getResponseInputStream().close(); } catch (IOException e) { throw new OXFException( "Exception while closing input stream for action: " + connectionURL); } } if (httpURLConnection != null) httpURLConnection.disconnect(); } }; // Get response information that needs to be forwarded { // Status code connectionResult.statusCode = (httpURLConnection != null) ? httpURLConnection.getResponseCode() : 200; // Headers connectionResult.responseHeaders = urlConnection.getHeaderFields(); connectionResult.setLastModified(NetUtils.getLastModifiedAsLong(urlConnection)); // Content-Type connectionResult.setResponseContentType(urlConnection.getContentType(), "application/xml"); } // Log response details except body if (isDebugEnabled) { connectionResult.logResponseDetailsIfNeeded(indentedLogger, Level.DEBUG, LOG_TYPE); } // Response stream connectionResult.setResponseInputStream(urlConnection.getInputStream()); // Log response body if (isDebugEnabled) { connectionResult.logResponseBody(indentedLogger, Level.DEBUG, LOG_TYPE, logBody); } return connectionResult; } catch (IOException e) { throw new ValidationException(e, new LocationData(connectionURL.toExternalForm(), -1, -1)); } } else if (!httpMethod.equals("GET") && (scheme.equals("file") || scheme.equals("oxf"))) { // TODO: implement writing to file: and oxf: // SHOULD be supported (should probably support oxf: as well) throw new OXFException("submission URL scheme not yet implemented: " + scheme); } else if (scheme.equals("mailto")) { // TODO: implement sending mail // MAY be supported throw new OXFException("submission URL scheme not yet implemented: " + scheme); } else { throw new OXFException("submission URL scheme not supported: " + scheme); } }
From source file:org.sakaiproject.lessonbuildertool.tool.beans.SimplePageBean.java
public String getTypeOfUrl(String url) { String mimeType = "text/html"; // try to find the mime type of the remote resource // this is only likely to be a problem if someone is pointing to // a url within Sakai. We think in realistic cases those that are // files will be handled as files, so anything that comes where // will be HTML. That's the default if this fails. URLConnection conn = null;/*from w ww . j a v a 2 s . c om*/ try { conn = new URL(new URL(ServerConfigurationService.getServerUrl()), url).openConnection(); conn.setConnectTimeout(10000); conn.setReadTimeout(10000); // generate cookie based on code in RequestFilter.java //String suffix = System.getProperty("sakai.serverId"); //if (suffix == null || suffix.equals("")) // suffix = "sakai"; //Session s = sessionManager.getCurrentSession(); //conn.setRequestProperty("Cookie", "JSESSIONID=" + s.getId() + "." + suffix); conn.connect(); String t = conn.getContentType(); if (t != null && !t.equals("")) { int i = t.indexOf(";"); if (i >= 0) t = t.substring(0, i); t = t.trim(); mimeType = t; } } catch (Exception e) { log.error("getTypeOfUrl connection error " + e); } finally { if (conn != null) { try { conn.getInputStream().close(); } catch (Exception e) { log.error("getTypeOfUrl unable to close " + e); } } } return mimeType; }
From source file:editeurpanovisu.EditeurPanovisu.java
private static boolean netIsAvailable() { try {// www . j a v a 2 s.c o m final URL url = new URL("http://www.google.com"); final URLConnection conn = url.openConnection(); conn.connect(); return true; } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { return false; } }