Search in sources :

Example 1 with MimeTypeResolver

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);
}
Also used : MimeTypeResolver(ddf.mime.MimeTypeResolver) MimeTypeMapper(ddf.mime.MimeTypeMapper) MimeTypeMapperImpl(ddf.mime.mapper.MimeTypeMapperImpl) IOException(java.io.IOException) Before(org.junit.Before)

Example 2 with MimeTypeResolver

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));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) PostResourcePlugin(ddf.catalog.plugin.PostResourcePlugin) UuidGenerator(org.codice.ddf.platform.util.uuidgenerator.UuidGenerator) FederationStrategy(ddf.catalog.federation.FederationStrategy) InputStream(java.io.InputStream) MockMemoryStorageProvider(ddf.catalog.content.impl.MockMemoryStorageProvider) InputTransformer(ddf.catalog.transform.InputTransformer) SourceMonitor(ddf.catalog.source.SourceMonitor) Source(ddf.catalog.source.Source) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MimeType(javax.activation.MimeType) SourcePoller(ddf.catalog.util.impl.SourcePoller) MimeTypeResolver(ddf.mime.MimeTypeResolver) CatalogProvider(ddf.catalog.source.CatalogProvider)

Example 3 with MimeTypeResolver

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;
}
Also used : MimeTypeResolver(ddf.mime.MimeTypeResolver) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) IOException(java.io.IOException) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException)

Example 4 with MimeTypeResolver

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;
}
Also used : MimeTypeResolver(ddf.mime.MimeTypeResolver) Comparator(java.util.Comparator)

Example 5 with MimeTypeResolver

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;
}
Also used : MimeTypeResolver(ddf.mime.MimeTypeResolver) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) IOException(java.io.IOException) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException)

Aggregations

MimeTypeResolver (ddf.mime.MimeTypeResolver)8 MimeTypeMapperImpl (ddf.mime.mapper.MimeTypeMapperImpl)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Before (org.junit.Before)3 MockMemoryStorageProvider (ddf.catalog.content.impl.MockMemoryStorageProvider)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 FederationStrategy (ddf.catalog.federation.FederationStrategy)2 PostResourcePlugin (ddf.catalog.plugin.PostResourcePlugin)2 CatalogProvider (ddf.catalog.source.CatalogProvider)2 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)2 ArrayList (java.util.ArrayList)2 ByteSource (com.google.common.io.ByteSource)1 ValidationQueryFactory (ddf.catalog.cache.solr.impl.ValidationQueryFactory)1 Metacard (ddf.catalog.data.Metacard)1 Result (ddf.catalog.data.Result)1 DefaultAttributeValueRegistryImpl (ddf.catalog.data.defaultvalues.DefaultAttributeValueRegistryImpl)1 AttributeRegistryImpl (ddf.catalog.data.impl.AttributeRegistryImpl)1 AttributeInjectorImpl (ddf.catalog.data.inject.AttributeInjectorImpl)1 GeotoolsFilterAdapterImpl (ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl)1