remove maps

This commit is contained in:
momo 2015-10-20 10:45:28 -04:00
parent 5a138d26f2
commit fade9f18d8
3 changed files with 55 additions and 54 deletions

View File

@ -39,7 +39,7 @@ public class FileTypeDetector {
private static final Tika tika = new Tika();
private static final int BUFFER_SIZE = 64 * 1024;
private final byte buffer[] = new byte[BUFFER_SIZE];
private final Map<String, List<FileType>> userDefinedFileTypes;
private final List<FileType> userDefinedFileTypes;
/**
* Constructs an object that detects the type of a file by an inspection of
@ -77,7 +77,12 @@ public class FileTypeDetector {
* @return True if MIME type is detectable.
*/
private boolean isDetectableAsUserDefinedType(String mimeType) {
return userDefinedFileTypes.containsKey(mimeType);
for (FileType fileType : userDefinedFileTypes) {
if (fileType.getMimeType().equals(mimeType)) {
return true;
}
}
return false;
}
/**
@ -216,25 +221,23 @@ public class FileTypeDetector {
* @throws TskCoreException
*/
private String detectUserDefinedType(AbstractFile file) throws TskCoreException {
for (List<FileType> fileTypes : userDefinedFileTypes.values()) {
for (FileType fileType : fileTypes) {
if (fileType.matches(file)) {
if (fileType.alertOnMatch()) {
BlackboardArtifact artifact;
artifact = file.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT);
BlackboardAttribute setNameAttribute = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID(), FileTypeIdModuleFactory.getModuleName(), fileType.getFilesSetName());
artifact.addAttribute(setNameAttribute);
for (FileType fileType : userDefinedFileTypes) {
if (fileType.matches(file)) {
if (fileType.alertOnMatch()) {
BlackboardArtifact artifact;
artifact = file.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT);
BlackboardAttribute setNameAttribute = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID(), FileTypeIdModuleFactory.getModuleName(), fileType.getFilesSetName());
artifact.addAttribute(setNameAttribute);
/**
* Use the MIME type as the category, i.e., the rule
* that determined this file belongs to the interesting
* files set.
*/
BlackboardAttribute ruleNameAttribute = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_CATEGORY.getTypeID(), FileTypeIdModuleFactory.getModuleName(), fileType.getMimeType());
artifact.addAttribute(ruleNameAttribute);
}
return fileType.getMimeType();
/**
* Use the MIME type as the category, i.e., the rule that
* determined this file belongs to the interesting files
* set.
*/
BlackboardAttribute ruleNameAttribute = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_CATEGORY.getTypeID(), FileTypeIdModuleFactory.getModuleName(), fileType.getMimeType());
artifact.addAttribute(ruleNameAttribute);
}
return fileType.getMimeType();
}
}
return null;

View File

