Autopsy  4.0
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-2015 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 KeywordSearch.getServer().getSolrContent(this.objectId, 1);
155  }
156  } catch (SolrServerException | NoOpenCoreException ex) {
157  logger.log(Level.WARNING, "Couldn't get extracted content.", ex); //NON-NLS
158  }
159  return NbBundle.getMessage(this.getClass(), "RawText.getText.error.msg");
160  }
161 
162  @Override
163  public String toString() {
164  return NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.toString");
165  }
166 
167  @Override
168  public boolean isSearchable() {
169  return false;
170  }
171 
172  @Override
173  public String getAnchorPrefix() {
174  return "";
175  }
176 
177  @Override
178  public int getNumberHits() {
179  return 0;
180  }
181 
182  @Override
183  public LinkedHashMap<Integer, Integer> getHitsPages() {
184  return null;
185  }
186 
187  @Override
188  public int getNumberPages() {
189  return numPages;
190  }
191 
195  private void initialize() {
196  final Server solrServer = KeywordSearch.getServer();
197 
198  try {
199  //add to page tracking if not there yet
200  numPages = solrServer.queryNumFileChunks(this.objectId);
201  if (numPages == 0) {
202  numPages = 1;
203  hasChunks = false;
204  } else {
205  hasChunks = true;
206  }
207  } catch (KeywordSearchModuleException ex) {
208  logger.log(Level.WARNING, "Could not get number of chunks: ", ex); //NON-NLS
209 
210  } catch (NoOpenCoreException ex) {
211  logger.log(Level.WARNING, "Could not get number of chunks: ", ex); //NON-NLS
212  }
213  }
214 
229  private String getContentText(int currentPage, boolean hasChunks) throws 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 name = content.getName();
237  String msg = null;
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 = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.knownFileMsg", name);
243  }
244  }
245  if (msg == null) {
246  msg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.noTxtYetMsg", name);
247  }
248  String htmlMsg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.txtBodyItal", msg);
249  return htmlMsg;
250  }
251 
252  int chunkId = currentPage;
253 
254  //check if cached
255  if (cachedString != null) {
256  if (cachedChunk == chunkId) {
257  return cachedString;
258  }
259  }
260 
261  //not cached
262  try {
263  String indexedText = solrServer.getSolrContent(this.objectId, chunkId);
264  cachedString = EscapeUtil.escapeHtml(indexedText).trim();
265  StringBuilder sb = new StringBuilder(cachedString.length() + 20);
266  sb.append("<pre>").append(cachedString).append("</pre>"); //NON-NLS
267  cachedString = sb.toString();
268  cachedChunk = chunkId;
269  } catch (NoOpenCoreException ex) {
270  logger.log(Level.WARNING, "Couldn't get text content.", ex); //NON-NLS
271  return "";
272  }
273  return cachedString;
274  }
275 }

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.