Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ObjectMapperUtil.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2023 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package com.basistech.df.cybertriage.autopsy.ctapi.util;
20
21import com.fasterxml.jackson.core.JsonParser;
22import com.fasterxml.jackson.core.JacksonException;
23import com.fasterxml.jackson.databind.DeserializationContext;
24import com.fasterxml.jackson.databind.JsonDeserializer;
25import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
28import java.io.IOException;
29import java.time.DateTimeException;
30import java.time.Instant;
31import java.time.LocalDate;
32import java.time.LocalDateTime;
33import java.time.ZoneOffset;
34import java.time.ZonedDateTime;
35import java.time.format.DateTimeFormatter;
36import java.time.format.DateTimeFormatterBuilder;
37import java.time.format.DateTimeParseException;
38import java.util.Locale;
39import java.util.function.Function;
40
44public class ObjectMapperUtil {
45
46 private static final ObjectMapperUtil instance = new ObjectMapperUtil();
47
48 public static ObjectMapperUtil getInstance() {
49 return instance;
50 }
51
52 private ObjectMapperUtil() {
53
54 }
55
56 public ObjectMapper getDefaultObjectMapper() {
57 ObjectMapper defaultMapper = new ObjectMapper();
58 defaultMapper.registerModule(new JavaTimeModule());
59 return defaultMapper;
60 }
61
62 public static class UTCBaseZonedDateTimeDeserializer extends JsonDeserializer<ZonedDateTime> {
63
64 private final DateTimeFormatter formatter;
65
66 public UTCBaseZonedDateTimeDeserializer(DateTimeFormatter formatter) {
67 this.formatter = formatter;
68 }
69
70 @Override
71 public ZonedDateTime deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException {
72 String date = jp.getText();
73 if (date == null) {
74 return null;
75 }
76
77 try {
78 LocalDateTime ldt = LocalDateTime.parse(date, formatter);
79 return ZonedDateTime.of(ldt, ZoneOffset.UTC);
80 } catch (DateTimeParseException ex) {
81 return null;
82 }
83 }
84 }
85
87
89 super(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
90 }
91 }
92
93 public static class MDYDateDeserializer extends JsonDeserializer<ZonedDateTime> {
94
95 private final DateTimeFormatter formatter;
96
98 this.formatter = new DateTimeFormatterBuilder()
99 .parseCaseInsensitive()
100 .appendPattern("MMM d, [uuuu][uu]")
101 .toFormatter(Locale.ENGLISH);
102 }
103
104 @Override
105 public ZonedDateTime deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException {
106 String date = jp.getText();
107 if (date == null) {
108 return null;
109 }
110
111 try {
112 LocalDate ld = LocalDate.parse(date, formatter);
113 LocalDateTime ldt = ld.atStartOfDay();
114 return ZonedDateTime.of(ldt, ZoneOffset.UTC);
115 } catch (DateTimeParseException ex) {
116 return null;
117 }
118 }
119 }
120
121 public static class EpochTimeDeserializer<T> extends JsonDeserializer<T> {
122
123 private final Function<Long, T> timeDeserializer;
124
125 public EpochTimeDeserializer(Function<Long, T> timeDeserializer) {
126 this.timeDeserializer = timeDeserializer;
127 }
128
129 @Override
130 public T deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException {
131 JsonNode node = jp.getCodec().readTree(jp);
132
133 Long timeVal = null;
134 if (node.isNumber()) {
135 timeVal = node.asLong();
136 } else {
137 String nodeText = node.asText();
138 try {
139 timeVal = Long.parseLong(nodeText);
140 } catch (NumberFormatException ex) {
141 // do nothing if can't parse as number
142 }
143 }
144
145 if (timeVal != null) {
146 try {
147 return timeDeserializer.apply(timeVal);
148 } catch (DateTimeException ex) {
149 // do nothing if can't parse to epoch
150 }
151 }
152
153 return null;
154 }
155 }
156
157 public static class InstantEpochMillisDeserializer extends EpochTimeDeserializer<Instant> {
158
162
163 private static Instant convert(Long longVal) {
164 try {
165 return Instant.ofEpochMilli(longVal);
166 } catch (DateTimeException ex) {
167 // do nothing if can't parse to epoch
168
169 return null;
170 }
171 }
172 }
173
174 public static class InstantEpochSecsDeserializer extends EpochTimeDeserializer<Instant> {
175
179
180 private static Instant convert(Long longVal) {
181 try {
182 return Instant.ofEpochSecond(longVal);
183 } catch (DateTimeException ex) {
184 // do nothing if can't parse to epoch
185
186 return null;
187 }
188 }
189 }
190}

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.