Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

feat: support downloading an emulator from an access controlled URL #513

Merged
merged 2 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
Expand Down Expand Up @@ -316,6 +317,7 @@ protected static class DownloadableEmulatorRunner implements EmulatorRunner {
private final String md5CheckSum;
private final URL downloadUrl;
private final String fileName;
private String accessToken;
private Process process;
private static final Logger log = Logger.getLogger(DownloadableEmulatorRunner.class.getName());

Expand All @@ -328,6 +330,12 @@ public DownloadableEmulatorRunner(
this.fileName = splitUrl[splitUrl.length - 1];
}

public DownloadableEmulatorRunner(
List<String> commandText, URL downloadUrl, String md5CheckSum, String accessToken) {
this(commandText, downloadUrl, md5CheckSum);
this.accessToken = accessToken;
}

@Override
public boolean isAvailable() {
try {
Expand Down Expand Up @@ -420,7 +428,11 @@ private File downloadZipFile() throws IOException {
if (log.isLoggable(Level.FINE)) {
log.fine("Fetching emulator");
}
ReadableByteChannel rbc = Channels.newChannel(downloadUrl.openStream());
URLConnection urlConnection = downloadUrl.openConnection();
if (accessToken != null) {
urlConnection.setRequestProperty("Authorization", "Bearer " + accessToken);
}
ReadableByteChannel rbc = Channels.newChannel(urlConnection.getInputStream());
try (FileOutputStream fos = new FileOutputStream(zipFile)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void testEmulatorHelperDownloadWithRetries()
String mockInputStream = "mockInputStream";
String mockProtocol = "mockProtocol";
String mockFile = "mockFile";
String mockAccessToken = "mockAccessToken";
String mockCommandText = "mockCommandText";

MockURLStreamHandler mockURLStreamHandler = EasyMock.createMock(MockURLStreamHandler.class);
Expand All @@ -119,6 +120,7 @@ public void testEmulatorHelperDownloadWithRetries()
EasyMock.expect(mockURLConnection.getInputStream())
.andReturn(new ByteArrayInputStream(mockInputStream.getBytes()))
.anyTimes();
mockURLConnection.setRequestProperty("Authorization", "Bearer " + mockAccessToken);
EasyMock.expect(mockURLStreamHandler.openConnection(EasyMock.anyObject(URL.class)))
.andThrow(new EOFException())
.times(1);
Expand All @@ -130,7 +132,7 @@ public void testEmulatorHelperDownloadWithRetries()
URL url = new URL(mockProtocol, null, 0, mockFile, mockURLStreamHandler);
BaseEmulatorHelper.DownloadableEmulatorRunner runner =
new BaseEmulatorHelper.DownloadableEmulatorRunner(
ImmutableList.of(mockCommandText), url, null);
ImmutableList.of(mockCommandText), url, null, mockAccessToken);

File cachedFile = new File(System.getProperty("java.io.tmpdir"), mockExternalForm);
cachedFile.delete(); // Clear the cached version so we're always testing the download
Expand Down