Skip to content

Commit fb5b6ad

Browse files
committed
Working on #353
1 parent f604566 commit fb5b6ad

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

moneta-convert/moneta-convert-imf/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
<artifactId>moneta-convert</artifactId>
3434
<version>${project.version}</version>
3535
</dependency>
36+
<dependency>
37+
<groupId>com.squareup.okhttp3</groupId>
38+
<artifactId>okhttp</artifactId>
39+
<version>4.12.0</version>
40+
</dependency>
3641
</dependencies>
3742
<name>Moneta Currency Conversion - IMF Provider</name>
3843

moneta-convert/moneta-convert-imf/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
requires org.javamoney.moneta.convert;
2121
requires static osgi.core;
2222
requires static osgi.annotation;
23+
requires okhttp3;
2324
provides javax.money.convert.ExchangeRateProvider with
2425
org.javamoney.moneta.convert.imf.IMFRateProvider, org.javamoney.moneta.convert.imf.IMFHistoricRateProvider;
2526
uses LoaderService;

moneta-convert/moneta-convert-imf/src/main/java/org/javamoney/moneta/convert/imf/IMFRemoteSearchCallable.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
import java.io.ByteArrayInputStream;
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.InputStream;
21-
import java.net.URL;
22-
import java.net.URLConnection;
2321
import java.time.YearMonth;
2422
import java.util.Objects;
2523
import java.util.concurrent.Callable;
2624
import java.util.logging.Level;
2725
import java.util.logging.Logger;
2826

27+
import okhttp3.Call;
28+
import okhttp3.OkHttpClient;
29+
import okhttp3.Request;
2930
import org.javamoney.moneta.convert.imf.IMFRemoteSearchCallable.IMFRemoteSearchResult;
3031

31-
3232
class 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

moneta-convert/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
<modules>
1616
<module>moneta-convert-base</module>
1717
<module>moneta-convert-ecb</module>
18-
<!-- FIXME this is only temporary to isolate ECB from IMF issues
1918
<module>moneta-convert-imf</module>
20-
-->
2119
</modules>
2220

2321
<build>

0 commit comments

Comments
 (0)