55import jakarta .inject .Singleton ;
66import org .slf4j .Logger ;
77import 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 ;
815
916import java .io .IOException ;
1017import java .io .InputStream ;
11- import java .net .URI ;
12- import java .net .http .HttpClient ;
13- import java .net .http .HttpRequest ;
14- import java .net .http .HttpResponse ;
1518import java .nio .file .Files ;
1619import java .nio .file .Path ;
1720import java .nio .file .StandardCopyOption ;
1821import java .util .UUID ;
22+ import java .util .concurrent .TimeUnit ;
1923
2024@ Singleton
2125public class FileDownloadService {
2226
2327 private static final Logger LOG = LoggerFactory .getLogger (FileDownloadService .class );
2428 private static final String TEMP_DIR_NAME = "bbl-validator" ;
25- private final HttpClient httpClient ;
29+
30+ private final PoolingHttpClientConnectionManager poolingHttpClientConnectionManager ;
31+ private final RequestConfig httpRequestConfig ;
32+ private final CloseableHttpClient httpClient ;
33+
2634 private final Path sharedTempDir ;
2735 private final RandomBasedGenerator generator = Generators .randomBasedGenerator ();
2836
2937 public FileDownloadService () {
30- this .httpClient = HttpClient .newBuilder ()
31- .followRedirects (HttpClient .Redirect .NORMAL )
38+ this .poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager ();
39+
40+ this .httpRequestConfig = RequestConfig .custom ()
41+ .setConnectTimeout (10_000 )
42+ .setSocketTimeout (10_000 )
43+ .setConnectionRequestTimeout (3_000 )
44+ .build ();
45+
46+ this .httpClient = HttpClients
47+ .custom ()
48+ .setConnectionManager (poolingHttpClientConnectionManager )
49+ .setDefaultRequestConfig (httpRequestConfig )
50+ .evictIdleConnections (30 , TimeUnit .SECONDS )
51+ .evictExpiredConnections ()
3252 .build ();
53+
3354 try {
3455 this .sharedTempDir = Files .createTempDirectory (TEMP_DIR_NAME );
3556 } catch (final IOException e ) {
@@ -49,28 +70,25 @@ public Path downloadToTemp(final String url) throws IOException {
4970 final String filename = generateUuidFilename ();
5071 final Path tempFile = sharedTempDir .resolve (filename );
5172
52- final HttpRequest request = HttpRequest .newBuilder ()
53- .uri (URI .create (url ))
54- .GET ()
55- .build ();
73+ final HttpGet httpGet = new HttpGet (url );
5674
57- final HttpResponse <InputStream > response = httpClient .send (
58- request ,
59- HttpResponse .BodyHandlers .ofInputStream ()
60- );
75+ try (CloseableHttpResponse response = httpClient .execute (httpGet )) {
76+
77+ int statusCode = response .getStatusLine ().getStatusCode ();
78+ if (statusCode != HttpStatus .SC_OK ) {
79+ throw new IOException ("Non Resolvable url: " + url );
80+ }
6181
62- try (InputStream inputStream = response .body ()) {
63- Files .copy (inputStream , tempFile , StandardCopyOption .REPLACE_EXISTING );
82+ try (InputStream inputStream = response .getEntity ().getContent ()) {
83+ Files .copy (inputStream , tempFile , StandardCopyOption .REPLACE_EXISTING );
84+ }
6485 }
6586
6687 LOG .trace ("Downloaded file from {} to {}" , url , tempFile );
6788 return tempFile ;
6889
6990 } catch (final IllegalArgumentException e ) {
7091 throw new IOException ("Invalid URL format: " + url , e );
71- } catch (final InterruptedException e ) {
72- Thread .currentThread ().interrupt ();
73- throw new IOException ("Download interrupted for URL: " + url , e );
7492 }
7593 }
7694
0 commit comments