use of ddf.catalog.content.operation.ReadStorageResponse in project ddf by codice.
the class ContentResourceReader method retrieveResource.
@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> arguments) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
LOGGER.trace("ENTERING: retrieveResource");
ResourceResponse response = null;
if (resourceUri == null) {
throw new ResourceNotFoundException("Unable to find resource - resource URI was null");
}
if (resourceUri.getScheme().equals(ContentItem.CONTENT_SCHEME)) {
LOGGER.debug("Resource URI is content scheme");
String contentId = resourceUri.getSchemeSpecificPart();
if (contentId != null && !contentId.isEmpty()) {
if (arguments != null && arguments.get(ContentItem.QUALIFIER) instanceof String && StringUtils.isNotBlank((String) arguments.get(ContentItem.QUALIFIER))) {
try {
resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), (String) arguments.get(ContentItem.QUALIFIER));
} catch (URISyntaxException e) {
throw new ResourceNotFoundException("Unable to create with qualifier", e);
}
}
ReadStorageRequest readRequest = new ReadStorageRequestImpl(resourceUri, arguments);
try {
ReadStorageResponse readResponse = storage.read(readRequest);
ContentItem contentItem = readResponse.getContentItem();
String fileName = contentItem.getFilename();
LOGGER.debug("resource name: {}", fileName);
InputStream is = contentItem.getInputStream();
response = new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), contentItem.getMimeType(), fileName));
} catch (StorageException e) {
throw new ResourceNotFoundException(e);
}
}
}
LOGGER.trace("EXITING: retrieveResource");
return response;
}
use of ddf.catalog.content.operation.ReadStorageResponse in project ddf by codice.
the class FileSystemStorageProviderTest method assertReadRequest.
private void assertReadRequest(String uriString, String mimeType, boolean extension) throws StorageException, IOException, URISyntaxException {
final URI uri = new URI(uriString);
ReadStorageRequest readRequest = new ReadStorageRequestImpl(uri, Collections.emptyMap());
ReadStorageResponse readResponse = provider.read(readRequest);
ContentItem item = readResponse.getContentItem();
LOGGER.debug("Item retrieved: {}", item);
assertThat(item, notNullValue());
assertThat(item.getId(), is(uri.getSchemeSpecificPart()));
if (uri.getFragment() != null) {
assertThat(item.getQualifier(), is(uri.getFragment()));
}
if (mimeType.equals(NITF_MIME_TYPE)) {
assertThat(item.getMimeTypeRawData(), is(NITF_MIME_TYPE));
}
List<String> parts = provider.getContentFilePathParts(uri.getSchemeSpecificPart(), uri.getFragment());
String expectedFilePath = baseDir + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_REPOSITORY + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_STORE + File.separator + parts.get(0) + File.separator + parts.get(1) + File.separator + parts.get(2) + (StringUtils.isNotBlank(item.getQualifier()) ? File.separator + item.getQualifier() : "") + File.separator + item.getFilename();
if (extension) {
expectedFilePath = expectedFilePath + "." + FileSystemStorageProvider.REF_EXT;
}
assertThat(Files.exists(Paths.get(expectedFilePath)), is(true));
assertTrue(item.getSize() > 0);
}
use of ddf.catalog.content.operation.ReadStorageResponse in project ddf by codice.
the class FileSystemStorageProviderTest method testReadDeletedReference.
@Test(expected = StorageException.class)
public void testReadDeletedReference() throws Exception {
Path tempFile = Files.createTempFile("test", "nitf");
Files.write(tempFile, TEST_INPUT_CONTENTS.getBytes());
CreateStorageResponse createResponse = assertContentItem(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME, Collections.singletonMap(Constants.STORE_REFERENCE_KEY, tempFile.toFile().getAbsolutePath()));
URI uri = new URI(createResponse.getCreatedContentItems().get(0).getUri());
ReadStorageResponse read = provider.read(new ReadStorageRequestImpl(new URI(createResponse.getCreatedContentItems().get(0).getUri()), Collections.emptyMap()));
assertThat(read.getContentItem(), notNullValue());
Files.delete(tempFile);
provider.read(new ReadStorageRequestImpl(new URI(createResponse.getCreatedContentItems().get(0).getUri()), Collections.emptyMap()));
}
use of ddf.catalog.content.operation.ReadStorageResponse in project ddf by codice.
the class HistorianTest method testTryCommitStorageException.
@Test(expected = IngestException.class)
public void testTryCommitStorageException() throws StorageException, UnsupportedQueryException, SourceUnavailableException, IngestException, URISyntaxException {
List<Metacard> metacards = getMetacardUpdatePair();
// Mock out a bad storage provider
StorageProvider exceptionStorageProvider = mock(StorageProvider.class);
doThrow(StorageException.class).when(exceptionStorageProvider).commit(any());
ContentItem item = mock(ContentItem.class);
when(item.getId()).thenReturn(METACARD_ID);
when(item.getUri()).thenReturn(RESOURCE_URI);
when(item.getMetacard()).thenReturn(metacards.get(0));
ReadStorageResponse readStorageResponse = mock(ReadStorageResponse.class);
when(readStorageResponse.getContentItem()).thenReturn(item);
when(exceptionStorageProvider.read(any())).thenReturn(readStorageResponse);
when(exceptionStorageProvider.create(any())).thenReturn(mock(CreateStorageResponse.class));
historian.setStorageProviders(Collections.singletonList(exceptionStorageProvider));
// Parameters for historian
UpdateStorageRequest storageRequest = mock(UpdateStorageRequest.class);
UpdateStorageResponse storageResponse = mock(UpdateStorageResponse.class);
UpdateResponse updateResponse = mock(UpdateResponse.class);
// send a request to update the metacard
updateMetacard(storageRequest, storageResponse, metacards.get(1));
mockQuery(metacards.get(1));
historian.version(storageRequest, storageResponse, updateResponse);
verify(exceptionStorageProvider).rollback(any(StorageRequest.class));
}
use of ddf.catalog.content.operation.ReadStorageResponse in project ddf by codice.
the class HistorianTest method testRollbackFailed.
@Test(expected = IngestException.class)
public void testRollbackFailed() throws StorageException, UnsupportedQueryException, SourceUnavailableException, IngestException {
List<Metacard> metacards = getMetacardUpdatePair();
// Mock out a bad storage provider
StorageProvider exceptionStorageProvider = mock(StorageProvider.class);
doThrow(StorageException.class).when(exceptionStorageProvider).commit(any());
doThrow(StorageException.class).when(exceptionStorageProvider).rollback(any());
ContentItem item = mock(ContentItem.class);
when(item.getId()).thenReturn(METACARD_ID);
when(item.getUri()).thenReturn(RESOURCE_URI);
when(item.getMetacard()).thenReturn(metacards.get(0));
ReadStorageResponse readStorageResponse = mock(ReadStorageResponse.class);
when(readStorageResponse.getContentItem()).thenReturn(item);
when(exceptionStorageProvider.read(any())).thenReturn(readStorageResponse);
when(exceptionStorageProvider.create(any())).thenReturn(mock(CreateStorageResponse.class));
historian.setStorageProviders(Collections.singletonList(exceptionStorageProvider));
// Parameters for historian
UpdateStorageRequest storageRequest = mock(UpdateStorageRequest.class);
UpdateStorageResponse storageResponse = mock(UpdateStorageResponse.class);
UpdateResponse updateResponse = mock(UpdateResponse.class);
// send a request to update the metacard
updateMetacard(storageRequest, storageResponse, metacards.get(1));
mockQuery(metacards.get(1));
historian.version(storageRequest, storageResponse, updateResponse);
verify(exceptionStorageProvider).rollback(any(StorageRequest.class));
}
Aggregations