|
25 | 25 | import java.io.IOException; |
26 | 26 | import java.io.InputStream; |
27 | 27 | import java.lang.ref.SoftReference; |
28 | | -import java.net.*; |
| 28 | +import java.net.URI; |
29 | 29 | import java.util.*; |
| 30 | +import java.util.concurrent.TimeUnit; |
30 | 31 | import java.util.concurrent.atomic.AtomicInteger; |
31 | 32 | import java.util.logging.Level; |
32 | 33 | import java.util.logging.Logger; |
| 34 | +import okhttp3.Call; |
| 35 | +import okhttp3.OkHttpClient; |
| 36 | +import okhttp3.Request; |
33 | 37 |
|
34 | 38 | /** |
35 | 39 | * This class represent a resource that automatically is reloaded, if needed. |
36 | 40 | * To create this instance use: {@link LoadableHttpResourceBuilder} |
37 | | - * @author Anatole Tresch |
| 41 | + * @author Werner Keil |
38 | 42 | */ |
39 | 43 | public class LoadableHttpResource implements DataStreamFactory { |
40 | 44 |
|
@@ -300,54 +304,71 @@ protected void writeCache() throws IOException { |
300 | 304 | * location. Also it can be an URL pointing to a current dataset, or an url directing to fallback resources, |
301 | 305 | * e.g. within the current classpath. |
302 | 306 | * |
303 | | - * @param itemToLoad the target {@link URL} |
| 307 | + * @param itemToLoad the target {@link URI} |
304 | 308 | * @param fallbackLoad true, for a fallback URL. |
305 | 309 | */ |
306 | 310 | protected boolean load(URI itemToLoad, boolean fallbackLoad) { |
307 | 311 | InputStream is = null; |
308 | 312 | 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); |
320 | 323 | } |
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); |
325 | 328 | } |
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); |
337 | 336 | } |
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); |
344 | 344 | } |
| 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 | +// |
345 | 361 |
|
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 | + |
349 | 369 | byte[] data = new byte[4096]; |
350 | | - is = conn.getInputStream(); |
| 370 | + //is = conn.getInputStream(); |
| 371 | + is = call.execute().body().byteStream(); |
351 | 372 | int read = is.read(data); |
352 | 373 | while (read > 0) { |
353 | 374 | stream.write(data, 0, read); |
@@ -413,7 +434,6 @@ protected final void setData(byte[] bytes) { |
413 | 434 | this.data = new SoftReference<>(bytes); |
414 | 435 | } |
415 | 436 |
|
416 | | - |
417 | 437 | public void unload() { |
418 | 438 | synchronized (lock) { |
419 | 439 | int count = accessCount.decrementAndGet(); |
@@ -471,6 +491,5 @@ public void close() throws IOException { |
471 | 491 | unload(); |
472 | 492 | } |
473 | 493 | } |
474 | | - |
475 | 494 | } |
476 | 495 | } |
0 commit comments