19 package org.sleuthkit.autopsy.keywordsearch;
 
   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;
 
   36 class RawText 
implements IndexedText {
 
   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;
 
   45     private String cachedString;
 
   46     private int cachedChunk;
 
   47     private static final Logger logger = Logger.getLogger(RawText.class.getName());
 
   60     RawText(Content content, 
long objectId) {
 
   61         this.content = content;
 
   62         this.blackboardArtifact = null;
 
   63         this.objectId = objectId;
 
   67     RawText(BlackboardArtifact bba, 
long objectId) {
 
   69         this.blackboardArtifact = bba;
 
   70         this.objectId = objectId;
 
   79     public long getObjectId() {
 
   84     public int getCurrentPage() {
 
   85         return this.currentPage;
 
   89     public boolean hasNextPage() {
 
   90         return currentPage < numPages;
 
   94     public boolean hasPreviousPage() {
 
   95         return currentPage > 1;
 
   99     public int nextPage() {
 
  100         if (!hasNextPage()) {
 
  101             throw new IllegalStateException(
 
  102                     NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.nextPage.exception.msg"));
 
  109     public int previousPage() {
 
  110         if (!hasPreviousPage()) {
 
  111             throw new IllegalStateException(
 
  112                     NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.previousPage.exception.msg"));
 
  119     public boolean hasNextItem() {
 
  120         throw new UnsupportedOperationException(
 
  121                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.hasNextItem.exception.msg"));
 
  125     public boolean hasPreviousItem() {
 
  126         throw new UnsupportedOperationException(
 
  127                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.hasPreviousItem.exception.msg"));
 
  131     public int nextItem() {
 
  132         throw new UnsupportedOperationException(
 
  133                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.nextItem.exception.msg"));
 
  137     public int previousItem() {
 
  138         throw new UnsupportedOperationException(
 
  139                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.previousItem.exception.msg"));
 
  143     public int currentItem() {
 
  144         throw new UnsupportedOperationException(
 
  145                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.currentItem.exception.msg"));
 
  149     public String getText() {
 
  151             if (this.content != null) {
 
  152                 return getContentText(currentPage, hasChunks);
 
  153             } 
else if (this.blackboardArtifact != null) {
 
  154                 return getArtifactText();
 
  156         } 
catch (SolrServerException ex) {
 
  157             logger.log(Level.SEVERE, 
"Couldn't get extracted content", ex); 
 
  159         return NbBundle.getMessage(this.getClass(), 
"RawText.getText.error.msg");
 
  163         "RawText.FileText=File Text",
 
  164         "RawText.ResultText=Result Text"})
 
  166     public String toString() {
 
  167         if (null != content) {
 
  168             return Bundle.RawText_FileText();
 
  170             return Bundle.RawText_ResultText();
 
  175     public boolean isSearchable() {
 
  180     public String getAnchorPrefix() {
 
  185     public int getNumberHits() {
 
  190     public LinkedHashMap<Integer, Integer> getHitsPages() {
 
  195     public int getNumberPages() {
 
  202     private void initialize() {
 
  203         final Server solrServer = KeywordSearch.getServer();
 
  207             numPages = solrServer.queryNumFileChunks(this.objectId);
 
  214         } 
catch (KeywordSearchModuleException ex) {
 
  215             logger.log(Level.WARNING, 
"Could not get number of chunks: ", ex); 
 
  217         } 
catch (NoOpenCoreException ex) {
 
  218             logger.log(Level.WARNING, 
"Could not get number of chunks: ", ex); 
 
  236     private String getContentText(
int currentPage, 
boolean hasChunks) 
throws SolrServerException {
 
  237         final Server solrServer = KeywordSearch.getServer();
 
  239         if (hasChunks == 
false) {
 
  243             String name = content.getName();
 
  245             if (content instanceof AbstractFile) {
 
  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);
 
  253                 msg = NbBundle.getMessage(this.getClass(), 
"ExtractedContentViewer.getSolrContent.noTxtYetMsg", name);
 
  255             String htmlMsg = NbBundle.getMessage(this.getClass(), 
"ExtractedContentViewer.getSolrContent.txtBodyItal", msg);
 
  259         int chunkId = currentPage;
 
  262         if (cachedString != null) {
 
  263             if (cachedChunk == chunkId) {
 
  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>"); 
 
  274             cachedString = sb.toString();
 
  275             cachedChunk = chunkId;
 
  276         } 
catch (NoOpenCoreException ex) {
 
  277             logger.log(Level.SEVERE, 
"No open core", ex); 
 
  283     private String getArtifactText() throws SolrServerException{
 
  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>"); 
 
  289             return sb.toString();
 
  290         } 
catch (NoOpenCoreException ex) {
 
  291             logger.log(Level.SEVERE, 
"No open core", ex);