Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
StartupWindowProvider.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-2019 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.casemodule;
20 
21 import java.awt.Frame;
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.charset.Charset;
25 import java.util.Collection;
26 import java.util.Iterator;
27 import java.util.logging.Level;
28 import javax.swing.SwingUtilities;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openide.util.Lookup;
32 import org.openide.util.NbBundle;
33 import org.openide.windows.WindowManager;
43 
54 
55  private static volatile StartupWindowProvider instance;
56  private static final Logger logger = Logger.getLogger(StartupWindowProvider.class.getName());
58 
60  if (instance == null) {
61  synchronized (StartupWindowProvider.class) {
62  if (instance == null) {
63  instance = new StartupWindowProvider();
64  instance.init();
65  }
66  }
67  }
68 
69  return instance;
70  }
71 
72  @NbBundle.Messages({
73  "# {0} - autFilePath",
74  "StartupWindowProvider.openCase.noFile=Unable to open previously open case because metadata file not found at: {0}",
75  "# {0} - reOpenFilePath",
76  "StartupWindowProvider.openCase.deleteOpenFailure=Unable to open or delete file containing path {0} to previously open case. The previous case will not be opened.",
77  "# {0} - autFilePath",
78  "StartupWindowProvider.openCase.cantOpen=Unable to open previously open case with metadata file: {0}"})
79  private void init() {
80  if (startupWindowToUse == null) {
81  // first check whether we are running from command line
82  // or if we are running headless. Assume headless if the top level
83  // window is not visible.
84  boolean headless = !WindowManager.getDefault().getMainWindow().isVisible();
85  if (isRunningFromCommandLine() || headless) {
86 
87  String defaultArg = getDefaultArgument();
88  if (defaultArg != null) {
89  new CommandLineOpenCaseManager(defaultArg).start();
90  return;
91  } else {
92  // Autopsy is running from command line
93  logger.log(Level.INFO, "Running from command line"); //NON-NLS
94  if(!headless) {
95  startupWindowToUse = new CommandLineStartupWindow();
96  }
97  // kick off command line processing
99  return;
100  }
101  }
102 
104  checkSolr();
105  }
106  //discover the registered windows
107  Collection<? extends StartupWindowInterface> startupWindows
108  = Lookup.getDefault().lookupAll(StartupWindowInterface.class);
109 
110  int windowsCount = startupWindows.size();
111  switch (windowsCount) {
112  case 1:
113  startupWindowToUse = startupWindows.iterator().next();
114  logger.log(Level.INFO, "Will use the default startup window: {0}", startupWindowToUse.toString()); //NON-NLS
115  break;
116  case 2: {
117  //pick the non default one
118  Iterator<? extends StartupWindowInterface> it = startupWindows.iterator();
119  while (it.hasNext()) {
120  StartupWindowInterface window = it.next();
121  if (!org.sleuthkit.autopsy.casemodule.StartupWindow.class.isInstance(window)) {
122  startupWindowToUse = window;
123  logger.log(Level.INFO, "Will use the custom startup window: {0}", startupWindowToUse.toString()); //NON-NLS
124  break;
125  }
126  }
127  break;
128  }
129  default: {
130  // select first non-Autopsy start up window
131  Iterator<? extends StartupWindowInterface> it = startupWindows.iterator();
132  while (it.hasNext()) {
133  StartupWindowInterface window = it.next();
134  if (!window.getClass().getCanonicalName().startsWith("org.sleuthkit.autopsy")) {
135  startupWindowToUse = window;
136  logger.log(Level.INFO, "Will use the custom startup window: {0}", startupWindowToUse.toString()); //NON-NLS
137  break;
138  }
139  }
140  break;
141  }
142  }
143 
144  if (startupWindowToUse == null) {
145  logger.log(Level.SEVERE, "Unexpected error, no startup window chosen, using the default"); //NON-NLS
146  startupWindowToUse = new org.sleuthkit.autopsy.casemodule.StartupWindow();
147  }
148  }
149  File openPreviousCaseFile = new File(ResetWindowsAction.getCaseToReopenFilePath());
150 
151  if (openPreviousCaseFile.exists()) {
152  //do actual opening on another thread
153  new Thread(() -> {
154  String caseFilePath = "";
155  String unableToOpenMessage = null;
156  try {
157  //avoid readFileToString having ambiguous arguments
158  Charset encoding = null;
159  caseFilePath = FileUtils.readFileToString(openPreviousCaseFile, encoding);
160  if (new File(caseFilePath).exists()) {
161  FileUtils.forceDelete(openPreviousCaseFile);
162  //close the startup window as we attempt to open the case
163  close();
164  Case.openAsCurrentCase(caseFilePath);
165 
166  } else {
167  unableToOpenMessage = Bundle.StartupWindowProvider_openCase_noFile(caseFilePath);
168  logger.log(Level.WARNING, unableToOpenMessage);
169  }
170  } catch (IOException ex) {
171  unableToOpenMessage = Bundle.StartupWindowProvider_openCase_deleteOpenFailure(ResetWindowsAction.getCaseToReopenFilePath());
172  logger.log(Level.WARNING, unableToOpenMessage, ex);
173  } catch (CaseActionException ex) {
174  unableToOpenMessage = Bundle.StartupWindowProvider_openCase_cantOpen(caseFilePath);
175  logger.log(Level.WARNING, unableToOpenMessage, ex);
176  }
177 
178  if (RuntimeProperties.runningWithGUI() && !StringUtils.isBlank(unableToOpenMessage)) {
179  final String message = unableToOpenMessage;
180  SwingUtilities.invokeLater(() -> {
181  MessageNotifyUtil.Message.warn(message);
182  //the case was not opened restore the startup window
183  open();
184  });
185  }
186  }).start();
187  }
188  }
189 
190  private void checkSolr() {
191 
192  // if Multi-User settings are enabled and Solr8 server is not configured,
193  // display an error message and a dialog
195  // Solr 8 host name is not configured. This could be the first time user
196  // runs Autopsy with Solr 8. Display a message.
197  MessageNotifyUtil.Notify.error(NbBundle.getMessage(CueBannerPanel.class, "SolrNotConfiguredDialog.title"),
198  NbBundle.getMessage(SolrNotConfiguredDialog.class, "SolrNotConfiguredDialog.EmptyKeywordSearchHostName"));
199 
200  SolrNotConfiguredDialog dialog = new SolrNotConfiguredDialog();
201  dialog.setVisible(true);
202  }
203  }
204 
213  private boolean isRunningFromCommandLine() {
214 
215  CommandLineOptionProcessor processor = Lookup.getDefault().lookup(CommandLineOptionProcessor.class);
216  if (processor != null) {
217  return processor.isRunFromCommandLine();
218  }
219  return false;
220  }
221 
227  private String getDefaultArgument() {
228  CommandLineOptionProcessor processor = Lookup.getDefault().lookup(CommandLineOptionProcessor.class);
229  if (processor != null) {
230  return processor.getDefaultArgument();
231  }
232  return null;
233  }
234 
235  @Override
236  public void open() {
237  if (startupWindowToUse != null) {
238  startupWindowToUse.open();
239  }
240  }
241 
242  @Override
243  public void close() {
244  if (startupWindowToUse != null) {
245  startupWindowToUse.close();
246  }
247  }
248 
255  return startupWindowToUse;
256  }
257 }
static void openAsCurrentCase(String caseMetadataFilePath)
Definition: Case.java:855
static void error(String title, String message)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Thu Dec 8 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.