From b5b196fd651319b7d4a8b7e90f50e4afec83c0d7 Mon Sep 17 00:00:00 2001 From: Greg DiCristofaro Date: Fri, 21 Jul 2023 10:06:34 -0400 Subject: [PATCH] fix for date --- .../ctapi/json/DecryptedLicenseResponse.java | 4 +- .../autopsy/ctapi/util/ObjectMapperUtil.java | 68 +++++++++++++------ 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/Core/src/com/basistech/df/cybertriage/autopsy/ctapi/json/DecryptedLicenseResponse.java b/Core/src/com/basistech/df/cybertriage/autopsy/ctapi/json/DecryptedLicenseResponse.java index d39e36686b..1a084e716e 100644 --- a/Core/src/com/basistech/df/cybertriage/autopsy/ctapi/json/DecryptedLicenseResponse.java +++ b/Core/src/com/basistech/df/cybertriage/autopsy/ctapi/json/DecryptedLicenseResponse.java @@ -14,7 +14,7 @@ package com.basistech.df.cybertriage.autopsy.ctapi.json; import com.basistech.df.cybertriage.autopsy.ctapi.util.ObjectMapperUtil.InstantEpochMillisDeserializer; -import com.basistech.df.cybertriage.autopsy.ctapi.util.ObjectMapperUtil.ZonedDateTimeDeserializer; +import com.basistech.df.cybertriage.autopsy.ctapi.util.ObjectMapperUtil.MDYDateDeserializer; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -41,7 +41,7 @@ public class DecryptedLicenseResponse { public DecryptedLicenseResponse( @JsonProperty("boostLicenseId") String boostLicenseId, @JsonProperty("licenseHostId") String licenseHostId, - @JsonDeserialize(using = ZonedDateTimeDeserializer.class) + @JsonDeserialize(using = MDYDateDeserializer.class) @JsonProperty("expirationDate") ZonedDateTime expirationDate, @JsonProperty("hashLookups") Long hashLookups, @JsonProperty("fileUploads") Long fileUploads, diff --git a/Core/src/com/basistech/df/cybertriage/autopsy/ctapi/util/ObjectMapperUtil.java b/Core/src/com/basistech/df/cybertriage/autopsy/ctapi/util/ObjectMapperUtil.java index 0c82e5bb68..ff424bae01 100644 --- a/Core/src/com/basistech/df/cybertriage/autopsy/ctapi/util/ObjectMapperUtil.java +++ b/Core/src/com/basistech/df/cybertriage/autopsy/ctapi/util/ObjectMapperUtil.java @@ -25,12 +25,15 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.DateTimeException; import java.time.Instant; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeParseException; import java.util.Date; +import java.util.Locale; import java.util.function.Function; /** @@ -55,14 +58,23 @@ public class ObjectMapperUtil { return defaultMapper; } - - public static class ZonedDateTimeDeserializer extends JsonDeserializer { + public static class UTCBaseZonedDateTimeDeserializer extends JsonDeserializer { + + private final DateTimeFormatter formatter; + + public UTCBaseZonedDateTimeDeserializer(DateTimeFormatter formatter) { + this.formatter = formatter; + } @Override public ZonedDateTime deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException { String date = jp.getText(); + if (date == null) { + return null; + } + try { - LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME); + LocalDateTime ldt = LocalDateTime.parse(date, formatter); return ZonedDateTime.of(ldt, ZoneOffset.UTC); } catch (DateTimeParseException ex) { return null; @@ -70,22 +82,40 @@ public class ObjectMapperUtil { } } -// public static class MDYDateDeserializer extends JsonDeserializer { -// -// private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("MMM dd, yyyy"); -// -// @Override -// public Date deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException { -// JsonNode node = jp.getCodec().readTree(jp); -// String nodeText = node.asText(); -// try { -// return FORMATTER.parse(nodeText); -// } catch (ParseException ex) { -// return null; -// } -// } -// -// } + public static class ZonedDateTimeDeserializer extends UTCBaseZonedDateTimeDeserializer { + + public ZonedDateTimeDeserializer() { + super(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + } + + public static class MDYDateDeserializer extends JsonDeserializer { + + private final DateTimeFormatter formatter; + + public MDYDateDeserializer() { + this.formatter = new DateTimeFormatterBuilder() + .parseCaseInsensitive() + .appendPattern("MMM d, [uuuu][uu]") + .toFormatter(Locale.ENGLISH); + } + + @Override + public ZonedDateTime deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException { + String date = jp.getText(); + if (date == null) { + return null; + } + + try { + LocalDate ld = LocalDate.parse(date, formatter); + LocalDateTime ldt = ld.atStartOfDay(); + return ZonedDateTime.of(ldt, ZoneOffset.UTC); + } catch (DateTimeParseException ex) { + return null; + } + } + } public static class EpochTimeDeserializer extends JsonDeserializer {