use of ddf.catalog.content.operation.CreateStorageResponse 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);
List<Metacard> updatedMetacards = updateStorageResponse.getUpdatedContentItems().stream().filter(ci -> StringUtils.isBlank(ci.getQualifier())).map(ContentItem::getMetacard).filter(Objects::nonNull).filter(isNotVersionNorDeleted).collect(Collectors.toList());
Map<String, Metacard> originalMetacards = query(forIds(updatedMetacards.stream().map(Metacard::getId).collect(Collectors.toList())));
Collection<ReadStorageRequest> ids = getReadStorageRequests(updatedMetacards);
Map<String, List<ContentItem>> content = getContent(ids);
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) {
LOGGER.debug("Could not version content items.");
return updateStorageResponse;
}
setResourceUriForContent(/*mutable*/
versionMetacards, createStorageResponse);
storeVersionMetacards(versionMetacards);
return updateStorageResponse;
}
use of ddf.catalog.content.operation.CreateStorageResponse in project ddf by codice.
the class Historian method version.
/**
* Versions deleted {@link Metacard}s.
*
* @param deleteResponse Versions this responses deleted metacards
*/
public DeleteResponse version(DeleteResponse deleteResponse) throws SourceUnavailableException, IngestException {
if (doSkip(deleteResponse)) {
return deleteResponse;
}
setSkipFlag(deleteResponse);
List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards().stream().filter(isNotVersionNorDeleted).collect(Collectors.toList());
// [ContentItem.getId: content items]
Map<String, List<ContentItem>> contentItems = getContent(getReadStorageRequests(deletedMetacards));
Function<String, Action> getAction = (id) -> contentItems.containsKey(id) ? Action.DELETED_CONTENT : Action.DELETED;
// [MetacardVersion.VERSION_OF_ID: versioned metacard]
Map<String, Metacard> versionedMap = getVersionMetacards(deletedMetacards, getAction, (Subject) deleteResponse.getRequest().getProperties().get(SecurityConstants.SECURITY_SUBJECT));
CreateStorageResponse createStorageResponse = versionContentItems(contentItems, versionedMap);
if (createStorageResponse != null) {
setResourceUriForContent(/*Mutable*/
versionedMap, createStorageResponse);
}
executeAsSystem(() -> catalogProvider().create(new CreateRequestImpl(new ArrayList<>(versionedMap.values()))));
String emailAddress = SubjectUtils.getEmailAddress((Subject) deleteResponse.getProperties().get(SecurityConstants.SECURITY_SUBJECT));
List<Metacard> deletionMetacards = versionedMap.entrySet().stream().map(s -> new DeletedMetacardImpl(uuidGenerator.generateUuid(), s.getKey(), emailAddress, s.getValue().getId(), MetacardVersionImpl.toMetacard(s.getValue(), metacardTypes))).collect(Collectors.toList());
executeAsSystem(() -> catalogProvider().create(new CreateRequestImpl(deletionMetacards, new HashMap<>())));
return deleteResponse;
}
use of ddf.catalog.content.operation.CreateStorageResponse in project ddf by codice.
the class Historian method versionContentItems.
private CreateStorageResponse versionContentItems(Map<String, List<ContentItem>> items, Map<String, Metacard> versionedMetacards) throws SourceUnavailableException, IngestException {
List<ContentItem> contentItems = items.entrySet().stream().map(e -> getVersionedContentItems(e.getValue(), versionedMetacards)).flatMap(Collection::stream).collect(Collectors.toList());
if (contentItems.isEmpty()) {
LOGGER.debug("No content items to version");
return null;
}
CreateStorageResponse createStorageResponse = executeAsSystem(() -> storageProvider().create(new CreateStorageRequestImpl(contentItems, new HashMap<>())));
tryCommitStorage(createStorageResponse);
return createStorageResponse;
}
use of ddf.catalog.content.operation.CreateStorageResponse in project ddf by codice.
the class FileSystemStorageProvider method create.
@Override
public CreateStorageResponse create(CreateStorageRequest createRequest) throws StorageException {
LOGGER.trace("ENTERING: create");
List<ContentItem> contentItems = createRequest.getContentItems();
List<ContentItem> createdContentItems = new ArrayList<>(createRequest.getContentItems().size());
for (ContentItem contentItem : contentItems) {
try {
if (!ContentItemValidator.validate(contentItem)) {
LOGGER.warn("Item is not valid: {}", contentItem);
continue;
}
Path contentIdDir = getTempContentItemDir(createRequest.getId(), new URI(contentItem.getUri()));
Path contentDirectory = Files.createDirectories(contentIdDir);
createdContentItems.add(generateContentFile(contentItem, contentDirectory, (String) createRequest.getPropertyValue(Constants.STORE_REFERENCE_KEY)));
} catch (IOException | URISyntaxException | IllegalArgumentException e) {
throw new StorageException(e);
}
}
CreateStorageResponse response = new CreateStorageResponseImpl(createRequest, createdContentItems);
updateMap.put(createRequest.getId(), createdContentItems.stream().map(ContentItem::getUri).collect(Collectors.toSet()));
LOGGER.trace("EXITING: create");
return response;
}
use of ddf.catalog.content.operation.CreateStorageResponse in project ddf by codice.
the class FileSystemStorageProviderTest method testCreateWithQualifierAndOneInvalidItem.
@Test
public void testCreateWithQualifierAndOneInvalidItem() throws Exception {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return IOUtils.toInputStream("This data is my data, this data is your data.");
}
};
ContentItem contentItem = new ContentItemImpl(uuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
String invalidUuid = "wow-this-isn't-a-valid-uuid-right?!@#%";
ContentItem badContentItem = new ContentItemImpl(invalidUuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
CreateStorageRequest createRequest = new CreateStorageRequestImpl(Lists.newArrayList(contentItem, badContentItem), null);
CreateStorageResponse createResponse = provider.create(createRequest);
assertThat(createResponse.getCreatedContentItems().size(), is(1));
assertThat(createResponse.getCreatedContentItems().get(0).getId(), is(uuid));
ContentItem updateContentItem = new ContentItemImpl(invalidUuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
UpdateStorageRequest updateRequest = new UpdateStorageRequestImpl(Lists.newArrayList(updateContentItem), null);
UpdateStorageResponse updateResponse = provider.update(updateRequest);
assertThat(updateResponse.getUpdatedContentItems().size(), is(0));
}
Aggregations