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;
}
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);
}
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;
}
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;
}
Aggregations