use of javax.activation.MimeType in project ddf by codice.
the class TestResourceMetacardTransformer method testFrameworkThrowsResourceNotSupportedException.
@Test
public void testFrameworkThrowsResourceNotSupportedException() throws Exception {
String filePath = ABSOLUTE_PATH + TEST_PATH + JPEG_FILE_NAME_1;
URI uri = getUri(filePath);
Metacard metacard = getMockMetacard(uri);
thrown.expect(CatalogTransformerException.class);
thrown.expectMessage("Unable to retrieve resource.");
thrown.expectMessage("Metacard id: " + TEST_ID);
thrown.expectMessage("Uri: " + uri);
thrown.expectMessage("Source: " + TEST_SITE);
boolean expectSuccess = false;
MimeType mimeType = getMimeType(JPEG_MIME_TYPE);
CatalogFramework framework = getFrameworkException(new ResourceNotSupportedException("Test Resource Not Supported Exception"));
testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
use of javax.activation.MimeType 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;
}
use of javax.activation.MimeType in project ddf by codice.
the class RESTEndpoint method updateDocument.
/**
* REST Put. Updates the specified entry with the provided document.
*
* @param id
* @param message
* @return
*/
@PUT
@Path("/{id}")
@Consumes("multipart/*")
public Response updateDocument(@PathParam("id") String id, @Context HttpHeaders headers, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
LOGGER.trace("PUT");
Response response;
try {
if (id != null && message != null) {
MimeType mimeType = getMimeType(headers);
CreateInfo createInfo = null;
if (multipartBody != null) {
List<Attachment> contentParts = multipartBody.getAllAttachments();
if (contentParts != null && contentParts.size() > 0) {
createInfo = parseAttachments(contentParts, transformerParam);
} else {
LOGGER.debug("No file contents attachment found");
}
}
if (createInfo == null) {
UpdateRequest updateRequest = new UpdateRequestImpl(id, generateMetacard(mimeType, id, message, transformerParam));
catalogFramework.update(updateRequest);
} else {
UpdateStorageRequest streamUpdateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, createInfo.getStream(), createInfo.getContentType(), createInfo.getFilename(), 0, createInfo.getMetacard())), null);
catalogFramework.update(streamUpdateRequest);
}
LOGGER.debug("Metacard {} updated.", id);
response = Response.ok().build();
} else {
String errorResponseString = "Both ID and content are needed to perform UPDATE.";
LOGGER.info(errorResponseString);
throw new ServerErrorException(errorResponseString, Status.BAD_REQUEST);
}
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot update catalog entry: Source is unavailable: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (InternalIngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (MetacardCreationException | IngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
}
return response;
}
use of javax.activation.MimeType in project ddf by codice.
the class TestVideoThumbnailPlugin method testCreatedItemNotVideoFile.
@Test
public void testCreatedItemNotVideoFile() throws Exception {
mockContentItem = mock(ContentItem.class);
doReturn(new MimeType("image/jpeg")).when(mockContentItem).getMimeType();
Metacard mockMetacard = new MetacardImpl();
doReturn(mockMetacard).when(mockContentItem).getMetacard();
final CreateStorageResponse mockCreateResponse = mock(CreateStorageResponse.class);
doReturn(Collections.singletonList(mockContentItem)).when(mockCreateResponse).getCreatedContentItems();
final CreateStorageResponse processedCreateResponse = videoThumbnailPlugin.process(mockCreateResponse);
assertThat(processedCreateResponse.getCreatedContentItems().get(0).getMetacard().getAttribute(Metacard.THUMBNAIL), CoreMatchers.is(nullValue()));
}
use of javax.activation.MimeType in project ddf by codice.
the class TestVideoThumbnailPlugin method testUpdatedItemNotVideoFile.
@Test
public void testUpdatedItemNotVideoFile() throws Exception {
mockContentItem = mock(ContentItem.class);
doReturn(new MimeType("application/pdf")).when(mockContentItem).getMimeType();
Metacard mockMetacard = new MetacardImpl();
doReturn(mockMetacard).when(mockContentItem).getMetacard();
final UpdateStorageResponse mockUpdateResponse = mock(UpdateStorageResponse.class);
doReturn(Collections.singletonList(mockContentItem)).when(mockUpdateResponse).getUpdatedContentItems();
final UpdateStorageResponse processedUpdateResponse = videoThumbnailPlugin.process(mockUpdateResponse);
assertThat(processedUpdateResponse.getUpdatedContentItems().get(0).getMetacard().getAttribute(Metacard.THUMBNAIL), CoreMatchers.is(nullValue()));
}
Aggregations