com.github.webdriverextensions.DriverDownloader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webdriverextensions-maven-plugin Show documentation
Show all versions of webdriverextensions-maven-plugin Show documentation
Use this plugin to manage, download and install WebDriver drivers directly from your pom.
The newest version!
package com.github.webdriverextensions;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Settings;
public class DriverDownloader {
public static final int FILE_DOWNLOAD_READ_TIMEOUT = 30 * 60 * 1000; // 30 min
public static final int FILE_DOWNLOAD_CONNECT_TIMEOUT = 30 * 1000; // 30 seconds
public static final int FILE_DOWNLOAD_RETRY_ATTEMPTS = 3;
private final Log log;
private final Proxy proxySettings;
public DriverDownloader(Settings settings, String proxyId, Log log) throws MojoExecutionException {
this.log = log;
this.proxySettings = ProxyUtils.getProxyFromSettings(settings, proxyId);
}
public Path downloadFile(Driver driver, String tempdirectory) throws MojoExecutionException {
String url = driver.getUrl();
Path downloadLocation = Paths.get(tempdirectory, driver.getFilenameFromUrl());
log.info(" Downloading " + url + " -> " + downloadLocation);
File fileToDownload = downloadLocation.toFile();
if (fileToDownload.exists()) {
log.info("file " + downloadLocation + " already downloaded");
} else {
HttpClientBuilder httpClientBuilder = prepareHttpClientBuilderWithTimeoutsAndProxySettings(proxySettings);
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(FILE_DOWNLOAD_RETRY_ATTEMPTS, true));
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
try (CloseableHttpResponse fileDownloadResponse = httpClient.execute(new HttpGet(url))) {
HttpEntity remoteFileStream = fileDownloadResponse.getEntity();
copyInputStreamToFile(remoteFileStream.getContent(), fileToDownload);
}
} catch (IOException ex) {
log.info("Problem downloading file from " + url + " cause of " + ex.getCause());
throw new MojoExecutionException("Failed to download file", ex);
}
}
return downloadLocation;
}
private static HttpClientBuilder prepareHttpClientBuilderWithTimeoutsAndProxySettings(Proxy proxySettings) throws MojoExecutionException {
SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(FILE_DOWNLOAD_READ_TIMEOUT).build();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(FILE_DOWNLOAD_CONNECT_TIMEOUT).build();
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder
.setDefaultSocketConfig(socketConfig)
.setDefaultRequestConfig(requestConfig)
.disableContentCompression();
HttpHost proxy = ProxyUtils.createProxyFromSettings(proxySettings);
if (proxy != null) {
httpClientBuilder.setProxy(proxy);
CredentialsProvider proxyCredentials = ProxyUtils.createProxyCredentialsFromSettings(proxySettings);
if (proxyCredentials != null) {
httpClientBuilder.setDefaultCredentialsProvider(proxyCredentials);
}
}
return httpClientBuilder;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy