use of javax.activation.MimeTypeParseException 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 e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
} catch (ResourceNotFoundException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
} catch (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.MimeTypeParseException in project ddf by codice.
the class ReliableResourceDownloadManagerTest method getMockResourceRetrieverWithRetryCapability.
private ResourceRetriever getMockResourceRetrieverWithRetryCapability(final RetryType retryType, final boolean readSlow) throws Exception {
// Mocking to support re-retrieval of product when error encountered
// during caching.
ResourceRetriever retriever = mock(ResourceRetriever.class);
when(retriever.retrieveResource()).thenAnswer(new Answer<Object>() {
int invocationCount = 0;
public Object answer(InvocationOnMock invocation) throws ResourceNotFoundException {
// Create new InputStream for retrieving the same product. This
// simulates re-retrieving the product from the remote source.
invocationCount++;
if (readSlow) {
mis = new MockInputStream(productInputFilename, true);
mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
} else {
mis = new MockInputStream(productInputFilename);
}
if (retryType == RetryType.INPUT_STREAM_IO_EXCEPTION) {
if (invocationCount == 1) {
mis.setInvocationCountToThrowIOException(5);
} else {
mis.setInvocationCountToThrowIOException(-1);
}
} else if (retryType == RetryType.TIMEOUT_EXCEPTION) {
if (invocationCount == 1) {
mis.setInvocationCountToTimeout(3);
mis.setReadDelay(MONITOR_PERIOD * 2, TimeUnit.SECONDS);
} else {
mis.setInvocationCountToTimeout(-1);
mis.setReadDelay(0, TimeUnit.SECONDS);
}
} else if (retryType == RetryType.NETWORK_CONNECTION_UP_AND_DOWN) {
mis.setInvocationCountToThrowIOException(2);
} else if (retryType == RetryType.NETWORK_CONNECTION_DROPPED) {
if (invocationCount == 1) {
mis.setInvocationCountToThrowIOException(2);
} else {
throw new ResourceNotFoundException();
}
}
// Reset the mock Resource so that it can be reconfigured to return
// the new InputStream
reset(resource);
when(resource.getInputStream()).thenReturn(mis);
when(resource.getName()).thenReturn("test-resource");
try {
when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
} catch (MimeTypeParseException e) {
}
// Reset the mock ResourceResponse so that it can be reconfigured to return
// the new Resource
reset(resourceResponse);
when(resourceResponse.getRequest()).thenReturn(resourceRequest);
when(resourceResponse.getResource()).thenReturn(resource);
when(resourceResponse.getProperties()).thenReturn(new HashMap<String, Serializable>());
return resourceResponse;
}
});
ArgumentCaptor<Long> bytesReadArg = ArgumentCaptor.forClass(Long.class);
// Mocking to support re-retrieval of product when error encountered
// during caching. This resource retriever supports skipping.
when(retriever.retrieveResource(anyLong())).thenAnswer(new Answer<Object>() {
int invocationCount = 0;
public Object answer(InvocationOnMock invocation) throws ResourceNotFoundException, IOException {
// Create new InputStream for retrieving the same product. This
// simulates re-retrieving the product from the remote source.
invocationCount++;
if (readSlow) {
mis = new MockInputStream(productInputFilename, true);
mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
} else {
mis = new MockInputStream(productInputFilename);
}
// Skip the number of bytes that have already been read
Object[] args = invocation.getArguments();
long bytesToSkip = (Long) args[0];
mis.skip(bytesToSkip);
if (retryType == RetryType.INPUT_STREAM_IO_EXCEPTION) {
if (invocationCount == 1) {
mis.setInvocationCountToThrowIOException(5);
} else {
mis.setInvocationCountToThrowIOException(-1);
}
} else if (retryType == RetryType.TIMEOUT_EXCEPTION) {
if (invocationCount == 1) {
mis.setInvocationCountToTimeout(3);
mis.setReadDelay(MONITOR_PERIOD * 2, TimeUnit.SECONDS);
} else {
mis.setInvocationCountToTimeout(-1);
mis.setReadDelay(0, TimeUnit.MILLISECONDS);
}
} else if (retryType == RetryType.NETWORK_CONNECTION_UP_AND_DOWN) {
mis.setInvocationCountToThrowIOException(2);
} else if (retryType == RetryType.NETWORK_CONNECTION_DROPPED) {
if (invocationCount == 1) {
mis.setInvocationCountToThrowIOException(2);
} else {
throw new ResourceNotFoundException();
}
}
// Reset the mock Resource so that it can be reconfigured to return
// the new InputStream
reset(resource);
when(resource.getInputStream()).thenReturn(mis);
when(resource.getName()).thenReturn("test-resource");
try {
when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
} catch (MimeTypeParseException e) {
}
// Reset the mock ResourceResponse so that it can be reconfigured to return
// the new Resource
reset(resourceResponse);
when(resourceResponse.getRequest()).thenReturn(resourceRequest);
when(resourceResponse.getResource()).thenReturn(resource);
Map<String, Serializable> responseProperties = new HashMap<>();
responseProperties.put("BytesSkipped", true);
when(resourceResponse.getProperties()).thenReturn(responseProperties);
when(resourceResponse.containsPropertyName("BytesSkipped")).thenReturn(true);
when(resourceResponse.getPropertyValue("BytesSkipped")).thenReturn(true);
return resourceResponse;
}
});
return retriever;
}
Aggregations