Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ParameterizedResourceConfig.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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  */
19 package org.sleuthkit.autopsy.integrationtesting.config;
20 
21 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22 import com.fasterxml.jackson.core.JsonParser;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.DeserializationContext;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
27 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
28 import com.fasterxml.jackson.databind.node.ArrayNode;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
30 import com.fasterxml.jackson.databind.node.TextNode;
31 import com.fasterxml.jackson.databind.node.ValueNode;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.Iterator;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.Map;
39 
43 @JsonIgnoreProperties(ignoreUnknown = true)
44 @JsonDeserialize(using = ParameterizedResourceConfig.ParameterizedResourceConfigDeserializer.class)
46 
54  public static class ParameterizedResourceConfigDeserializer extends StdDeserializer<ParameterizedResourceConfig> {
55 
56  private static final long serialVersionUID = 1L;
57 
62  this(null);
63  }
64 
71  super(vc);
72  }
73 
74  @Override
75  public ParameterizedResourceConfig deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
76  JsonNode node = jp.getCodec().readTree(jp);
77 
78  // if no node, return null.
79  if (node == null) {
80  return null;
81  } else if (node instanceof TextNode) {
82  // if just a string, return a ParameterizedResourceConfig where the resource is the string.
83  return new ParameterizedResourceConfig(node.textValue());
84  } else {
85  // otherwise, determine the resource and create an object
86  JsonNode resourceNode = node.get("resource");
87  String resource = (resourceNode != null) ? resourceNode.asText() : null;
88 
89  Map<String, Object> parameters = null;
90  JsonNode parametersNode = node.get("parameters");
91  if (parametersNode != null && parametersNode.isObject()) {
92  parameters = readMap((ObjectNode) parametersNode);
93  }
94 
95  return new ParameterizedResourceConfig(resource, parameters);
96  }
97  }
98 
105  Map<String, Object> readMap(ObjectNode node) {
106  Map<String, Object> jsonObject = new LinkedHashMap<>();
107  Iterator<Map.Entry<String, JsonNode>> keyValIter = node.fields();
108  while (keyValIter.hasNext()) {
109  Map.Entry<String, JsonNode> keyVal = keyValIter.next();
110  jsonObject.put(keyVal.getKey(), readItem(keyVal.getValue()));
111  }
112  return jsonObject;
113  }
114 
121  List<Object> readList(ArrayNode node) {
122  List<Object> objArr = new ArrayList<>();
123  for (JsonNode childNode : node) {
124  objArr.add(readItem(childNode));
125  }
126  return objArr;
127  }
128 
136  Object readJsonPrimitive(ValueNode vNode) {
137  if (vNode.isTextual()) {
138  return vNode.asText();
139  } else if (vNode.isBoolean()) {
140  return vNode.asBoolean();
141  } else if (vNode.isLong()) {
142  return vNode.asLong();
143  } else if (vNode.isInt()) {
144  return vNode.asInt();
145  } else if (vNode.isDouble()) {
146  return vNode.asDouble();
147  }
148 
149  return null;
150  }
151 
158  Object readItem(JsonNode node) {
159  if (node == null) {
160  return null;
161  }
162 
163  if (node.isObject()) {
164  return readMap((ObjectNode) node);
165  } else if (node.isArray()) {
166  return readList((ArrayNode) node);
167  } else if (node.isValueNode()) {
168  return readJsonPrimitive((ValueNode) node);
169  }
170 
171  return null;
172  }
173  }
174 
175  private final String resource;
176  private final Map<String, Object> parameters;
177 
184  public ParameterizedResourceConfig(String resource, Map<String, Object> parameters) {
185  this.resource = resource;
186  this.parameters = (parameters == null) ? Collections.emptyMap() : parameters;
187  }
188 
194  public ParameterizedResourceConfig(String resource) {
195  this(resource, null);
196  }
197 
201  public String getResource() {
202  return resource;
203  }
204 
210  public Map<String, Object> getParameters() {
211  return parameters == null ? Collections.emptyMap() : Collections.unmodifiableMap(parameters);
212  }
213 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.