Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
RawTextMarkup.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;
30 
35 class RawTextMarkup implements TextMarkup {
36  private int numPages = 0;
37  private int currentPage = 0;
38  private boolean hasChunks = false;
39 
40  private final Content content;
41  private final long objectId;
42  //keep last content cached
43  private String cachedString;
44  private int cachedChunk;
45  private static final Logger logger = Logger.getLogger(RawTextMarkup.class.getName());
46 
57  RawTextMarkup(Content content, long objectId) {
58  this.content = content;
59  this.objectId = objectId;
60  initialize();
61  }
62 
67  public long getObjectId() {
68  return this.objectId;
69  }
70 
71  @Override
72  public int getCurrentPage() {
73  return this.currentPage;
74  }
75 
76  @Override
77  public boolean hasNextPage() {
78  return currentPage < numPages;
79  }
80 
81  @Override
82  public boolean hasPreviousPage() {
83  return currentPage > 1;
84  }
85 
86  @Override
87  public int nextPage() {
88  if (!hasNextPage()) {
89  throw new IllegalStateException(
90  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.nextPage.exception.msg"));
91  }
92  ++currentPage;
93  return currentPage;
94  }
95 
96  @Override
97  public int previousPage() {
98  if (!hasPreviousPage()) {
99  throw new IllegalStateException(
100  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.previousPage.exception.msg"));
101  }
102  --currentPage;
103  return currentPage;
104  }
105 
106  @Override
107  public boolean hasNextItem() {
108  throw new UnsupportedOperationException(
109  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.hasNextItem.exception.msg"));
110  }
111 
112  @Override
113  public boolean hasPreviousItem() {
114  throw new UnsupportedOperationException(
115  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.hasPreviousItem.exception.msg"));
116  }
117 
118  @Override
119  public int nextItem() {
120  throw new UnsupportedOperationException(
121  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.nextItem.exception.msg"));
122  }
123 
124  @Override
125  public int previousItem() {
126  throw new UnsupportedOperationException(
127  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.previousItem.exception.msg"));
128  }
129 
130  @Override
131  public int currentItem() {
132  throw new UnsupportedOperationException(
133  NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.currentItem.exception.msg"));
134  }
135 
136  @Override
137  public String getMarkup() {
138  try {
139  return getSolrContent(currentPage, hasChunks);
140  } catch (SolrServerException ex) {
141  logger.log(Level.WARNING, "Couldn't get extracted content.", ex); //NON-NLS
142  return "";
143  }
144  }
145 
146  @Override
147  public String toString() {
148  return NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.toString");
149  }
150 
151  @Override
152  public boolean isSearchable() {
153  return false;
154  }
155 
156  @Override
157  public String getAnchorPrefix() {
158  return "";
159  }
160 
161  @Override
162  public int getNumberHits() {
163  return 0;
164  }
165 
166  @Override
167  public LinkedHashMap<Integer, Integer> getHitsPages() {
168  return null;
169  }
170 
171  @Override
172  public int getNumberPages() {
173  return numPages;
174  }
175 
179  private void initialize() {
180  final Server solrServer = KeywordSearch.getServer();
181 
182  try {
183  //add to page tracking if not there yet
184  numPages = solrServer.queryNumFileChunks(this.objectId);
185  if (numPages == 0) {
186  numPages = 1;
187  hasChunks = false;
188  } else {
189  hasChunks = true;
190  }
191  } catch (KeywordSearchModuleException ex) {
192  logger.log(Level.WARNING, "Could not get number of chunks: ", ex); //NON-NLS
193 
194  } catch (NoOpenCoreException ex) {
195  logger.log(Level.WARNING, "Could not get number of chunks: ", ex); //NON-NLS
196  }
197  }
198 
199 
211  private String getSolrContent(int currentPage, boolean hasChunks) throws SolrServerException {
212  final Server solrServer = KeywordSearch.getServer();
213 
214  if (hasChunks == false) {
215  //if no chunks, it is safe to assume there is no text content
216  //because we are storing extracted text in chunks only
217  //and the non-chunk stores meta-data only
218  String name = content.getName();
219  String msg = null;
220  if (content instanceof AbstractFile) {
221  //we know it's AbstractFile, but do quick check to make sure if we index other objects in future
222  boolean isKnown = TskData.FileKnown.KNOWN.equals(((AbstractFile) content).getKnown());
223  if (isKnown && KeywordSearchSettings.getSkipKnown()) {
224  msg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.knownFileMsg", name);
225  }
226  }
227  if (msg == null) {
228  msg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.noTxtYetMsg", name);
229  }
230  String htmlMsg = NbBundle.getMessage(this.getClass(), "ExtractedContentViewer.getSolrContent.txtBodyItal", msg);
231  return htmlMsg;
232  }
233 
234  int chunkId = currentPage;
235 
236  //check if cached
237  if (cachedString != null) {
238  if (cachedChunk == chunkId) {
239  return cachedString;
240  }
241  }
242 
243  //not cached
244  try {
245  String indexedText = solrServer.getSolrContent(this.objectId, chunkId);
246  cachedString = EscapeUtil.escapeHtml(indexedText).trim();
247  StringBuilder sb = new StringBuilder(cachedString.length() + 20);
248  sb.append("<pre>").append(cachedString).append("</pre>"); //NON-NLS
249  cachedString = sb.toString();
250  cachedChunk = chunkId;
251  } catch (NoOpenCoreException ex) {
252  logger.log(Level.WARNING, "Couldn't get text content.", ex); //NON-NLS
253  return "";
254  }
255  return cachedString;
256  }
257 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.