commit b8c7ab876a692ef4cee3db2dda7c257f61f98a1f
parent 76a0e4976671ac1c5322a97f7db69554613faa8e
Author: Wim Dupont <wim@wimdupont.com>
Date: Fri, 1 Dec 2023 18:16:03 +0100
java 21 upgrade
Diffstat:
2 files changed, 25 insertions(+), 19 deletions(-)
diff --git a/pom.xml b/pom.xml
@@ -8,8 +8,8 @@
<version>1.0-SNAPSHOT</version>
<properties>
- <maven.compiler.source>17</maven.compiler.source>
- <maven.compiler.target>17</maven.compiler.target>
+ <maven.compiler.source>21</maven.compiler.source>
+ <maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
diff --git a/src/main/java/com/wimdupont/client/Client.java b/src/main/java/com/wimdupont/client/Client.java
@@ -3,11 +3,12 @@ package com.wimdupont.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
-import java.net.URL;
+import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
@@ -17,13 +18,13 @@ public class Client {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final Map<String, String> POST_HEADER_MAP = Map.of("Content-type", "application/json");
- public static ObjectMapper getObjectMapper(){
+ public static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}
public static <T> Optional<T> get(String url, Class<T> responseClazz) {
try {
- HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
+ HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
@@ -38,26 +39,31 @@ public class Client {
public static <T> Optional<T> post(String url, Class<T> responseClazz, String body) {
try {
- HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
+ HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
connection.setRequestMethod("POST");
POST_HEADER_MAP.forEach(connection::setRequestProperty);
connection.setDoOutput(true);
- OutputStream outStream = connection.getOutputStream();
- OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, StandardCharsets.UTF_8);
- outStreamWriter.write(body);
- outStreamWriter.flush();
- outStreamWriter.close();
- outStream.close();
-
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- StringBuilder stringBuilder = new StringBuilder();
- for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
- stringBuilder.append(line);
- }
- return Optional.of(OBJECT_MAPPER.readValue(stringBuilder.toString(), responseClazz));
+ return Optional.of(OBJECT_MAPPER.readValue(sendAndGetResponse(body, connection), responseClazz));
} catch (Exception e) {
System.out.println(e.getMessage());
return Optional.empty();
}
}
+
+ private static String sendAndGetResponse(String body,
+ HttpURLConnection connection) throws IOException {
+ OutputStream outStream = connection.getOutputStream();
+ OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, StandardCharsets.UTF_8);
+ outStreamWriter.write(body);
+ outStreamWriter.flush();
+ outStreamWriter.close();
+ outStream.close();
+
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+ StringBuilder stringBuilder = new StringBuilder();
+ for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
+ stringBuilder.append(line);
+ }
+ return stringBuilder.toString();
+ }
}