List of usage examples for java.net HttpURLConnection getHeaderFieldLong
public long getHeaderFieldLong(String name, long Default)
From source file:io.webfolder.cdp.ChromiumDownloader.java
public Path download(ChromiumVersion version) { final Path destinationRoot = getChromiumPath(version); final Path executable = getExecutable(version); String url;//from w w w .j a va2 s. c o m if (WINDOWS) { url = format("%s/Win_x64/%d/chrome-win.zip", DOWNLOAD_HOST, version.getRevision()); } else if (LINUX) { url = format("%s/Linux_x64/%d/chrome-linux.zip", DOWNLOAD_HOST, version.getRevision()); } else if (MAC) { url = format("%s/Mac/%d/chrome-mac.zip", DOWNLOAD_HOST, version.getRevision()); } else { throw new CdpException("Unsupported OS found - " + OS); } try { URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setRequestMethod("HEAD"); conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); if (conn.getResponseCode() != 200) { throw new CdpException(conn.getResponseCode() + " - " + conn.getResponseMessage()); } long contentLength = conn.getHeaderFieldLong("x-goog-stored-content-length", 0); String fileName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")) + "-r" + version.getRevision() + ".zip"; Path archive = get(getProperty("java.io.tmpdir")).resolve(fileName); if (exists(archive) && contentLength != size(archive)) { delete(archive); } if (!exists(archive)) { logger.info("Downloading Chromium [revision=" + version.getRevision() + "] 0%"); u = new URL(url); if (conn.getResponseCode() != 200) { throw new CdpException(conn.getResponseCode() + " - " + conn.getResponseMessage()); } conn = (HttpURLConnection) u.openConnection(); conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); Thread thread = null; AtomicBoolean halt = new AtomicBoolean(false); Runnable progress = () -> { try { long fileSize = size(archive); logger.info("Downloading Chromium [revision={}] {}%", version.getRevision(), round((fileSize * 100L) / contentLength)); } catch (IOException e) { // ignore } }; try (InputStream is = conn.getInputStream()) { logger.info("Download location: " + archive.toString()); thread = new Thread(() -> { while (true) { try { if (halt.get()) { break; } progress.run(); sleep(1000); } catch (Throwable e) { // ignore } } }); thread.setName("cdp4j"); thread.setDaemon(true); thread.start(); copy(conn.getInputStream(), archive); } finally { if (thread != null) { progress.run(); halt.set(true); } } } logger.info("Extracting to: " + destinationRoot.toString()); if (exists(archive)) { createDirectories(destinationRoot); unpack(archive.toFile(), destinationRoot.toFile()); } if (!exists(executable) || !isExecutable(executable)) { throw new CdpException("Chromium executable not found: " + executable.toString()); } if (!WINDOWS) { Set<PosixFilePermission> permissions = getPosixFilePermissions(executable); if (!permissions.contains(OWNER_EXECUTE)) { permissions.add(OWNER_EXECUTE); setPosixFilePermissions(executable, permissions); } if (!permissions.contains(GROUP_EXECUTE)) { permissions.add(GROUP_EXECUTE); setPosixFilePermissions(executable, permissions); } } } catch (IOException e) { throw new CdpException(e); } return executable; }
From source file:org.apache.tajo.storage.http.ExampleHttpFileTablespace.java
@Override public long getTableVolume(TableDesc table, Optional<EvalNode> notUsed) { HttpURLConnection connection = null; try {//from w ww . ja v a 2 s . c o m connection = (HttpURLConnection) new URL(table.getUri().toASCIIString()).openConnection(); connection.setRequestMethod("HEAD"); connection.connect(); return connection.getHeaderFieldLong(Names.CONTENT_LENGTH, -1); } catch (IOException e) { throw new TajoInternalError(e); } finally { if (connection != null) { connection.disconnect(); } } }