From b2f89ddd36f1b0bec791e5588bb5504545a16ff8 Mon Sep 17 00:00:00 2001
From: Greg DiCristofaro <gregd@basistech.com>
Date: Fri, 22 Sep 2023 10:52:58 -0400
Subject: [PATCH] upgrade from solr4

---
 .../ModalDialogProgressIndicator.java         |  7 ++
 .../keywordsearch/Bundle.properties-MERGED    | 10 ++-
 .../autopsy/keywordsearch/Server.java         | 17 ++--
 .../keywordsearch/SolrSearchService.java      | 84 +++++++++++++++++++
 4 files changed, 111 insertions(+), 7 deletions(-)

diff --git a/Core/src/org/sleuthkit/autopsy/progress/ModalDialogProgressIndicator.java b/Core/src/org/sleuthkit/autopsy/progress/ModalDialogProgressIndicator.java
index 31fb900c56..b3d5c6b828 100644
--- a/Core/src/org/sleuthkit/autopsy/progress/ModalDialogProgressIndicator.java
+++ b/Core/src/org/sleuthkit/autopsy/progress/ModalDialogProgressIndicator.java
@@ -263,4 +263,11 @@ private void displayDialog() {
         dialog.setLocationRelativeTo(parent);
         this.dialog.setVisible(true);
     }
+    
+    /**
+     * @return The GUI dialog presenting the progress.  Possibly null.
+     */
+    public Dialog getDialog() {
+        return this.dialog;
+    }
 }
diff --git a/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Bundle.properties-MERGED b/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Bundle.properties-MERGED
index 0bf48a5370..6db88c1ba0 100755
--- a/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Bundle.properties-MERGED
+++ b/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Bundle.properties-MERGED
@@ -52,7 +52,7 @@ KeywordSearchResultFactory.createNodeForKey.noResultsFound.text=No results found
 KeywordSearchResultFactory.query.exception.msg=Could not perform the query 
 OpenIDE-Module-Display-Category=Ingest Module
 
-OpenIDE-Module-Long-Description=Keyword Search ingest module.\n\nThe module indexes files found in the disk image at ingest time.\nIt then periodically runs the search on the indexed files using one or more keyword lists (containing pure words and/or regular expressions) and posts results.\n\n\The module also contains additional tools integrated in the main GUI, such as keyword list configuration, keyword search bar in the top-right corner, extracted text viewer and search results viewer showing highlighted keywords found.
+OpenIDE-Module-Long-Description=Keyword Search ingest module.\n\nThe module indexes files found in the disk image at ingest time.\nIt then periodically runs the search on the indexed files using one or more keyword lists (containing pure words and/or regular expressions) and posts results.\n\nThe module also contains additional tools integrated in the main GUI, such as keyword list configuration, keyword search bar in the top-right corner, extracted text viewer and search results viewer showing highlighted keywords found.
 OpenIDE-Module-Name=KeywordSearch
 OptionsCategory_Name_KeywordSearchOptions=Keyword Search
 OptionsCategory_Keywords_KeywordSearchOptions=Keyword Search
@@ -306,6 +306,14 @@ KeywordSearchModuleFactory.getIngestJobSettingsPanel.exception.msg=Expected sett
 KeywordSearchModuleFactory.createFileIngestModule.exception.msg=Expected settings argument to be instanceof KeywordSearchJobSettings
 SearchRunner.Searcher.done.err.msg=Error performing keyword search
 Server.status.failed.msg=Local Solr server did not respond to status request. This may be because the server failed to start or is taking too long to initialize.
+# {0} - indexVersion
+Server_configureSolrConnection_illegalSolrVersion=The solr version in the case: {0}, is not supported.
+# {0} - solrVersion
+# {1} - caseName
+Server_configureSolrConnection_unsupportedSolrDesc=<html><body><p style="width: 400px">The current Solr version: {0} in the case: {1} is no longer supported.  You can continue without upgrading, but Solr will not be usable while the case is open, and you will encounter errors.  You can also choose to upgrade the Solr version for the case.  If you choose to do this, you will need to run Keyword Search with Solr indexing selected in order to use Solr features like ad hoc search with images in the case.</p></body></html>
+Server_configureSolrConnection_unsupportedSolrDisableOpt=Continue
+Server_configureSolrConnection_unsupportedSolrTitle=Unsupported Solr Version
+Server_configureSolrConnection_unsupportedSolrUpgradeOpt=Upgrade Solr Core
 SolrConnectionCheck.HostnameOrPort=Invalid hostname and/or port number.
 SolrConnectionCheck.Hostname=Invalid hostname.
 SolrConnectionCheck.MissingHostname=Missing hostname.
