Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
RawText.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.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 RawText implements IndexedText {
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  //keep last content cached
44  private String cachedString;
45  private int cachedChunk;
46  private static final Logger logger = Logger.getLogger(RawText.class.getName());
47 
59  RawText(Content content, long objectId) {
60  this.content = content;
61  this.blackboardArtifact = null;
62  this.objectId = objectId;
63  initialize();
64  }
65 
66  RawText(BlackboardArtifact bba, long objectId) {
67  this.content = null;
68  this.blackboardArtifact = bba;
69  this.objectId = objectId;
70  initialize();
71  }
72 
78  public long getObjectId() {
79  return this.objectId;
80  }
81 
82  @Override
83  public int getCurrentPage() {
84  return this.currentPage;
85  }
86 
87  @Override
88  public boolean hasNextPage() {
89  return currentPage < numPages;
90  }
91 
92  @Override
93  public boolean hasPreviousPage() {
94  return currentPage > 1;
95  }
96 
97  @Override
98  public int nextPage() {
99  if (!hasNextPage()) {
100  throw new IllegalStateException(
101  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.nextPage.exception.msg"));
102  }
103  ++currentPage;
104  return currentPage;
105  }
106 
107  @Override
108  public int previousPage() {
109  if (!hasPreviousPage()) {
110  throw new IllegalStateException(
111  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.previousPage.exception.msg"));
112  }
113  --currentPage;
114  return currentPage;
115  }
116 
117  @Override
118  public boolean hasNextItem() {
119  throw new UnsupportedOperationException(
120  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.hasNextItem.exception.msg"));
121  }
122 
123  @Override
124  public boolean hasPreviousItem() {
125  throw new UnsupportedOperationException(
126  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.hasPreviousItem.exception.msg"));
127  }
128 
129  @Override
130  public int nextItem() {
131  throw new UnsupportedOperationException(
132  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.nextItem.exception.msg"));
133  }
134 
135  @Override
136  public int previousItem() {
137  throw new UnsupportedOperationException(
138  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.previousItem.exception.msg"));
139  }
140 
141  @Override
142  public int currentItem() {
143  throw new UnsupportedOperationException(
144  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.currentItem.exception.msg"));
145  }
146 
147  @Override
148  public String getText() {
149  try {
150  if (this.content != null) {
151  return getContentText(currentPage, hasChunks);
152  } else if (this.blackboardArtifact != null) {
153  return getArtifactText();
154  }
155  } catch (SolrServerException | NoOpenCoreException ex) {
156  logger.log(Level.SEVERE, "Couldn't get extracted text", ex); //NON-NLS
157  }
158  return Bundle.IndexedText_errorMessage_errorGettingText();
159  }
160 
161  @NbBundle.Messages({
162  "RawText.FileText=File Text",
163  "RawText.ResultText=Result Text"})
164  @Override
165  public String toString() {
166  if (null != content) {
167  return Bundle.RawText_FileText();
168  } else {
169  return Bundle.RawText_ResultText();
170  }
171  }
172 
173  @Override
174  public boolean isSearchable() {
175  return false;
176  }
177 
178  @Override
179  public String getAnchorPrefix() {
180  return "";
181  }
182 
183  @Override
184  public int getNumberHits() {
185  return 0;
186  }
187 
188  @Override
189  public int getNumberPages() {
190  return numPages;
191  }
192 
196  private void initialize() {
197  final Server solrServer = KeywordSearch.getServer();
198 
199  try {
200  //add to page tracking if not there yet
201  numPages = solrServer.queryNumFileChunks(this.objectId);
202  if (numPages == 0) {
203  numPages = 1;
204  hasChunks = false;
205  } else {
206  hasChunks = true;
207  }
208  } catch (KeywordSearchModuleException | NoOpenCoreException ex) {
209  logger.log(Level.SEVERE, "Could not get number of chunks: ", ex); //NON-NLS
210  }
211  }
212 
229  private String getContentText(int currentPage, boolean hasChunks) throws NoOpenCoreException, SolrServerException {
230  final Server solrServer = KeywordSearch.getServer();
231 
232  if (hasChunks == false) {
233  //if no chunks, it is safe to assume there is no text content
234  //because we are storing extracted text in chunks only
235  //and the non-chunk stores meta-data only
236  String msg = null;
237 
238  if (content instanceof AbstractFile) {
239  //we know it's AbstractFile, but do quick check to make sure if we index other objects in future
240  boolean isKnown = TskData.FileKnown.KNOWN.equals(((AbstractFile) content).getKnown());
241  if (isKnown && KeywordSearchSettings.getSkipKnown()) {
242  msg = Bundle.IndexedText_warningMessage_knownFile();
243  }
244  }
245  if (msg == null) {
246  msg = Bundle.IndexedText_warningMessage_noTextAvailable();
247  }
248  return msg;
249  }
250 
251  int chunkId = currentPage;
252 
253  //check if cached
254  if (cachedString != null) {
255  if (cachedChunk == chunkId) {
256  return cachedString;
257  }
258  }
259 
260  //not cached
261  String indexedText = solrServer.getSolrContent(this.objectId, chunkId);
262  if (indexedText == null) {
263  if (content instanceof AbstractFile) {
264  return Bundle.IndexedText_errorMessage_errorGettingText();
265  } else {
266  return Bundle.IndexedText_warningMessage_noTextAvailable();
267  }
268  } else if (indexedText.isEmpty()) {
269  return Bundle.IndexedText_warningMessage_noTextAvailable();
270  }
271 
272  cachedString = EscapeUtil.escapeHtml(indexedText).trim();
273  StringBuilder sb = new StringBuilder(cachedString.length() + 20);
274  sb.append("<pre>").append(cachedString).append("</pre>"); //NON-NLS
275  cachedString = sb.toString();
276  cachedChunk = chunkId;
277 
278  return cachedString;
279  }
280 
290  private String getArtifactText() throws NoOpenCoreException, SolrServerException {
291  String indexedText = KeywordSearch.getServer().getSolrContent(this.objectId, 1);
292  if (indexedText == null || indexedText.isEmpty()) {
293  return Bundle.IndexedText_errorMessage_errorGettingText();
294  }
295 
296  indexedText = EscapeUtil.escapeHtml(indexedText).trim();
297  StringBuilder sb = new StringBuilder(indexedText.length() + 20);
298  sb.append("<pre>").append(indexedText).append("</pre>"); //NON-NLS
299 
300  return sb.toString();
301  }
302 
303 }

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