Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
UserMachinePreferences.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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.machinesettings;
20 
21 import java.io.File;
22 import java.nio.file.Paths;
23 import java.util.Optional;
24 import java.util.prefs.Preferences;
25 import java.util.stream.Stream;
26 import org.apache.commons.lang3.StringUtils;
27 import org.openide.util.NbBundle;
28 import org.openide.util.NbPreferences;
31 
38 public final class UserMachinePreferences {
39 
40  private static final Logger logger = Logger.getLogger(UserMachinePreferences.class.getName());
41  private static final Preferences preferences = NbPreferences.forModule(UserMachinePreferences.class);
42 
46  public enum TempDirChoice {
59 
68  static Optional<TempDirChoice> getValue(String val) {
69  if (val == null) {
70  return Optional.empty();
71  }
72 
73  return Stream.of(TempDirChoice.values())
74  .filter(tempChoice -> tempChoice.name().equalsIgnoreCase(val.trim()))
75  .findFirst();
76  }
77  }
78 
79  private static final String CUSTOM_TEMP_DIR_KEY = "TempDirectory";
80  private static final String TEMP_DIR_CHOICE_KEY = "TempDirChoice";
81 
83 
87  public static String getCustomTempDirectory() {
88  return preferences.get(CUSTOM_TEMP_DIR_KEY, "");
89  }
90 
103  @NbBundle.Messages({
104  "# {0} - path",
105  "UserMachinePreferences_validateTempDirectory_errorOnCreate_text=There was an error creating the temp directory for path: {0}",
106  "# {0} - path",
107  "UserMachinePreferences_validateTempDirectory_errorOnReadWrite_text=There was an error reading or writing to temp directory path: {0}"
108  })
109  private static boolean validateTempDirectory(String path) throws UserMachinePreferencesException {
110  if (StringUtils.isBlank(path)) {
111  // in this instance, the default path will be used.
112  return true;
113  }
114 
115  File f = new File(path);
116  if (!f.exists() && !f.mkdirs()) {
117  throw new UserMachinePreferencesException(Bundle.UserMachinePreferences_validateTempDirectory_errorOnCreate_text(path));
118  }
119 
120  if (!FileUtil.hasReadWriteAccess(Paths.get(path))) {
121  throw new UserMachinePreferencesException(Bundle.UserMachinePreferences_validateTempDirectory_errorOnReadWrite_text(path));
122  }
123  return true;
124  }
125 
134  public static void setCustomTempDirectory(String path) throws UserMachinePreferencesException {
135  validateTempDirectory(path);
136  preferences.put(CUSTOM_TEMP_DIR_KEY, path);
137  }
138 
144  public static TempDirChoice getTempDirChoice() {
145  return TempDirChoice.getValue(preferences.get(TEMP_DIR_CHOICE_KEY, null))
146  .orElse(DEFAULT_CHOICE);
147  }
148 
156  public static void setTempDirChoice(TempDirChoice tempDirChoice) throws UserMachinePreferencesException {
157  if (tempDirChoice == null) {
158  throw new UserMachinePreferencesException("Expected non-null temp dir choice");
159  }
160 
161  preferences.put(TEMP_DIR_CHOICE_KEY, tempDirChoice.name());
162  }
163 
165  }
166 }
static boolean hasReadWriteAccess(Path dirPath)
Definition: FileUtil.java:184
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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