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

Commit

Permalink
feat: support downloading an emulator from an access controlled URL
Browse files Browse the repository at this point in the history
fixes googleapis/java-datastore#376
used in < googleapis/java-datastore#PR>
  • Loading branch information
aristide-n committed Aug 9, 2021
1 parent eeee5fa commit 1cb4002
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit 1cb4002

Please sign in to comment.