diff --git a/CHANGELOG.md b/CHANGELOG.md index d0577d3..ba50fdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. ### Breaking * Java 11 or later required +### Changes +* Using native Java 11 HTTP client + ## 1.3.0 - 2019-12-04 ### Security diff --git a/src/main/java/de/stklcode/pubtrans/ura/UraClient.java b/src/main/java/de/stklcode/pubtrans/ura/UraClient.java index 8f5f6a0..016bca0 100644 --- a/src/main/java/de/stklcode/pubtrans/ura/UraClient.java +++ b/src/main/java/de/stklcode/pubtrans/ura/UraClient.java @@ -23,8 +23,12 @@ import de.stklcode.pubtrans.ura.model.Trip; import de.stklcode.pubtrans.ura.reader.AsyncUraTripReader; import java.io.*; +import java.net.URI; import java.net.URL; import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -383,7 +387,7 @@ public class UraClient implements Serializable { * * @param returnList Fields to fetch. * @param query The query. - * @return Input stream of the URL + * @return Response {@link InputStream}. * @throws IOException on errors */ private InputStream requestInstant(final String[] returnList, final Query query) throws IOException { @@ -435,11 +439,19 @@ public class UraClient implements Serializable { * Open given URL as InputStream. * * @param url The URL. - * @return Input Stream of results. + * @return Response {@link InputStream}. * @throws IOException Error opening connection or reading data. */ private InputStream request(String url) throws IOException { - return new URL(url).openStream(); + try { + return HttpClient.newHttpClient().send( + HttpRequest.newBuilder(URI.create(url)).GET().build(), + HttpResponse.BodyHandlers.ofInputStream() + ).body(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("API request interrupted", e); + } } /** diff --git a/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java b/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java index 1a3bc13..2fc132a 100644 --- a/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java +++ b/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java @@ -23,10 +23,17 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.URI; import java.net.URL; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.function.Consumer; /** @@ -145,6 +152,14 @@ public class AsyncUraTripReader implements AutoCloseable { * @throws IOException On errors. */ private static InputStream getInputStream(URL url) throws IOException { - return url.openStream(); + try { + return HttpClient.newHttpClient().send( + HttpRequest.newBuilder().uri(URI.create(url.toString())).GET().build(), + HttpResponse.BodyHandlers.ofInputStream() + ).body(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("API request interrupted", e); + } } }