diff --git a/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Server.java b/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Server.java
index 9d04595f09..b918a81054 100644
--- a/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Server.java
+++ b/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Server.java
@@ -57,6 +57,7 @@
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 import static java.util.stream.Collectors.toList;
+import javax.swing.JOptionPane;
 import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrRequest;
 import org.apache.solr.client.solrj.SolrServerException;
@@ -82,6 +83,7 @@
 import org.openide.modules.InstalledFileLocator;
 import org.openide.modules.Places;
 import org.openide.util.NbBundle;
+import org.openide.util.NbBundle.Messages;
 import org.openide.windows.WindowManager;
 import org.sleuthkit.autopsy.casemodule.Case;
 import org.sleuthkit.autopsy.casemodule.Case.CaseType;
@@ -611,18 +613,22 @@ void start() throws KeywordSearchModuleException, SolrServerNoPortException, Sol
         startLocalSolr(SOLR_VERSION.SOLR8);
     }
     
+    @Messages({
+        "# {0} - indexVersion",
+        "Server_configureSolrConnection_illegalSolrVersion=The solr version in the case: {0}, is not supported."
+    })
     private void configureSolrConnection(Case theCase, Index index) throws KeywordSearchModuleException, SolrServerNoPortException {
         
         try {
             if (theCase.getCaseType() == CaseType.SINGLE_USER_CASE) {
 
                 // makes sure the proper local Solr server is running
-                if (IndexFinder.getCurrentSolrVersion().equals(index.getSolrVersion())) {
-                    startLocalSolr(SOLR_VERSION.SOLR8);
-                } else {
-                    startLocalSolr(SOLR_VERSION.SOLR4);
+                if (!IndexFinder.getCurrentSolrVersion().equals(index.getSolrVersion())) {
+                    throw new KeywordSearchModuleException(Bundle.Server_configureSolrConnection_illegalSolrVersion(index.getSolrVersion()));
                 }
 
+                startLocalSolr(SOLR_VERSION.SOLR8);
+                
                 // check if the local Solr server is running
                 if (!this.isLocalSolrRunning()) {
                     logger.log(Level.SEVERE, "Local Solr server is not running"); //NON-NLS
@@ -684,8 +690,7 @@ synchronized void startLocalSolr(SOLR_VERSION version) throws KeywordSearchModul
         if (version == SOLR_VERSION.SOLR8) {
             localSolrFolder = InstalledFileLocator.getDefault().locate("solr", Server.class.getPackage().getName(), false); //NON-NLS
         } else {
-            // solr4
-            localSolrFolder = InstalledFileLocator.getDefault().locate("solr4", Server.class.getPackage().getName(), false); //NON-NLS
+            throw new KeywordSearchModuleException(Bundle.Server_configureSolrConnection_illegalSolrVersion(version.name()));
         }
 
         if (isLocalSolrRunning()) {
diff --git a/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/SolrSearchService.java b/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/SolrSearchService.java
index d9dd4d921a..f7f82a7b27 100644
--- a/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/SolrSearchService.java
+++ b/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/SolrSearchService.java
@@ -18,6 +18,7 @@
  */
 package org.sleuthkit.autopsy.keywordsearch;
 
+import java.awt.Component;
 import java.io.File;
 import java.io.IOException;
 import java.io.Reader;
@@ -26,18 +27,22 @@
 import java.util.List;
 import java.util.MissingResourceException;
 import java.util.logging.Level;
+import javax.swing.JOptionPane;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.openide.util.NbBundle;
 import org.openide.util.lookup.ServiceProvider;
 import org.openide.util.lookup.ServiceProviders;
+import org.openide.windows.WindowManager;
 import org.sleuthkit.autopsy.appservices.AutopsyService;
 import org.sleuthkit.autopsy.casemodule.Case;
 import org.sleuthkit.autopsy.casemodule.CaseMetadata;
+import org.sleuthkit.autopsy.core.RuntimeProperties;
 import org.sleuthkit.autopsy.coreutils.FileUtil;
 import org.sleuthkit.autopsy.coreutils.Logger;
 import org.sleuthkit.autopsy.ingest.IngestManager;
 import org.sleuthkit.autopsy.keywordsearchservice.KeywordSearchService;
 import org.sleuthkit.autopsy.keywordsearchservice.KeywordSearchServiceException;
+import org.sleuthkit.autopsy.progress.ModalDialogProgressIndicator;
 import org.sleuthkit.autopsy.progress.ProgressIndicator;
 import org.sleuthkit.autopsy.textextractors.TextExtractor;
 import org.sleuthkit.autopsy.textextractors.TextExtractorFactory;
@@ -315,11 +320,25 @@ public void openCaseResources(CaseContext context) throws AutopsyServiceExceptio
                     }
                     throw new AutopsyServiceException(Bundle.SolrSearch_unableToFindIndex_msg());
                 }
+                
+
 
                 if (context.cancelRequested()) {
                     return;
                 }
 
+                if (!IndexFinder.getCurrentSolrVersion().equals(indexToUse.getSolrVersion())) {
+                    Index prevIndex = indexToUse;
+                    indexToUse = tryUpgradeSolrVersion(context, indexToUse);
+                    if (indexToUse != prevIndex) {
+                        indexes.add(indexToUse);    
+                    }
+                }
+                
+                if (context.cancelRequested()) {
+                    return;
+                }
+                                
                 // check if schema is compatible
                 if (!indexToUse.isCompatible(IndexFinder.getCurrentSchemaVersion())) {
                     String msg = "Text index schema version " + indexToUse.getSchemaVersion() + " is not compatible with current schema";
@@ -355,6 +374,71 @@ public void openCaseResources(CaseContext context) throws AutopsyServiceExceptio
 
         progress.progress(Bundle.SolrSearch_complete_msg(), totalNumProgressUnits);
     }
+    
+    
+    private static final long WAIT_TIME_MILLIS = 2000;
+    
+    /**
+     * Attempts to upgrade the solr version to most recent version first prompting the user.
+     * @param context The case context.
+     * @param index The current index.
+     * @return The new index.
+     * @throws org.sleuthkit.autopsy.appservices.AutopsyService.AutopsyServiceException 
+     */
+    @NbBundle.Messages({
+        "Server_configureSolrConnection_unsupportedSolrTitle=Unsupported Solr Version",
+        "# {0} - solrVersion",
+        "# {1} - caseName",
+        "Server_configureSolrConnection_unsupportedSolrDesc=<html><body><p style=\"width: 400px\">The current Solr version: {0} in the case: {1} is no longer supported.  You can continue without upgrading, but Solr will not be usable while the case is open, and you will encounter errors.  You can also choose to upgrade the Solr version for the case.  If you choose to do this, you will need to run Keyword Search with Solr indexing selected in order to use Solr features like ad hoc search with images in the case.</p></body></html>",
+        "Server_configureSolrConnection_unsupportedSolrDisableOpt=Continue",
+        "Server_configureSolrConnection_unsupportedSolrUpgradeOpt=Upgrade Solr Core"
+    })
+    private Index tryUpgradeSolrVersion(CaseContext context, Index index) throws AutopsyServiceException {
+        // if not, attempt to fix issue
+        if (RuntimeProperties.runningWithGUI()) {
+            Component parentComponent = WindowManager.getDefault().getMainWindow();
+            if (context.getProgressIndicator() instanceof ModalDialogProgressIndicator progInd && progInd.getDialog() != null) {
+                parentComponent = progInd.getDialog();
+
+            }
+            
+            if (context.cancelRequested()) {
+                return index;
+            }
+
+            try {
+                // progress updates occur right before this in the same window, so there is the possibility that
+                // the progress window will update just after the option pane is shown causing the option pane to
+                // not be visible or selectable.  This sleep is added to give the window enough time to finish
+                Thread.sleep(WAIT_TIME_MILLIS);
+            } catch (InterruptedException ex) {
+                // just proceed if interrupted
+            }
+            
+            if (context.cancelRequested()) {
+                return index;
+            }
+
+            int selection = JOptionPane.showOptionDialog(
+                    parentComponent,
+                    Bundle.Server_configureSolrConnection_unsupportedSolrDesc(index.getSolrVersion(), context.getCase().getDisplayName()),
+                    Bundle.Server_configureSolrConnection_unsupportedSolrTitle(),
+                    JOptionPane.YES_NO_OPTION,
+                    JOptionPane.WARNING_MESSAGE,
+                    null,
+                    new Object[]{
+                        Bundle.Server_configureSolrConnection_unsupportedSolrDisableOpt(),
+                        Bundle.Server_configureSolrConnection_unsupportedSolrUpgradeOpt()
+                    },
+                    Bundle.Server_configureSolrConnection_unsupportedSolrDisableOpt());
+
+            if (selection == 1) {
+                return IndexFinder.createLatestVersionIndex(context.getCase());
+            }
+        }
+
+        throw new AutopsyServiceException("Unsupported Solr version: " + index.getSolrVersion());
+    }
 
     /**
      * Closes the open core.
-- 
GitLab