Search in sources :

Example 86 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class VideoThumbnailPluginTest method createMockContentItemOfMimeType.

private ContentItem createMockContentItemOfMimeType(String mimeType) throws MimeTypeParseException {
    final ContentItem mockContentItem = mock(ContentItem.class);
    doReturn(new MimeType(mimeType)).when(mockContentItem).getMimeType();
    doReturn(new MetacardImpl()).when(mockContentItem).getMetacard();
    doReturn(UUID.randomUUID().toString()).when(mockContentItem).getId();
    return mockContentItem;
}
Also used : ContentItem(ddf.catalog.content.data.ContentItem) MimeType(javax.activation.MimeType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 87 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class ReliableResourceDownloader method setupDownload.

public ResourceResponse setupDownload(Metacard metacard, DownloadStatusInfo downloadStatusInfo) {
    Resource resource = resourceResponse.getResource();
    MimeType mimeType = resource.getMimeType();
    String resourceName = resource.getName();
    fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
    countingFbos = new CountingOutputStream(fbos);
    streamReadByClient = new ReliableResourceInputStream(fbos, countingFbos, downloadState, downloadIdentifier, resourceResponse);
    this.metacard = metacard;
    // Create new ResourceResponse to return that will encapsulate the
    // ReliableResourceInputStream that will be read by the client simultaneously as the product
    // is cached to disk (if caching is enabled)
    ResourceImpl newResource = new ResourceImpl(streamReadByClient, mimeType, resourceName);
    resourceResponse = new ResourceResponseImpl(resourceResponse.getRequest(), resourceResponse.getProperties(), newResource);
    // Get handle to retrieved product's InputStream
    resourceInputStream = resource.getInputStream();
    eventListener.setDownloadMap(downloadIdentifier, resourceResponse);
    downloadStatusInfo.addDownloadInfo(downloadIdentifier, this, resourceResponse);
    if (downloaderConfig.isCacheEnabled()) {
        CacheKey keyMaker = null;
        String key = null;
        try {
            keyMaker = new CacheKey(metacard, resourceResponse.getRequest());
            key = keyMaker.generateKey();
        } catch (IllegalArgumentException e) {
            LOGGER.info("Cannot create cache key for resource with metacard ID = {}", metacard.getId());
            return resourceResponse;
        }
        if (!resourceCache.isPending(key)) {
            // Fully qualified path to cache file that will be written to.
            // Example:
            // <INSTALL-DIR>/data/product-cache/<source-id>-<metacard-id>
            // <INSTALL-DIR>/data/product-cache/ddf.distribution-abc123
            filePath = FilenameUtils.concat(resourceCache.getProductCacheDirectory(), key);
            if (filePath == null) {
                LOGGER.info("Unable to create cache for cache directory {} and key {} - no caching will be done.", resourceCache.getProductCacheDirectory(), key);
                return resourceResponse;
            }
            reliableResource = new ReliableResource(key, filePath, mimeType, resourceName, metacard);
            resourceCache.addPendingCacheEntry(reliableResource);
            try {
                fos = FileUtils.openOutputStream(new File(filePath));
                doCaching = true;
                this.downloadState.setCacheEnabled(true);
            } catch (IOException e) {
                LOGGER.info("Unable to open cache file {} - no caching will be done.", filePath);
            }
        } else {
            LOGGER.debug("Cache key {} is already pending caching", key);
        }
    }
    return resourceResponse;
}
Also used : ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ReliableResource(ddf.catalog.resource.data.ReliableResource) CountingOutputStream(com.google.common.io.CountingOutputStream) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) File(java.io.File) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 88 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class ResourceCacheImplTest method createCachedResource.

private ReliableResource createCachedResource(Metacard metacard) {
    String fileName = "15bytes.txt";
    String productLocation = this.getClass().getResource("/" + fileName).getFile();
    File rrCachedFile = new File(productLocation);
    return new ReliableResource(CACHED_RESOURCE_KEY, rrCachedFile.getAbsolutePath(), new MimeType(), fileName, metacard);
}
Also used : File(java.io.File) ReliableResource(ddf.catalog.resource.data.ReliableResource) MimeType(javax.activation.MimeType)

Example 89 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class CswQueryResponseTransformerTest method verifyResultOrderIsMaintained.

@Test
public void verifyResultOrderIsMaintained() throws CatalogTransformerException, IOException {
    // when
    when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(mockPrintWriter);
    when(mockPrintWriter.makeString()).thenReturn(new String());
    when(mockSourceResponse.getResults()).thenReturn(createResults(1, 10));
    when(mockSourceResponse.getRequest()).thenReturn(mockQueryRequest);
    when(mockQueryRequest.getQuery()).thenReturn(mockQuery);
    when(mockArguments.get(CswConstants.RESULT_TYPE_PARAMETER)).thenReturn(ResultType.RESULTS);
    when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenAnswer(invocationOnMock -> {
        Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
        BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ",", StandardCharsets.UTF_8), new MimeType("application/xml"));
        return bci;
    });
    // given
    transformer.init();
    transformer.transform(mockSourceResponse, mockArguments);
    transformer.destroy();
    // then
    ArgumentCaptor<String> tmCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockTransformerManager, times(1)).getTransformerBySchema(tmCaptor.capture());
    ArgumentCaptor<Map> mapCaptor = ArgumentCaptor.forClass(Map.class);
    ArgumentCaptor<Metacard> mcCaptor = ArgumentCaptor.forClass(Metacard.class);
    verify(mockMetacardTransformer, times(10)).transform(mcCaptor.capture(), mapCaptor.capture());
    ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockPrintWriter, times(2)).setRawValue(strCaptor.capture());
    String order = strCaptor.getAllValues().get(1);
    String[] ids = order.split(",");
    for (int i = 1; i < ids.length; i++) {
        assertThat(ids[i - 1], is(String.valueOf("id_" + i)));
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Map(java.util.Map) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 90 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class CswTransformProviderTest method testMarshalOtherSchema.

@Test
public void testMarshalOtherSchema() throws Exception {
    when(mockMetacardManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord(), StandardCharsets.UTF_8), new MimeType(MediaType.APPLICATION_XML)));
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Aggregations

MimeType (javax.activation.MimeType)146 Test (org.junit.Test)74 Metacard (ddf.catalog.data.Metacard)36 MimeTypeParseException (javax.activation.MimeTypeParseException)34 URI (java.net.URI)28 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)25 HashMap (java.util.HashMap)25 BundleContext (org.osgi.framework.BundleContext)25 ServiceReference (org.osgi.framework.ServiceReference)25 CatalogFramework (ddf.catalog.CatalogFramework)20 Serializable (java.io.Serializable)16 ResourceResponse (ddf.catalog.operation.ResourceResponse)15 IOException (java.io.IOException)14 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)11 InputStream (java.io.InputStream)10 StringWriter (java.io.StringWriter)10 MetacardCreationException (ddf.catalog.data.MetacardCreationException)9 Resource (ddf.catalog.resource.Resource)9 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)9 File (java.io.File)9