use of ddf.catalog.content.StorageException in project ddf by codice.
the class FileSystemStorageProvider method update.
@Override
public UpdateStorageResponse update(UpdateStorageRequest updateRequest) throws StorageException {
LOGGER.trace("ENTERING: update");
List<ContentItem> contentItems = updateRequest.getContentItems();
List<ContentItem> updatedItems = new ArrayList<>(updateRequest.getContentItems().size());
for (ContentItem contentItem : contentItems) {
try {
if (!ContentItemValidator.validate(contentItem)) {
LOGGER.warn("Item is not valid: {}", contentItem);
continue;
}
ContentItem updateItem = contentItem;
if (StringUtils.isBlank(contentItem.getFilename()) || StringUtils.equals(contentItem.getFilename(), ContentItem.DEFAULT_FILE_NAME)) {
ContentItem existingItem = readContent(new URI(contentItem.getUri()));
updateItem = new ContentItemDecorator(contentItem, existingItem);
}
Path contentIdDir = getTempContentItemDir(updateRequest.getId(), new URI(updateItem.getUri()));
updatedItems.add(generateContentFile(updateItem, contentIdDir, (String) updateRequest.getPropertyValue(Constants.STORE_REFERENCE_KEY)));
} catch (IOException | URISyntaxException | IllegalArgumentException e) {
throw new StorageException(e);
}
}
for (ContentItem contentItem : updatedItems) {
if (contentItem.getMetacard().getResourceURI() == null && StringUtils.isBlank(contentItem.getQualifier())) {
contentItem.getMetacard().setAttribute(new AttributeImpl(Metacard.RESOURCE_URI, contentItem.getUri()));
try {
contentItem.getMetacard().setAttribute(new AttributeImpl(Metacard.RESOURCE_SIZE, contentItem.getSize()));
} catch (IOException e) {
LOGGER.info("Could not set size of content item [{}] on metacard [{}]", contentItem.getId(), contentItem.getMetacard().getId(), e);
}
}
}
UpdateStorageResponse response = new UpdateStorageResponseImpl(updateRequest, updatedItems);
updateMap.put(updateRequest.getId(), updatedItems.stream().map(ContentItem::getUri).collect(Collectors.toSet()));
LOGGER.trace("EXITING: update");
return response;
}
use of ddf.catalog.content.StorageException in project ddf by codice.
the class FileSystemStorageProvider method getContentFilePath.
private Path getContentFilePath(URI uri) throws StorageException {
Path contentIdDir = getContentItemDir(uri);
List<Path> contentFiles;
if (contentIdDir != null && Files.exists(contentIdDir)) {
try {
contentFiles = listPaths(contentIdDir);
} catch (IOException e) {
throw new StorageException(e);
}
contentFiles.removeIf(Files::isDirectory);
if (contentFiles.size() != 1) {
throw new StorageException("Content ID: " + uri.getSchemeSpecificPart() + " storage folder is corrupted.");
}
//there should only be one file
return contentFiles.get(0);
}
return null;
}
use of ddf.catalog.content.StorageException in project ddf by codice.
the class FileSystemStorageProvider method delete.
@Override
public DeleteStorageResponse delete(DeleteStorageRequest deleteRequest) throws StorageException {
LOGGER.trace("ENTERING: delete");
List<Metacard> itemsToBeDeleted = new ArrayList<>();
List<ContentItem> deletedContentItems = new ArrayList<>(deleteRequest.getMetacards().size());
for (Metacard metacard : deleteRequest.getMetacards()) {
LOGGER.debug("File to be deleted: {}", metacard.getId());
ContentItem deletedContentItem = new ContentItemImpl(metacard.getId(), "", null, "", "", 0, metacard);
if (!ContentItemValidator.validate(deletedContentItem)) {
LOGGER.warn("Cannot delete invalid content item ({})", deletedContentItem);
continue;
}
try {
// For deletion we can ignore the qualifier and assume everything under a given ID is
// to be removed.
Path contentIdDir = getContentItemDir(new URI(deletedContentItem.getUri()));
if (Files.exists(contentIdDir)) {
List<Path> paths = new ArrayList<>();
if (Files.isDirectory(contentIdDir)) {
paths = listPaths(contentIdDir);
} else {
paths.add(contentIdDir);
}
for (Path path : paths) {
if (Files.exists(path)) {
deletedContentItems.add(deletedContentItem);
}
}
itemsToBeDeleted.add(metacard);
}
} catch (IOException | URISyntaxException e) {
throw new StorageException("Could not delete file: " + metacard.getId(), e);
}
}
deletionMap.put(deleteRequest.getId(), itemsToBeDeleted);
DeleteStorageResponse response = new DeleteStorageResponseImpl(deleteRequest, deletedContentItems);
LOGGER.trace("EXITING: delete");
return response;
}
use of ddf.catalog.content.StorageException 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;
}
use of ddf.catalog.content.StorageException in project ddf by codice.
the class CreateOperations method create.
public CreateResponse create(CreateStorageRequest streamCreateRequest, List<String> fanoutTagBlacklist) throws IngestException, SourceUnavailableException {
Map<String, Metacard> metacardMap = new HashMap<>();
List<ContentItem> contentItems = new ArrayList<>(streamCreateRequest.getContentItems().size());
HashMap<String, Map<String, Path>> tmpContentPaths = new HashMap<>();
CreateResponse createResponse = null;
CreateStorageRequest createStorageRequest = null;
CreateStorageResponse createStorageResponse;
streamCreateRequest = opsStorageSupport.prepareStorageRequest(streamCreateRequest, streamCreateRequest::getContentItems);
// Operation populates the metacardMap, contentItems, and tmpContentPaths
opsMetacardSupport.generateMetacardAndContentItems(streamCreateRequest.getContentItems(), metacardMap, contentItems, tmpContentPaths);
if (blockCreateMetacards(metacardMap.values(), fanoutTagBlacklist)) {
String message = "Fanout proxy does not support create operations with blacklisted metacard tag";
LOGGER.debug("{}. Tags blacklist: {}", message, fanoutTagBlacklist);
throw new IngestException(message);
}
streamCreateRequest.getProperties().put(CONTENT_PATHS, tmpContentPaths);
injectAttributes(metacardMap);
setDefaultValues(metacardMap);
streamCreateRequest = applyAttributeOverrides(streamCreateRequest, metacardMap);
try {
if (!contentItems.isEmpty()) {
createStorageRequest = new CreateStorageRequestImpl(contentItems, streamCreateRequest.getId(), streamCreateRequest.getProperties());
createStorageRequest = processPreCreateStoragePlugins(createStorageRequest);
try {
createStorageResponse = sourceOperations.getStorage().create(createStorageRequest);
createStorageResponse.getProperties().put(CONTENT_PATHS, tmpContentPaths);
} catch (StorageException e) {
throw new IngestException("Could not store content items.", e);
}
createStorageResponse = processPostCreateStoragePlugins(createStorageResponse);
populateMetacardMap(metacardMap, createStorageResponse);
}
CreateRequest createRequest = new CreateRequestImpl(new ArrayList<>(metacardMap.values()), Optional.ofNullable(createStorageRequest).map(StorageRequest::getProperties).orElseGet(HashMap::new));
createResponse = doCreate(createRequest);
} catch (IOException | RuntimeException e) {
if (createStorageRequest != null) {
try {
sourceOperations.getStorage().rollback(createStorageRequest);
} catch (StorageException e1) {
LOGGER.info("Unable to remove temporary content for id: {}", createStorageRequest.getId(), e1);
}
}
throw new IngestException("Unable to store products for request: " + streamCreateRequest.getId(), e);
} finally {
opsStorageSupport.commitAndCleanup(createStorageRequest, tmpContentPaths);
}
createResponse = doPostIngest(createResponse);
return createResponse;
}
Aggregations