Remove optional parameters from MIME types

This commit is contained in:
Sophie Mori 2016-11-21 17:06:31 -05:00
parent 7a08914b6d
commit e4f351bf8c
5 changed files with 83 additions and 61 deletions

View File

@ -1,11 +1,23 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* Autopsy Forensic Browser
*
* Copyright 2011-2016 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sleuthkit.autopsy.filesearch;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@ -14,18 +26,11 @@ import java.util.SortedSet;
import java.util.logging.Level;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector;
/**
*
* @author oliver
*/
public class MimeTypePanel extends javax.swing.JPanel {
private static final SortedSet<MediaType> mediaTypes = MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes();
private static final Logger logger = Logger.getLogger(MimeTypePanel.class.getName());
private static final long serialVersionUID = 1L;
@ -45,8 +50,8 @@ public class MimeTypePanel extends javax.swing.JPanel {
private String[] getMimeTypeArray() {
Set<String> fileTypesCollated = new HashSet<>();
for (MediaType mediaType : mediaTypes) {
fileTypesCollated.add(mediaType.toString());
for (String mediaType : FileTypeDetector.getDetectedTypes()) {
fileTypesCollated.add(mediaType);
}
FileTypeDetector fileTypeDetector;

View File

@ -19,11 +19,13 @@
package org.sleuthkit.autopsy.modules.filetypeid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.apache.tika.Tika;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.openide.util.NbBundle;
import org.sleuthkit.autopsy.casemodule.Case;
@ -49,6 +51,7 @@ public class FileTypeDetector {
private final byte buffer[] = new byte[BUFFER_SIZE];
private final List<FileType> userDefinedFileTypes;
private final List<FileType> autopsyDefinedFileTypes;
private static SortedSet<String> detectedTypes; //no optional parameters
/**
* Constructs an object that detects the MIME type of a file by an
@ -100,6 +103,21 @@ public class FileTypeDetector {
|| isDetectableByTika(mimeType);
}
/**
* Returns an unmodifiable list of MIME types that does not contain types
* with optional parameters. The list has no duplicate types and is in
* alphabetical order.
*
* @return an unmodifiable view of a set of MIME types
*/
public static synchronized SortedSet<String> getDetectedTypes() {
if (detectedTypes == null) {
detectedTypes = org.apache.tika.mime.MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes()
.stream().filter(t -> !t.hasParameters()).map(s -> s.toString()).collect(Collectors.toCollection(TreeSet::new));
}
return Collections.unmodifiableSortedSet(detectedTypes);
}
/**
* Determines whether or not a given MIME type is detectable as a
* user-defined MIME type by this detector.
@ -126,15 +144,7 @@ public class FileTypeDetector {
* @return True or false.
*/
private boolean isDetectableByTika(String mimeType) {
String[] split = mimeType.split("/");
if (split.length == 2) {
String type = split[0];
String subtype = split[1];
MediaType mediaType = new MediaType(type, subtype);
SortedSet<MediaType> m = MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes();
return m.contains(mediaType);
}
return false;
return this.getDetectedTypes().contains(removeOptionalParameter(mimeType));
}
/**
@ -196,7 +206,7 @@ public class FileTypeDetector {
*/
String mimeType = file.getMIMEType();
if (null != mimeType) {
return mimeType;
return removeOptionalParameter(mimeType);
}
/*
@ -248,6 +258,7 @@ public class FileTypeDetector {
* Remove the Tika suffix from the MIME type name.
*/
mimeType = tikaType.replace("tika-", ""); //NON-NLS
mimeType = removeOptionalParameter(mimeType);
} catch (Exception ignored) {
/*
@ -288,6 +299,20 @@ public class FileTypeDetector {
return mimeType;
}
/**
* Removes the optional parameter from a MIME type string
* @param mimeType
* @return MIME type without the optional parameter
*/
private String removeOptionalParameter(String mimeType) {
int indexOfSemicolon = mimeType.indexOf(";");
if (indexOfSemicolon != -1 ) {
return mimeType.substring(0, indexOfSemicolon).trim();
} else {
return mimeType;
}
}
/**
* Determines whether or not the a file matches a user-defined custom file
* type.

View File

@ -24,9 +24,6 @@ import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.BlackboardArtifact;
import org.sleuthkit.datamodel.BlackboardAttribute;
import org.sleuthkit.datamodel.TskCoreException;
/**
* @deprecated Use org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector

View File

@ -31,8 +31,6 @@ import java.util.regex.PatternSyntaxException;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.NbBundle;
@ -58,7 +56,6 @@ final class FilesSetRulePanel extends javax.swing.JPanel {
"FilesSetRulePanel.ZeroFileSizeError=File size condition value must not be 0 (Unless = is selected)."
})
private static final SortedSet<MediaType> mediaTypes = MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes();
private static final Logger logger = Logger.getLogger(FilesSetRulePanel.class.getName());
private static final String SLEUTHKIT_PATH_SEPARATOR = "/"; // NON-NLS
private static final List<String> ILLEGAL_FILE_NAME_CHARS = InterestingItemDefsManager.getIllegalFileNameChars();
@ -106,8 +103,8 @@ final class FilesSetRulePanel extends javax.swing.JPanel {
private void populateMimeTypesComboBox() {
Set<String> fileTypesCollated = new HashSet<>();
for (MediaType mediaType : mediaTypes) {
fileTypesCollated.add(mediaType.toString());
for (String mediaType : FileTypeDetector.getDetectedTypes()) {
fileTypesCollated.add(mediaType);
}
FileTypeDetector fileTypeDetector;

View File

@ -34,7 +34,6 @@ import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.netbeans.spi.options.OptionsPanelController;
import org.openide.util.NbBundle;
import org.sleuthkit.autopsy.corecomponents.OptionsPanel;
@ -57,12 +56,11 @@ final class InterestingItemDefsPanel extends IngestModuleGlobalSettingsPanel imp
"InterestingItemsDefsPanel.saveError=Error saving interesting files sets to file."
})
private static final SortedSet<MediaType> mediaTypes = MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes();
private final DefaultListModel<FilesSet> setsListModel = new DefaultListModel<>();
private final DefaultListModel<FilesSet.Rule> rulesListModel = new DefaultListModel<>();
private final Logger logger = Logger.getLogger(InterestingItemDefsPanel.class.getName());
private JButton okButton = new JButton("OK");
private JButton cancelButton = new JButton("Cancel");
private final JButton okButton = new JButton("OK");
private final JButton cancelButton = new JButton("Cancel");
// The following is a map of interesting files set names to interesting
// files set definitions. It is a snapshot of the files set definitions
@ -90,8 +88,8 @@ final class InterestingItemDefsPanel extends IngestModuleGlobalSettingsPanel imp
setName(Bundle.InterestingItemDefsPanel_Title());
Set<String> fileTypesCollated = new HashSet<>();
for (MediaType mediaType : mediaTypes) {
fileTypesCollated.add(mediaType.toString());
for (String mediaType : FileTypeDetector.getDetectedTypes()) {
fileTypesCollated.add(mediaType);
}
FileTypeDetector fileTypeDetector;