Skip to content

Commit

Permalink
Adding proxy settings to fetching artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrussell authored and hoisie committed Dec 17, 2022
1 parent ed2d7d3 commit bed3ca5
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
Expand Up @@ -7,18 +7,34 @@
*/
@Deprecated
public class MavenRoboSettings {

private static final int DEFAULT_PROXY_PORT = 0;
private static String mavenRepositoryId;
private static String mavenRepositoryUrl;
private static String mavenRepositoryUserName;
private static String mavenRepositoryPassword;
private static String mavenProxyHost = "";
private static int mavenProxyPort = DEFAULT_PROXY_PORT;

static {
mavenRepositoryId = System.getProperty("robolectric.dependency.repo.id", "mavenCentral");
mavenRepositoryUrl =
System.getProperty("robolectric.dependency.repo.url", "https://repo1.maven.org/maven2");
mavenRepositoryUserName = System.getProperty("robolectric.dependency.repo.username");
mavenRepositoryPassword = System.getProperty("robolectric.dependency.repo.password");

String proxyHost = System.getProperty("robolectric.dependency.proxy.host");
if (proxyHost != null && !proxyHost.isEmpty()) {
mavenProxyHost = proxyHost;
}

String proxyPort = System.getProperty("robolectric.dependency.proxy.port");
if (proxyPort != null && !proxyPort.isEmpty()) {
try {
mavenProxyPort = Integer.parseInt(proxyPort);
} catch (NumberFormatException numberFormatException) {
mavenProxyPort = DEFAULT_PROXY_PORT;
}
}
}

public static String getMavenRepositoryId() {
Expand Down Expand Up @@ -52,4 +68,20 @@ public static String getMavenRepositoryPassword() {
public static void setMavenRepositoryPassword(String mavenRepositoryPassword) {
MavenRoboSettings.mavenRepositoryPassword = mavenRepositoryPassword;
}

public static String getMavenProxyHost() {
return mavenProxyHost;
}

public static void setMavenProxyHost(String mavenProxyHost) {
MavenRoboSettings.mavenProxyHost = mavenProxyHost;
}

public static int getMavenProxyPort() {
return mavenProxyPort;
}

public static void setMavenProxyPort(int mavenProxyPort) {
MavenRoboSettings.mavenProxyPort = mavenProxyPort;
}
}
Expand Up @@ -14,7 +14,9 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -34,6 +36,8 @@ public class MavenArtifactFetcher {
private final String repositoryUrl;
private final String repositoryUserName;
private final String repositoryPassword;
private final String proxyHost;
private final int proxyPort;
private final File localRepositoryDir;
private final ExecutorService executorService;
private File stagingRepositoryDir;
Expand All @@ -42,11 +46,15 @@ public MavenArtifactFetcher(
String repositoryUrl,
String repositoryUserName,
String repositoryPassword,
String proxyHost,
int proxyPort,
File localRepositoryDir,
ExecutorService executorService) {
this.repositoryUrl = repositoryUrl;
this.repositoryUserName = repositoryUserName;
this.repositoryPassword = repositoryPassword;
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.localRepositoryDir = localRepositoryDir;
this.executorService = executorService;
}
Expand Down Expand Up @@ -152,7 +160,8 @@ private ListenableFuture<Void> fetchToStagingRepository(String path) {

protected ListenableFuture<Void> createFetchToFileTask(URL remoteUrl, File tempFile) {
return Futures.submitAsync(
new FetchToFileTask(remoteUrl, tempFile, repositoryUserName, repositoryPassword),
new FetchToFileTask(
remoteUrl, tempFile, repositoryUserName, repositoryPassword, proxyHost, proxyPort),
this.executorService);
}

Expand All @@ -168,18 +177,34 @@ static class FetchToFileTask implements AsyncCallable<Void> {
private final File localFile;
private String repositoryUserName;
private String repositoryPassword;
private String proxyHost;
private int proxyPort;

public FetchToFileTask(
URL remoteURL, File localFile, String repositoryUserName, String repositoryPassword) {
URL remoteURL,
File localFile,
String repositoryUserName,
String repositoryPassword,
String proxyHost,
int proxyPort) {
this.remoteURL = remoteURL;
this.localFile = localFile;
this.repositoryUserName = repositoryUserName;
this.repositoryPassword = repositoryPassword;
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
}

@Override
public ListenableFuture<Void> call() throws Exception {
URLConnection connection = remoteURL.openConnection();
URLConnection connection;
if (this.proxyHost != null && !this.proxyHost.isEmpty() && this.proxyPort > 0) {
Proxy proxy =
new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.proxyHost, this.proxyPort));
connection = remoteURL.openConnection(proxy);
} else {
connection = remoteURL.openConnection();
}
// Add authorization header if applicable.
if (!Strings.isNullOrEmpty(this.repositoryUserName)) {
String encoded =
Expand Down
Expand Up @@ -44,18 +44,31 @@ public class MavenDependencyResolver implements DependencyResolver {
private final File localRepositoryDir;

public MavenDependencyResolver() {
this(MavenRoboSettings.getMavenRepositoryUrl(), MavenRoboSettings.getMavenRepositoryId(), MavenRoboSettings
.getMavenRepositoryUserName(), MavenRoboSettings.getMavenRepositoryPassword());
this(
MavenRoboSettings.getMavenRepositoryUrl(),
MavenRoboSettings.getMavenRepositoryId(),
MavenRoboSettings.getMavenRepositoryUserName(),
MavenRoboSettings.getMavenRepositoryPassword(),
MavenRoboSettings.getMavenProxyHost(),
MavenRoboSettings.getMavenProxyPort());
}

public MavenDependencyResolver(String repositoryUrl, String repositoryId, String repositoryUserName, String repositoryPassword) {
public MavenDependencyResolver(
String repositoryUrl,
String repositoryId,
String repositoryUserName,
String repositoryPassword,
String proxyHost,
int proxyPort) {
this.executorService = createExecutorService();
this.localRepositoryDir = getLocalRepositoryDir();
this.mavenArtifactFetcher =
createMavenFetcher(
repositoryUrl,
repositoryUserName,
repositoryPassword,
proxyHost,
proxyPort,
localRepositoryDir,
this.executorService);
}
Expand Down Expand Up @@ -163,10 +176,18 @@ protected MavenArtifactFetcher createMavenFetcher(
String repositoryUrl,
String repositoryUserName,
String repositoryPassword,
String proxyHost,
int proxyPort,
File localRepositoryDir,
ExecutorService executorService) {
return new MavenArtifactFetcher(
repositoryUrl, repositoryUserName, repositoryPassword, localRepositoryDir, executorService);
repositoryUrl,
repositoryUserName,
repositoryPassword,
proxyHost,
proxyPort,
localRepositoryDir,
executorService);
}

protected ExecutorService createExecutorService() {
Expand Down
Expand Up @@ -15,13 +15,17 @@ public class MavenRoboSettingsTest {
private String originalMavenRepositoryUrl;
private String originalMavenRepositoryUserName;
private String originalMavenRepositoryPassword;
private String originalMavenRepositoryProxyHost;
private int originalMavenProxyPort;

@Before
public void setUp() {
originalMavenRepositoryId = MavenRoboSettings.getMavenRepositoryId();
originalMavenRepositoryUrl = MavenRoboSettings.getMavenRepositoryUrl();
originalMavenRepositoryUserName = MavenRoboSettings.getMavenRepositoryUserName();
originalMavenRepositoryPassword = MavenRoboSettings.getMavenRepositoryPassword();
originalMavenRepositoryProxyHost = MavenRoboSettings.getMavenProxyHost();
originalMavenProxyPort = MavenRoboSettings.getMavenProxyPort();
}

@After
Expand All @@ -30,6 +34,8 @@ public void tearDown() {
MavenRoboSettings.setMavenRepositoryUrl(originalMavenRepositoryUrl);
MavenRoboSettings.setMavenRepositoryUserName(originalMavenRepositoryUserName);
MavenRoboSettings.setMavenRepositoryPassword(originalMavenRepositoryPassword);
MavenRoboSettings.setMavenProxyHost(originalMavenRepositoryProxyHost);
MavenRoboSettings.setMavenProxyPort(originalMavenProxyPort);
}

@Test
Expand Down Expand Up @@ -65,4 +71,16 @@ public void setMavenRepositoryPassword() {
MavenRoboSettings.setMavenRepositoryPassword("password");
assertEquals("password", MavenRoboSettings.getMavenRepositoryPassword());
}

@Test
public void setMavenProxyHost() {
MavenRoboSettings.setMavenProxyHost("123.4.5.678");
assertEquals("123.4.5.678", MavenRoboSettings.getMavenProxyHost());
}

@Test
public void setMavenProxyPort() {
MavenRoboSettings.setMavenProxyPort(9000);
assertEquals(9000, MavenRoboSettings.getMavenProxyPort());
}
}
Expand Up @@ -27,6 +27,8 @@ public class MavenDependencyResolverTest {
private static final String REPOSITORY_URL;
private static final String REPOSITORY_USERNAME = "username";
private static final String REPOSITORY_PASSWORD = "password";
private static final String PROXY_HOST = "123.4.5.678";
private static final int PROXY_PORT = 9000;
private static final HashFunction SHA512 = Hashing.sha512();

private static DependencyJar[] successCases =
Expand Down Expand Up @@ -65,6 +67,8 @@ public void setUp() throws Exception {
REPOSITORY_URL,
REPOSITORY_USERNAME,
REPOSITORY_PASSWORD,
PROXY_HOST,
PROXY_PORT,
localRepositoryDir,
executorService);
mavenDependencyResolver = new TestMavenDependencyResolver();
Expand Down Expand Up @@ -167,6 +171,8 @@ protected MavenArtifactFetcher createMavenFetcher(
String repositoryUrl,
String repositoryUserName,
String repositoryPassword,
String proxyHost,
int proxyPort,
File localRepositoryDir,
ExecutorService executorService) {
return mavenArtifactFetcher;
Expand Down Expand Up @@ -200,12 +206,16 @@ public TestMavenArtifactFetcher(
String repositoryUrl,
String repositoryUserName,
String repositoryPassword,
String proxyHost,
int proxyPort,
File localRepositoryDir,
ExecutorService executorService) {
super(
repositoryUrl,
repositoryUserName,
repositoryPassword,
proxyHost,
proxyPort,
localRepositoryDir,
executorService);
this.executorService = executorService;
Expand All @@ -214,7 +224,7 @@ public TestMavenArtifactFetcher(
@Override
protected ListenableFuture<Void> createFetchToFileTask(URL remoteUrl, File tempFile) {
return Futures.submitAsync(
new FetchToFileTask(remoteUrl, tempFile, null, null) {
new FetchToFileTask(remoteUrl, tempFile, null, null, null, 0) {
@Override
public ListenableFuture<Void> call() throws Exception {
numRequests += 1;
Expand Down

0 comments on commit bed3ca5

Please sign in to comment.