Search in sources :

Example 1 with MimeTypeException

use of org.codelibs.fess.crawler.exception.MimeTypeException in project fess-crawler by codelibs.

the class MimeTypeHelperImplTest method test_getContentType_null.

public void test_getContentType_null() {
    final MimeTypeHelper mimeTypeHelper = container.getComponent("mimeTypeHelper");
    final InputStream is = ResourceUtil.getResourceAsStream("test/text1.txt");
    try {
        mimeTypeHelper.getContentType(null, "");
        fail();
    } catch (final MimeTypeException e) {
    }
    try {
        mimeTypeHelper.getContentType(is, "");
        fail();
    } catch (final MimeTypeException e) {
    }
    assertEquals("text/plain", mimeTypeHelper.getContentType(is, " "));
}
Also used : MimeTypeHelper(org.codelibs.fess.crawler.helper.MimeTypeHelper) InputStream(java.io.InputStream) MimeTypeException(org.codelibs.fess.crawler.exception.MimeTypeException)

Example 2 with MimeTypeException

use of org.codelibs.fess.crawler.exception.MimeTypeException in project fess-crawler by codelibs.

the class MimeTypeHelperImpl method getContentType.

@Override
public String getContentType(final InputStream is, final Map<String, String> params) {
    final String filename = params.get(TikaMetadataKeys.RESOURCE_NAME_KEY);
    if (StringUtil.isEmpty(filename) && is == null) {
        throw new MimeTypeException("The filename or input stream is empty.");
    }
    final Metadata metadata = new Metadata();
    metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, normalizeFilename(filename));
    try {
        final MediaType mediaType = mimeTypes.detect(is == null || is.markSupported() ? is : new BufferedInputStream(is), metadata);
        return mediaType.getType() + "/" + mediaType.getSubtype();
    } catch (final IOException e) {
        throw new MimeTypeException("Could not detect a content type.", e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) MimeTypeException(org.codelibs.fess.crawler.exception.MimeTypeException) Metadata(org.apache.tika.metadata.Metadata) MediaType(org.apache.tika.mime.MediaType) IOException(java.io.IOException)

Aggregations

MimeTypeException (org.codelibs.fess.crawler.exception.MimeTypeException)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Metadata (org.apache.tika.metadata.Metadata)1 MediaType (org.apache.tika.mime.MediaType)1 MimeTypeHelper (org.codelibs.fess.crawler.helper.MimeTypeHelper)1