Search in sources :

Example 6 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class ContentProducerDataAccessObject method getMimeType.

public String getMimeType(ContentEndpoint endpoint, File ingestedFile) throws ContentComponentException {
    String fileExtension = FilenameUtils.getExtension(ingestedFile.getAbsolutePath());
    String mimeType = null;
    MimeTypeMapper mimeTypeMapper = endpoint.getComponent().getMimeTypeMapper();
    if (mimeTypeMapper != null && ingestedFile.exists()) {
        try (InputStream inputStream = Files.asByteSource(ingestedFile).openStream()) {
            if (fileExtension.equalsIgnoreCase("xml")) {
                mimeType = mimeTypeMapper.guessMimeType(inputStream, fileExtension);
            } else {
                mimeType = mimeTypeMapper.getMimeTypeForFileExtension(fileExtension);
            }
        } catch (MimeTypeResolutionException | IOException e) {
            throw new ContentComponentException(e);
        }
    } else if (ingestedFile.exists()) {
        LOGGER.debug("Did not find a MimeTypeMapper service");
        throw new ContentComponentException("Unable to find a mime type for the ingested file " + ingestedFile.getName());
    }
    return mimeType;
}
Also used : MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) MimeTypeMapper(ddf.mime.MimeTypeMapper) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 7 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class FtpRequestHandlerTest method testMimeTypeResolutionFailure.

@Test
public void testMimeTypeResolutionFailure() throws MimeTypeResolutionException {
    String mimeType;
    when(mimeTypeMapper.guessMimeType(any(InputStream.class), eq("xml"))).thenThrow(new MimeTypeResolutionException());
    mimeType = ftplet.getMimeType("xml", new TemporaryFileBackedOutputStream());
    assertEquals("", mimeType);
}
Also used : MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class RESTEndpoint method parseAttachment.

CreateInfo parseAttachment(Attachment contentPart) {
    CreateInfo createInfo = new CreateInfo();
    InputStream stream = null;
    FileBackedOutputStream fbos = null;
    String filename = null;
    String contentType = null;
    // at the beginning
    try {
        stream = contentPart.getDataHandler().getInputStream();
        if (stream != null && stream.available() == 0) {
            stream.reset();
        }
        createInfo.setStream(stream);
    } catch (IOException e) {
        LOGGER.info("IOException reading stream from file attachment in multipart body", e);
    }
    // Content-Type: application/json;id=geojson
    if (contentPart.getContentType() != null) {
        contentType = contentPart.getContentType().toString();
    }
    if (contentPart.getContentDisposition() != null) {
        filename = contentPart.getContentDisposition().getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME);
    }
    // specified content type.
    if (StringUtils.isEmpty(filename)) {
        LOGGER.debug("No filename parameter provided - generating default filename");
        String fileExtension = DEFAULT_FILE_EXTENSION;
        try {
            // DDF-2307
            fileExtension = mimeTypeMapper.getFileExtensionForMimeType(contentType);
            if (StringUtils.isEmpty(fileExtension)) {
                fileExtension = DEFAULT_FILE_EXTENSION;
            }
        } catch (MimeTypeResolutionException e) {
            LOGGER.debug("Exception getting file extension for contentType = {}", contentType);
        }
        // DDF-2263
        filename = DEFAULT_FILE_NAME + "." + fileExtension;
        LOGGER.debug("No filename parameter provided - default to {}", filename);
    } else {
        filename = FilenameUtils.getName(filename);
        // by determining the mime type based on the filename's extension.
        if (StringUtils.isEmpty(contentType) || REFINEABLE_MIME_TYPES.contains(contentType)) {
            String fileExtension = FilenameUtils.getExtension(filename);
            LOGGER.debug("fileExtension = {}, contentType before refinement = {}", fileExtension, contentType);
            try {
                contentType = mimeTypeMapper.getMimeTypeForFileExtension(fileExtension);
            } catch (MimeTypeResolutionException e) {
                LOGGER.debug("Unable to refine contentType {} based on filename extension {}", contentType, fileExtension);
            }
            LOGGER.debug("Refined contentType = {}", contentType);
        }
    }
    createInfo.setContentType(contentType);
    createInfo.setFilename(filename);
    return createInfo;
}
Also used : MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) IOException(java.io.IOException)

