This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 250264
Collapse All | Expand All

(-)a/openide.filesystems/apichanges.xml (+19 lines)
Lines 49-54 Link Here
49
        <apidef name="filesystems">Filesystems API</apidef>
49
        <apidef name="filesystems">Filesystems API</apidef>
50
    </apidefs>
50
    </apidefs>
51
    <changes>
51
    <changes>
52
        <change id="FileUtil.checkMIMEType">
53
            <api name="filesystems"/>
54
            <summary>Convenient method for checking MIME-type of FileObjects</summary>
55
            <version major="9" minor="5"/>
56
            <date year="2015" month="2" day="6"/>
57
            <author login="jhavlin"/>
58
            <compatibility addition="yes" deprecation="yes"/>
59
            <description>
60
                <p>
61
                    Add method <code>FileUtil.checkMIMEType(FileObject, String...)</code>
62
                    for checking whether the file has one of specified MIME types.
63
                </p>
64
                <p>
65
                    Deprecate method <code>FileUtil.getMIMEType(FileObject, String...)</code>.
66
                </p>
67
            </description>
68
            <class package="org.openide.filesystems" name="FileUtil"/>
69
            <issue number="250264"/>
70
        </change>
52
        <change id="FileObject.symbolicLinks">
71
        <change id="FileObject.symbolicLinks">
53
            <api name="filesystems"/>
72
            <api name="filesystems"/>
54
            <summary>Support detection and reading of symbolic links.</summary>
73
            <summary>Support detection and reading of symbolic links.</summary>
(-)a/openide.filesystems/manifest.mf (-1 / +1 lines)
Lines 2-7 Link Here
2
OpenIDE-Module: org.openide.filesystems
2
OpenIDE-Module: org.openide.filesystems
3
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
4
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
4
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
5
OpenIDE-Module-Specification-Version: 9.4
5
OpenIDE-Module-Specification-Version: 9.5
6
6
7
7
(-)a/openide.filesystems/src/org/openide/filesystems/FileUtil.java (+47 lines)
Lines 99-104 Link Here
99
99
100
    private static final Logger LOG = Logger.getLogger(FileUtil.class.getName());
100
    private static final Logger LOG = Logger.getLogger(FileUtil.class.getName());
101
101
102
    private static final String MIME_CONTENT_UNKNOWN = "content/unknown";
103
102
    /** Normal header for ZIP files. */
104
    /** Normal header for ZIP files. */
103
    private static byte[] ZIP_HEADER_1 = {0x50, 0x4b, 0x03, 0x04};
105
    private static byte[] ZIP_HEADER_1 = {0x50, 0x4b, 0x03, 0x04};
104
    /** 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 */
106
    /** 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 */
Lines 1318-1329 Link Here
1318
     * the FileObject is unrecognized. It may return {@code content/unknown} instead of {@code null}.
1320
     * the FileObject is unrecognized. It may return {@code content/unknown} instead of {@code null}.
1319
     * It is possible for the resulting MIME type to not be a member of given list.
1321
     * It is possible for the resulting MIME type to not be a member of given list.
1320
     * @since 7.13
1322
     * @since 7.13
