Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SolrIndexedText.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2023 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.util.logging.Level;
22 import org.apache.solr.client.solrj.SolrServerException;
23 import org.openide.util.NbBundle;
26 import org.sleuthkit.datamodel.AbstractFile;
27 import org.sleuthkit.datamodel.BlackboardArtifact;
28 import org.sleuthkit.datamodel.Content;
29 import org.sleuthkit.datamodel.TskData;
30 
35 class SolrIndexedText implements ExtractedText {
36 
37  private int numPages = 0;
38  private int currentPage = 0;
39  private boolean hasChunks = false;
40  private final Content content;
41  private final BlackboardArtifact blackboardArtifact;
42  private final long objectId;
43  private static final Logger logger = Logger.getLogger(SolrIndexedText.class.getName());
44 
56  SolrIndexedText(Content content, long objectId) {
57  this.content = content;
58  this.blackboardArtifact = null;
59  this.objectId = objectId;
60  initialize();
61  }
62 
63  SolrIndexedText(BlackboardArtifact bba, long objectId) {
64  this.content = null;
65  this.blackboardArtifact = bba;
66  this.objectId = objectId;
67  initialize();
68  }
69 
75  public long getObjectId() {
76  return this.objectId;
77  }
78 
79  @Override
80  public int getCurrentPage() {
81  return this.currentPage;
82  }
83 
84  @Override
85  public boolean hasNextPage() {
86  return currentPage < numPages;
87  }
88 
89  @Override
90  public boolean hasPreviousPage() {
91  return currentPage > 1;
92  }
93 
94  @Override
95  public int nextPage() {
96  if (!hasNextPage()) {
97  throw new IllegalStateException(
98  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.nextPage.exception.msg"));
99  }
100  ++currentPage;
101  return currentPage;
102  }
103 
104  @Override
105  public int previousPage() {
106  if (!hasPreviousPage()) {
107  throw new IllegalStateException(
108  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.previousPage.exception.msg"));
109  }
110  --currentPage;
111  return currentPage;
112  }
113 
114  @Override
115  public boolean hasNextItem() {
116  throw new UnsupportedOperationException(
117  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.hasNextItem.exception.msg"));
118  }
119 
120  @Override
121  public boolean hasPreviousItem() {
122  throw new UnsupportedOperationException(
123  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.hasPreviousItem.exception.msg"));
124  }
125 
126  @Override
127  public int nextItem() {
128  throw new UnsupportedOperationException(
129  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.nextItem.exception.msg"));
130  }
131 
132  @Override
133  public int previousItem() {
134  throw new UnsupportedOperationException(
135  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.previousItem.exception.msg"));
136  }
137 
138  @Override
139  public int currentItem() {
140  throw new UnsupportedOperationException(
141  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.currentItem.exception.msg"));
142  }
143 
144  @Override
145  public String getText() {
146  try {
147  if (this.content != null) {
148  return getContentText(currentPage, hasChunks);
149  } else if (this.blackboardArtifact != null) {
150  return getArtifactText();
151  }
152  } catch (SolrServerException | NoOpenCoreException ex) {
153  logger.log(Level.SEVERE, "Couldn't get extracted text", ex); //NON-NLS
154  }
155  return Bundle.ExtractedText_errorMessage_errorGettingText();
156  }
157 
158  @NbBundle.Messages({
159  "SolrIndexedText.FileText=File Text",
160  "SolrIndexedText.ResultText=Result Text"})
161  @Override
162  public String toString() {
163  if (null != content) {
164  return Bundle.SolrIndexedText_FileText();
165  } else {
166  return Bundle.SolrIndexedText_ResultText();
167  }
168  }
169 
170  @Override
171  public boolean isSearchable() {
172  return false;
173  }
174 
175  @Override
176  public String getAnchorPrefix() {
177  return "";
178  }
179 
180  @Override
181  public int getNumberHits() {
182  return 0;
183  }
184 
185  @Override
186  public int getNumberPages() {
187  return numPages;
188  }
189 
193  private void initialize() {
194  final Server solrServer = KeywordSearch.getServer();
195 
196  try {
197  //add to page tracking if not there yet
198  numPages = solrServer.queryNumFileChunks(this.objectId);
199  if (numPages == 0) {
200  numPages = 1;
201  hasChunks = false;
202  } else {
203  hasChunks = true;
204  }
205  } catch (KeywordSearchModuleException | NoOpenCoreException ex) {
206  logger.log(Level.SEVERE, "Could not get number of chunks: ", ex); //NON-NLS
207  }
208  }
209 
226  private String getContentText(int currentPage, boolean hasChunks) throws NoOpenCoreException, SolrServerException {
227  final Server solrServer = KeywordSearch.getServer();
228 
229  if (hasChunks == false) {
230  //if no chunks, it is safe to assume there is no text content
231  //because we are storing extracted text in chunks only
232  //and the non-chunk stores meta-data only
233  String msg = null;
234 
235  if (content instanceof AbstractFile) {
236  //we know it's AbstractFile, but do quick check to make sure if we index other objects in future
237  boolean isKnown = TskData.FileKnown.KNOWN.equals(((AbstractFile) content).getKnown());
238  if (isKnown && KeywordSearchSettings.getSkipKnown()) {
239  msg = Bundle.ExtractedText_warningMessage_knownFile();
240  }
241  }
242  if (msg == null) {
243  msg = Bundle.ExtractedText_warningMessage_noTextAvailable();
244  }
245  return msg;
246  }
247 
248  int chunkId = currentPage;
249  //not cached
250  String indexedText = solrServer.getSolrContent(this.objectId, chunkId);
251  if (indexedText == null) {
252  if (content instanceof AbstractFile) {
253  return Bundle.ExtractedText_errorMessage_errorGettingText();
254  } else {
255  return Bundle.ExtractedText_warningMessage_noTextAvailable();
256  }
257  } else if (indexedText.isEmpty()) {
258  return Bundle.ExtractedText_warningMessage_noTextAvailable();
259  }
260 
261  indexedText = EscapeUtil.escapeHtml(indexedText).trim();
262  StringBuilder sb = new StringBuilder(indexedText.length() + 20);
263  sb.append("<pre>").append(indexedText).append("</pre>"); //NON-NLS
264  return sb.toString();
265  }
266 
276  private String getArtifactText() throws NoOpenCoreException, SolrServerException {
277  String indexedText = KeywordSearch.getServer().getSolrContent(this.objectId, 1);
278  if (indexedText == null || indexedText.isEmpty()) {
279  return Bundle.ExtractedText_errorMessage_errorGettingText();
280  }
281 
282  indexedText = EscapeUtil.escapeHtml(indexedText).trim();
283  StringBuilder sb = new StringBuilder(indexedText.length() + 20);
284  sb.append("<pre>").append(indexedText).append("</pre>"); //NON-NLS
285 
286  return sb.toString();
287  }
288 
289 }

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