Autopsy  4.4.1
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 String solrDocumentId;
42  private final long solrObjectId;
43  private final int chunkId;
44  private final String snippet;
45  private final long contentID;
46  private final boolean hitOnArtifact;
47  private final String hit;
48 
63  KeywordHit(String solrDocumentId, String snippet, String hit) throws TskCoreException {
64  this.snippet = StringUtils.stripToEmpty(snippet);
65  this.hit = hit;
66  this.solrDocumentId = solrDocumentId;
67 
68  /*
69  * Parse the Solr document id to get the Solr object id and chunk id.
70  * The Solr object id will either be the object id of a file id or an
71  * artifact id from the case database.
72  *
73  * For every object (file or artifact) there will at least two Solr
74  * documents. One contains object metadata (chunk #1) and the second and
75  * subsequent documents contain chunks of the text.
76  */
77  String[] split = solrDocumentId.split(Server.CHUNK_ID_SEPARATOR);
78  if (split.length == 1) {
79  //chunk 0 has only the bare document id without the chunk id.
80  this.solrObjectId = Long.parseLong(solrDocumentId);
81  this.chunkId = 0;
82  } else {
83  this.solrObjectId = Long.parseLong(split[0]);
84  this.chunkId = Integer.parseInt(split[1]);
85  }
86 
87  //artifacts have negative obj ids
88  hitOnArtifact = this.solrObjectId < 0;
89 
90  if (hitOnArtifact) {
91  // If the hit was in an artifact, look up the source content for the artifact.
92  SleuthkitCase caseDb = Case.getCurrentCase().getSleuthkitCase();
93  try (SleuthkitCase.CaseDbQuery executeQuery =
94  caseDb.executeQuery(GET_CONTENT_ID_FROM_ARTIFACT_ID + this.solrObjectId);
95  ResultSet resultSet = executeQuery.getResultSet();) {
96  if (resultSet.next()) {
97  contentID = resultSet.getLong("obj_id");
98  } else {
99  throw new TskCoreException("Failed to get obj_id for artifact with artifact_id =" + this.solrObjectId + ". No matching artifact was found.");
100  }
101  } catch (SQLException ex) {
102  throw new TskCoreException("Error getting obj_id for artifact with artifact_id =" + this.solrObjectId, ex);
103  }
104  } else {
105  //else the object id is for content.
106  contentID = this.solrObjectId;
107  }
108  }
109 
110  String getHit() {
111  return hit;
112  }
113 
114  String getSolrDocumentId() {
115  return this.solrDocumentId;
116  }
117 
118  long getSolrObjectId() {
119  return this.solrObjectId;
120  }
121 
122  int getChunkId() {
123  return this.chunkId;
124  }
125 
126  boolean hasSnippet() {
127  return StringUtils.isNotBlank(this.snippet);
128  }
129 
130  String getSnippet() {
131  return this.snippet;
132  }
133 
134  long getContentID() {
135  return this.contentID;
136  }
137 
143  boolean isArtifactHit() {
144  return hitOnArtifact;
145  }
146 
152  Optional<Long> getArtifactID() {
153  if (hitOnArtifact) {
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: Fri Sep 29 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.