Skip to content

Commit bc8e871

Browse files
committed
optimize JSON serialization
1 parent 6d73d3a commit bc8e871

30 files changed

+254
-258
lines changed

src/main/java/com/alvarium/PublishWrapper.java

+6-31
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@
1414
*******************************************************************************/
1515
package com.alvarium;
1616

17-
import java.io.Serializable;
18-
import java.util.Base64;
17+
import com.alvarium.serializers.AlvariumPersistence;
1918

20-
import com.alvarium.contracts.Annotation;
21-
import com.alvarium.serializers.AnnotationConverter;
22-
import com.google.gson.Gson;
23-
import com.google.gson.GsonBuilder;
24-
import com.google.gson.JsonElement;
19+
import java.io.Serializable;
2520

2621
/**
2722
* A java bean that encapsulates the content sent through the stream providers
@@ -32,6 +27,7 @@ public class PublishWrapper implements Serializable {
3227
private final String messageType;
3328
private final Object content;
3429

30+
3531
public PublishWrapper(SdkAction action, String messageType, Object content) {
3632
this.action = action;
3733
this.messageType = messageType;
@@ -50,33 +46,12 @@ public Object getContent() {
5046
return content;
5147
}
5248

49+
5350
/**
54-
* The content field in the returned JSON will be Base64 string encoded
51+
* The content field in the returned JSON will be Base64 string encoded
5552
* @return String representation of the PublishWrapper JSON
5653
*/
5754
public String toJson() {
58-
Gson gson = new GsonBuilder()
59-
.registerTypeAdapter(Annotation.class, new AnnotationConverter())
60-
.disableHtmlEscaping()
61-
.create();
62-
63-
// Change the content field to a base64 encoded string before serializing to json
64-
final JsonElement decodedContent = gson.toJsonTree(this.content);
65-
final String encodedContent;
66-
67-
// `toString()` will work if the content is a primitive type, but will add additional
68-
// quotes (e.g. "foo" will be "\"foo\"") but `getAsString()` will produce correct behavior but
69-
// using `getAsString()` on a non-primitive type will throw an exception.
70-
// This condition ensures that the correct method is called on the correct type
71-
if (decodedContent.isJsonPrimitive()) {
72-
encodedContent = Base64.getEncoder().encodeToString(decodedContent.getAsString().getBytes());
73-
} else {
74-
encodedContent = Base64.getEncoder().encodeToString(decodedContent.toString().getBytes());
75-
}
76-
77-
// new publish wrapper returned as JSON string with encoded content
78-
// to prevent setting the object content value
79-
final PublishWrapper wrapper = new PublishWrapper(action, messageType, encodedContent);
80-
return gson.toJson(wrapper);
55+
return AlvariumPersistence.GSON.toJson(this);
8156
}
8257
}

src/main/java/com/alvarium/annotators/ChecksumAnnotator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.Files;
2222
import java.nio.file.Paths;
2323
import java.time.Instant;
24+
import java.time.ZonedDateTime;
2425

2526
import org.apache.logging.log4j.Logger;
2627

@@ -85,7 +86,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
8586
this.kind,
8687
null,
8788
isSatisfied,
88-
Instant.now()
89+
ZonedDateTime.now()
8990
);
9091

9192
final String annotationSignature = super.signAnnotation(

src/main/java/com/alvarium/annotators/MockAnnotator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.net.InetAddress;
1818
import java.net.UnknownHostException;
1919
import java.time.Instant;
20+
import java.time.ZonedDateTime;
2021

2122
import com.alvarium.contracts.Annotation;
2223
import com.alvarium.contracts.AnnotationType;
@@ -50,7 +51,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
5051
final String host = InetAddress.getLocalHost().getHostName();
5152
final String sig = signature.getPublicKey().getType().toString();
5253

53-
final Annotation annotation = new Annotation(key, hash, host, layer, kind, sig, cfg.getShouldSatisfy(), Instant.now());
54+
final Annotation annotation = new Annotation(key, hash, host, layer, kind, sig, cfg.getShouldSatisfy(), ZonedDateTime.now());
5455
return annotation;
5556
} catch (UnknownHostException e) {
5657
throw new AnnotatorException("Could not get hostname", e);

src/main/java/com/alvarium/annotators/PkiAnnotator.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
*******************************************************************************/
1515
package com.alvarium.annotators;
1616

17-
import java.net.InetAddress;
18-
import java.net.UnknownHostException;
19-
import java.time.Instant;
20-
21-
import org.apache.logging.log4j.Logger;
22-
2317
import com.alvarium.contracts.Annotation;
2418
import com.alvarium.contracts.AnnotationType;
2519
import com.alvarium.contracts.LayerType;
2620
import com.alvarium.hash.HashType;
2721
import com.alvarium.sign.SignatureInfo;
2822
import com.alvarium.utils.PropertyBag;
23+
import org.apache.logging.log4j.Logger;
24+
25+
import java.net.InetAddress;
26+
import java.net.UnknownHostException;
27+
import java.time.ZonedDateTime;
2928

3029
class PkiAnnotator extends AbstractPkiAnnotator implements Annotator {
3130
private final HashType hash;
@@ -63,7 +62,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
6362
kind,
6463
null,
6564
isSatisfied,
66-
Instant.now());
65+
ZonedDateTime.now());
6766

6867
final String annotationSignature = super.signAnnotation(signature.getPrivateKey(), annotation);
6968
annotation.setSignature(annotationSignature);

src/main/java/com/alvarium/annotators/PkiHttpAnnotator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.Paths;
2222
import java.time.Instant;
2323
import java.nio.file.Path;
24+
import java.time.ZonedDateTime;
2425

