use of ddf.mime.MimeTypeResolver in project ddf by codice.
the class FileSystemStorageProviderTest method setUp.
@Before
public void setUp() throws IOException {
tempFolder.create();
baseDir = tempFolder.getRoot().getAbsolutePath();
baseTmpDir = baseDir + File.separator + FileSystemStorageProvider.DEFAULT_TMP;
MimeTypeResolver resolver = new MockMimeTypeResolver();
MimeTypeMapper mapper = new MimeTypeMapperImpl(Collections.singletonList(resolver));
this.provider = new FileSystemStorageProvider();
try {
provider.setBaseContentDirectory(baseDir);
} catch (IOException e) {
fail(e.getMessage());
}
provider.setMimeTypeMapper(mapper);
}
use of ddf.mime.MimeTypeResolver in project ddf by codice.
the class RemoteDeleteOperationsTest method setUpMocks.
private void setUpMocks() throws IOException, CatalogTransformerException {
String localProviderName = "ddf";
mockPoller = mock(SourcePoller.class);
when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
provider = mock(CatalogProvider.class);
when(provider.getId()).thenReturn(localProviderName);
when(provider.isAvailable(isA(SourceMonitor.class))).thenReturn(true);
when(provider.isAvailable()).thenReturn(true);
mockPostResourcePlugin = mock(PostResourcePlugin.class);
mockPostResourcePlugins = new ArrayList<PostResourcePlugin>();
mockPostResourcePlugins.add(mockPostResourcePlugin);
mockFederationStrategy = mock(FederationStrategy.class);
postIngestPlugins = new ArrayList<>();
storageProvider = new MockMemoryStorageProvider();
mimeTypeResolver = mock(MimeTypeResolver.class);
mimeTypeToTransformerMapper = mock(MimeTypeToTransformerMapper.class);
uuidGenerator = mock(UuidGenerator.class);
when(uuidGenerator.generateUuid()).thenReturn(UUID.randomUUID().toString());
inputTransformer = mock(InputTransformer.class);
when(inputTransformer.transform(any(InputStream.class))).thenReturn(new MetacardImpl());
when(mimeTypeToTransformerMapper.findMatches(any(Class.class), any(MimeType.class))).thenReturn(Collections.singletonList(inputTransformer));
}
use of ddf.mime.MimeTypeResolver in project ddf by codice.
the class MimeTypeMapperImpl method getMimeTypeForFileExtension.
@Override
public String getMimeTypeForFileExtension(String fileExtension) throws MimeTypeResolutionException {
LOGGER.trace("ENTERING: getMimeTypeForFileExtension()");
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);
// 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 {
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: getMimeTypeForFileExtension()");
return mimeType;
}
use of ddf.mime.MimeTypeResolver in project ddf by codice.
the class MimeTypeMapperImpl method sortResolvers.
/**
* Sort the list of {@link MimeTypeResolver}s by their descending priority, i.e., the lower the
* priority the later the {@link MimeTypeResolver} is invoked.
*
* @param resolvers the {@link List} of {@link MimeTypeResolver}s
* @return the sorted list of {@link MimeTypeResolver}s by descending priority
*/
private List<MimeTypeResolver> sortResolvers(List<MimeTypeResolver> resolvers) {
LOGGER.debug("ENTERING: sortResolvers()");
List<MimeTypeResolver> sortedResolvers = null;
if (resolvers != null) {
// Log sorted list of PreIngestServices for debugging
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Unsorted services");
LOGGER.debug("------------------");
for (MimeTypeResolver resolver : resolvers) {
LOGGER.debug("{} (priority: {})", resolver.getName(), resolver.getPriority());
}
}
// Make copy of input services list because OSGi/Blueprint marks this input list as
// read-only
sortedResolvers = new ArrayList<MimeTypeResolver>(resolvers);
// Inner class Comparator for comparing/sorting
Comparator<MimeTypeResolver> comparator = new Comparator<MimeTypeResolver>() {
@Override
public int compare(MimeTypeResolver arg0, MimeTypeResolver arg1) {
LOGGER.debug("INSIDE: Comparator");
return (arg0.getPriority() - arg1.getPriority());
}
};
if (sortedResolvers.size() > 1) {
LOGGER.debug("Sorting resolvers");
Collections.sort(sortedResolvers, comparator);
Collections.reverse(sortedResolvers);
}
// Log sorted list of PreIngestServices for debugging
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Sorted/prioritized services");
LOGGER.debug("---------------------------");
for (MimeTypeResolver resolver : sortedResolvers) {
LOGGER.debug("{} (priority: {})", resolver.getName(), resolver.getPriority());
}
}
}
LOGGER.debug("EXITING: sortResolvers()");
return sortedResolvers;
}
use of ddf.mime.MimeTypeResolver in project ddf by codice.
the class MimeTypeMapperImpl method getFileExtensionForMimeType.
@Override
public String getFileExtensionForMimeType(String mimeType) throws MimeTypeResolutionException {
LOGGER.trace("ENTERING: getFileExtensionForMimeType()");
String extension = null;
LOGGER.debug("Looping through {} MimeTypeResolvers", mimeTypeResolvers.size());
// Sort the mime type resolvers in descending order of priority. This should
// insure custom mime type resolvers are called before the (default) Apache Tika
// mime type resolver.
List<MimeTypeResolver> sortedResolvers = sortResolvers(mimeTypeResolvers);
// 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 {
extension = resolver.getFileExtensionForMimeType(mimeType);
} catch (Exception e) {
LOGGER.debug("Error resolving file extension for mime type: {}", mimeType);
throw new MimeTypeResolutionException(e);
}
if (StringUtils.isNotEmpty(extension)) {
LOGGER.debug("extension [{}] retrieved from MimeTypeResolver: {}", extension, resolver.getName());
break;
}
}
LOGGER.debug("mimeType = {}, file extension = [{}]", mimeType, extension);
LOGGER.trace("EXITING: getFileExtensionForMimeType()");
return extension;
}
Aggregations