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

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