Example 9 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class MimeTypeMapperImpl method guessMimeType.

@Override
public String guessMimeType(InputStream is, String fileExtension) throws MimeTypeResolutionException {
    LOGGER.trace("ENTERING: guessMimeType()");
    String mimeType = null;
    LOGGER.debug("Looping through{} MimeTypeResolvers", mimeTypeResolvers.size());
    // This is to force the TikaMimeTypeResolver to be called
    // after the CustomMimeTypeResolvers to prevent Tika default mapping
    // from being used when a CustomMimeTypeResolver may be more appropriate.
    List<MimeTypeResolver> sortedResolvers = sortResolvers(mimeTypeResolvers);
    if (StringUtils.isEmpty(fileExtension)) {
        try (TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {
            IOUtils.copy(is, tfbos);
            try (InputStream inputStream = tfbos.asByteSource().openStream()) {
                Detector detector = new DefaultDetector();
                MediaType mediaType = detector.detect(inputStream, new Metadata());
                fileExtension = getFileExtensionForMimeType(mediaType.toString()).replace(".", "");
            } finally {
                is = tfbos.asByteSource().openStream();
            }
        } catch (Exception e) {
            LOGGER.debug("Failed to guess mimeType for file without extension.");
        }
    }
    // If file has XML extension, then read root element namespace once so
    // each MimeTypeResolver does not have to open the stream and read the namespace
    String namespace = null;
    if (fileExtension.equals(XML_FILE_EXTENSION)) {
        try {
            namespace = XMLUtils.getRootNamespace(IOUtils.toString(is));
        } catch (IOException ioe) {
            LOGGER.debug("Could not read namespace from input stream.", ioe);
        }
        LOGGER.debug("namespace = {}", namespace);
    }
    // Once a file extension is find for the given mime type, exit the loop.
    for (MimeTypeResolver resolver : sortedResolvers) {
        LOGGER.debug("Calling MimeTypeResolver {}", resolver.getName());
        try {
            // an InputTransformer to create a metacard for that "generic" XML file.
            if (fileExtension.equals(XML_FILE_EXTENSION)) {
                if (namespace != null && resolver.hasSchema()) {
                    if (namespace.equals(resolver.getSchema())) {
                        mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
                    }
                }
            } else {
                mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
            }
        } catch (Exception e) {
            LOGGER.debug("Error resolving mime type for file extension: {}", fileExtension);
            throw new MimeTypeResolutionException(e);
        }
        if (StringUtils.isNotEmpty(mimeType)) {
            LOGGER.debug("mimeType [{}] retrieved from MimeTypeResolver:  ", mimeType, resolver.getName());
            break;
        }
    }
    LOGGER.debug("mimeType = {},   file extension = [{}]", mimeType, fileExtension);
    LOGGER.trace("EXITING: guessMimeType()");
    return mimeType;
}
Also used : MimeTypeResolver(ddf.mime.MimeTypeResolver) DefaultDetector(org.apache.tika.detect.DefaultDetector) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) Detector(org.apache.tika.detect.Detector) DefaultDetector(org.apache.tika.detect.DefaultDetector) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) MediaType(org.apache.tika.mime.MediaType) IOException(java.io.IOException) IOException(java.io.IOException) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException)

Aggregations

MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)9 IOException (java.io.IOException)8 InputStream (java.io.InputStream)7 MimeTypeResolver (ddf.mime.MimeTypeResolver)3 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)3 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)2 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)2 BufferedInputStream (java.io.BufferedInputStream)2 Detector (org.apache.tika.detect.Detector)2 Metadata (org.apache.tika.metadata.Metadata)2 MediaType (org.apache.tika.mime.MediaType)2 FileBackedOutputStream (com.google.common.io.FileBackedOutputStream)1 ResourceResponse (ddf.catalog.operation.ResourceResponse)1 MimeTypeMapper (ddf.mime.MimeTypeMapper)1 Subject (ddf.security.Subject)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 URLConnection (java.net.URLConnection)1 WebApplicationException (javax.ws.rs.WebApplicationException)1