public void testFileExtension() throws IOException{ new FileTypeDetector() { @Override public String probeContentType(Path file) throws IOException { String type; // Basic check on the type using the filename if (file.toString().lastIndexOf('.') != -1) { type = file.toString().substring( file.toString().lastIndexOf('.') ); } else { type = null; } return type; } }; }
private static FileTypeDetector createDefaultFileTypeDetector() { return AccessController .doPrivileged(new PrivilegedAction<FileTypeDetector>() { @Override public FileTypeDetector run() { return sun.nio.fs.DefaultFileTypeDetector.create(); }}); }
private static List<FileTypeDetector> loadInstalledDetectors() { return AccessController .doPrivileged(new PrivilegedAction<List<FileTypeDetector>>() { @Override public List<FileTypeDetector> run() { List<FileTypeDetector> list = new ArrayList<>(); ServiceLoader<FileTypeDetector> loader = ServiceLoader .load(FileTypeDetector.class, ClassLoader.getSystemClassLoader()); for (FileTypeDetector detector: loader) { list.add(detector); } return list; }}); }
/** * Returns a {@code FileTypeDetector} for this platform. */ FileTypeDetector getFileTypeDetector() { return new AbstractFileTypeDetector() { @Override public String implProbeContentType(Path file) { return null; } }; }
/** * Returns a {@code FileTypeDetector} that chains the given array of file * type detectors. When the {@code implProbeContentType} method is invoked * then each of the detectors is invoked in turn, the result from the * first to detect the file type is returned. */ final FileTypeDetector chain(final AbstractFileTypeDetector... detectors) { return new AbstractFileTypeDetector() { @Override protected String implProbeContentType(Path file) throws IOException { for (AbstractFileTypeDetector detector : detectors) { String result = detector.implProbeContentType(file); if (result != null && !result.isEmpty()) { return result; } } return null; } }; }
@Override FileTypeDetector getFileTypeDetector() { Path userMimeTypes = Paths.get(AccessController.doPrivileged( new GetPropertyAction("user.home")), ".mime.types"); Path etcMimeTypes = Paths.get("/etc/mime.types"); return chain(new GnomeFileTypeDetector(), new MimeTypesFileTypeDetector(userMimeTypes), new MimeTypesFileTypeDetector(etcMimeTypes), new MagicFileTypeDetector()); }
@Override FileTypeDetector getFileTypeDetector() { Path userMimeTypes = Paths.get(AccessController.doPrivileged( new GetPropertyAction("user.home")), ".mime.types"); Path etcMimeTypes = Paths.get("/etc/mime.types"); return chain(new GnomeFileTypeDetector(), new MimeTypesFileTypeDetector(userMimeTypes), new MimeTypesFileTypeDetector(etcMimeTypes)); }
public static FileTypeDetector create() { return new AbstractFileTypeDetector() { public String implProbeContentType(Path obj) throws IOException { return null; } }; }
private static FileTypeDetector createDefaultFileTypeDetector() { return AccessController .doPrivileged(new PrivilegedAction<>() { @Override public FileTypeDetector run() { return sun.nio.fs.DefaultFileTypeDetector.create(); }}); }
private static List<FileTypeDetector> loadInstalledDetectors() { return AccessController .doPrivileged(new PrivilegedAction<>() { @Override public List<FileTypeDetector> run() { List<FileTypeDetector> list = new ArrayList<>(); ServiceLoader<FileTypeDetector> loader = ServiceLoader .load(FileTypeDetector.class, ClassLoader.getSystemClassLoader()); for (FileTypeDetector detector: loader) { list.add(detector); } return list; }}); }
@Override FileTypeDetector getFileTypeDetector() { String userHome = GetPropertyAction.privilegedGetProperty("user.home"); Path userMimeTypes = Paths.get(userHome, ".mime.types"); Path etcMimeTypes = Paths.get("/etc/mime.types"); return chain(new MimeTypesFileTypeDetector(userMimeTypes), new MimeTypesFileTypeDetector(etcMimeTypes)); }
@Override FileTypeDetector getFileTypeDetector() { Path userMimeTypes = Paths.get(GetPropertyAction .privilegedGetProperty("user.home"), ".mime.types"); return chain(new MimeTypesFileTypeDetector(userMimeTypes), new UTIFileTypeDetector()); }
@Override FileTypeDetector getFileTypeDetector() { Path userMimeTypes = Paths.get( GetPropertyAction.privilegedGetProperty("user.home"), ".mime.types"); Path etcMimeTypes = Paths.get("/etc/mime.types"); return chain(new MimeTypesFileTypeDetector(userMimeTypes), new MimeTypesFileTypeDetector(etcMimeTypes)); }
@Override FileTypeDetector getFileTypeDetector() { String userHome = GetPropertyAction.privilegedGetProperty("user.home"); Path userMimeTypes = Paths.get(userHome, ".mime.types"); Path etcMimeTypes = Paths.get("/etc/mime.types"); return chain(new GioFileTypeDetector(), new MimeTypesFileTypeDetector(userMimeTypes), new MimeTypesFileTypeDetector(etcMimeTypes), new MagicFileTypeDetector()); }
@Override FileTypeDetector getFileTypeDetector() { Path userMimeTypes = Paths.get( GetPropertyAction.privilegedGetProperty("user.home"), ".mime.types"); Path etcMimeTypes = Paths.get("/etc/mime.types"); return chain(new GioFileTypeDetector(), new MimeTypesFileTypeDetector(userMimeTypes), new MimeTypesFileTypeDetector(etcMimeTypes)); }
private static List<FileTypeDetector> getInstalledDetectors() { ArrayList<FileTypeDetector> detectors = new ArrayList<>(); ServiceLoader<FileTypeDetector> serviceLoader = ServiceLoader.load(FileTypeDetector.class); for (FileTypeDetector fileTypeDetector : serviceLoader) { LOGGER.info("Adding content type detector: {}", fileTypeDetector.getClass()); detectors.add(fileTypeDetector); } return detectors; }
/** * Probes the content type of a file. * * Same as {@link Files#probeContentType(Path)} but uses context class loader. * @param path * the path to the file to probe * @return The content type of the file, or null if the content type cannot be determined * @throws IOException if an I/O error occurs */ public static String probeContentType(Path path) throws IOException { LOGGER.debug("Probing content type: {}", path); for (FileTypeDetector fileTypeDetector : fileTypeDetectors) { LOGGER.debug("Using type detector: {}", fileTypeDetector.getClass()); String contentType = fileTypeDetector.probeContentType(path); if (contentType != null) { LOGGER.debug("Content type: {}", contentType); return contentType; } } return Files.probeContentType(path); }