Search in sources :

Example 11 with MimeType

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

the class TestMimeTypeToTransformerMapperImpl method testOnlyBaseTypeMatch.

/**
     * Tests if a basetype matches that the list will return the correct sorted list.
     *
     * @throws MimeTypeParseException
     * @throws InvalidSyntaxException
     */
@Test
public void testOnlyBaseTypeMatch() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = createMockReference(1, Arrays.asList(MediaType.APPLICATION_ATOM_XML), "a1");
    ServiceReference ref2 = createMockReference(2, Arrays.asList(MediaType.APPLICATION_JSON), "a2");
    ServiceReference ref3 = createMockReference(3, null, null);
    ServiceReference[] refs = { ref2, ref3, ref1 };
    Object simpleTransformer1 = new Object();
    Object simpleTransformer2 = new Object();
    Object simpleTransformer3 = new Object();
    // when
    when(context.getService(ref1)).thenReturn(simpleTransformer1);
    when(context.getService(ref2)).thenReturn(simpleTransformer2);
    when(context.getService(ref3)).thenReturn(simpleTransformer3);
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_JSON));
    // then
    assertThat(matches.size(), is(1));
    assertThat(matches.get(0), is(simpleTransformer2));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) MimeType(javax.activation.MimeType) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 12 with MimeType

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

the class MetacardFactory method generateMetacard.

Metacard generateMetacard(String mimeTypeRaw, String id, String fileName, Path tmpContentPath) throws MetacardCreationException, MimeTypeParseException {
    Metacard generatedMetacard = null;
    MimeType mimeType = new MimeType(mimeTypeRaw);
    List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
    List<String> stackTraceList = new ArrayList<>();
    LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    for (InputTransformer candidate : listOfCandidates) {
        try (InputStream transformerStream = com.google.common.io.Files.asByteSource(tmpContentPath.toFile()).openStream()) {
            generatedMetacard = candidate.transform(transformerStream);
        } catch (CatalogTransformerException | IOException e) {
            List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
            stackTraceList.add(String.format("Transformer [%s] could not create metacard.", candidate));
            stackTraceList.addAll(stackTraces);
            LOGGER.debug("Transformer [{}] could not create metacard.", candidate, e);
        }
        if (generatedMetacard != null) {
            break;
        }
    }
    if (generatedMetacard == null) {
        throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeTypeRaw, StringUtils.join(stackTraceList, "\n")));
    }
    if (id != null) {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
    } else {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, uuidGenerator.generateUuid()));
    }
    if (StringUtils.isBlank(generatedMetacard.getTitle())) {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.TITLE, fileName));
    }
    return generatedMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with MimeType

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

the class TestVideoThumbnailPlugin method setUpMockContentItem.

private void setUpMockContentItem(final String resource) throws IOException, MimeTypeParseException, URISyntaxException {
    mockContentItem = mock(ContentItem.class);
    Metacard mockMetacard = new MetacardImpl();
    doReturn(mockMetacard).when(mockContentItem).getMetacard();
    doReturn(ID).when(mockContentItem).getId();
    doReturn(new MimeType("video/mp4")).when(mockContentItem).getMimeType();
    HashMap<String, Path> contentItemPaths = new HashMap<>();
    Path tmpPath = Paths.get(getClass().getResource(resource).toURI());
    contentItemPaths.put(null, tmpPath);
    HashMap<String, Map> tmpContentPaths = new HashMap<>();
    tmpContentPaths.put(ID, contentItemPaths);
    properties = new HashMap<>();
    properties.put(Constants.CONTENT_PATHS, tmpContentPaths);
}
Also used : Path(java.nio.file.Path) Metacard(ddf.catalog.data.Metacard) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ContentItem(ddf.catalog.content.data.ContentItem) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MimeType(javax.activation.MimeType)

Example 14 with MimeType

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

the class VideoThumbnailPlugin method isVideo.

private boolean isVideo(final ContentItem contentItem) {
    final MimeType createdMimeType = contentItem.getMimeType();
    final MediaType createdMediaType = MediaType.create(createdMimeType.getPrimaryType(), createdMimeType.getSubType());
    return createdMediaType.is(MediaType.ANY_VIDEO_TYPE);
}
Also used : MediaType(com.google.common.net.MediaType) MimeType(javax.activation.MimeType)

Example 15 with MimeType

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

the class ReliableResourceDownloaderTest method getMockResourceResponse.

private ResourceResponse getMockResourceResponse(InputStream stream) throws Exception {
    ResourceRequest resourceRequest = mock(ResourceRequest.class);
    Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
    when(resourceRequest.getPropertyNames()).thenReturn(requestProperties.keySet());
    mockResource = mock(Resource.class);
    when(mockResource.getInputStream()).thenReturn(stream);
    when(mockResource.getName()).thenReturn("test-resource");
    when(mockResource.getMimeType()).thenReturn(new MimeType("text/plain"));
    mockResponse = mock(ResourceResponse.class);
    when(mockResponse.getRequest()).thenReturn(resourceRequest);
    when(mockResponse.getResource()).thenReturn(mockResource);
    Map<String, Serializable> responseProperties = new HashMap<String, Serializable>();
    when(mockResponse.getProperties()).thenReturn(responseProperties);
    return mockResponse;
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceRequest(ddf.catalog.operation.ResourceRequest) Matchers.anyString(org.mockito.Matchers.anyString) MimeType(javax.activation.MimeType)

Aggregations

MimeType (javax.activation.MimeType)67 Test (org.junit.Test)38 Metacard (ddf.catalog.data.Metacard)21 URI (java.net.URI)14 HashMap (java.util.HashMap)14 MimeTypeParseException (javax.activation.MimeTypeParseException)14 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)13 BundleContext (org.osgi.framework.BundleContext)13 ServiceReference (org.osgi.framework.ServiceReference)13 CatalogFramework (ddf.catalog.CatalogFramework)10 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 Serializable (java.io.Serializable)9 Resource (ddf.catalog.resource.Resource)8 IOException (java.io.IOException)8 File (java.io.File)7 Matchers.anyString (org.mockito.Matchers.anyString)7 MetacardCreationException (ddf.catalog.data.MetacardCreationException)6 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)6 Response (javax.ws.rs.core.Response)6 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)5