Sleuth Kit Java Bindings (JNI)  4.11.0
Java bindings for using The Sleuth Kit
BlackboardJsonAttrUtil.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
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.datamodel.blackboardutils.attributes;
20 
21 import com.google.gson.Gson;
22 import com.google.gson.JsonSyntaxException;
24 
29 public final class BlackboardJsonAttrUtil {
30 
44  public static <T> BlackboardAttribute toAttribute(BlackboardAttribute.Type attrType, String moduleName, T attrValue) {
45  if (attrType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.JSON) {
46  throw new IllegalArgumentException(String.format("Attribute type %s does not have value type BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.JSON", attrType.getTypeName()));
47  }
48  return new BlackboardAttribute(attrType, moduleName, (new Gson()).toJson(attrValue));
49  }
50 
67  public static <T> T fromAttribute(BlackboardAttribute attr, Class<T> clazz) throws InvalidJsonException {
68  BlackboardAttribute.Type attrType = attr.getAttributeType();
69  if (attrType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.JSON) {
70  throw new IllegalArgumentException(String.format("Attribute type %s does not have value type BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.JSON", attrType.getTypeName()));
71  }
72  String json = attr.getValueString();
73  if (json == null || json.isEmpty()) {
74  throw new InvalidJsonException("The string value (JSON) of the attribute is null or empty");
75  }
76  try {
77  return (new Gson()).fromJson(json, clazz);
78  } catch (JsonSyntaxException ex) {
79  throw new InvalidJsonException(String.format("The string value (JSON) could not be deserialized as a %s", clazz.getName()), ex);
80  }
81  }
82 
87  public static class InvalidJsonException extends Exception {
88 
89  private static final long serialVersionUID = 1L;
90 
97  public InvalidJsonException(String message) {
98  super(message);
99  }
100 
108  public InvalidJsonException(String message, Throwable cause) {
109  super(message, cause);
110  }
111  }
112 
116  private BlackboardJsonAttrUtil() {
117  }
118 
119 }
static< T > BlackboardAttribute toAttribute(BlackboardAttribute.Type attrType, String moduleName, T attrValue)
static< T > T fromAttribute(BlackboardAttribute attr, Class< T > clazz)

Copyright © 2011-2021 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.