JavaFilesProvider.java

1
package fr.sii.ogham.core.mimetype;
2
3
import java.io.File;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.nio.file.Files;
7
import java.nio.file.spi.FileTypeDetector;
8
9
import javax.activation.MimeType;
10
import javax.activation.MimeTypeParseException;
11
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14
15
import fr.sii.ogham.core.exception.mimetype.MimeTypeDetectionException;
16
17
/**
18
 * Mime Type detection based on Java 7 features. This implementation relies on
19
 * the {@link FileTypeDetector} implementations. It may use either the file
20
 * extension or the magic number mechanisms. It really depends on what is
21
 * defined by the Virtual Machine.
22
 * 
23
 * @author Aurélien Baudet
24
 * @see Files#probeContentType(java.nio.file.Path)
25
 */
26
public class JavaFilesProvider implements MimeTypeProvider {
27
	private static final Logger LOG = LoggerFactory.getLogger(JavaFilesProvider.class);
28
29
	@Override
30
	public MimeType getMimeType(File file) throws MimeTypeDetectionException {
31
		try {
32
			LOG.debug("Detect mime type for file {}", file);
33
			String contentType = Files.probeContentType(file.toPath());
34 1 1. getMimeType : negated conditional → NO_COVERAGE
			if (contentType == null) {
35
				LOG.debug("Detected mime type for file {} is null", file);
36
				throw new MimeTypeDetectionException("Can't determine mimetype for file " + file);
37
			}
38 1 1. getMimeType : replaced return value with null for fr/sii/ogham/core/mimetype/JavaFilesProvider::getMimeType → NO_COVERAGE
			return new MimeType(contentType);
39
		} catch (MimeTypeParseException | IOException e) {
40
			throw new MimeTypeDetectionException("Failed to detect mimetype for " + file, e);
41
		}
42
	}
43
44
	@Override
45
	public MimeType getMimeType(String fileName) throws MimeTypeDetectionException {
46 1 1. getMimeType : replaced return value with null for fr/sii/ogham/core/mimetype/JavaFilesProvider::getMimeType → NO_COVERAGE
		return getMimeType(new File(fileName));
47
	}
48
49
	@Override
50
	public MimeType detect(InputStream stream) throws MimeTypeDetectionException {
51
		// TODO delegate to another mimetype engine capable of detecting
52
		return null;
53
	}
54
55
	@Override
56
	public MimeType detect(String content) throws MimeTypeDetectionException {
57
		// TODO delegate to another mimetype engine capable of detecting
58
		return null;
59
	}
60
61
}

Mutations

34

1.1
Location : getMimeType
Killed by : none
negated conditional → NO_COVERAGE

38

1.1
Location : getMimeType
Killed by : none
replaced return value with null for fr/sii/ogham/core/mimetype/JavaFilesProvider::getMimeType → NO_COVERAGE

46

1.1
Location : getMimeType
Killed by : none
replaced return value with null for fr/sii/ogham/core/mimetype/JavaFilesProvider::getMimeType → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM