Autopsy  4.6.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
KeywordHit.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 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.keywordsearch;
20 
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.Comparator;
24 import java.util.Optional;
25 import org.apache.commons.lang3.StringUtils;
28 import org.sleuthkit.datamodel.SleuthkitCase;
29 import org.sleuthkit.datamodel.TskCoreException;
30 
38 class KeywordHit implements Comparable<KeywordHit> {
39 
40  private static final String GET_CONTENT_ID_FROM_ARTIFACT_ID = "SELECT obj_id FROM blackboard_artifacts WHERE artifact_id = ";
41 
42  private final long solrObjectId;
43  private final int chunkId;
44  private final String snippet;
45  private final String hit;
46 
58  KeywordHit(String solrDocumentId, String snippet, String hit) {
59  this.snippet = StringUtils.stripToEmpty(snippet);
60  this.hit = hit;
61 
62  /*
63  * Parse the Solr document id to get the Solr object id and chunk id.
64  * The Solr object id will either be the object id of a file id or an
65  * artifact id from the case database.
66  *
67  * For every object (file or artifact) there will at least two Solr
68  * documents. One contains object metadata (chunk #1) and the second and
69  * subsequent documents contain chunks of the text.
70  */
71  String[] split = solrDocumentId.split(Server.CHUNK_ID_SEPARATOR);
72  if (split.length == 1) {
73  //chunk 0 has only the bare document id without the chunk id.
74  this.solrObjectId = Long.parseLong(solrDocumentId);
75  this.chunkId = 0;
76  } else {
77  this.solrObjectId = Long.parseLong(split[0]);
78  this.chunkId = Integer.parseInt(split[1]);
79  }
80  }
81 
82  String getHit() {
83  return hit;
84  }
85 
86  String getSolrDocumentId() {
87  return Long.toString(solrObjectId) + Server.CHUNK_ID_SEPARATOR + Long.toString(chunkId);
88  }
89 
90  long getSolrObjectId() {
91  return this.solrObjectId;
92  }
93 
94  int getChunkId() {
95  return this.chunkId;
96  }
97 
98  boolean hasSnippet() {
99  return StringUtils.isNotBlank(this.snippet);
100  }
101 
102  String getSnippet() {
103  return this.snippet;
104  }
105 
117  long getContentID() throws TskCoreException {
118  if (isArtifactHit()) {
119  // If the hit was in an artifact, look up the source content for the artifact.
120  SleuthkitCase caseDb;
121  try {
122  caseDb = Case.getOpenCase().getSleuthkitCase();
123  } catch (NoCurrentCaseException ex) {
124  throw new TskCoreException("Exception while getting open case.", ex);
125  }
126  try (SleuthkitCase.CaseDbQuery executeQuery =
127  caseDb.executeQuery(GET_CONTENT_ID_FROM_ARTIFACT_ID + this.solrObjectId);
128  ResultSet resultSet = executeQuery.getResultSet();) {
129  if (resultSet.next()) {
130  return resultSet.getLong("obj_id");
131  } else {
132  throw new TskCoreException("Failed to get obj_id for artifact with artifact_id =" + this.solrObjectId + ". No matching artifact was found.");
133  }
134  } catch (SQLException ex) {
135  throw new TskCoreException("Error getting obj_id for artifact with artifact_id =" + this.solrObjectId, ex);
136  }
137  } else {
138  //else the object id is for content.
139  return this.solrObjectId;
140  }
141  }
142 
148  boolean isArtifactHit() {
149  // artifacts have negative obj ids
150  return this.solrObjectId < 0;
151  }
152 
158  Optional<Long> getArtifactID() {
159  if (isArtifactHit()) {
160  return Optional.of(solrObjectId);
161  } else {
162  return Optional.empty();
163  }
164  }
165 
166  @Override
167  public boolean equals(Object obj) {
168  if (null == obj) {
169  return false;
170  }
171  if (getClass() != obj.getClass()) {
172  return false;
173  }
174  final KeywordHit other = (KeywordHit) obj;
175  return this.compareTo(other) == 0;
176  }
177 
178  @Override
179  public int hashCode() {
180  int hash = 3;
181  hash = 41 * hash + (int) this.solrObjectId + this.chunkId;
182  return hash;
183  }
184 
185  @Override
186  public int compareTo(KeywordHit o) {
187  return Comparator.comparing(KeywordHit::getSolrObjectId)
188  .thenComparing(KeywordHit::getChunkId)
189  .compare(this, o);
190  }
191 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.