@ -59,7 +59,7 @@ final class FileTypeIdGlobalSettingsPanel extends IngestModuleGlobalSettingsPane
* is obtained from the user-defined types manager.
*/
private DefaultListModel<FileType> typesListModel;
private Map<String, java.util.List<FileType>> fileTypes;
private java.util.List<FileType> fileTypes;
/**
* This panel implements a property change listener that listens to ingest
@ -227,7 +227,7 @@ final class FileTypeIdGlobalSettingsPanel extends IngestModuleGlobalSettingsPane
ex.getLocalizedMessage(),
NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.loadFailed.title"),
JOptionPane.ERROR_MESSAGE);
fileTypes = Collections.emptyMap();
fileTypes = Collections.emptyList();
}
enableButtons();
}
@ -236,13 +236,9 @@ final class FileTypeIdGlobalSettingsPanel extends IngestModuleGlobalSettingsPane
* Sets the list model for the file types list component.
*/
private void updateFileTypesListModel() {
ArrayList<String> mimeTypes = new ArrayList<>(fileTypes.keySet());
Collections.sort(mimeTypes);
typesListModel.clear();
for (String mimeType : mimeTypes) {
for (FileType fileType : fileTypes.get(mimeType)) {
typesListModel.addElement(fileType);
}
for (FileType fileType : fileTypes) {
typesListModel.addElement(fileType);
}
}
@ -561,7 +557,7 @@ final class FileTypeIdGlobalSettingsPanel extends IngestModuleGlobalSettingsPane
private void deleteTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteTypeButtonActionPerformed
FileType fileType = typesList.getSelectedValue();
UserDefinedFileTypesManager.getInstance().removeFileTypeFromMap(fileTypes, fileType);
fileTypes.remove(fileType);
updateFileTypesListModel();
if (!typesListModel.isEmpty()) {
typesList.setSelectedIndex(0);
@ -630,8 +626,9 @@ final class FileTypeIdGlobalSettingsPanel extends IngestModuleGlobalSettingsPane
* Get the interesting files set details.
*/
String filesSetName = "";
if(postHitCheckBox.isSelected())
if (postHitCheckBox.isSelected()) {
filesSetName = filesSetNameTextField.getText();
}
if (postHitCheckBox.isSelected() && filesSetName.isEmpty()) {
JOptionPane.showMessageDialog(null,
NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidInterestingFilesSetName.message"),
@ -646,9 +643,10 @@ final class FileTypeIdGlobalSettingsPanel extends IngestModuleGlobalSettingsPane
FileType.Signature signature = new FileType.Signature(signatureBytes, offset, sigType);
FileType fileType = new FileType(typeName, signature, filesSetName, postHitCheckBox.isSelected());
FileType selected = typesList.getSelectedValue();
if (selected != null)
UserDefinedFileTypesManager.getInstance().removeFileTypeFromMap(fileTypes, selected);
UserDefinedFileTypesManager.getInstance().addFileTypeToMap(fileTypes, fileType);
if (selected != null) {
fileTypes.remove(selected);
}
fileTypes.add(fileType);
updateFileTypesListModel();
typesList.setSelectedValue(fileType.getMimeType(), true);
}//GEN-LAST:event_saveTypeButtonActionPerformed

View File

@ -82,7 +82,7 @@ final class UserDefinedFileTypesManager {
* map is guarded by the intrinsic lock of the user-defined file types
* manager for thread-safety.
*/
private final Map<String, List<FileType>> userDefinedFileTypes = new HashMap<>();
private final List<FileType> userDefinedFileTypes = new ArrayList<>();
/**
* The combined set of user-defined file types and file types predefined by
@ -91,7 +91,7 @@ final class UserDefinedFileTypesManager {
* the intrinsic lock of the user-defined file types manager for
* thread-safety.
*/
private final Map<String, List<FileType>> fileTypes = new HashMap<>();
private final List<FileType> fileTypes = new ArrayList<>();
/**
* Gets the singleton manager of user-defined file types characterized by
@ -122,7 +122,7 @@ final class UserDefinedFileTypesManager {
* @throws
* org.sleuthkit.autopsy.modules.filetypeid.UserDefinedFileTypesManager.UserDefinedFileTypesException
*/
synchronized Map<String, List<FileType>> getFileTypes() throws UserDefinedFileTypesException {
synchronized List<FileType> getFileTypes() throws UserDefinedFileTypesException {
loadFileTypes();
/**
@ -131,7 +131,7 @@ final class UserDefinedFileTypesManager {
* Collections.unmodifiableCollection() is not used here because this
* view of the file types is a snapshot.
*/
return new HashMap<>(fileTypes);
return new ArrayList<>(fileTypes);
}
/**
@ -142,7 +142,7 @@ final class UserDefinedFileTypesManager {
* @throws
* org.sleuthkit.autopsy.modules.filetypeid.UserDefinedFileTypesManager.UserDefinedFileTypesException
*/
synchronized Map<String, List<FileType>> getUserDefinedFileTypes() throws UserDefinedFileTypesException {
synchronized List<FileType> getUserDefinedFileTypes() throws UserDefinedFileTypesException {
loadFileTypes();
/**
@ -151,7 +151,7 @@ final class UserDefinedFileTypesManager {
* Collections.unmodifiableCollection() is not used here because this
* view of the file types is a snapshot.
*/
return new HashMap<>(userDefinedFileTypes);
return new ArrayList<>(userDefinedFileTypes);
}
/**
@ -182,11 +182,11 @@ final class UserDefinedFileTypesManager {
private void loadPredefinedFileTypes() throws UserDefinedFileTypesException {
try {
FileType fileTypeXml = new FileType("text/xml", new Signature("<?xml".getBytes(ASCII_ENCODING), 0L, FileType.Signature.Type.ASCII), "", false); //NON-NLS
addFileTypeToMap(fileTypes, fileTypeXml);
fileTypes.add(fileTypeXml);
byte[] gzip = DatatypeConverter.parseHexBinary("1F8B08");
FileType fileTypeGzip = new FileType("application/x-gzip", new Signature(gzip, 0L, FileType.Signature.Type.ASCII), "", false); //NON-NLS
addFileTypeToMap(fileTypes, fileTypeGzip);
fileTypes.add(fileTypeGzip);
} catch (UnsupportedEncodingException ex) {
/**
@ -231,14 +231,15 @@ final class UserDefinedFileTypesManager {
* @param fileType The file type to add.
*/
private void addUserDefinedFileType(FileType fileType) {
addFileTypeToMap(userDefinedFileTypes, fileType);
addFileTypeToMap(fileTypes, fileType);
userDefinedFileTypes.add(fileType);
fileTypes.add(fileType);
}
/**
* Adds given FileType to given map:
* if the mimetype exists, add fileType to that mimetype's list.
* otherwise, create a new mimetype with the new fileType.
* Adds given FileType to given map: if the mimetype exists, add fileType to
* that mimetype's list. otherwise, create a new mimetype with the new
* fileType.
*
* @param map The map to be modified.
* @param fileType The added FileType
*/
@ -255,8 +256,9 @@ final class UserDefinedFileTypesManager {
/**
* Removes given fileType from given map if it exists.
*
* @param map
* @param fileType
* @param fileType
*/
void removeFileTypeFromMap(Map<String, List<FileType>> map, FileType fileType) {
String mimeType = fileType.getMimeType();
@ -271,10 +273,10 @@ final class UserDefinedFileTypesManager {
* @param newFileTypes A mapping of file type names to user-defined file
* types.
*/
synchronized void setUserDefinedFileTypes(Map<String, List<FileType>> newFileTypes) throws UserDefinedFileTypesException {
synchronized void setUserDefinedFileTypes(List<FileType> newFileTypes) throws UserDefinedFileTypesException {
try {
String filePath = getFileTypeDefinitionsFilePath(USER_DEFINED_TYPE_DEFINITIONS_FILE);
XmlWriter.writeFileTypes(newFileTypes.values(), filePath);
XmlWriter.writeFileTypes(newFileTypes, filePath);
} catch (ParserConfigurationException | FileNotFoundException | UnsupportedEncodingException | TransformerException ex) {
throwUserDefinedFileTypesException(ex, "UserDefinedFileTypesManager.saveFileTypes.errorMessage");
} catch (IOException ex) {
@ -312,15 +314,13 @@ final class UserDefinedFileTypesManager {
* @throws UnsupportedEncodingException
* @throws TransformerException
*/
private static void writeFileTypes(Collection<List<FileType>> fileTypes, String filePath) throws ParserConfigurationException, IOException, FileNotFoundException, UnsupportedEncodingException, TransformerException {
private static void writeFileTypes(List<FileType> fileTypes, String filePath) throws ParserConfigurationException, IOException, FileNotFoundException, UnsupportedEncodingException, TransformerException {
Document doc = XMLUtil.createDocument();
Element fileTypesElem = doc.createElement(FILE_TYPES_TAG_NAME);
doc.appendChild(fileTypesElem);
for (List<FileType> fileTypeList : fileTypes) {
for (FileType fileType : fileTypeList) {
Element fileTypeElem = XmlWriter.createFileTypeElement(fileType, doc);
fileTypesElem.appendChild(fileTypeElem);
}
for (FileType fileType : fileTypes) {
Element fileTypeElem = XmlWriter.createFileTypeElement(fileType, doc);
fileTypesElem.appendChild(fileTypeElem);
}
XMLUtil.saveDocument(doc, ENCODING_FOR_XML_FILE, filePath);
}