Skip to content
Snippets Groups Projects
Commit 016f8fb8 authored by Martin Vogel's avatar Martin Vogel
Browse files

refactor: reuse object mapper in envelope builder (planning#2016)

parent e86d1319
No related branches found
No related tags found
1 merge request!394planning#2016: ZBP Client
......@@ -23,6 +23,12 @@ public class ObjectMapperProvider {
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
public static ObjectMapper createObjectMapper() {
return new ObjectMapper()
.registerModule(new SimpleModule().addSerializer(new CreateDateSerializer()))
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
private static class AttachmentSerializer extends StdSerializer<ZBPApiAttachment> {
private final CryptoService cryptoService;
......
......@@ -2,9 +2,6 @@ package dev.fitko.fitconnect.client.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.jwk.RSAKey;
import dev.fitko.fitconnect.api.domain.zbp.AuthorKeyPair;
......@@ -25,11 +22,7 @@ import java.util.Base64;
*/
public final class ZBPEnvelopeBuilder {
private static final ObjectWriter WRITER = new ObjectMapper()
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.writerWithDefaultPrettyPrinter();
private static final ObjectMapper MAPPER = ObjectMapperProvider.createObjectMapper();
/**
* Write payload to a {@link ZBPEnvelope} as byte[] that is signed with the authors private key.
......@@ -56,7 +49,7 @@ public final class ZBPEnvelopeBuilder {
private static byte[] serializeToByteArray(Object obj) {
try {
return WRITER.writeValueAsBytes(obj);
return MAPPER.writeValueAsBytes(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
......@@ -70,7 +63,7 @@ public final class ZBPEnvelopeBuilder {
try {
Signature signature = Signature.getInstance("SHA512withRSA");
signature.initSign(key.toPrivateKey());
signature.update(WRITER.writeValueAsBytes(input));
signature.update(MAPPER.writeValueAsBytes(input));
return Base64.getEncoder().encodeToString(signature.sign());
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | JOSEException | JsonProcessingException e) {
throw new RuntimeException(e);
......
......@@ -2,9 +2,7 @@ package dev.fitko.fitconnect.client.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.fitko.fitconnect.api.services.crypto.CryptoService;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.time.Instant;
......@@ -19,7 +17,7 @@ class ObjectMapperProviderTest {
// Given
Instant instant = Instant.parse("2023-11-01T11:23:21.223Z");
final ObjectMapper objectMapper = ObjectMapperProvider.createObjectMapper(Mockito.mock(CryptoService.class));
final ObjectMapper objectMapper = ObjectMapperProvider.createObjectMapper();
// When
final String instantAsString = objectMapper.writeValueAsString(instant);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment