19 package org.sleuthkit.autopsy.keywordsearch;
 
   21 import java.util.ArrayList;
 
   22 import java.util.Collection;
 
   23 import java.util.HashMap;
 
   24 import java.util.List;
 
   27 import java.util.logging.Level;
 
   28 import javax.swing.SwingUtilities;
 
   29 import javax.swing.SwingWorker;
 
   30 import org.apache.commons.lang.StringUtils;
 
   31 import org.openide.util.NbBundle;
 
   55     private static final Logger logger = Logger.getLogger(QueryResults.class.getName());
 
   56     private static final String MODULE_NAME = KeywordSearchModuleFactory.getModuleName();
 
   57     private final KeywordSearchQuery query;
 
   58     private final Map<Keyword, List<KeywordHit>> results = 
new HashMap<>();
 
   60     private static final int MAX_INBOX_NOTIFICATIONS_PER_KW_TERM = 20;
 
   72     QueryResults(KeywordSearchQuery query) {
 
   82     KeywordSearchQuery getQuery() {
 
   94     void addResult(Keyword keyword, List<KeywordHit> hits) {
 
   95         results.put(keyword, hits);
 
  105     List<KeywordHit> getResults(Keyword keyword) {
 
  106         return results.get(keyword);
 
  115     Set<Keyword> getKeywords() {
 
  116         return results.keySet();
 
  145     void process(SwingWorker<?, ?> worker, 
boolean notifyInbox, 
boolean saveResults, Long ingestJobId) {
 
  146         final Collection<BlackboardArtifact> hitArtifacts = 
new ArrayList<>();
 
  148         int notificationCount = 0;
 
  149         for (
final Keyword keyword : getKeywords()) {
 
  153             if (worker.isCancelled()) {
 
  154                 logger.log(Level.INFO, 
"Processing cancelled, exiting before processing search term {0}", keyword.getSearchTerm()); 
 
  163             for (KeywordHit hit : getOneHitPerTextSourceObject(keyword)) {
 
  170                 String snippet = hit.getSnippet();
 
  171                 if (StringUtils.isBlank(snippet)) {
 
  172                     final String snippetQuery = KeywordSearchUtil.escapeLuceneQuery(keyword.getSearchTerm());
 
  174                         snippet = LuceneQuery.querySnippet(snippetQuery, hit.getSolrObjectId(), hit.getChunkId(), !query.isLiteral(), 
true);
 
  175                     } 
catch (NoOpenCoreException e) {
 
  176                         logger.log(Level.SEVERE, 
"Solr core closed while executing snippet query " + snippetQuery, e); 
 
  178                     } 
catch (Exception e) {
 
  179                         logger.log(Level.SEVERE, 
"Error executing snippet query " + snippetQuery, e); 
 
  188                 Content content = null;
 
  190                     SleuthkitCase tskCase = Case.getCurrentCaseThrows().getSleuthkitCase();
 
  191                     content = tskCase.getContentById(hit.getContentID());
 
  192                 } 
catch (TskCoreException | NoCurrentCaseException tskCoreException) {
 
  193                     logger.log(Level.SEVERE, 
"Failed to get text source object for keyword hit", tskCoreException); 
 
  196                 if ((content != null) && saveResults) {
 
  200                     BlackboardArtifact artifact = query.createKeywordHitArtifact(content, keyword, hit, snippet, query.getKeywordList().getName(), ingestJobId);
 
  205                     if (null != artifact) {
 
  206                         hitArtifacts.add(artifact);
 
  207                         if (notifyInbox && notificationCount < MAX_INBOX_NOTIFICATIONS_PER_KW_TERM) {
 
  213                                 writeSingleFileInboxMessage(artifact, content);
 
  214                             } 
catch (TskCoreException ex) {
 
  215                                 logger.log(Level.SEVERE, 
"Error sending message to ingest messages inbox", ex); 
 
  227         if (!hitArtifacts.isEmpty()) {
 
  229                 SleuthkitCase tskCase = Case.getCurrentCaseThrows().getSleuthkitCase();
 
  230                 Blackboard blackboard = tskCase.getBlackboard();
 
  232                 blackboard.postArtifacts(hitArtifacts, MODULE_NAME, ingestJobId);
 
  233             } 
catch (NoCurrentCaseException | Blackboard.BlackboardException ex) {
 
  234                 logger.log(Level.SEVERE, 
"Failed to post KWH artifact to blackboard.", ex); 
 
  248     private Collection<KeywordHit> getOneHitPerTextSourceObject(Keyword keyword) {
 
  254         HashMap< Long, KeywordHit> hits = 
new HashMap<>();
 
  255         getResults(keyword).forEach((hit) -> {
 
  256             if (!hits.containsKey(hit.getSolrObjectId())) {
 
  257                 hits.put(hit.getSolrObjectId(), hit);
 
  258             } 
else if (hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
 
  259                 hits.put(hit.getSolrObjectId(), hit);
 
  262         return hits.values();
 
  275     private void writeSingleFileInboxMessage(
final BlackboardArtifact artifact, 
final Content hitContent) 
throws TskCoreException {
 
  276         if (artifact != null && hitContent != null && RuntimeProperties.runningWithGUI()) {
 
  277             final StringBuilder subjectSb = 
new StringBuilder(1024);
 
  278             if (!query.isLiteral()) {
 
  279                 subjectSb.append(NbBundle.getMessage(
this.getClass(), 
"KeywordSearchIngestModule.regExpHitLbl"));
 
  281                 subjectSb.append(NbBundle.getMessage(
this.getClass(), 
"KeywordSearchIngestModule.kwHitLbl"));
 
  284             final StringBuilder detailsSb = 
new StringBuilder(1024);
 
  285             String uniqueKey = null;
 
  286             BlackboardAttribute attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD));
 
  288                 final String keyword = attr.getValueString();
 
  289                 subjectSb.append(keyword);
 
  290                 uniqueKey = keyword.toLowerCase();
 
  291                 detailsSb.append(
"<table border='0' cellpadding='4' width='280'>"); 
 
  292                 detailsSb.append(
"<tr>"); 
 
  293                 detailsSb.append(NbBundle.getMessage(
this.getClass(), 
"KeywordSearchIngestModule.kwHitThLbl"));
 
  294                 detailsSb.append(
"<td>").append(EscapeUtil.escapeHtml(keyword)).append(
"</td>"); 
 
  295                 detailsSb.append(
"</tr>"); 
 
  299             attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW));
 
  301                 detailsSb.append(
"<tr>"); 
 
  302                 detailsSb.append(NbBundle.getMessage(
this.getClass(), 
"KeywordSearchIngestModule.previewThLbl"));
 
  303                 detailsSb.append(
"<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append(
"</td>"); 
 
  304                 detailsSb.append(
"</tr>"); 
 
  308             detailsSb.append(
"<tr>"); 
 
  309             detailsSb.append(NbBundle.getMessage(
this.getClass(), 
"KeywordSearchIngestModule.fileThLbl"));
 
  310             if (hitContent instanceof AbstractFile) {
 
  311                 AbstractFile hitFile = (AbstractFile) hitContent;
 
  312                 detailsSb.append(
"<td>").append(hitFile.getParentPath()).append(hitFile.getName()).append(
"</td>"); 
 
  314                 detailsSb.append(
"<td>").append(hitContent.getName()).append(
"</td>"); 
 
  316             detailsSb.append(
"</tr>"); 
 
  319             attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME));
 
  321                 detailsSb.append(
"<tr>"); 
 
  322                 detailsSb.append(NbBundle.getMessage(
this.getClass(), 
"KeywordSearchIngestModule.listThLbl"));
 
  323                 detailsSb.append(
"<td>").append(attr.getValueString()).append(
"</td>"); 
 
  324                 detailsSb.append(
"</tr>"); 
 
  328             if (!query.isLiteral()) {
 
  329                 attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP));
 
  331                     detailsSb.append(
"<tr>"); 
 
  332                     detailsSb.append(NbBundle.getMessage(
this.getClass(), 
"KeywordSearchIngestModule.regExThLbl"));
 
  333                     detailsSb.append(
"<td>").append(attr.getValueString()).append(
"</td>"); 
 
  334                     detailsSb.append(
"</tr>"); 
 
  337             detailsSb.append(
"</table>"); 
 
  339             final String key = uniqueKey; 
 
  340             SwingUtilities.invokeLater(() -> {
 
  341                 IngestServices.getInstance().postMessage(IngestMessage.createDataMessage(MODULE_NAME, subjectSb.toString(), detailsSb.toString(), key, artifact));