Skip to content

Commit 9abe6b6

Browse files
committed
Working on #353
1 parent c890e32 commit 9abe6b6

File tree

10 files changed

+64
-67
lines changed

10 files changed

+64
-67
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.javamoney.moneta.spi.loader.LoaderService;
22

33
/*
4-
Copyright (c) 2012, 2023, Werner Keil and others by the @author tag.
4+
Copyright (c) 2012, 2024, Werner Keil and others by the @author tag.
55
66
Licensed under the Apache License, Version 2.0 (the "License"); you may not
77
use this file except in compliance with the License. You may obtain a copy of

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ abstract class IMFAbstractRateProvider extends AbstractRateProvider implements L
6363
CurrencyUnitBuilder.of("SDR", CurrencyContextBuilder.of(IMFRateProvider.class.getSimpleName()).build())
6464
.setDefaultFractionDigits(3).build(true);
6565

66-
protected Map<CurrencyUnit, List<ExchangeRate>> currencyToSdr = Collections.emptyMap();
66+
protected Map<CurrencyUnit, List<ExchangeRate>> currencyToSdr = new HashMap<>();
6767

68-
protected Map<CurrencyUnit, List<ExchangeRate>> sdrToCurrency = Collections.emptyMap();
68+
protected Map<CurrencyUnit, List<ExchangeRate>> sdrToCurrency = new HashMap<>();
6969

7070
protected volatile String loadState;
7171

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ public class IMFHistoricRateProvider extends IMFAbstractRateProvider {
5151
private static final String DATA_ID = IMFHistoricRateProvider.class.getSimpleName();
5252

5353
private static final ProviderContext CONTEXT = ProviderContextBuilder.of("IMF-HIST", RateType.HISTORIC)
54-
.set("providerDescription", "Historic International Monetary Fund")
54+
.set("providerDescription", "Historic International Monetary Fund")
5555
.set("days", 0)
56-
.set("User-Agent", "Chrome/51.0.2704.103")
56+
//.set("User-Agent", "Chrome/51.0.2704.103") This was a URLConnection hack, OkHttp works without the User-Agent
5757
.build();
5858

59-
6059
private final List<YearMonth> cachedHistoric = new ArrayList<>();
6160
public IMFHistoricRateProvider() {
6261
super(CONTEXT);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class IMFRateProvider extends IMFAbstractRateProvider {
5151
private static final ProviderContext CONTEXT = ProviderContextBuilder.of("IMF", RateType.DEFERRED)
5252
.set("providerDescription", "International Monetary Fund")
5353
.set("days", 1)
54-
.set("User-Agent", "Chrome/51.0.2704.103")
54+
//.set("User-Agent", "Chrome/51.0.2704.103")
5555
.build();
5656

5757
public IMFRateProvider() {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class IMFRemoteSearchCallable implements Callable<IMFRemoteSearchResult>{
4646

4747
@Override
4848
public IMFRemoteSearchResult call() throws Exception {
49-
//connection.addRequestProperty("User-Agent", userAgent);
50-
// TODO apply userAgent where applicable
5149
final OkHttpClient client = new OkHttpClient.Builder()
5250
.build();
5351

@@ -83,7 +81,7 @@ public String toString() {
8381
" type: " + type + ", yearMonth: " + yearMonth + '}';
8482
}
8583

86-
class IMFRemoteSearchResult {
84+
static class IMFRemoteSearchResult {
8785

8886
private final IMFHistoricalType type;
8987

moneta-core/src/main/java/org/javamoney/moneta/spi/loader/okhttp/LoadableHttpResource.java

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
import org.javamoney.moneta.spi.loader.LoaderService;
2121
import org.javamoney.moneta.spi.loader.ResourceCache;
2222

23-
import java.io.ByteArrayInputStream;
24-
import java.io.ByteArrayOutputStream;
25-
import java.io.IOException;
26-
import java.io.InputStream;
23+
import java.io.*;
2724
import java.lang.ref.SoftReference;
2825
import java.net.InetSocketAddress;
2926
import java.net.Proxy;
3027
import java.net.URI;
28+
import java.net.URL;
3129
import java.util.*;
3230
import java.util.concurrent.TimeUnit;
3331
import java.util.concurrent.atomic.AtomicInteger;
@@ -314,63 +312,65 @@ protected boolean load(URI itemToLoad, boolean fallbackLoad) {
314312
ByteArrayOutputStream stream = new ByteArrayOutputStream();
315313

316314
try {
317-
OkHttpClient.Builder builder = new OkHttpClient.Builder();
318-
319-
String proxyPort = this.properties.get("proxy.port");
320-
String proxyHost = this.properties.get("proxy.host");
321-
String proxyType = this.properties.get("proxy.type");
322-
if(proxyPort != null && proxyHost != null) {
323-
if (proxyType == null) {
324-
proxyType = Proxy.Type.HTTP.name();
315+
if (fallbackLoad) {
316+
is = new BufferedInputStream(itemToLoad.toURL().openStream());
317+
} else {
318+
OkHttpClient.Builder builder = new OkHttpClient.Builder();
319+
320+
String proxyPort = this.properties.get("proxy.port");
321+
String proxyHost = this.properties.get("proxy.host");
322+
String proxyType = this.properties.get("proxy.type");
323+
if (proxyPort != null && proxyHost != null) {
324+
if (proxyType == null) {
325+
proxyType = Proxy.Type.HTTP.name();
326+
}
327+
Proxy proxy = new Proxy(Proxy.Type.valueOf(proxyType.toUpperCase()),
328+
InetSocketAddress.createUnresolved(proxyHost, Integer.parseInt(proxyPort)));
329+
builder = builder.proxy(proxy);
325330
}
326-
Proxy proxy = new Proxy(Proxy.Type.valueOf(proxyType.toUpperCase()),
327-
InetSocketAddress.createUnresolved(proxyHost, Integer.parseInt(proxyPort)));
328-
builder = builder.proxy(proxy);
329-
}
330331

331-
String connectTimeout = this.properties.get("connection.connect.timeout");
332-
if(connectTimeout != null) {
333-
int seconds = Integer.parseInt(connectTimeout);
334-
builder = builder.connectTimeout(seconds, TimeUnit.SECONDS);
332+
String connectTimeout = this.properties.get("connection.connect.timeout");
333+
if (connectTimeout != null) {
334+
int seconds = Integer.parseInt(connectTimeout);
335+
builder = builder.connectTimeout(seconds, TimeUnit.SECONDS);
335336
// }else{
336337
// conn.setConnectTimeout(10000);
337-
}
338-
final String readTimeout = this.properties.get("connection.read.timeout");
339-
if(readTimeout != null) {
340-
int seconds = Integer.parseInt(readTimeout);
341-
builder = builder.readTimeout(seconds, TimeUnit.SECONDS);
342-
}
343-
// else{
344-
// conn.setReadTimeout(10000);
345-
// }
346-
final String writeTimeout = this.properties.get("connection.write.timeout");
347-
if(writeTimeout != null) {
348-
int seconds = Integer.parseInt(writeTimeout);
349-
builder = builder.readTimeout(seconds, TimeUnit.SECONDS);
350-
}
338+
}
339+
final String readTimeout = this.properties.get("connection.read.timeout");
340+
if (readTimeout != null) {
341+
int seconds = Integer.parseInt(readTimeout);
342+
builder = builder.readTimeout(seconds, TimeUnit.SECONDS);
343+
}
351344

352-
final OkHttpClient client = builder.build();
345+
final String writeTimeout = this.properties.get("connection.write.timeout");
346+
if (writeTimeout != null) {
347+
int seconds = Integer.parseInt(writeTimeout);
348+
builder = builder.readTimeout(seconds, TimeUnit.SECONDS);
349+
}
353350

354-
Request.Builder requestBuilder = new Request.Builder();
355-
final String userAgent = this.properties.get("useragent");
356-
if(userAgent != null) {
357-
requestBuilder = requestBuilder.header("User-Agent", userAgent);
358-
}
351+
final OkHttpClient client = builder.build();
352+
353+
Request.Builder requestBuilder = new Request.Builder();
354+
// final String userAgent = this.properties.get("useragent");
355+
// if (userAgent != null) {
356+
// requestBuilder = requestBuilder.header("User-Agent", userAgent);
357+
// }
358+
359+
final Request request = requestBuilder
360+
.url(itemToLoad.toString())
361+
.build();
359362

360-
final Request request = requestBuilder
361-
.url(itemToLoad.toString())
362-
.build();
363-
364363
// conn.setRequestProperty("Accept", "application/xhtml+xml");
365364
// conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
366365
// conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
367366
// TODO check if any of those are necessary?
368367

369-
final Call call = client.newCall(request);
368+
final Call call = client.newCall(request);
369+
//is = conn.getInputStream();
370+
is = call.execute().body().byteStream();
371+
}
370372

371373
byte[] data = new byte[4096];
372-
//is = conn.getInputStream();
373-
is = call.execute().body().byteStream();
374374
int read = is.read(data);
375375
while (read > 0) {
376376
stream.write(data, 0, read);

moneta-core/src/test/java/org/javamoney/moneta/spi/loader/LoadDataInformationBuilderTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.net.URI;
1919
import java.net.URISyntaxException;
20-
import java.util.Collections;
2120
import java.util.HashMap;
2221
import java.util.Map;
2322

@@ -33,7 +32,7 @@ public class LoadDataInformationBuilderTest {
3332
public void shouldCreateLoadDataInformation() throws URISyntaxException {
3433
String resourceId = "resourceId";
3534
UpdatePolicy updatePolicy = UpdatePolicy.LAZY;
36-
Map<String, String> properties = Collections.emptyMap();
35+
Map<String, String> properties = new HashMap<>();
3736
LoaderService.Listener listener = (id, is) -> {};
3837
URI backupResource = new URI("localhost");
3938
URI[] resourceLocations = new URI[]{new URI("localhost")};
@@ -83,7 +82,7 @@ public void shouldCreateLoadDataInformationWithOutListener() throws URISyntaxExc
8382
public void shouldCreateLoadDataInformationWithOutBackupResource() throws URISyntaxException {
8483
String resourceId = "resourceId";
8584
UpdatePolicy updatePolicy = UpdatePolicy.LAZY;
86-
Map<String, String> properties = Collections.emptyMap();
85+
Map<String, String> properties = new HashMap<>();
8786
LoaderService.Listener listener = (id, is) -> {};
8887
URI[] resourceLocations = new URI[]{new URI("localhost")};
8988

@@ -107,7 +106,7 @@ public void shouldCreateLoadDataInformationWithOutBackupResource() throws URISyn
107106
@Test(expectedExceptions = IllegalStateException.class)
108107
public void shouldReturnsErrorWhenResourceIdWasNotInformed() throws URISyntaxException {
109108
UpdatePolicy updatePolicy = UpdatePolicy.LAZY;
110-
Map<String, String> properties = Collections.emptyMap();
109+
Map<String, String> properties = new HashMap<>();
111110
LoaderService.Listener listener = (id, is) -> {};
112111
URI[] resourceLocations = new URI[]{new URI("localhost")};
113112

@@ -123,7 +122,7 @@ public void shouldReturnsErrorWhenResourceIdWasNotInformed() throws URISyntaxExc
123122
@Test(expectedExceptions = IllegalStateException.class)
124123
public void shouldReturnsErrorWhenUpdatePolicyWasNotInformed() throws URISyntaxException {
125124
String resourceId = "resourceId";
126-
Map<String, String> properties = Collections.emptyMap();
125+
Map<String, String> properties = new HashMap<>();
127126
LoaderService.Listener listener = (id, is) -> {};
128127
URI[] resourceLocations = new URI[]{new URI("localhost")};
129128

@@ -156,7 +155,7 @@ public void shouldReturnsErrorWhenPropertiesWasNotInformed() throws URISyntaxExc
156155
public void shouldReturnsErrorWhenResourceLocationsWasNotInformed() throws URISyntaxException {
157156
String resourceId = "resourceId";
158157
UpdatePolicy updatePolicy = UpdatePolicy.LAZY;
159-
Map<String, String> properties = Collections.emptyMap();
158+
Map<String, String> properties = new HashMap<>();
160159
LoaderService.Listener listener = (id, is) -> {};
161160

162161
new LoadDataInformationBuilder()

moneta-core/src/test/java/org/javamoney/moneta/spi/loader/okhttp/LoadableHttpResourceBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import java.net.URI;
2626
import java.net.URISyntaxException;
27-
import java.util.Collections;
27+
import java.util.HashMap;
2828
import java.util.Map;
2929

3030
import static org.testng.Assert.assertNotNull;
@@ -37,7 +37,7 @@ public class LoadableHttpResourceBuilderTest {
3737
public void setup() throws URISyntaxException {
3838
String resourceId = "resourceId";
3939
UpdatePolicy updatePolicy = UpdatePolicy.LAZY;
40-
Map<String, String> properties = Collections.emptyMap();
40+
Map<String, String> properties = new HashMap<>();
4141
Listener listener = (id, is) -> {};
4242
URI backupResource = new URI("localhost");
4343
URI[] resourceLocations = new URI[]{new URI("localhost")};

moneta-core/src/test/java/org/javamoney/moneta/spi/loader/urlconnection/LoadableURLResourceBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import java.net.URI;
2121
import java.net.URISyntaxException;
22-
import java.util.Collections;
22+
import java.util.HashMap;
2323
import java.util.Map;
2424

2525
import org.javamoney.moneta.spi.loader.LoadDataInformation;
@@ -37,7 +37,7 @@ public class LoadableURLResourceBuilderTest {
3737
public void setup() throws URISyntaxException {
3838
String resourceId = "resourceId";
3939
UpdatePolicy updatePolicy = UpdatePolicy.LAZY;
40-
Map<String, String> properties = Collections.emptyMap();
40+
Map<String, String> properties = new HashMap<>();
4141
Listener listener = (id, is) -> {};
4242
URI backupResource = new URI("localhost");
4343
URI[] resourceLocations = new URI[]{new URI("localhost")};

src/etc/JVM_options.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.regex=ALL-UNNAMED

0 commit comments

Comments
 (0)