use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class DuplicateCommands method ingestMetacards.
protected List<Metacard> ingestMetacards(CatalogFacade provider, List<Metacard> metacards) {
if (metacards.isEmpty()) {
return Collections.emptyList();
}
List<Metacard> createdMetacards = new ArrayList<>();
LOGGER.debug("Preparing to ingest {} records", metacards.size());
CreateRequest createRequest = new CreateRequestImpl(metacards);
CreateResponse createResponse;
try {
createResponse = provider.create(createRequest);
createdMetacards = createResponse.getCreatedMetacards();
} catch (IngestException e) {
printErrorMessage(String.format("Received error while ingesting: %s%n", e.getMessage()));
LOGGER.debug("Error during ingest. Attempting to ingest batch individually.");
return ingestSingly(provider, metacards);
} catch (SourceUnavailableException e) {
printErrorMessage(String.format("Received error while ingesting: %s%n", e.getMessage()));
LOGGER.debug("Error during ingest:", e);
return createdMetacards;
} catch (Exception e) {
printErrorMessage(String.format("Unexpected Exception received while ingesting: %s%n", e.getMessage()));
LOGGER.debug("Unexpected Exception during ingest:", e);
return createdMetacards;
}
ingestedCount.addAndGet(createdMetacards.size());
failedCount.addAndGet(metacards.size() - createdMetacards.size());
failedMetacards.addAll(subtract(metacards, createdMetacards));
return createdMetacards;
}
use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class ContentProducerDataAccessObjectTest method testCreateContentItem.
@Test
public void testCreateContentItem() throws Exception {
File testFile = temporaryFolder.newFile("testCreateContentItem.txt");
// make sample list of metacard and set of keys
List<MetacardImpl> metacardList = ImmutableList.of(new MetacardImpl());
String uri = testFile.toURI().toASCIIString();
Set<String> keys = new HashSet<>(Collections.singletonList(String.valueOf(DigestUtils.sha1Hex(uri))));
// mock out responses for create, delete, update
CreateResponse mockCreateResponse = mock(CreateResponse.class);
doReturn(metacardList).when(mockCreateResponse).getCreatedMetacards();
DeleteResponse mockDeleteResponse = mock(DeleteResponse.class);
doReturn(metacardList).when(mockDeleteResponse).getDeletedMetacards();
UpdateResponse mockUpdateResponse = mock(UpdateResponse.class);
doReturn(metacardList).when(mockUpdateResponse).getUpdatedMetacards();
// setup mockFileSystemPersistenceProvider
FileSystemPersistenceProvider mockFileSystemPersistenceProvider = mock(FileSystemPersistenceProvider.class);
doReturn(keys).when(mockFileSystemPersistenceProvider).loadAllKeys();
doAnswer(invocationOnMock -> keys.remove(invocationOnMock.getArguments()[0])).when(mockFileSystemPersistenceProvider).delete(anyString());
doReturn("sample").when(mockFileSystemPersistenceProvider).loadFromPersistence(any(String.class));
// setup mockCatalogFramework
CatalogFramework mockCatalogFramework = mock(CatalogFramework.class);
doReturn(mockCreateResponse).when(mockCatalogFramework).create(any(CreateStorageRequest.class));
doReturn(mockDeleteResponse).when(mockCatalogFramework).delete(any(DeleteRequest.class));
// setup mockSourceInfo
SourceInfoResponse mockSourceInfoResponse = mock(SourceInfoResponse.class);
SourceDescriptor mockSourceDescriptor = mock(SourceDescriptor.class);
when(mockSourceDescriptor.isAvailable()).thenReturn(true);
when(mockSourceInfoResponse.getSourceInfo()).thenReturn(Collections.singleton(mockSourceDescriptor));
when(mockCatalogFramework.getSourceInfo(any(SourceInfoRequest.class))).thenReturn(mockSourceInfoResponse);
// setup mockComponent
ContentComponent mockComponent = mock(ContentComponent.class);
doReturn(mockCatalogFramework).when(mockComponent).getCatalogFramework();
// setup mockEndpoint
ContentEndpoint mockEndpoint = mock(ContentEndpoint.class);
doReturn(mockComponent).when(mockEndpoint).getComponent();
WatchEvent.Kind<Path> kind;
String mimeType = "txt";
Map<String, Object> headers = new HashedMap();
Map<String, Serializable> attributeOverrides = new HashMap<>();
attributeOverrides.put("example", ImmutableList.of("something", "something1"));
attributeOverrides.put("example2", ImmutableList.of("something2"));
headers.put(Constants.ATTRIBUTE_OVERRIDES_KEY, attributeOverrides);
headers.put(Constants.STORE_REFERENCE_KEY, uri);
kind = StandardWatchEventKinds.ENTRY_CREATE;
contentProducerDataAccessObject.createContentItem(mockFileSystemPersistenceProvider, mockEndpoint, testFile, kind, mimeType, headers);
verify(mockCatalogFramework).create(any(CreateStorageRequest.class));
kind = StandardWatchEventKinds.ENTRY_MODIFY;
contentProducerDataAccessObject.createContentItem(mockFileSystemPersistenceProvider, mockEndpoint, testFile, kind, mimeType, headers);
verify(mockCatalogFramework).update(any(UpdateStorageRequest.class));
kind = StandardWatchEventKinds.ENTRY_DELETE;
contentProducerDataAccessObject.createContentItem(mockFileSystemPersistenceProvider, mockEndpoint, testFile, kind, mimeType, headers);
verify(mockCatalogFramework).delete(any(DeleteRequest.class));
contentProducerDataAccessObject.createContentItem(mockFileSystemPersistenceProvider, mockEndpoint, testFile, kind, mimeType, headers);
verify(mockCatalogFramework).delete(any(DeleteRequest.class));
}
use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class IngestCommand method processBatch.
private boolean processBatch(CatalogFacade catalog, ArrayList<Metacard> metacards) throws SourceUnavailableException {
CreateResponse createResponse = null;
try {
createResponse = createMetacards(catalog, metacards);
} catch (IngestException e) {
printErrorMessage("Error executing command: " + e.getMessage());
if (INGEST_LOGGER.isWarnEnabled()) {
INGEST_LOGGER.warn("Error ingesting metacard batch {}", buildIngestLog(metacards), e);
}
} catch (SourceUnavailableException e) {
if (INGEST_LOGGER.isWarnEnabled()) {
INGEST_LOGGER.warn("Error on process batch, local Provider not available. {}" + " metacards failed to ingest. {}", metacards.size(), buildIngestLog(metacards), e);
}
} finally {
IntStream range = IntStream.range(0, metacards.size());
range.forEach(i -> phaser.arriveAndDeregister());
range.close();
}
if (createResponse != null) {
ingestCount.getAndAdd(metacards.size());
}
return createResponse != null;
}
use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class MockMemoryProvider method create.
@Override
public CreateResponse create(CreateRequest request) {
List<Metacard> oldCards = request.getMetacards();
hasReceivedCreate = true;
List<Metacard> returnedMetacards = new ArrayList<>(oldCards.size());
Map<String, Serializable> properties = new HashMap<>();
for (Metacard curCard : oldCards) {
MetacardImpl card = new MetacardImpl(curCard);
if (card.getId() == null) {
card.setId(UUID.randomUUID().toString());
}
LOGGER.debug("Storing metacard with id: {}", card.getId());
store.put(card.getId(), card);
properties.put(card.getId(), card);
returnedMetacards.add(card);
}
CreateResponse ingestResponseImpl = new CreateResponseImpl(request, properties, returnedMetacards);
return ingestResponseImpl;
}
use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class Historian method version.
/**
* Versions updated {@link Metacard}s and {@link ContentItem}s.
*
* @param streamUpdateRequest Needed to pass {@link
* ddf.catalog.core.versioning.MetacardVersion#SKIP_VERSIONING} flag into downstream update
* @param updateStorageResponse Versions this response's updated items
* @return the update response originally passed in
* @throws UnsupportedQueryException
* @throws SourceUnavailableException
* @throws IngestException
*/
public UpdateStorageResponse version(UpdateStorageRequest streamUpdateRequest, UpdateStorageResponse updateStorageResponse, UpdateResponse updateResponse) throws UnsupportedQueryException, SourceUnavailableException, IngestException {
if (doSkip(updateStorageResponse)) {
return updateStorageResponse;
}
setSkipFlag(streamUpdateRequest);
setSkipFlag(updateStorageResponse);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Versioning updated metacards and content: {}", updateStorageResponse);
}
List<Metacard> updatedMetacards = updateStorageResponse.getUpdatedContentItems().stream().filter(ci -> StringUtils.isBlank(ci.getQualifier())).map(ContentItem::getMetacard).filter(Objects::nonNull).filter(isNotVersionNorDeleted).collect(Collectors.toList());
if (updatedMetacards.isEmpty()) {
LOGGER.trace("No updated metacards applicable to versioning");
securityLogger.audit("Skipping versioning updated metacards with ids: {}", updateStorageResponse.getUpdatedContentItems().stream().map(ContentItem::getMetacard).filter(Objects::nonNull).map(Metacard::getId).collect(TO_A_STRING));
return updateStorageResponse;
}
Map<String, Metacard> originalMetacards = updateResponse.getUpdatedMetacards().stream().map(Update::getOldMetacard).collect(Collectors.toMap(Metacard::getId, Function.identity(), Historian::firstInWinsMerge));
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Found current data for the following metacards: {}", getList(originalMetacards));
}
Collection<ReadStorageRequest> ids = getReadStorageRequests(updatedMetacards);
Map<String, List<ContentItem>> content = getContent(ids);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Found resources for the following metacards: {}", getList(content));
}
Function<String, Action> getAction = id -> content.containsKey(id) ? Action.VERSIONED_CONTENT : Action.VERSIONED;
Map<String, Metacard> versionMetacards = getVersionMetacards(originalMetacards.values(), getAction, (Subject) updateResponse.getProperties().get(SecurityConstants.SECURITY_SUBJECT));
CreateStorageResponse createStorageResponse = versionContentItems(content, versionMetacards);
if (createStorageResponse == null) {
String message = "Could not version content items for: " + getList(originalMetacards);
securityLogger.audit(message);
LOGGER.debug(message);
return updateStorageResponse;
}
setResourceUriForContent(/*mutable*/
versionMetacards, createStorageResponse);
CreateResponse createResponse = storeVersionMetacards(versionMetacards);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Successfully created metacard versions under ids: {}", createResponse.getCreatedMetacards().stream().map(Metacard::getId).collect(TO_A_STRING));
}
return updateStorageResponse;
}
Aggregations