diff --git a/openide.filesystems/apichanges.xml b/openide.filesystems/apichanges.xml --- a/openide.filesystems/apichanges.xml +++ b/openide.filesystems/apichanges.xml @@ -49,6 +49,25 @@ Filesystems API + + + Convenient method for checking MIME-type of FileObjects + + + + + +

+ Add method FileUtil.checkMIMEType(FileObject, String...) + for checking whether the file has one of specified MIME types. +

+

+ Deprecate method FileUtil.getMIMEType(FileObject, String...). +

+
+ + +
Support detection and reading of symbolic links. diff --git a/openide.filesystems/manifest.mf b/openide.filesystems/manifest.mf --- a/openide.filesystems/manifest.mf +++ b/openide.filesystems/manifest.mf @@ -2,6 +2,6 @@ OpenIDE-Module: org.openide.filesystems OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml -OpenIDE-Module-Specification-Version: 9.4 +OpenIDE-Module-Specification-Version: 9.5 diff --git a/openide.filesystems/src/org/openide/filesystems/FileUtil.java b/openide.filesystems/src/org/openide/filesystems/FileUtil.java --- a/openide.filesystems/src/org/openide/filesystems/FileUtil.java +++ b/openide.filesystems/src/org/openide/filesystems/FileUtil.java @@ -99,6 +99,8 @@ private static final Logger LOG = Logger.getLogger(FileUtil.class.getName()); + private static final String MIME_CONTENT_UNKNOWN = "content/unknown"; + /** Normal header for ZIP files. */ private static byte[] ZIP_HEADER_1 = {0x50, 0x4b, 0x03, 0x04}; /** Also seems to be used at least in apisupport/project/test/unit/data/example-external-projects/suite3/nbplatform/random/modules/ext/stuff.jar; not known why */ @@ -1318,12 +1320,57 @@ * the FileObject is unrecognized. It may return {@code content/unknown} instead of {@code null}. * It is possible for the resulting MIME type to not be a member of given list. * @since 7.13 + * @deprecated Use {@link #checkMIMEType(FileObject, String...)} instead. */ + @Deprecated public static String getMIMEType(FileObject fo, String... withinMIMETypes) { Parameters.notNull("withinMIMETypes", withinMIMETypes); //NOI18N return MIMESupport.findMIMEType(fo, withinMIMETypes); } + /** + * Check whether FileObject has one of specified MIME types. + * + *

+ * Note: Checking MIME type "content/unknown" (or null) may be more + * expensive than checking specific MIME types, because all resolvers need + * to be queried. + *

+ * + * @param fo The FileObject to check. + * @param mimeTypes Array of MIME types. It can also contain null or value + * "content/unknown", which means that the method will return value + * "content/unknown" if MIME type of {@code fo} is unrecognized. + * @return The actual MIME type if MIME type of {@code fo} is equal to one + * of values in {@code mimeTypes}; or value "content/unknown" if the MIME + * type of {@code fo} is unrecognized and {@code mimeTypes} contains null or + * value "content/unknown". Null otherwise (i.e. MIME type of the file is + * not contained in {@code mimeTypes}) + */ + public static String checkMIMEType(FileObject fo, String... mimeTypes) { + + // If we want to check that the MIME type is unrecognized, we need + // to query all resolvers. + boolean checkUnrecognized = false; + + String theMime = getMIMEType(fo, mimeTypes); + for (String oneOfTypes : mimeTypes) { + if (oneOfTypes == null || oneOfTypes.equals(MIME_CONTENT_UNKNOWN)) { + checkUnrecognized = true; + } else if (oneOfTypes.equals(theMime)) { + return theMime; + } + } + if (checkUnrecognized) { + String checkAllResult = getMIMEType(fo); + if (checkAllResult == null + || checkAllResult.equals(MIME_CONTENT_UNKNOWN)) { + return MIME_CONTENT_UNKNOWN; + } + } + return null; + } + /** Registers specified extension to be recognized as specified MIME type. * If MIME type parameter is null, it cancels previous registration. * Note that you may register a case-sensitive extension if that is diff --git a/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java b/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java --- a/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java +++ b/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java @@ -86,6 +86,7 @@ // These tests have to be run in correct order, see bug 231316. List orderedMethods = new ArrayList(); orderedMethods.add("testGetMIMETypeConstrained"); + orderedMethods.add("testCheckMimeType"); orderedMethods.add("testSetMIMEType"); for (String methodName : orderedMethods) { @@ -506,6 +507,52 @@ } } + public void testCheckMimeType() throws IOException { + MIMESupport.resetCache(); // Needed when changing default lookup + AbcResolver resolver = new AbcResolver(); + MockLookup.setInstances(resolver); + assertNotNull(Lookup.getDefault().lookup(AbcResolver.class)); + + FileObject testFolder = FileUtil.createMemoryFileSystem().getRoot(); + FileObject foAbc = FileUtil.createData(testFolder, "file.abc"); + FileObject foUnk = FileUtil.createData(testFolder, "file.unk"); + + assertNotNull(FileUtil.checkMIMEType(foAbc, "text/abc")); + assertNotNull(FileUtil.checkMIMEType(foAbc, "text/q", "text/abc", "x/y")); + + assertNull(FileUtil.checkMIMEType(foAbc, "text/q", "x/y")); + assertNull(FileUtil.checkMIMEType(foAbc, "text/q", "x/y", null)); + assertNull(FileUtil.checkMIMEType(foAbc, (String) null)); + assertNull(FileUtil.checkMIMEType(foAbc, "content/unknown")); + assertNull(FileUtil.checkMIMEType(foAbc, new String[0])); + + assertNotNull(FileUtil.checkMIMEType(foUnk, "content/unknown")); + assertNotNull(FileUtil.checkMIMEType(foUnk, (String) null)); + assertNotNull(FileUtil.checkMIMEType(foUnk, "x/y", "content/unknown", "y/z")); + assertNotNull(FileUtil.checkMIMEType(foUnk, "x/y", null, "y/z")); + + assertNull(FileUtil.checkMIMEType(foUnk, "text/abc")); + assertNull(FileUtil.checkMIMEType(foUnk, "x/y", "text/abc", "y/z")); + assertNull(FileUtil.checkMIMEType(foUnk, new String[0])); + } + + public static final class AbcResolver extends MIMEResolver { + + @Override + public String findMIMEType(FileObject fo) { + if("abc".equals(fo.getExt())) { + return "text/abc"; + } else { + return null; + } + } + + @Override + String[] getMIMETypes() { + return new String[] {"text/abc"}; + } + } + /** Test recovery of FileUtil.createFolder(FileObject, String) method when * other thread created folder in the middle of processing (see #152219). */