19 package org.sleuthkit.autopsy.keywordsearch;
 
   21 import java.util.logging.Level;
 
   22 import org.apache.solr.client.solrj.SolrServerException;
 
   23 import org.openide.util.NbBundle;
 
   35 class RawText 
implements IndexedText {
 
   37     private int numPages = 0;
 
   38     private int currentPage = 0;
 
   39     private boolean hasChunks = 
false;
 
   40     private final Content content;
 
   41     private final BlackboardArtifact blackboardArtifact;
 
   42     private final long objectId;
 
   44     private String cachedString;
 
   45     private int cachedChunk;
 
   46     private static final Logger logger = Logger.getLogger(RawText.class.getName());
 
   59     RawText(Content content, 
long objectId) {
 
   60         this.content = content;
 
   61         this.blackboardArtifact = null;
 
   62         this.objectId = objectId;
 
   66     RawText(BlackboardArtifact bba, 
long objectId) {
 
   68         this.blackboardArtifact = bba;
 
   69         this.objectId = objectId;
 
   78     public long getObjectId() {
 
   83     public int getCurrentPage() {
 
   84         return this.currentPage;
 
   88     public boolean hasNextPage() {
 
   89         return currentPage < numPages;
 
   93     public boolean hasPreviousPage() {
 
   94         return currentPage > 1;
 
   98     public int nextPage() {
 
  100             throw new IllegalStateException(
 
  101                     NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.nextPage.exception.msg"));
 
  108     public int previousPage() {
 
  109         if (!hasPreviousPage()) {
 
  110             throw new IllegalStateException(
 
  111                     NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.previousPage.exception.msg"));
 
  118     public boolean hasNextItem() {
 
  119         throw new UnsupportedOperationException(
 
  120                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.hasNextItem.exception.msg"));
 
  124     public boolean hasPreviousItem() {
 
  125         throw new UnsupportedOperationException(
 
  126                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.hasPreviousItem.exception.msg"));
 
  130     public int nextItem() {
 
  131         throw new UnsupportedOperationException(
 
  132                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.nextItem.exception.msg"));
 
  136     public int previousItem() {
 
  137         throw new UnsupportedOperationException(
 
  138                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.previousItem.exception.msg"));
 
  142     public int currentItem() {
 
  143         throw new UnsupportedOperationException(
 
  144                 NbBundle.getMessage(
this.getClass(), 
"ExtractedContentViewer.currentItem.exception.msg"));
 
  148     public String getText() {
 
  150             if (this.content != null) {
 
  151                 return getContentText(currentPage, hasChunks);
 
  152             } 
else if (this.blackboardArtifact != null) {
 
  153                 return getArtifactText();
 
  155         } 
catch (SolrServerException | NoOpenCoreException ex) {
 
  156             logger.log(Level.SEVERE, 
"Couldn't get extracted text", ex); 
 
  158         return Bundle.IndexedText_errorMessage_errorGettingText();
 
  162         "RawText.FileText=File Text",
 
  163         "RawText.ResultText=Result Text"})
 
  165     public String toString() {
 
  166         if (null != content) {
 
  167             return Bundle.RawText_FileText();
 
  169             return Bundle.RawText_ResultText();
 
  174     public boolean isSearchable() {
 
  179     public String getAnchorPrefix() {
 
  184     public int getNumberHits() {
 
  189     public int getNumberPages() {
 
  196     private void initialize() {
 
  197         final Server solrServer = KeywordSearch.getServer();
 
  201             numPages = solrServer.queryNumFileChunks(this.objectId);
 
  208         } 
catch (KeywordSearchModuleException | NoOpenCoreException ex) {
 
  209             logger.log(Level.SEVERE, 
"Could not get number of chunks: ", ex); 
 
  229     private String getContentText(
int currentPage, 
boolean hasChunks) 
throws NoOpenCoreException, SolrServerException {
 
  230         final Server solrServer = KeywordSearch.getServer();
 
  232         if (hasChunks == 
false) {
 
  238             if (content instanceof AbstractFile) {
 
  240                 boolean isKnown = TskData.FileKnown.KNOWN.equals(((AbstractFile) content).getKnown());
 
  241                 if (isKnown && KeywordSearchSettings.getSkipKnown()) {
 
  242                     msg = Bundle.IndexedText_warningMessage_knownFile();
 
  246                 msg = Bundle.IndexedText_warningMessage_noTextAvailable();
 
  251         int chunkId = currentPage;
 
  254         if (cachedString != null) {
 
  255             if (cachedChunk == chunkId) {
 
  261         String indexedText = solrServer.getSolrContent(this.objectId, chunkId);
 
  262         if (indexedText == null) {
 
  263             if (content instanceof AbstractFile) {
 
  264                 return Bundle.IndexedText_errorMessage_errorGettingText();
 
  266                 return Bundle.IndexedText_warningMessage_noTextAvailable();
 
  268         } 
else if (indexedText.isEmpty()) {
 
  269             return Bundle.IndexedText_warningMessage_noTextAvailable();
 
  272         cachedString = EscapeUtil.escapeHtml(indexedText).trim();
 
  273         StringBuilder sb = 
new StringBuilder(cachedString.length() + 20);
 
  274         sb.append(
"<pre>").append(cachedString).append(
"</pre>"); 
 
  275         cachedString = sb.toString();
 
  276         cachedChunk = chunkId;
 
  290     private String getArtifactText() throws NoOpenCoreException, SolrServerException {
 
  291         String indexedText = KeywordSearch.getServer().getSolrContent(this.objectId, 1);
 
  292         if (indexedText == null || indexedText.isEmpty()) {
 
  293             return Bundle.IndexedText_errorMessage_errorGettingText();
 
  296         indexedText = EscapeUtil.escapeHtml(indexedText).trim();
 
  297         StringBuilder sb = 
new StringBuilder(indexedText.length() + 20);
 
  298         sb.append(
"<pre>").append(indexedText).append(
"</pre>"); 
 
  300         return sb.toString();