33import com .fasterxml .uuid .Generators ;
44import com .fasterxml .uuid .impl .RandomBasedGenerator ;
55import jakarta .inject .Singleton ;
6+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
7+ import org .apache .hc .client5 .http .config .RequestConfig ;
8+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
9+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
10+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
11+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
12+ import org .apache .hc .core5 .http .HttpStatus ;
13+ import org .apache .hc .core5 .util .TimeValue ;
14+ import org .apache .hc .core5 .util .Timeout ;
615import org .slf4j .Logger ;
716import org .slf4j .LoggerFactory ;
8- import org .apache .http .HttpStatus ;
9- import org .apache .http .client .config .RequestConfig ;
10- import org .apache .http .client .methods .CloseableHttpResponse ;
11- import org .apache .http .client .methods .HttpGet ;
12- import org .apache .http .impl .client .CloseableHttpClient ;
13- import org .apache .http .impl .client .HttpClients ;
14- import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
1517
1618import java .io .IOException ;
1719import java .io .InputStream ;
1820import java .nio .file .Files ;
1921import java .nio .file .Path ;
2022import java .nio .file .StandardCopyOption ;
2123import java .util .UUID ;
22- import java .util .concurrent .TimeUnit ;
2324
2425@ Singleton
2526public class FileDownloadService {
@@ -37,13 +38,13 @@ public FileDownloadService() {
3738 this .poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager ();
3839 this .poolingHttpClientConnectionManager .setMaxTotal (20 );
3940 this .poolingHttpClientConnectionManager .setDefaultMaxPerRoute (15 );
40- this .poolingHttpClientConnectionManager .setValidateAfterInactivity (15_000 );
41+ this .poolingHttpClientConnectionManager .setValidateAfterInactivity (TimeValue . ofMilliseconds ( 15_000 ) );
4142
4243 this .httpRequestConfig = RequestConfig .custom ()
43- .setConnectTimeout (10_000 )
44- . setSocketTimeout ( 10_000 )
45- .setConnectionRequestTimeout (3_000 )
46- .build ();
44+ .setConnectTimeout (Timeout . ofMilliseconds ( 10_000 ) )
45+ . setResponseTimeout ( Timeout . ofMilliseconds ( 10_000 ) )
46+ .setConnectionRequestTimeout (Timeout . ofMilliseconds ( 3_000 ) )
47+ .build ();
4748
4849 try {
4950 this .sharedTempDir = Files .createTempDirectory (TEMP_DIR_NAME );
@@ -57,7 +58,7 @@ private CloseableHttpClient buildHttpClient() {
5758 .custom ()
5859 .setConnectionManager (poolingHttpClientConnectionManager )
5960 .setDefaultRequestConfig (httpRequestConfig )
60- .evictIdleConnections (30 , TimeUnit . SECONDS )
61+ .evictIdleConnections (TimeValue . ofSeconds ( 30 ) )
6162 .evictExpiredConnections ()
6263 .build ();
6364 }
@@ -77,8 +78,9 @@ public Path downloadToTemp(final String url) throws IOException {
7778 final HttpGet httpGet = new HttpGet (url );
7879
7980 try (final CloseableHttpResponse response = buildHttpClient ().execute (httpGet )) {
80-
81- final int statusCode = response .getStatusLine ().getStatusCode ();
81+
82+ final int statusCode = response .getCode ();
83+
8284 if (statusCode != HttpStatus .SC_OK ) {
8385 throw new IOException ("Non Resolvable url: " + url );
8486 }
@@ -92,6 +94,8 @@ public Path downloadToTemp(final String url) throws IOException {
9294 return tempFile ;
9395
9496 } catch (final IllegalArgumentException e ) {
97+ // Note: HttpGet in v5 might throw different runtime exceptions for bad URIs,
98+ // but catching IllegalArgumentException covers the general URI format errors.
9599 throw new IOException ("Invalid URL format: " + url , e );
96100 }
97101 }
@@ -122,4 +126,4 @@ private String generateUuidFilename() {
122126 final UUID uuid = generator .generate ();
123127 return uuid .toString () + ".csv" ;
124128 }
125- }
129+ }
0 commit comments