Skip to content

Instantly share code, notes, and snippets.

@fprochazka fprochazka/MimeTest.java
Last active Sep 27, 2019

Embed
What would you like to do?
Test what mime type detection utilities are available on your installation

Debug mime types in alpine (or any other docker containers)

  • start the container and mount the image
docker run --rm -it -v $(pwd):/srv cogvio/maven-nodejs:3.5.3-8u172b11_jdk_unlimited-10.1.0js-2.7.14py /bin/bash
  • cd into /srv/core/target/classes
  • compile the project
  • debug
java -cp . com.cogvio.util.nio.MimeTest

Example output:

installeDetectors:
fileTypeDetector:
sun.nio.fs.GnomeFileTypeDetector
sun.nio.fs.GnomeFileTypeDetector#gioAvailable: false
sun.nio.fs.GnomeFileTypeDetector#gnomeVfsAvailable: false
sun.nio.fs.MimeTypesFileTypeDetector
sun.nio.fs.MimeTypesFileTypeDetector#mimeTypesFile: /root/.mime.types
sun.nio.fs.MimeTypesFileTypeDetector#loaded: false
sun.nio.fs.MimeTypesFileTypeDetector
sun.nio.fs.MimeTypesFileTypeDetector#mimeTypesFile: /etc/mime.types
sun.nio.fs.MimeTypesFileTypeDetector#loaded: false
sun.nio.fs.MagicFileTypeDetector
sun.nio.fs.MagicFileTypeDetector#libmagicAvailable: true
package com.cogvio.util.nio;
import sun.nio.fs.AbstractFileTypeDetector;
import sun.nio.fs.GnomeFileTypeDetector;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.nio.file.spi.FileTypeDetector;
import java.util.*;
public class MimeTest
{
public static void main(String[] arg) throws Exception
{
Class<?> ftdClass = Class.forName("java.nio.file.Files$FileTypeDetectors", true, MimeTest.class.getClassLoader());
List<FileTypeDetector> installeDetectors = readStaticPrivateField(ftdClass, "installeDetectors");
System.out.println("installeDetectors:");
dumpFileTypeDetector(installeDetectors);
FileTypeDetector fileTypeDetector = readStaticPrivateField(ftdClass, "defaultFileTypeDetector");
System.out.println("fileTypeDetector:");
dumpFileTypeDetector(fileTypeDetector);
}
private static void dumpFileTypeDetector(final List<FileTypeDetector> detectorList) throws Exception
{
for (FileTypeDetector typeDetector : detectorList) {
dumpFileTypeDetector(typeDetector);
}
}
private static void dumpFileTypeDetector(final FileTypeDetector fileTypeDetector) throws Exception
{
Class<? extends FileTypeDetector> ftdClass = fileTypeDetector.getClass();
if (Class.forName("sun.nio.fs.AbstractFileTypeDetector").isAssignableFrom(ftdClass) && ftdClass.isAnonymousClass()) {
for (Field declaredField : ftdClass.getDeclaredFields()) {
declaredField.setAccessible(true);
Object fieldValue = declaredField.get(fileTypeDetector);
if (fieldValue instanceof AbstractFileTypeDetector[]) {
for (AbstractFileTypeDetector innerAftd : (AbstractFileTypeDetector[]) fieldValue) {
dumpFileTypeDetector(innerAftd);
}
return;
}
}
throw new RuntimeException("fuck");
}
System.out.println(ftdClass.getName());
if (fileTypeDetector instanceof GnomeFileTypeDetector) {
printPrivateProperty(ftdClass, fileTypeDetector, "gioAvailable", Boolean.class);
printPrivateProperty(ftdClass, fileTypeDetector, "gnomeVfsAvailable", Boolean.class);
}
if (Class.forName("sun.nio.fs.MimeTypesFileTypeDetector").isAssignableFrom(ftdClass)) {
printPrivateProperty(ftdClass, fileTypeDetector, "mimeTypesFile", Path.class);
printPrivateProperty(ftdClass, fileTypeDetector, "loaded", Boolean.class);
}
if (Class.forName("sun.nio.fs.MagicFileTypeDetector").isAssignableFrom(ftdClass)) {
printPrivateProperty(ftdClass, fileTypeDetector, "libmagicAvailable", Boolean.class);
}
}
private static <T> void printStaticPrivateProperty(final Class<?> aClass, final String fieldName, final Class<T> fieldClass) throws Exception
{
T value = readStaticPrivateField(aClass, fieldName);
System.out.println(String.format("%s#%s: %s", aClass.getName(), fieldName, formatValue(value, fieldClass)));
}
private static <T> void printPrivateProperty(final Class<?> aClass, final Object instance, final String fieldName, final Class<T> fieldClass) throws Exception
{
T value = readPrivateField(aClass, instance, fieldName);
System.out.println(String.format("%s#%s: %s", aClass.getName(), fieldName, formatValue(value, fieldClass)));
}
@SuppressWarnings({"unchecked"})
private static <T> T readStaticPrivateField(final Class<?> aClass, final String fieldName) throws Exception
{
Field field = aClass.getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(null);
}
@SuppressWarnings({"unchecked"})
private static <T> T readPrivateField(final Class<?> aClass, final Object instance, final String fieldName) throws Exception
{
Field field = aClass.getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(instance);
}
private static <T> String formatValue(final T value, final Class<T> fieldClass)
{
if (Boolean.class.isAssignableFrom(fieldClass)) {
return ((Boolean) value).booleanValue() ? "true" : "false";
}
if (Path.class.isAssignableFrom(fieldClass)) {
return ((Path) value).toString();
}
throw new RuntimeException(String.format("unhandled type %s", fieldClass));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.