Parcourir la source

replace GSON in "hub"

Sebastian Stenzel il y a 1 an
Parent
commit
5f2497de90

+ 4 - 3
src/main/java/org/cryptomator/ui/keyloading/hub/AuthFlowTask.java

@@ -1,6 +1,6 @@
 package org.cryptomator.ui.keyloading.hub;
 
-import com.google.gson.JsonParser;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import io.github.coffeelibs.tinyoauth2client.AuthFlow;
 import io.github.coffeelibs.tinyoauth2client.TinyOAuth2;
 import io.github.coffeelibs.tinyoauth2client.http.response.Response;
@@ -12,6 +12,8 @@ import java.util.function.Consumer;
 
 class AuthFlowTask extends Task<String> {
 
+	private static final ObjectMapper JSON = new ObjectMapper();
+
 	private final HubConfig hubConfig;
 	private final AuthFlowContext authFlowContext;
 	private final Consumer<URI> redirectUriConsumer;
@@ -39,8 +41,7 @@ class AuthFlowTask extends Task<String> {
 		if (response.statusCode() != 200) {
 			throw new NotOkResponseException("Authorization returned status code " + response.statusCode());
 		}
-		var json = JsonParser.parseString(response.body());
-		return json.getAsJsonObject().get("access_token").getAsString();
+		return JSON.reader().readTree(response.body()).get("access_token").asText();
 	}
 
 	public static class NotOkResponseException extends RuntimeException {

+ 1 - 5
src/main/java/org/cryptomator/ui/keyloading/hub/CreateDeviceDto.java

@@ -1,9 +1,5 @@
 package org.cryptomator.ui.keyloading.hub;
 
-class CreateDeviceDto {
-
-	public String id;
-	public String name;
-	public String publicKey;
+record CreateDeviceDto(String id, String name, String publicKey) {
 
 }

+ 0 - 4
src/main/java/org/cryptomator/ui/keyloading/hub/HttpHelper.java

@@ -1,14 +1,10 @@
 package org.cryptomator.ui.keyloading.hub;
 
 import com.google.common.io.CharStreams;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParseException;
-import com.google.gson.JsonParser;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.Reader;
 import java.net.http.HttpResponse;
 import java.nio.charset.StandardCharsets;
 

+ 13 - 8
src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java

@@ -2,9 +2,9 @@ package org.cryptomator.ui.keyloading.hub;
 
 import com.auth0.jwt.JWT;
 import com.auth0.jwt.interfaces.DecodedJWT;
+import com.fasterxml.jackson.core.JacksonException;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.io.BaseEncoding;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.nimbusds.jose.JWEObject;
 import dagger.Lazy;
 import org.cryptomator.common.settings.DeviceKey;
@@ -46,7 +46,7 @@ import java.util.concurrent.atomic.AtomicReference;
 public class RegisterDeviceController implements FxController {
 
 	private static final Logger LOG = LoggerFactory.getLogger(RegisterDeviceController.class);
-	private static final Gson GSON = new GsonBuilder().setLenient().create();
+	private static final ObjectMapper JSON = new ObjectMapper().setDefaultLeniency(true);
 	private static final List<Integer> EXPECTED_RESPONSE_CODES = List.of(201, 409);
 
 	private final Stage window;
@@ -101,11 +101,8 @@ public class RegisterDeviceController implements FxController {
 
 		var keyUri = URI.create(hubConfig.devicesResourceUrl + deviceId);
 		var deviceKey = keyPair.getPublic().getEncoded();
-		var dto = new CreateDeviceDto();
-		dto.id = deviceId;
-		dto.name = deviceNameField.getText();
-		dto.publicKey = BaseEncoding.base64Url().omitPadding().encode(deviceKey);
-		var json = GSON.toJson(dto); // TODO: do we want to keep GSON? doesn't support records -.-
+		var dto = new CreateDeviceDto(deviceId, deviceNameField.getText(), BaseEncoding.base64Url().omitPadding().encode(deviceKey));
+		var json = toJson(dto);
 		var request = HttpRequest.newBuilder(keyUri) //
 				.header("Authorization", "Bearer " + bearerToken) //
 				.header("Content-Type", "application/json").PUT(HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8)) //
@@ -127,6 +124,14 @@ public class RegisterDeviceController implements FxController {
 				}, Platform::runLater);
 	}
 
+	private String toJson(CreateDeviceDto dto) {
+		try {
+			return JSON.writer().writeValueAsString(dto);
+		} catch (JacksonException e) {
+			throw new IllegalStateException("Failed to serialize DTO", e);
+		}
+	}
+
 	private void handleResponse(HttpResponse<Void> voidHttpResponse) {
 		assert EXPECTED_RESPONSE_CODES.contains(voidHttpResponse.statusCode());