|
20 | 20 | import org.javamoney.moneta.spi.loader.LoaderService; |
21 | 21 | import org.javamoney.moneta.spi.loader.ResourceCache; |
22 | 22 |
|
23 | | -import java.io.ByteArrayInputStream; |
24 | | -import java.io.ByteArrayOutputStream; |
25 | | -import java.io.IOException; |
26 | | -import java.io.InputStream; |
| 23 | +import java.io.*; |
27 | 24 | import java.lang.ref.SoftReference; |
28 | 25 | import java.net.InetSocketAddress; |
29 | 26 | import java.net.Proxy; |
30 | 27 | import java.net.URI; |
| 28 | +import java.net.URL; |
31 | 29 | import java.util.*; |
32 | 30 | import java.util.concurrent.TimeUnit; |
33 | 31 | import java.util.concurrent.atomic.AtomicInteger; |
@@ -314,63 +312,65 @@ protected boolean load(URI itemToLoad, boolean fallbackLoad) { |
314 | 312 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
315 | 313 |
|
316 | 314 | 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); |
325 | 330 | } |
326 | | - Proxy proxy = new Proxy(Proxy.Type.valueOf(proxyType.toUpperCase()), |
327 | | - InetSocketAddress.createUnresolved(proxyHost, Integer.parseInt(proxyPort))); |
328 | | - builder = builder.proxy(proxy); |
329 | | - } |
330 | 331 |
|
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); |
335 | 336 | // }else{ |
336 | 337 | // 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 | + } |
351 | 344 |
|
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 | + } |
353 | 350 |
|
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(); |
359 | 362 |
|
360 | | - final Request request = requestBuilder |
361 | | - .url(itemToLoad.toString()) |
362 | | - .build(); |
363 | | - |
364 | 363 | // conn.setRequestProperty("Accept", "application/xhtml+xml"); |
365 | 364 | // conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br"); |
366 | 365 | // conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9"); |
367 | 366 | // TODO check if any of those are necessary? |
368 | 367 |
|
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 | + } |
370 | 372 |
|
371 | 373 | byte[] data = new byte[4096]; |
372 | | - //is = conn.getInputStream(); |
373 | | - is = call.execute().body().byteStream(); |
374 | 374 | int read = is.read(data); |
375 | 375 | while (read > 0) { |
376 | 376 | stream.write(data, 0, read); |
|
0 commit comments