Skip to content

Commit 0a3fb69

Browse files
committed
Working on #353
1 parent 1888429 commit 0a3fb69

File tree

5 files changed

+73
-50
lines changed

5 files changed

+73
-50
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ class IMFRemoteSearchCallable implements Callable<IMFRemoteSearchResult>{
4747
@Override
4848
public IMFRemoteSearchResult call() throws Exception {
4949
//connection.addRequestProperty("User-Agent", userAgent);
50-
51-
OkHttpClient client = new OkHttpClient.Builder()
50+
// TODO apply userAgent where applicable
51+
final OkHttpClient client = new OkHttpClient.Builder()
5252
.build();
5353

54-
Request request = new Request.Builder()
54+
final Request request = new Request.Builder()
5555
.url(getUrl())
5656
.build();
5757

58-
Call call = client.newCall(request);
58+
final Call call = client.newCall(request);
5959

6060
try (InputStream inputStream = call.execute().body().byteStream();
6161
ByteArrayOutputStream stream = new ByteArrayOutputStream()) {

moneta-core/src/main/java/module-info.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import org.javamoney.moneta.spi.*;
22
import org.javamoney.moneta.spi.format.DefaultAmountFormatProviderSpi;
3-
import org.javamoney.moneta.spi.loader.urlconnection.URLConnectionLoaderService;
3+
//import org.javamoney.moneta.spi.loader.urlconnection.URLConnectionLoaderService;
4+
import org.javamoney.moneta.spi.loader.okhttp.OkHttpLoaderService;
45
import org.javamoney.moneta.spi.loader.LoaderService;
56

67
/*
@@ -41,7 +42,8 @@
4142
provides javax.money.spi.MonetaryCurrenciesSingletonSpi with DefaultMonetaryCurrenciesSingletonSpi;
4243
provides javax.money.spi.RoundingProviderSpi with DefaultRoundingProvider;
4344
provides javax.money.spi.ServiceProvider with PriorityAwareServiceProvider;
44-
provides LoaderService with URLConnectionLoaderService;
45+
//provides LoaderService with URLConnectionLoaderService;
46+
provides LoaderService with OkHttpLoaderService;
4547
provides org.javamoney.moneta.spi.MonetaryConfigProvider with DefaultConfigProvider;
4648

4749
uses org.javamoney.moneta.spi.MonetaryConfigProvider;

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

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.lang.ref.SoftReference;
28-
import java.net.*;
28+
import java.net.URI;
2929
import java.util.*;
30+
import java.util.concurrent.TimeUnit;
3031
import java.util.concurrent.atomic.AtomicInteger;
3132
import java.util.logging.Level;
3233
import java.util.logging.Logger;
34+
import okhttp3.Call;
35+
import okhttp3.OkHttpClient;
36+
import okhttp3.Request;
3337

3438
/**
3539
* This class represent a resource that automatically is reloaded, if needed.
3640
* To create this instance use: {@link LoadableHttpResourceBuilder}
37-
* @author Anatole Tresch
41+
* @author Werner Keil
3842
*/
3943
public class LoadableHttpResource implements DataStreamFactory {
4044

@@ -300,54 +304,71 @@ protected void writeCache() throws IOException {
300304
* location. Also it can be an URL pointing to a current dataset, or an url directing to fallback resources,
301305
* e.g. within the current classpath.
302306
*
303-
* @param itemToLoad the target {@link URL}
307+
* @param itemToLoad the target {@link URI}
304308
* @param fallbackLoad true, for a fallback URL.
305309
*/
306310
protected boolean load(URI itemToLoad, boolean fallbackLoad) {
307311
InputStream is = null;
308312
ByteArrayOutputStream stream = new ByteArrayOutputStream();
309-
try{
310-
URLConnection conn;
311-
String proxyPort = this.properties.get("proxy.port");
312-
String proxyHost = this.properties.get("proxy.host");
313-
String proxyType = this.properties.get("proxy.type");
314-
if(proxyType!=null){
315-
Proxy proxy = new Proxy(Proxy.Type.valueOf(proxyType.toUpperCase()),
316-
InetSocketAddress.createUnresolved(proxyHost, Integer.parseInt(proxyPort)));
317-
conn = itemToLoad.toURL().openConnection(proxy);
318-
}else{
319-
conn = itemToLoad.toURL().openConnection();
313+
314+
try {
315+
OkHttpClient.Builder builder = new OkHttpClient.Builder();
316+
317+
String connectTimeout = this.properties.get("connection.connect.timeout");
318+
if(connectTimeout != null) {
319+
int seconds = Integer.parseInt(connectTimeout);
320+
builder = builder.connectTimeout(seconds, TimeUnit.SECONDS);
321+
// }else{
322+
// conn.setConnectTimeout(10000);
320323
}
321-
322-
String userAgent = this.properties.get("useragent");
323-
if(userAgent!=null && conn instanceof HttpURLConnection) {
324-
conn.setRequestProperty("User-Agent", userAgent);
324+
final String readTimeout = this.properties.get("connection.read.timeout");
325+
if(readTimeout != null) {
326+
int seconds = Integer.parseInt(readTimeout);
327+
builder = builder.readTimeout(seconds, TimeUnit.SECONDS);
325328
}
326-
327-
conn.setRequestProperty("Accept", "application/xhtml+xml");
328-
conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
329-
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
330-
331-
String timeout = this.properties.get("connection.connect.timeout");
332-
if(timeout!=null){
333-
int seconds = Integer.parseInt(timeout);
334-
conn.setConnectTimeout(seconds*1000);
335-
}else{
336-
conn.setConnectTimeout(10000);
329+
// else{
330+
// conn.setReadTimeout(10000);
331+
// }
332+
final String writeTimeout = this.properties.get("connection.write.timeout");
333+
if(writeTimeout != null) {
334+
int seconds = Integer.parseInt(writeTimeout);
335+
builder = builder.readTimeout(seconds, TimeUnit.SECONDS);
337336
}
338-
timeout = this.properties.get("connection.read.timeout");
339-
if(timeout!=null){
340-
int seconds = Integer.parseInt(timeout);
341-
conn.setReadTimeout(seconds*1000);
342-
}else{
343-
conn.setReadTimeout(10000);
337+
338+
final OkHttpClient client = builder.build();
339+
340+
Request.Builder requestBuilder = new Request.Builder();
341+
final String userAgent = this.properties.get("useragent");
342+
if(userAgent != null) {
343+
requestBuilder = requestBuilder.header("User-Agent", userAgent);
344344
}
345+
346+
final Request request = requestBuilder
347+
.url(itemToLoad.toString())
348+
.build();
349+
350+
// String proxyPort = this.properties.get("proxy.port");
351+
// String proxyHost = this.properties.get("proxy.host");
352+
// String proxyType = this.properties.get("proxy.type");
353+
// if(proxyType!=null){
354+
// Proxy proxy = new Proxy(Proxy.Type.valueOf(proxyType.toUpperCase()),
355+
// InetSocketAddress.createUnresolved(proxyHost, Integer.parseInt(proxyPort)));
356+
// conn = itemToLoad.toURL().openConnection(proxy);
357+
// }else{
358+
// conn = itemToLoad.toURL().openConnection();
359+
// }
360+
//
345361

346-
int newReadTimeout = conn.getReadTimeout();
347-
int newTimeout = conn.getConnectTimeout();
348-
362+
// conn.setRequestProperty("Accept", "application/xhtml+xml");
363+
// conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
364+
// conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
365+
//
366+
367+
final Call call = client.newCall(request);
368+
349369
byte[] data = new byte[4096];
350-
is = conn.getInputStream();
370+
//is = conn.getInputStream();
371+
is = call.execute().body().byteStream();
351372
int read = is.read(data);
352373
while (read > 0) {
353374
stream.write(data, 0, read);
@@ -413,7 +434,6 @@ protected final void setData(byte[] bytes) {
413434
this.data = new SoftReference<>(bytes);
414435
}
415436

416-
417437
public void unload() {
418438
synchronized (lock) {
419439
int count = accessCount.decrementAndGet();
@@ -471,6 +491,5 @@ public void close() throws IOException {
471491
unload();
472492
}
473493
}
474-
475494
}
476495
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
/**
2424
* Builder for {@link LoadableHttpResource}.
25+
* @author Werner Keil
2526
*/
2627
class LoadableHttpResourceBuilder {
2728

@@ -41,10 +42,10 @@ public LoadableHttpResourceBuilder withCache(ResourceCache cache) {
4142

4243
public LoadableHttpResource build() {
4344
if(Objects.isNull(cache)) {
44-
throw new IllegalStateException("The cache should be informed");
45+
throw new IllegalStateException("The cache should be present");
4546
}
4647
if(Objects.isNull(loadDataInformation)) {
47-
throw new IllegalStateException("The loadDataInformation should be informed");
48+
throw new IllegalStateException("The loadDataInformation should be present");
4849
}
4950
return new LoadableHttpResource(cache, loadDataInformation);
5051
}

moneta-core/src/main/resources/META-INF/services/org.javamoney.moneta.spi.loader.LoaderService

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
# License for the specific language governing permissions and limitations under
1313
# the License.
1414
#
15-
org.javamoney.moneta.spi.loader.urlconnection.URLConnectionLoaderService
15+
#org.javamoney.moneta.spi.loader.urlconnection.URLConnectionLoaderService
16+
org.javamoney.moneta.spi.loader.okhttp.OkHttpLoaderService

0 commit comments

Comments
 (0)