Autopsy  4.18.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ContentViewerTagManager.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019 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.casemodule.services.contentviewertags;
20 
21 import com.fasterxml.jackson.core.JsonProcessingException;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.io.IOException;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
28 import org.sleuthkit.datamodel.ContentTag;
29 import org.sleuthkit.datamodel.TskCoreException;
30 
38 
39  //Used to convert Java beans into the physical representation that will be stored
40  //in the database.
41  private static final ObjectMapper SERIALIZER = new ObjectMapper();
42 
43  public static final String TABLE_NAME = "beta_tag_app_data";
44  public static final String TABLE_SCHEMA_SQLITE = "(app_data_id INTEGER PRIMARY KEY, "
45  + "content_tag_id INTEGER NOT NULL, app_data TEXT NOT NULL, "
46  + "FOREIGN KEY(content_tag_id) REFERENCES content_tags(tag_id))";
47  public static final String TABLE_SCHEMA_POSTGRESQL = "(app_data_id BIGSERIAL PRIMARY KEY, "
48  + "content_tag_id INTEGER NOT NULL, app_data TEXT NOT NULL, "
49  + "FOREIGN KEY(content_tag_id) REFERENCES content_tags(tag_id))";
50 
51  private static final String INSERT_TAG_DATA = "(content_tag_id, app_data) VALUES (%d, '%s')";
52  private static final String UPDATE_TAG_DATA = "SET content_tag_id = %d, app_data = '%s' WHERE app_data_id = %d";
53  private static final String SELECT_TAG_DATA = "* FROM " + TABLE_NAME + " WHERE content_tag_id = %d";
54  private static final String DELETE_TAG_DATA = "WHERE app_data_id = %d";
55 
75  public static <T> ContentViewerTag<T> saveTag(ContentTag contentTag, T tagDataBean)
76  throws SerializationException, TskCoreException, NoCurrentCaseException {
77  try {
78  long contentTagId = contentTag.getId();
79  String serialAppData = SERIALIZER.writeValueAsString(tagDataBean);
80  String insertTemplateInstance = String.format(INSERT_TAG_DATA,
81  contentTagId, serialAppData);
82  long insertId = Case.getCurrentCaseThrows()
84  .getCaseDbAccessManager()
85  .insert(TABLE_NAME, insertTemplateInstance);
86  return new ContentViewerTag<>(insertId, contentTag, tagDataBean);
87  } catch (JsonProcessingException ex) {
88  throw new SerializationException("Unable to convert object instance into a storable format", ex);
89  }
90  }
91 
107  public static <T> ContentViewerTag<T> updateTag(ContentViewerTag<T> oldTag, T tagDataBean)
108  throws SerializationException, TskCoreException, NoCurrentCaseException {
109  try {
110  String serialAppData = SERIALIZER.writeValueAsString(tagDataBean);
111  String updateTemplateInstance = String.format(UPDATE_TAG_DATA,
112  oldTag.getContentTag().getId(), serialAppData, oldTag.getId());
115  .getCaseDbAccessManager()
116  .update(TABLE_NAME, updateTemplateInstance);
117  return new ContentViewerTag<>(oldTag.getId(), oldTag.getContentTag(), tagDataBean);
118  } catch (JsonProcessingException ex) {
119  throw new SerializationException("Unable to convert object instance into a storable format", ex);
120  }
121  }
122 
142  public static <T> ContentViewerTag<T> getTag(ContentTag contentTag, Class<T> clazz) throws TskCoreException, NoCurrentCaseException {
143  String selectTemplateInstance = String.format(SELECT_TAG_DATA, contentTag.getId());
147  .getCaseDbAccessManager()
148  .select(selectTemplateInstance, (ResultSet rs) -> {
149  try {
150  if (rs.next()) {
151  long tagId = rs.getLong(1);
152  String appDetails = rs.getString(3);
153  try {
154  T instance = SERIALIZER.readValue(appDetails, clazz);
155  result.setResult(new ContentViewerTag<>(tagId, contentTag, instance));
156  } catch (IOException ex) {
157  //Databind for type T failed. Not a system error
158  //but rather a logic error on the part of the caller.
159  result.setResult(null);
160  }
161  }
162  } catch (SQLException ex) {
163  result.setException(ex);
164  }
165  });
166 
167  if (result.hasException()) {
168  throw new TskCoreException("Unable to select tag from case db", result.getException());
169  }
170 
171  return result.getResult();
172  }
173 
180  private static class ResultWrapper<T> {
181 
182  private T result;
183  private SQLException ex = null;
184 
185  public void setResult(T result) {
186  this.result = result;
187  }
188 
189  public void setException(SQLException ex) {
190  this.ex = ex;
191  }
192 
193  public boolean hasException() {
194  return this.ex != null;
195  }
196 
197  public SQLException getException() {
198  return ex;
199  }
200 
201  public T getResult() {
202  return result;
203  }
204  }
205 
215  public static <T> void deleteTag(ContentViewerTag<T> contentViewerTag) throws TskCoreException, NoCurrentCaseException {
216  String deleteTemplateInstance = String.format(DELETE_TAG_DATA, contentViewerTag.getId());
219  .getCaseDbAccessManager()
220  .delete(TABLE_NAME, deleteTemplateInstance);
221  }
222 
231  public static class ContentViewerTag<T> {
232 
233  private final long id;
234  private final ContentTag contentTag;
235  private final T details;
236 
237  private ContentViewerTag(long id, ContentTag contentTag, T details) {
238  this.id = id;
239  this.contentTag = contentTag;
240  this.details = details;
241  }
242 
243  public long getId() {
244  return id;
245  }
246 
247  public ContentTag getContentTag() {
248  return contentTag;
249  }
250 
251  public T getDetails() {
252  return details;
253  }
254  }
255 
260  public static class SerializationException extends Exception {
261 
262  public SerializationException(String message, Exception source) {
263  super(message, source);
264  }
265  }
266 
267  //Prevent this class from being instantiated.
269  }
270 }
static< T > ContentViewerTag< T > updateTag(ContentViewerTag< T > oldTag, T tagDataBean)
static< T > ContentViewerTag< T > getTag(ContentTag contentTag, Class< T > clazz)
static< T > ContentViewerTag< T > saveTag(ContentTag contentTag, T tagDataBean)

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