1818import java .io .ByteArrayInputStream ;
1919import java .io .ByteArrayOutputStream ;
2020import java .io .InputStream ;
21- import java .net .URL ;
22- import java .net .URLConnection ;
2321import java .time .YearMonth ;
2422import java .util .Objects ;
2523import java .util .concurrent .Callable ;
2624import java .util .logging .Level ;
2725import java .util .logging .Logger ;
2826
27+ import okhttp3 .Call ;
28+ import okhttp3 .OkHttpClient ;
29+ import okhttp3 .Request ;
2930import org .javamoney .moneta .convert .imf .IMFRemoteSearchCallable .IMFRemoteSearchResult ;
3031
31-
3232class IMFRemoteSearchCallable implements Callable <IMFRemoteSearchResult >{
3333
3434 private static final Logger LOG = Logger .getLogger (IMFRemoteSearchCallable .class .getName ());
@@ -46,13 +46,19 @@ class IMFRemoteSearchCallable implements Callable<IMFRemoteSearchResult>{
4646
4747 @ Override
4848 public IMFRemoteSearchResult call () throws Exception {
49+ //connection.addRequestProperty("User-Agent", userAgent);
4950
50- URLConnection connection = getConnection ();
51- if (Objects .isNull (connection )) {
52- return null ;
53- }
54- connection .addRequestProperty ("User-Agent" , userAgent );
55- try (InputStream inputStream = connection .getInputStream (); ByteArrayOutputStream stream = new ByteArrayOutputStream ()) {
51+ OkHttpClient client = new OkHttpClient .Builder ()
52+ .build ();
53+
54+ Request request = new Request .Builder ()
55+ .url (getUrl ())
56+ .build ();
57+
58+ Call call = client .newCall (request );
59+
60+ try (InputStream inputStream = call .execute ().body ().byteStream ();
61+ ByteArrayOutputStream stream = new ByteArrayOutputStream ()) {
5662 byte [] data = new byte [4096 ];
5763 int read = inputStream .read (data );
5864 while (read > 0 ) {
@@ -61,20 +67,14 @@ public IMFRemoteSearchResult call() throws Exception {
6167 }
6268 return new IMFRemoteSearchResult (type , new ByteArrayInputStream (stream .toByteArray ()));
6369 } catch (Exception e ) {
64- LOG .log (Level .WARNING , "Failed to load resource from url " + type . getUrl (yearMonth ), e );
70+ LOG .log (Level .WARNING , "Failed to load resource from url " + getUrl (), e );
6571 }
6672 return null ;
6773 }
6874
6975
70- private URLConnection getConnection () {
71- try {
72- return new URL (type .getUrl (yearMonth )).openConnection ();
73- } catch (Exception e ) {
74- LOG .log (Level .WARNING , "Failed to load resource from url "
75- + type .getUrl (yearMonth ), e );
76- }
77- return null ;
76+ private String getUrl () {
77+ return type .getUrl (yearMonth );
7878 }
7979
8080 @ Override
0 commit comments