19 package org.sleuthkit.autopsy.logicalimager.configuration;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonDeserializationContext;
23 import com.google.gson.JsonDeserializer;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParseException;
27 import java.lang.reflect.Type;
28 import java.util.ArrayList;
29 import java.util.List;
32 import org.openide.util.NbBundle;
38 "LogicalImagerConfigDeserializer.missingRuleSetException=Missing rule-set",
40 "LogicalImagerConfigDeserializer.unsupportedKeyException=Unsupported key: {0}",
41 "LogicalImagerConfigDeserializer.fullPathsException=A rule with full-paths cannot have other rule definitions",})
42 class LogicalImagerConfigDeserializer implements JsonDeserializer<LogicalImagerConfig> {
45 public LogicalImagerConfig deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException {
46 String version = LogicalImagerConfig.getCurrentVersion();
47 boolean finalizeImageWriter =
false;
48 boolean promptBeforeExit =
true;
49 boolean createVHD =
false;
51 final JsonObject jsonObject = je.getAsJsonObject();
52 final JsonElement jsonVersion = jsonObject.get(
"version");
53 if (jsonVersion != null) {
54 version = jsonVersion.getAsString();
57 final JsonElement jsonFinalizeImageWriter = jsonObject.get(
"finalize-image-writer");
58 if (jsonFinalizeImageWriter != null) {
59 finalizeImageWriter = jsonFinalizeImageWriter.getAsBoolean();
62 final JsonElement jsonPromptBeforeExit = jsonObject.get(
"prompt-before-exit");
63 if (jsonPromptBeforeExit != null) {
64 promptBeforeExit = jsonPromptBeforeExit.getAsBoolean();
67 final JsonElement jsonCreateVHD = jsonObject.get(
"create-VHD");
68 if (jsonCreateVHD != null) {
69 createVHD = jsonCreateVHD.getAsBoolean();
72 JsonArray asJsonArray = jsonObject.get(
"rule-sets").getAsJsonArray();
73 if (asJsonArray == null) {
74 throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_missingRuleSetException());
77 List<LogicalImagerRuleSet> ruleSets =
new ArrayList<>();
78 for (JsonElement element : asJsonArray) {
79 String setName = null;
80 List<LogicalImagerRule> rules = null;
81 JsonObject asJsonObject = element.getAsJsonObject();
82 JsonElement setNameElement = asJsonObject.get(
"set-name");
83 setName = setNameElement.getAsString();
84 JsonElement rulesElement = asJsonObject.get(
"rules");
85 rules = parseRules(rulesElement.getAsJsonArray());
86 LogicalImagerRuleSet ruleSet =
new LogicalImagerRuleSet(setName, rules);
87 ruleSets.add(ruleSet);
89 return new LogicalImagerConfig(version, finalizeImageWriter, promptBeforeExit, createVHD, ruleSets);
92 private List<LogicalImagerRule> parseRules(JsonArray asJsonArray) {
93 List<LogicalImagerRule> rules =
new ArrayList<>();
95 for (JsonElement element : asJsonArray) {
97 Boolean shouldSave =
false;
98 Boolean shouldAlert =
true;
100 String description = null;
101 List<String> extensions = null;
102 List<String> paths = null;
103 List<String> fullPaths = null;
104 List<String> filenames = null;
105 Long minFileSize = null;
106 Long maxFileSize = null;
107 Integer minDays = null;
108 Integer minDate = null;
109 Integer maxDate = null;
111 Set<Map.Entry<String, JsonElement>> entrySet = element.getAsJsonObject().entrySet();
113 for (Map.Entry<String, JsonElement> entry1 : entrySet) {
114 key1 = entry1.getKey();
117 shouldAlert = entry1.getValue().getAsBoolean();
120 shouldSave = entry1.getValue().getAsBoolean();
123 name = entry1.getValue().getAsString();
126 description = entry1.getValue().getAsString();
129 JsonArray extensionsArray = entry1.getValue().getAsJsonArray();
130 extensions =
new ArrayList<>();
131 for (JsonElement e : extensionsArray) {
132 extensions.add(e.getAsString());
136 JsonArray pathsArray = entry1.getValue().getAsJsonArray();
137 paths =
new ArrayList<>();
138 for (JsonElement e : pathsArray) {
139 paths.add(e.getAsString());
143 JsonArray filenamesArray = entry1.getValue().getAsJsonArray();
144 filenames =
new ArrayList<>();
145 for (JsonElement e : filenamesArray) {
146 filenames.add(e.getAsString());
150 JsonArray fullPathsArray = entry1.getValue().getAsJsonArray();
151 fullPaths =
new ArrayList<>();
152 for (JsonElement e : fullPathsArray) {
153 fullPaths.add(e.getAsString());
157 JsonObject sizeRangeObject = entry1.getValue().getAsJsonObject();
158 Set<Map.Entry<String, JsonElement>> entrySet1 = sizeRangeObject.entrySet();
159 for (Map.Entry<String, JsonElement> entry2 : entrySet1) {
160 String sizeKey = entry2.getKey();
163 minFileSize = entry2.getValue().getAsLong();
166 maxFileSize = entry2.getValue().getAsLong();
169 throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(sizeKey));
174 JsonObject dateRangeObject = entry1.getValue().getAsJsonObject();
175 Set<Map.Entry<String, JsonElement>> entrySet2 = dateRangeObject.entrySet();
176 for (Map.Entry<String, JsonElement> entry2 : entrySet2) {
177 String dateKey = entry2.getKey();
180 minDate = entry2.getValue().getAsInt();
183 maxDate = entry2.getValue().getAsInt();
186 minDays = entry2.getValue().getAsInt();
189 throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(dateKey));
194 throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(key1));
199 if ((fullPaths != null && !fullPaths.isEmpty()) && ((extensions != null && !extensions.isEmpty())
200 || (paths != null && !paths.isEmpty())
201 || (filenames != null && !filenames.isEmpty()))) {
202 throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_fullPathsException());
205 LogicalImagerRule rule =
new LogicalImagerRule.Builder()
206 .getShouldAlert(shouldAlert)
207 .getShouldSave(shouldSave)
209 .getDescription(description)
210 .getExtensions(extensions)
212 .getFullPaths(fullPaths)
213 .getFilenames(filenames)
214 .getMinFileSize(minFileSize)
215 .getMaxFileSize(maxFileSize)