HttpURLConnection newHttpConnection(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS); connection.setReadTimeout(READ_TIMEOUT_MILLISECONDS); connection.setInstanceFollowRedirects(true); connection.setRequestMethod("GET"); connection.setRequestProperty("User-Agent", getUserAgent()); return connection; }
private List<File> downloadBatchFiles() { try { List<File> files = new ArrayList<File>(); String libs = remoteContent(BATCH_PATH); for (String lib : libs.split(",")) { File file = new File(bootDir, lib); remoteContentToFile(BATCH_PATH + lib, file); files.add(file); } return files; } catch (Exception e) { throw new BootstrapException(e); } } }
String remoteContent(String path) throws IOException { String fullUrl = serverUrl + path; HttpURLConnection conn = newHttpConnection(new URL(fullUrl)); Reader reader = new InputStreamReader((InputStream) conn.getContent()); try { int statusCode = conn.getResponseCode(); if (statusCode != HttpURLConnection.HTTP_OK) { throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode); } return BootstrapperIOUtils.toString(reader); } finally { BootstrapperIOUtils.closeQuietly(reader); conn.disconnect(); } }
/** * @return server version */ public String getServerVersion() { if (serverVersion == null) { try { serverVersion = remoteContent(VERSION_PATH); } catch (IOException e) { throw new BootstrapException(e.getMessage(), e); } } return serverVersion; }
/** * Download batch files from server and creates {@link BootstrapClassLoader}. * To use this method version of Sonar should be at least 2.6. * * @param urls additional URLs for loading classes and resources * @param parent parent ClassLoader * @param unmaskedPackages only classes and resources from those packages would be available for loading from parent */ public BootstrapClassLoader createClassLoader(URL[] urls, ClassLoader parent, String... unmaskedPackages) { BootstrapClassLoader classLoader = new BootstrapClassLoader(parent, unmaskedPackages); List<File> files = downloadBatchFiles(); for (URL url : urls) { classLoader.addURL(url); } for (File file : files) { try { classLoader.addURL(file.toURI().toURL()); } catch (MalformedURLException e) { throw new BootstrapException(e); } } return classLoader; }
private void remoteContentToFile(String path, File toFile) { InputStream input = null; FileOutputStream output = null; String fullUrl = serverUrl + path; try { HttpURLConnection connection = newHttpConnection(new URL(fullUrl)); output = new FileOutputStream(toFile, false); input = connection.getInputStream(); BootstrapperIOUtils.copyLarge(input, output); } catch (IOException e) { BootstrapperIOUtils.closeQuietly(output); BootstrapperIOUtils.deleteFileQuietly(toFile); throw new BootstrapException("Fail to download the file: " + fullUrl, e); } finally { BootstrapperIOUtils.closeQuietly(input); BootstrapperIOUtils.closeQuietly(output); } }