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