Skip to content

Commit

Permalink
Working on #353
Browse files Browse the repository at this point in the history
  • Loading branch information
keilw committed Jan 23, 2024
1 parent 1888429 commit 0a3fb69
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ class IMFRemoteSearchCallable implements Callable<IMFRemoteSearchResult>{
@Override
public IMFRemoteSearchResult call() throws Exception {
//connection.addRequestProperty("User-Agent", userAgent);

OkHttpClient client = new OkHttpClient.Builder()
// TODO apply userAgent where applicable
final OkHttpClient client = new OkHttpClient.Builder()
.build();

Request request = new Request.Builder()
final Request request = new Request.Builder()
.url(getUrl())
.build();

Call call = client.newCall(request);
final Call call = client.newCall(request);

try (InputStream inputStream = call.execute().body().byteStream();
ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
Expand Down
6 changes: 4 additions & 2 deletions moneta-core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import org.javamoney.moneta.spi.*;
import org.javamoney.moneta.spi.format.DefaultAmountFormatProviderSpi;
import org.javamoney.moneta.spi.loader.urlconnection.URLConnectionLoaderService;
//import org.javamoney.moneta.spi.loader.urlconnection.URLConnectionLoaderService;
import org.javamoney.moneta.spi.loader.okhttp.OkHttpLoaderService;
import org.javamoney.moneta.spi.loader.LoaderService;

/*
Expand Down Expand Up @@ -41,7 +42,8 @@
provides javax.money.spi.MonetaryCurrenciesSingletonSpi with DefaultMonetaryCurrenciesSingletonSpi;
provides javax.money.spi.RoundingProviderSpi with DefaultRoundingProvider;
provides javax.money.spi.ServiceProvider with PriorityAwareServiceProvider;
provides LoaderService with URLConnectionLoaderService;
//provides LoaderService with URLConnectionLoaderService;
provides LoaderService with OkHttpLoaderService;
provides org.javamoney.moneta.spi.MonetaryConfigProvider with DefaultConfigProvider;

uses org.javamoney.moneta.spi.MonetaryConfigProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.*;
import java.net.URI;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
* This class represent a resource that automatically is reloaded, if needed.
* To create this instance use: {@link LoadableHttpResourceBuilder}
* @author Anatole Tresch
* @author Werner Keil
*/
public class LoadableHttpResource implements DataStreamFactory {

Expand Down Expand Up @@ -300,54 +304,71 @@ protected void writeCache() throws IOException {
* location. Also it can be an URL pointing to a current dataset, or an url directing to fallback resources,
* e.g. within the current classpath.
*
* @param itemToLoad the target {@link URL}
* @param itemToLoad the target {@link URI}
* @param fallbackLoad true, for a fallback URL.
*/
protected boolean load(URI itemToLoad, boolean fallbackLoad) {
InputStream is = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try{
URLConnection conn;
String proxyPort = this.properties.get("proxy.port");
String proxyHost = this.properties.get("proxy.host");
String proxyType = this.properties.get("proxy.type");
if(proxyType!=null){
Proxy proxy = new Proxy(Proxy.Type.valueOf(proxyType.toUpperCase()),
InetSocketAddress.createUnresolved(proxyHost, Integer.parseInt(proxyPort)));
conn = itemToLoad.toURL().openConnection(proxy);
}else{
conn = itemToLoad.toURL().openConnection();

try {
OkHttpClient.Builder builder = new OkHttpClient.Builder();

String connectTimeout = this.properties.get("connection.connect.timeout");
if(connectTimeout != null) {
int seconds = Integer.parseInt(connectTimeout);
builder = builder.connectTimeout(seconds, TimeUnit.SECONDS);
// }else{
// conn.setConnectTimeout(10000);
}

String userAgent = this.properties.get("useragent");
if(userAgent!=null && conn instanceof HttpURLConnection) {
conn.setRequestProperty("User-Agent", userAgent);
final String readTimeout = this.properties.get("connection.read.timeout");
if(readTimeout != null) {
int seconds = Integer.parseInt(readTimeout);
builder = builder.readTimeout(seconds, TimeUnit.SECONDS);
}

conn.setRequestProperty("Accept", "application/xhtml+xml");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");

String timeout = this.properties.get("connection.connect.timeout");
if(timeout!=null){
int seconds = Integer.parseInt(timeout);
conn.setConnectTimeout(seconds*1000);
}else{
conn.setConnectTimeout(10000);
// else{
// conn.setReadTimeout(10000);
// }
final String writeTimeout = this.properties.get("connection.write.timeout");
if(writeTimeout != null) {
int seconds = Integer.parseInt(writeTimeout);
builder = builder.readTimeout(seconds, TimeUnit.SECONDS);
}
timeout = this.properties.get("connection.read.timeout");
if(timeout!=null){
int seconds = Integer.parseInt(timeout);
conn.setReadTimeout(seconds*1000);
}else{
conn.setReadTimeout(10000);

final OkHttpClient client = builder.build();

Request.Builder requestBuilder = new Request.Builder();
final String userAgent = this.properties.get("useragent");
if(userAgent != null) {
requestBuilder = requestBuilder.header("User-Agent", userAgent);
}

final Request request = requestBuilder
.url(itemToLoad.toString())
.build();

// String proxyPort = this.properties.get("proxy.port");
// String proxyHost = this.properties.get("proxy.host");
// String proxyType = this.properties.get("proxy.type");
// if(proxyType!=null){
// Proxy proxy = new Proxy(Proxy.Type.valueOf(proxyType.toUpperCase()),
// InetSocketAddress.createUnresolved(proxyHost, Integer.parseInt(proxyPort)));
// conn = itemToLoad.toURL().openConnection(proxy);
// }else{
// conn = itemToLoad.toURL().openConnection();
// }
//

int newReadTimeout = conn.getReadTimeout();
int newTimeout = conn.getConnectTimeout();

// conn.setRequestProperty("Accept", "application/xhtml+xml");
// conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
// conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
//

final Call call = client.newCall(request);

byte[] data = new byte[4096];
is = conn.getInputStream();
//is = conn.getInputStream();
is = call.execute().body().byteStream();
int read = is.read(data);
while (read > 0) {
stream.write(data, 0, read);
Expand Down Expand Up @@ -413,7 +434,6 @@ protected final void setData(byte[] bytes) {
this.data = new SoftReference<>(bytes);
}


public void unload() {
synchronized (lock) {
int count = accessCount.decrementAndGet();
Expand Down Expand Up @@ -471,6 +491,5 @@ public void close() throws IOException {
unload();
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

/**
* Builder for {@link LoadableHttpResource}.
* @author Werner Keil
*/
class LoadableHttpResourceBuilder {

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

public LoadableHttpResource build() {
if(Objects.isNull(cache)) {
throw new IllegalStateException("The cache should be informed");
throw new IllegalStateException("The cache should be present");
}
if(Objects.isNull(loadDataInformation)) {
throw new IllegalStateException("The loadDataInformation should be informed");
throw new IllegalStateException("The loadDataInformation should be present");
}
return new LoadableHttpResource(cache, loadDataInformation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
# License for the specific language governing permissions and limitations under
# the License.
#
org.javamoney.moneta.spi.loader.urlconnection.URLConnectionLoaderService
#org.javamoney.moneta.spi.loader.urlconnection.URLConnectionLoaderService
org.javamoney.moneta.spi.loader.okhttp.OkHttpLoaderService

0 comments on commit 0a3fb69

Please sign in to comment.