@@ -37,10 +37,8 @@ public class HttpFileDownloader {
3737 private final Duration requestTimeout ;
3838
3939 public HttpFileDownloader () {
40- this (DEFAULT_CONNECT_TIMEOUT , DEFAULT_REQUEST_TIMEOUT , (request , target , resolvedConnectTimeout ) -> HttpClient .newBuilder ()
41- .connectTimeout (resolvedConnectTimeout )
42- .build ()
43- .send (request , HttpResponse .BodyHandlers .ofFile (target )));
40+ this (DEFAULT_CONNECT_TIMEOUT , DEFAULT_REQUEST_TIMEOUT , (request , target , resolvedConnectTimeout ) ->
41+ createHttpClient (resolvedConnectTimeout ).send (request , HttpResponse .BodyHandlers .ofFile (target )));
4442 }
4543
4644 HttpFileDownloader (Duration connectTimeout , Duration requestTimeout , DownloadExecutor downloadExecutor ) {
@@ -69,15 +67,9 @@ public Path downloadTo(Path workspace, URI sourceUri, String targetFileName, Str
6967 .header ("User-Agent" , userAgent )
7068 .GET ()
7169 .build ();
70+ HttpResponse <Path > response ;
7271 try {
73- HttpResponse <Path > response = downloadExecutor .download (request , targetFile , connectTimeout );
74- if (response .statusCode () < 200 || response .statusCode () >= 300 ) {
75- deletePartialFile (targetFile );
76- throw new IOException ("Download failed while fetching " + downloadLabel
77- + " with HTTP " + response .statusCode () + " from " + sourceUri
78- + ". Check your network connection and retry." );
79- }
80- return targetFile ;
72+ response = downloadExecutor .download (request , targetFile , connectTimeout );
8173 } catch (IOException e ) {
8274 deletePartialFile (targetFile );
8375 throw new IOException ("Download failed while fetching " + downloadLabel
@@ -88,6 +80,21 @@ public Path downloadTo(Path workspace, URI sourceUri, String targetFileName, Str
8880 throw new IOException ("Download interrupted while fetching " + downloadLabel
8981 + ". Retry the command once the interruption is cleared." , e );
9082 }
83+
84+ if (response .statusCode () < 200 || response .statusCode () >= 300 ) {
85+ deletePartialFile (targetFile );
86+ throw new IOException ("Download failed while fetching " + downloadLabel
87+ + " with HTTP " + response .statusCode () + " from " + sourceUri
88+ + ". Check your network connection and retry." );
89+ }
90+ return targetFile ;
91+ }
92+
93+ static HttpClient createHttpClient (Duration connectTimeout ) {
94+ return HttpClient .newBuilder ()
95+ .connectTimeout (connectTimeout )
96+ .followRedirects (HttpClient .Redirect .NORMAL )
97+ .build ();
9198 }
9299
93100 private void deletePartialFile (Path targetFile ) throws IOException {
0 commit comments