1323
     * @deprecated Use {@link #checkMIMEType(FileObject, String...)} instead.
1321
     */
1324
     */
1325
    @Deprecated
1322
    public static String getMIMEType(FileObject fo, String... withinMIMETypes) {
1326
    public static String getMIMEType(FileObject fo, String... withinMIMETypes) {
1323
        Parameters.notNull("withinMIMETypes", withinMIMETypes);  //NOI18N
1327
        Parameters.notNull("withinMIMETypes", withinMIMETypes);  //NOI18N
1324
        return MIMESupport.findMIMEType(fo, withinMIMETypes);
1328
        return MIMESupport.findMIMEType(fo, withinMIMETypes);
1325
    }
1329
    }
1326
    
1330
    
1331
    /**
1332
     * Check whether FileObject has one of specified MIME types.
1333
     *
1334
     * <p>
1335
     * Note: Checking MIME type "content/unknown" (or null) may be more
1336
     * expensive than checking specific MIME types, because all resolvers need
1337
     * to be queried.
1338
     * </p>
1339
     *
1340
     * @param fo The FileObject to check.
1341
     * @param mimeTypes Array of MIME types. It can also contain null or value
1342
     * "content/unknown", which means that the method will return value
1343
     * "content/unknown" if MIME type of {@code fo} is unrecognized.
1344
     * @return The actual MIME type if MIME type of {@code fo} is equal to one
1345
     * of values in {@code mimeTypes}; or value "content/unknown" if the MIME
1346
     * type of {@code fo} is unrecognized and {@code mimeTypes} contains null or
1347
     * value "content/unknown". Null otherwise (i.e. MIME type of the file is
1348
     * not contained in {@code mimeTypes})
1349
     */
1350
    public static String checkMIMEType(FileObject fo, String... mimeTypes) {
1351
1352
        // If we want to check that the MIME type is unrecognized, we need
1353
        // to query all resolvers.
1354
        boolean checkUnrecognized = false;
1355
1356
        String theMime = getMIMEType(fo, mimeTypes);
1357
        for (String oneOfTypes : mimeTypes) {
1358
            if (oneOfTypes == null || oneOfTypes.equals(MIME_CONTENT_UNKNOWN)) {
1359
                checkUnrecognized = true;
1360
            } else if (oneOfTypes.equals(theMime)) {
1361
                return theMime;
1362
            }
1363
        }
1364
        if (checkUnrecognized) {
1365
            String checkAllResult = getMIMEType(fo);
1366
            if (checkAllResult == null
1367
                    || checkAllResult.equals(MIME_CONTENT_UNKNOWN)) {
1368
                return MIME_CONTENT_UNKNOWN;
1369
            }
1370
        }
1371
        return null;
1372
    }
1373
1327
    /** Registers specified extension to be recognized as specified MIME type.
1374
    /** Registers specified extension to be recognized as specified MIME type.
1328
     * If MIME type parameter is null, it cancels previous registration.
1375
     * If MIME type parameter is null, it cancels previous registration.
1329
     * Note that you may register a case-sensitive extension if that is
1376
     * Note that you may register a case-sensitive extension if that is
(-)a/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java (+47 lines)
Lines 86-91 Link Here
86
        // These tests have to be run in correct order, see bug 231316.
86
        // These tests have to be run in correct order, see bug 231316.
87
        List<String> orderedMethods = new ArrayList<String>();
87
        List<String> orderedMethods = new ArrayList<String>();
88
        orderedMethods.add("testGetMIMETypeConstrained");
88
        orderedMethods.add("testGetMIMETypeConstrained");
89
        orderedMethods.add("testCheckMimeType");
89
        orderedMethods.add("testSetMIMEType");
90
        orderedMethods.add("testSetMIMEType");
90
91
91
        for (String methodName : orderedMethods) {
92
        for (String methodName : orderedMethods) {
Lines 506-511 Link Here
506
        }
507
        }
507
    }
508
    }
508
509
510
    public void testCheckMimeType() throws IOException {
511
        MIMESupport.resetCache(); // Needed when changing default lookup
512
        AbcResolver resolver = new AbcResolver();
513
        MockLookup.setInstances(resolver);
514
        assertNotNull(Lookup.getDefault().lookup(AbcResolver.class));
515
516
        FileObject testFolder = FileUtil.createMemoryFileSystem().getRoot();
517
        FileObject foAbc = FileUtil.createData(testFolder, "file.abc");
518
        FileObject foUnk = FileUtil.createData(testFolder, "file.unk");
519
520
        assertNotNull(FileUtil.checkMIMEType(foAbc, "text/abc"));
521
        assertNotNull(FileUtil.checkMIMEType(foAbc, "text/q", "text/abc", "x/y"));
522
523
        assertNull(FileUtil.checkMIMEType(foAbc, "text/q", "x/y"));
524
        assertNull(FileUtil.checkMIMEType(foAbc, "text/q", "x/y", null));
525
        assertNull(FileUtil.checkMIMEType(foAbc, (String) null));
526
        assertNull(FileUtil.checkMIMEType(foAbc, "content/unknown"));
527
        assertNull(FileUtil.checkMIMEType(foAbc, new String[0]));
528
529
        assertNotNull(FileUtil.checkMIMEType(foUnk, "content/unknown"));
530
        assertNotNull(FileUtil.checkMIMEType(foUnk, (String) null));
531
        assertNotNull(FileUtil.checkMIMEType(foUnk, "x/y", "content/unknown", "y/z"));
532
        assertNotNull(FileUtil.checkMIMEType(foUnk, "x/y", null, "y/z"));
533
534
        assertNull(FileUtil.checkMIMEType(foUnk, "text/abc"));
535
        assertNull(FileUtil.checkMIMEType(foUnk, "x/y", "text/abc", "y/z"));
536
        assertNull(FileUtil.checkMIMEType(foUnk, new String[0]));
537
    }
538
539
    public static final class AbcResolver extends MIMEResolver {
540
541
        @Override
542
        public String findMIMEType(FileObject fo) {
543
            if("abc".equals(fo.getExt())) {
544
                return "text/abc";
545
            } else {
546
                return null;
547
            }
548
        }
549
550
        @Override
551
        String[] getMIMETypes() {
552
            return new String[] {"text/abc"};
553
        }
554
    }
555
509
    /** Test recovery of FileUtil.createFolder(FileObject, String) method when
556
    /** Test recovery of FileUtil.createFolder(FileObject, String) method when
510
     * other thread created folder in the middle of processing (see #152219).
557
     * other thread created folder in the middle of processing (see #152219).
511
     */
558
     */

Return to bug 250264