Skip to content
Permalink
Browse files
feat: support downloading an emulator from an access controlled URL (#…
…513)

* feat: support downloading an emulator from an access controlled URL

fixes googleapis/java-datastore#376
used in googleapis/java-datastore#492

* fix format
  • Loading branch information
aristide-n committed Aug 16, 2021
1 parent 4edf733 commit f42a7078522461018da96196a67657809d28b15b
@@ -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;
@@ -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());

@@ -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 {
@@ -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);
}
@@ -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);
@@ -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);
@@ -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

0 comments on commit f42a707

Please sign in to comment.