Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom deserialization to handle list extensions #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/io/calidog/certstream/CertStream.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.calidog.certstream;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -60,13 +61,13 @@ public static void onMessage(CertStreamMessageHandler handler)
CertStreamMessagePOJO msg;
try
{
msg = new Gson().fromJson(string, CertStreamMessagePOJO.class);
msg = certStreamGson.fromJson(string, CertStreamMessagePOJO.class);

if (msg.messageType.equalsIgnoreCase("heartbeat"))
{
return;
}
}catch (JsonSyntaxException e)
} catch (JsonSyntaxException e)
{
System.out.println(e.getMessage());
logger.warn("onMessage had an exception parsing some json", e);
Expand All @@ -87,6 +88,15 @@ public static void onMessage(CertStreamMessageHandler handler)
});
}

private static Gson certStreamGson =
new GsonBuilder()
.registerTypeAdapter
(
CertStreamCertificatePOJO.class,
new CertStreamCertificatePOJODeserializer()
)
.create();

/**
* @param handler A {@link Consumer<CertStreamMessage>} that we'll
* run in a Thread that stays alive as long
Expand All @@ -99,7 +109,7 @@ public static void onMessageAlternativeServer (CertStreamMessageHandler handler,
CertStreamMessagePOJO msg;

try {
msg = new Gson().fromJson(string, CertStreamMessagePOJO.class);
msg = certStreamGson.fromJson(string, CertStreamMessagePOJO.class);

if (msg.messageType.equalsIgnoreCase("heartbeat")) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/io/calidog/certstream/CertStreamCertificate.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class CertStreamCertificate extends X509Certificate {
private HashMap<String, String> subject;
private HashMap<String, String> extensions;
private HashMap<String, String[]> extensions;

private double notBefore;
private double notAfter;
Expand Down Expand Up @@ -257,7 +257,7 @@ public byte[] getExtensionValue(String s) {
* passed-in oid String. The oid string is represented
* by whatever CertStream passes us.
*/
public String getStringExtensionValue(String key)
public String[] getStringExtensionValue(String key)
{
return extensions.get(key);
}
Expand Down
4 changes: 3 additions & 1 deletion src/io/calidog/certstream/CertStreamCertificatePOJO.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class CertStreamCertificatePOJO {

HashMap<String, String> subject;

HashMap<String, String> extensions;
// values can be either strings or lists of strings, so we use a a custom deserializer
// that converts strings into singleton arrays
HashMap<String, String[]> extensions;

@SerializedName("not_before")
double notBefore;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.calidog.certstream;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.HashMap;

public class CertStreamCertificatePOJODeserializer implements JsonDeserializer<CertStreamCertificatePOJO> {

@Override
public CertStreamCertificatePOJO deserialize(
JsonElement jsonElement,
Type type,
JsonDeserializationContext jsonDeserializationContext
) throws JsonParseException {
JsonObject jsonObj = jsonElement.getAsJsonObject();

JsonObject jsonExtensions;

HashMap<String, String[]> extensionMap = null;

if (jsonObj.has("extensions"))
{
jsonExtensions = jsonObj.remove("extensions").getAsJsonObject();

final HashMap<String, String[]> finalExtensionMap = new HashMap<>(jsonExtensions.size());

jsonExtensions
.entrySet()
.forEach
(
(java.util.Map.Entry<String, JsonElement> entry) ->
{
String key = entry.getKey();
JsonElement value = entry.getValue();
String[] extensionValueList;
try {
extensionValueList = new String[] { value.getAsString() };
} catch (IllegalStateException | UnsupportedOperationException e) {
JsonArray extensionJsonArray = value.getAsJsonArray();
extensionValueList = new String[extensionJsonArray.size()];

for (int i = 0; i < extensionJsonArray.size(); i++) {
extensionValueList[i] = extensionJsonArray.get(i).getAsString();
}
}

finalExtensionMap.put(key, extensionValueList);
}
);

extensionMap = finalExtensionMap;
}

CertStreamCertificatePOJO retVal =
jsonDeserializationContext.deserialize(jsonElement, type);

retVal.extensions = extensionMap;

return retVal;
}
}