@@ -47,12 +47,23 @@ class LocalBinary {
47
47
LocalBinary (String path , String key ) throws LocalException {
48
48
this .key = key ;
49
49
initialize ();
50
- if (path != "" ) {
51
- getBinaryOnPath (path );
52
- } else {
53
- getBinary ();
50
+ downloadAndVerifyBinary (path );
51
+ }
52
+
53
+ private void downloadAndVerifyBinary (String path ) throws LocalException {
54
+ try {
55
+ if (path != "" ) {
56
+ getBinaryOnPath (path );
57
+ } else {
58
+ getBinary ();
59
+ }
60
+ checkBinary ();
61
+ } catch (Throwable e ) {
62
+ if (fallbackEnabled ) throw e ;
63
+ fallbackEnabled = true ;
64
+ downloadFailureThrowable = e ;
65
+ downloadAndVerifyBinary (path );
54
66
}
55
- checkBinary ();
56
67
}
57
68
58
69
private void initialize () throws LocalException {
@@ -181,6 +192,11 @@ private boolean makePath(String path) {
181
192
}
182
193
183
194
private void fetchSourceUrl () {
195
+ if ((!fallbackEnabled && sourceUrl ) || (fallbackEnabled && !downloadFailureThrowable )) {
196
+ /* Retry because binary (from any of the endpoints) validation failed */
197
+ return ;
198
+ }
199
+
184
200
URL url = new URL ("https://local.browserstack.com/binary/api/v1/endpoint" );
185
201
URLConnection connection = url .openConnection ();
186
202
@@ -207,6 +223,7 @@ private void fetchSourceUrl() {
207
223
String responseBody = response .toString ();
208
224
JSONObject json = new JSONObject (responseBody );
209
225
this .sourceUrl = json .getJSONObject ("data" ).getString ("endpoint" );
226
+ if (fallbackEnabled ) downloadFailureThrowable = null ;
210
227
}
211
228
}
212
229
@@ -227,15 +244,8 @@ private void downloadBinary(String destParentDir, Boolean custom) throws LocalEx
227
244
URL url = new URL (sourceUrl + '/' + binaryFileName );
228
245
229
246
File f = new File (source );
230
- try {
231
- newCopyToFile (url , f );
232
- } catch (IOException e ) {
233
- if (fallbackEnabled ) throw e ;
234
- /* Binary download failed due to a server error */
235
- fallbackEnabled = true ;
236
- downloadFailureThrowable = e ;
237
- downloadBinary (destParentDir , custom );
238
- }
247
+ newCopyToFile (url , f );
248
+
239
249
changePermissions (binaryPath );
240
250
} catch (Throwable e ) {
241
251
throw new LocalException ("Error trying to download BrowserStackLocal binary: " + e .getMessage ());
0 commit comments