Autopsy  4.1
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-2016 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.LinkedHashMap;
22 import java.util.logging.Level;
23 import org.apache.solr.client.solrj.SolrServerException;
24 import org.openide.util.NbBundle;
27 import org.sleuthkit.datamodel.AbstractFile;
28 import org.sleuthkit.datamodel.BlackboardArtifact;
29 import org.sleuthkit.datamodel.Content;
30 import org.sleuthkit.datamodel.TskData;
31 
36 class RawText implements IndexedText {
37 
38  private int numPages = 0;
39  private int currentPage = 0;
40  private boolean hasChunks = false;
41  private final Content content;
42  private final BlackboardArtifact blackboardArtifact;
43  private final long objectId;
44  //keep last content cached
45  private String cachedString;
46  private int cachedChunk;
47  private static final Logger logger = Logger.getLogger(RawText.class.getName());
48 
60  RawText(Content content, long objectId) {
61  this.content = content;
62  this.blackboardArtifact = null;
63  this.objectId = objectId;
64  initialize();
65  }
66 
67  RawText(BlackboardArtifact bba, long objectId) {
68  this.content = null;
69  this.blackboardArtifact = bba;
70  this.objectId = objectId;
71  initialize();
72  }
73 
79  public long getObjectId() {
80  return this.objectId;
81  }
82 
83  @Override
84  public int getCurrentPage() {
85  return this.currentPage;
86  }
87 
88  @Override
89  public boolean hasNextPage() {
90  return currentPage < numPages;
91  }
92 
93  @Override
94  public boolean hasPreviousPage() {
95  return currentPage > 1;
96  }
97 
98  @Override
99  public int nextPage() {
100  if (!hasNextPage()) {
101  throw new IllegalStateException(
102  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.nextPage.exception.msg"));
103  }
104  ++currentPage;
105  return currentPage;
106  }
107 
108  @Override
109  public int previousPage() {
110  if (!hasPreviousPage()) {
111  throw new IllegalStateException(
112  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.previousPage.exception.msg"));
113  }
114  --currentPage;
115  return currentPage;
116  }
117 
118  @Override
119  public boolean hasNextItem() {
120  throw new UnsupportedOperationException(
121  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.hasNextItem.exception.msg"));
122  }
123 
124  @Override
125  public boolean hasPreviousItem() {
126  throw new UnsupportedOperationException(
127  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.hasPreviousItem.exception.msg"));
128  }
129 
130  @Override
131  public int nextItem() {
132  throw new UnsupportedOperationException(
133  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.nextItem.exception.msg"));
134  }
135 
136  @Override
137  public int previousItem() {
138  throw new UnsupportedOperationException(
139  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.previousItem.exception.msg"));
140  }
141 
142  @Override
143  public int currentItem() {
144  throw new UnsupportedOperationException(
145  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.currentItem.exception.msg"));
146  }
147 
148  @Override
149  public String getText() {
150  try {
151  if (this.content != null) {
152  return getContentText(currentPage, hasChunks);
153  } else if (this.blackboardArtifact != null) {
154  return getArtifactText();
155  }
156  } catch (SolrServerException ex) {
157  logger.log(Level.SEVERE, "Couldn't get extracted content", ex); //NON-NLS
158  }
159  return NbBundle.getMessage(this.getClass(), "RawText.getText.error.msg");
160  }
161 
162  @NbBundle.Messages({
163  "RawText.FileText=File Text",
164  "RawText.ResultText=Result Text"})
165  @Override
166  public String toString() {
167  if (null != content) {
168  return Bundle.RawText_FileText();
169  } else {
170  return Bundle.RawText_ResultText();
171  }
172  }
173 
174  @Override
175  public boolean isSearchable() {
176  return false;
177  }
178 
179  @Override
180  public String getAnchorPrefix() {
181  return "";
182  }
183 
184  @Override
185  public int getNumberHits() {
186  return 0;
187  }
188 
189  @Override
190  public LinkedHashMap<Integer, Integer> getHitsPages() {
191  return null;
192  }
193 
194  @Override
195  public int getNumberPages() {
196  return numPages;
197  }
198 
202  private void initialize() {
203  final Server solrServer = KeywordSearch.getServer();
204 
205  try {
206  //add to page tracking if not there yet
207  numPages = solrServer.queryNumFileChunks(this.objectId);
208  if (numPages == 0) {
209  numPages = 1;
210  hasChunks = false;
211  } else {
212  hasChunks = true;
213  }
214  } catch (KeywordSearchModuleException ex) {
215  logger.log(Level.WARNING, "Could not get number of chunks: ", ex); //NON-NLS
216 
217  } catch (NoOpenCoreException ex) {
218  logger.log(Level.WARNING, "Could not get number of chunks: ", ex); //NON-NLS
219  }
220  }
221 
236  private String getContentText(int currentPage, boolean hasChunks) throws SolrServerException {
237  final Server solrServer = KeywordSearch.getServer();
238 
239  if (hasChunks == false) {
240  //if no chunks, it is safe to assume there is no text content
241  //because we are storing extracted text in chunks only
242  //and the non-chunk stores meta-data only
243  String name = content.getName();
244  String msg = null;
245  if (content instanceof AbstractFile) {
246  //we know it's AbstractFile, but do quick check to make sure if we index other objects in future
247  boolean isKnown = TskData.FileKnown.KNOWN.equals(((AbstractFile) content).getKnown());
248  if (isKnown && KeywordSearchSettings.getSkipKnown()) {
249  msg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.knownFileMsg", name);
250  }
251  }
252  if (msg == null) {
253  msg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.noTxtYetMsg", name);
254  }
255  String htmlMsg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.txtBodyItal", msg);
256  return htmlMsg;
257  }
258 
259  int chunkId = currentPage;
260 
261  //check if cached
262  if (cachedString != null) {
263  if (cachedChunk == chunkId) {
264  return cachedString;
265  }
266  }
267 
268  //not cached
269  try {
270  String indexedText = solrServer.getSolrContent(this.objectId, chunkId);
271  cachedString = EscapeUtil.escapeHtml(indexedText).trim();
272  StringBuilder sb = new StringBuilder(cachedString.length() + 20);
273  sb.append("<pre>").append(cachedString).append("</pre>"); //NON-NLS
274  cachedString = sb.toString();
275  cachedChunk = chunkId;
276  } catch (NoOpenCoreException ex) {
277  logger.log(Level.SEVERE, "No open core", ex); //NON-NLS
278  return "";
279  }
280  return cachedString;
281  }
282 
283  private String getArtifactText() throws SolrServerException{
284  try {
285  String indexedText = KeywordSearch.getServer().getSolrContent(this.objectId, 1);
286  indexedText = EscapeUtil.escapeHtml(indexedText).trim();
287  StringBuilder sb = new StringBuilder(indexedText.length() + 20);
288  sb.append("<pre>").append(indexedText).append("</pre>"); //NON-NLS
289  return sb.toString();
290  } catch (NoOpenCoreException ex) {
291  logger.log(Level.SEVERE, "No open core", ex); //NON-NLS
292  return "";
293  }
294  }
295 
296 }

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