rewrite HTTP requests using Java 11 HTTP components
This commit is contained in:
parent
10681f4eb3
commit
88c10d5333
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user