use of javax.activation.MimeType in project ddf by codice.
the class ResourceMetacardTransformerTest method testNullMetacardId.
@Test
public void testNullMetacardId() throws Exception {
thrown.expect(CatalogTransformerException.class);
thrown.expectMessage("Could not transform metacard to a resource because the metacard is not valid.");
String filePath = ABSOLUTE_PATH + TEST_PATH + TS_FILE_NAME_1;
URI uri = getUri(filePath);
Metacard metacard = getMockMetacard(uri);
when(metacard.getId()).thenReturn(null);
boolean expectSuccess = false;
MimeType mimeType = getMimeType(VIDEO_MIME_TYPE);
CatalogFramework framework = getFramework(getResourceResponse(getResource(mimeType, uri)));
testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
use of javax.activation.MimeType in project ddf by codice.
the class ResourceMetacardTransformerTest method testResourceHasNullMimeType.
@Test
public void testResourceHasNullMimeType() throws Exception {
String filePath = ABSOLUTE_PATH + TEST_PATH + TS_FILE_NAME_1;
URI uri = getUri(filePath);
Metacard metacard = getMockMetacard(uri);
boolean expectSuccess = true;
MimeType mimeType = getMimeType(DEFAULT_MIME_TYPE);
CatalogFramework framework = getFramework(getResourceResponse(getResource(null, uri)));
testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
use of javax.activation.MimeType in project ddf by codice.
the class ResourceMetacardTransformer method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
LOGGER.trace("Entering resource ResourceMetacardTransformer.transform");
if (!isValid(metacard)) {
throw new CatalogTransformerException("Could not transform metacard to a resource because the metacard is not valid.");
}
if (StringUtils.isNotEmpty(metacard.getResourceSize())) {
arguments.put(Metacard.RESOURCE_SIZE, metacard.getResourceSize());
}
String id = metacard.getId();
LOGGER.debug("executing resource request with id '{}'", id);
final ResourceRequest resourceRequest = new ResourceRequestById(id, arguments);
ResourceResponse resourceResponse = null;
String sourceName = metacard.getSourceId();
if (StringUtils.isBlank(sourceName)) {
sourceName = catalogFramework.getId();
}
String resourceUriAscii = "";
if (metacard.getResourceURI() != null) {
resourceUriAscii = metacard.getResourceURI().toASCIIString();
}
try {
resourceResponse = catalogFramework.getResource(resourceRequest, sourceName);
} catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
}
if (resourceResponse == null) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii));
}
Resource transformedContent = resourceResponse.getResource();
MimeType mimeType = transformedContent.getMimeType();
if (mimeType == null) {
try {
mimeType = new MimeType(DEFAULT_MIME_TYPE_STR);
// There is no method to set the MIME type, so in order to set it to our default
// one, we need to create a new object.
transformedContent = new ResourceImpl(transformedContent.getInputStream(), mimeType, transformedContent.getName());
} catch (MimeTypeParseException e) {
throw new CatalogTransformerException("Could not create default mime type upon null mimeType, for default mime type '" + DEFAULT_MIME_TYPE_STR + "'.", e);
}
}
LOGGER.debug("Found mime type: '{}' for product of metacard with id: '{}'.\nGetting associated resource from input stream. \n", mimeType, id);
LOGGER.trace("Exiting resource transform for metacard id: '{}'", id);
return transformedContent;
}
use of javax.activation.MimeType in project ddf by codice.
the class InputTransformerProducer method transform.
@Override
protected Object transform(Message in, String mimeType, String transformerId, MimeTypeToTransformerMapper mapper) throws MimeTypeParseException, CatalogTransformerException {
MimeType derivedMimeType = null;
try (InputStream message = in.getBody(InputStream.class);
TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {
if (message == null) {
throw new CatalogTransformerException("Message body was null; unable to generate Metacard!");
}
// First try to get mimeType from file extension passed in the Camel Message headers
IOUtils.copy(message, tfbos);
String fileExtensionHeader = getHeaderAsStringAndRemove(in, FILE_EXTENSION_HEADER);
if (StringUtils.isNotEmpty(fileExtensionHeader)) {
Optional<String> fileMimeType = getMimeTypeFor(tfbos.asByteSource().openBufferedStream(), fileExtensionHeader);
if (fileMimeType.isPresent()) {
LOGGER.trace("Setting mimetype to [{}] from Message header [{}]", fileMimeType.get(), FILE_EXTENSION_HEADER);
derivedMimeType = new MimeType(fileMimeType.get());
}
}
if (derivedMimeType == null) {
if (StringUtils.isNotEmpty(mimeType)) {
// mimeType and tranformerId
if (StringUtils.isNotEmpty(transformerId)) {
derivedMimeType = new MimeType(mimeType + ";" + MimeTypeToTransformerMapper.ID_KEY + "=" + transformerId);
LOGGER.trace("Using mimeType to [{}]", derivedMimeType);
} else {
LOGGER.trace("Using CatalogEndpoint's configured mimeType [{}]", mimeType);
derivedMimeType = new MimeType(mimeType);
}
} else {
LOGGER.debug("Unable to determine mimeType. Defaulting to [{}]", DEFAULT_MIME_TYPE);
derivedMimeType = new MimeType(DEFAULT_MIME_TYPE);
}
}
String metacardUpdateID = getHeaderAsStringAndRemove(in, METACARD_ID_HEADER);
return generateMetacard(derivedMimeType, mapper, tfbos, metacardUpdateID).orElseThrow(() -> new CatalogTransformerException(String.format("Did not find an InputTransformer for MIME Type [%s] and %s [%s]", mimeType, MimeTypeToTransformerMapper.ID_KEY, transformerId)));
} catch (IOException e) {
throw new CatalogTransformerException("Unable to transform incoming product", e);
}
}
use of javax.activation.MimeType in project ddf by codice.
the class BinaryContentStringTypeConverter method convertTo.
@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
String mimeTypeString = exchange.getMessage().getHeader(HttpHeaders.CONTENT_TYPE, String.class);
if (null == mimeTypeString) {
mimeTypeString = MediaType.TEXT_PLAIN;
}
MimeType mimeType = null;
try {
mimeType = new MimeType(mimeTypeString);
} catch (MimeTypeParseException e) {
LOGGER.debug("Failed to parse mimetype: " + mimeTypeString, e);
}
T result = null;
try {
result = type.cast(new BinaryContentImpl(exchange.getMessage().getBody(InputStream.class), mimeType));
} catch (ClassCastException e) {
LOGGER.debug("Failed to create BinaryContent", e);
}
return result;
}
Aggregations