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-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.util.LinkedHashMap;
22 import java.util.logging.Level;
23 import org.apache.solr.client.solrj.SolrServerException;
24 import org.openide.util.NbBundle;
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 
190  @Override
191  public int getNumberPages() {
192  return numPages;
193  }
194 
198  private void initialize() {
199  final Server solrServer = KeywordSearch.getServer();
200 
201  try {
202  //add to page tracking if not there yet
203  numPages = solrServer.queryNumFileChunks(this.objectId);
204  if (numPages == 0) {
205  numPages = 1;
206  hasChunks = false;
207  } else {
208  hasChunks = true;
209  }
210  } catch (KeywordSearchModuleException ex) {
211  logger.log(Level.WARNING, "Could not get number of chunks: ", ex); //NON-NLS
212 
213  } catch (NoOpenCoreException ex) {
214  logger.log(Level.WARNING, "Could not get number of chunks: ", ex); //NON-NLS
215  }
216  }
217 
232  private String getContentText(int currentPage, boolean hasChunks) throws SolrServerException {
233  final Server solrServer = KeywordSearch.getServer();
234 
235  if (hasChunks == false) {
236  //if no chunks, it is safe to assume there is no text content
237  //because we are storing extracted text in chunks only
238  //and the non-chunk stores meta-data only
239  String name = content.getName();
240  String msg = null;
241  if (content instanceof AbstractFile) {
242  //we know it's AbstractFile, but do quick check to make sure if we index other objects in future
243  boolean isKnown = TskData.FileKnown.KNOWN.equals(((AbstractFile) content).getKnown());
244  if (isKnown && KeywordSearchSettings.getSkipKnown()) {
245  msg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.knownFileMsg", name);
246  }
247  }
248  if (msg == null) {
249  msg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.noTxtYetMsg", name);
250  }
251  String htmlMsg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.txtBodyItal", msg);
252  return htmlMsg;
253  }
254 
255  int chunkId = currentPage;
256 
257  //check if cached
258  if (cachedString != null) {
259  if (cachedChunk == chunkId) {
260  return cachedString;
261  }
262  }
263 
264  //not cached
265  try {
266  String indexedText = solrServer.getSolrContent(this.objectId, chunkId);
267  cachedString = EscapeUtil.escapeHtml(indexedText).trim();
268  StringBuilder sb = new StringBuilder(cachedString.length() + 20);
269  sb.append("<pre>").append(cachedString).append("</pre>"); //NON-NLS
270  cachedString = sb.toString();
271  cachedChunk = chunkId;
272  } catch (NoOpenCoreException ex) {
273  logger.log(Level.SEVERE, "No open core", ex); //NON-NLS
274  return "";
275  }
276  return cachedString;
277  }
278 
279  private String getArtifactText() throws SolrServerException{
280  try {
281  String indexedText = KeywordSearch.getServer().getSolrContent(this.objectId, 1);
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  return sb.toString();
286  } catch (NoOpenCoreException ex) {
287  logger.log(Level.SEVERE, "No open core", ex); //NON-NLS
288  return "";
289  }
290  }
291 
292 }

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