2526
import com.alvarium.annotators.http.ParseResult;
2627
import com.alvarium.annotators.http.ParseResultException;
@@ -101,7 +102,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
101102
kind,
102103
null,
103104
isSatisfied,
104-
Instant.now());
105+
ZonedDateTime.now());
105106

106107
final String annotationSignature = super.signAnnotation(sig.getPrivateKey(), annotation);
107108
annotation.setSignature(annotationSignature);

src/main/java/com/alvarium/annotators/SbomAnnotator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.net.InetAddress;
1717
import java.net.UnknownHostException;
1818
import java.time.Instant;
19+
import java.time.ZonedDateTime;
1920

2021
import org.apache.logging.log4j.Logger;
2122

@@ -76,7 +77,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
7677
kind,
7778
null,
7879
isSatisfied,
79-
Instant.now()
80+
ZonedDateTime.now()
8081
);
8182

8283
final String annotationSignature = super.signAnnotation(

src/main/java/com/alvarium/annotators/SourceAnnotator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.net.InetAddress;
1818
import java.net.UnknownHostException;
1919
import java.time.Instant;
20+
import java.time.ZonedDateTime;
2021

2122
import org.apache.logging.log4j.Logger;
2223

@@ -60,7 +61,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
6061

6162
// create an annotation without signature
6263
final Annotation annotation = new Annotation(key, this.hash, host, layer, this.kind, null, isSatisfied,
63-
Instant.now());
64+
ZonedDateTime.now());
6465

6566
final String signature = super.signAnnotation(signatureInfo.getPrivateKey(), annotation);
6667
annotation.setSignature(signature);

src/main/java/com/alvarium/annotators/SourceCodeAnnotator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.file.Paths;
2626
import java.text.Collator;
2727
import java.time.Instant;
28+
import java.time.ZonedDateTime;
2829
import java.util.ArrayList;
2930
import java.util.Collections;
3031
import java.util.List;
@@ -88,7 +89,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
8889
kind,
8990
null,
9091
isSatisfied,
91-
Instant.now());
92+
ZonedDateTime.now());
9293

9394
final String annotationSignature = super.signAnnotation(signature.getPrivateKey(), annotation);
9495
annotation.setSignature(annotationSignature);

src/main/java/com/alvarium/annotators/TlsAnnotator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.logging.log4j.Logger;
2222

2323
import java.time.Instant;
24+
import java.time.ZonedDateTime;
2425

2526
import com.alvarium.contracts.Annotation;
2627
import com.alvarium.contracts.AnnotationType;
@@ -68,7 +69,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
6869

6970
// create an annotation without signature
7071
final Annotation annotation = new Annotation(key, hash, host, layer, kind, null, isSatisfied,
71-
Instant.now());
72+
ZonedDateTime.now());
7273

7374
// sign annotation
7475
final String signature = super.signAnnotation(signatureInfo.getPrivateKey(),

src/main/java/com/alvarium/annotators/TpmAnnotator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.net.InetAddress;
3030
import java.net.UnknownHostException;
3131
import java.time.Instant;
32+
import java.time.ZonedDateTime;
3233

3334
class TpmAnnotator extends AbstractAnnotator implements Annotator {
3435
private final HashType hash;
@@ -68,7 +69,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
6869
kind,
6970
null,
7071
isSatisfied,
71-
Instant.now());
72+
ZonedDateTime.now());
7273

7374
final String annotationSignature = super.signAnnotation(signature.getPrivateKey(), annotation);
7475
annotation.setSignature(annotationSignature);

src/main/java/com/alvarium/annotators/VulnerabilityAnnotator.java

+9-15
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,20 @@
1313
*******************************************************************************/
1414
package com.alvarium.annotators;
1515

16-
import java.net.InetAddress;
17-
import java.net.UnknownHostException;
18-
import java.time.Instant;
19-
import java.util.List;
20-
import java.util.Map;
21-
22-
import org.apache.logging.log4j.Logger;
23-
24-
import com.alvarium.annotators.vulnerability.PackageFileHandler;
25-
import com.alvarium.annotators.vulnerability.PackageFileHandlerFactory;
26-
import com.alvarium.annotators.vulnerability.VulnerabilityAnnotatorConfig;
27-
import com.alvarium.annotators.vulnerability.VulnerabilityApiHandler;
28-
import com.alvarium.annotators.vulnerability.VulnerabilityApiHandlerFactory;
29-
import com.alvarium.annotators.vulnerability.VulnerabilityException;
16+
import com.alvarium.annotators.vulnerability.*;
3017
import com.alvarium.contracts.Annotation;
3118
import com.alvarium.contracts.AnnotationType;
3219
import com.alvarium.contracts.LayerType;
3320
import com.alvarium.hash.HashType;
3421
import com.alvarium.sign.SignatureInfo;
3522
import com.alvarium.utils.PropertyBag;
23+
import org.apache.logging.log4j.Logger;
24+
25+
import java.net.InetAddress;
26+
import java.net.UnknownHostException;
27+
import java.time.ZonedDateTime;
28+
import java.util.List;
29+
import java.util.Map;
3630

3731

3832

@@ -86,7 +80,7 @@ public Annotation execute(PropertyBag ctx, byte[] data, String key) throws Annot
8680
this.kind,
8781
null,
8882
isSatisfied,
89-
Instant.now()
83+
ZonedDateTime.now()
9084
);
9185

9286
final String annotationSignature = super.signAnnotation(

0 commit comments

Comments
 (0)