use of ddf.catalog.content.operation.UpdateStorageRequest 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.UpdateStorageRequest in project ddf by codice.
the class HistorianTest method testUpdateStorageResponseNoContentItems.
@Test
public void testUpdateStorageResponseNoContentItems() throws StorageException, UnsupportedQueryException, SourceUnavailableException, IngestException, URISyntaxException {
// The metacard and updated metacard
List<Metacard> metacards = getMetacardUpdatePair();
// 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);
assertThat(storageProvider.storageMap.size(), equalTo(0));
}
use of ddf.catalog.content.operation.UpdateStorageRequest in project ddf by codice.
the class InMemoryProcessingFramework method storeContentItemUpdates.
private void storeContentItemUpdates(Map<String, ContentItem> contentItemsToUpdate, Map<String, Serializable> properties) {
if (MapUtils.isNotEmpty(contentItemsToUpdate)) {
LOGGER.trace("Storing content item updates(s)");
UpdateStorageRequest updateStorageRequest = new UpdateStorageRequestImpl(new ArrayList<>(contentItemsToUpdate.values()), properties);
Subject subject = (Subject) updateStorageRequest.getProperties().get(SecurityConstants.SECURITY_SUBJECT);
if (subject == null) {
LOGGER.debug("No subject to send UpdateStorageRequest. Updates will not be sent back to the catalog");
} else {
subject.execute(() -> {
try {
catalogFramework.update(updateStorageRequest);
LOGGER.debug("Successfully completed update storage request");
} catch (IngestException | SourceUnavailableException | RuntimeException e) {
LOGGER.info("Unable to complete update storage request", e);
}
return null;
});
}
} else {
LOGGER.debug("No content items to update");
}
}
use of ddf.catalog.content.operation.UpdateStorageRequest in project ddf by codice.
the class ContentProducerDataAccessObject method createContentItem.
public void createContentItem(FileSystemPersistenceProvider fileIdMap, ContentEndpoint endpoint, File ingestedFile, WatchEvent.Kind<Path> eventType, String mimeType, Map<String, Object> headers) throws SourceUnavailableException, IngestException {
LOGGER.debug("Creating content item.");
String key = String.valueOf(ingestedFile.getAbsolutePath().hashCode());
String id = fileIdMap.loadAllKeys().contains(key) ? String.valueOf(fileIdMap.loadFromPersistence(key)) : null;
if (StandardWatchEventKinds.ENTRY_CREATE.equals(eventType)) {
CreateStorageRequest createRequest = new CreateStorageRequestImpl(Collections.singletonList(new ContentItemImpl(uuidGenerator.generateUuid(), Files.asByteSource(ingestedFile), mimeType, ingestedFile.getName(), 0L, null)), null);
processHeaders(headers, createRequest, ingestedFile);
CreateResponse createResponse = endpoint.getComponent().getCatalogFramework().create(createRequest);
if (createResponse != null) {
List<Metacard> createdMetacards = createResponse.getCreatedMetacards();
for (Metacard metacard : createdMetacards) {
fileIdMap.store(key, metacard.getId());
LOGGER.debug("content item created with id = {}", metacard.getId());
}
}
} else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(eventType)) {
UpdateStorageRequest updateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new ContentItemImpl(id, Files.asByteSource(ingestedFile), mimeType, ingestedFile.getName(), 0, null)), null);
processHeaders(headers, updateRequest, ingestedFile);
UpdateResponse updateResponse = endpoint.getComponent().getCatalogFramework().update(updateRequest);
if (updateResponse != null) {
List<Update> updatedMetacards = updateResponse.getUpdatedMetacards();
for (Update update : updatedMetacards) {
LOGGER.debug("content item updated with id = {}", update.getNewMetacard().getId());
}
}
} else if (StandardWatchEventKinds.ENTRY_DELETE.equals(eventType)) {
DeleteRequest deleteRequest = new DeleteRequestImpl(id);
DeleteResponse deleteResponse = endpoint.getComponent().getCatalogFramework().delete(deleteRequest);
if (deleteResponse != null) {
List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards();
for (Metacard delete : deletedMetacards) {
fileIdMap.delete(ingestedFile.getAbsolutePath());
LOGGER.debug("content item deleted with id = {}", delete.getId());
}
}
}
}
use of ddf.catalog.content.operation.UpdateStorageRequest in project ddf by codice.
the class UpdateOperations method update.
public UpdateResponse update(UpdateStorageRequest streamUpdateRequest) throws IngestException, SourceUnavailableException {
Map<String, Metacard> metacardMap = new HashMap<>();
List<ContentItem> contentItems = new ArrayList<>(streamUpdateRequest.getContentItems().size());
HashMap<String, Map<String, Path>> tmpContentPaths = new HashMap<>();
UpdateResponse updateResponse = null;
UpdateStorageRequest updateStorageRequest = null;
UpdateStorageResponse updateStorageResponse = null;
streamUpdateRequest = opsStorageSupport.prepareStorageRequest(streamUpdateRequest, streamUpdateRequest::getContentItems);
// Operation populates the metacardMap, contentItems, and tmpContentPaths
opsMetacardSupport.generateMetacardAndContentItems(streamUpdateRequest.getContentItems(), metacardMap, contentItems, tmpContentPaths);
streamUpdateRequest.getProperties().put(CONTENT_PATHS, tmpContentPaths);
streamUpdateRequest = applyAttributeOverrides(streamUpdateRequest, metacardMap);
try {
if (!contentItems.isEmpty()) {
updateStorageRequest = new UpdateStorageRequestImpl(contentItems, streamUpdateRequest.getId(), streamUpdateRequest.getProperties());
updateStorageRequest = processPreUpdateStoragePlugins(updateStorageRequest);
try {
updateStorageResponse = sourceOperations.getStorage().update(updateStorageRequest);
updateStorageResponse.getProperties().put(CONTENT_PATHS, tmpContentPaths);
} catch (StorageException e) {
throw new IngestException("Could not store content items. Removed created metacards.", e);
}
updateStorageResponse = processPostUpdateStoragePlugins(updateStorageResponse);
for (ContentItem contentItem : updateStorageResponse.getUpdatedContentItems()) {
if (StringUtils.isBlank(contentItem.getQualifier())) {
Metacard metacard = metacardMap.get(contentItem.getId());
Metacard overrideMetacard = contentItem.getMetacard();
Metacard updatedMetacard = OverrideAttributesSupport.overrideMetacard(metacard, overrideMetacard, true, true);
updatedMetacard.setAttribute(new AttributeImpl(Metacard.RESOURCE_SIZE, String.valueOf(contentItem.getSize())));
metacardMap.put(contentItem.getId(), updatedMetacard);
}
}
}
UpdateRequestImpl updateRequest = new UpdateRequestImpl(Iterables.toArray(metacardMap.values().stream().map(Metacard::getId).collect(Collectors.toList()), String.class), new ArrayList<>(metacardMap.values()));
updateRequest.setProperties(streamUpdateRequest.getProperties());
historian.setSkipFlag(updateRequest);
updateResponse = doUpdate(updateRequest);
historian.version(streamUpdateRequest, updateStorageResponse, updateResponse);
} catch (Exception e) {
if (updateStorageRequest != null) {
try {
sourceOperations.getStorage().rollback(updateStorageRequest);
} catch (StorageException e1) {
LOGGER.info("Unable to remove temporary content for id: {}", updateStorageRequest.getId(), e1);
}
}
throw new IngestException("Unable to store products for request: " + streamUpdateRequest.getId(), e);
} finally {
opsStorageSupport.commitAndCleanup(updateStorageRequest, tmpContentPaths);
}
updateResponse = doPostIngest(updateResponse);
return updateResponse;
}
Aggregations