Search in sources :

Example 6 with StorageException

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;
}
Also used : Path(java.nio.file.Path) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) UpdateStorageResponseImpl(ddf.catalog.content.operation.impl.UpdateStorageResponseImpl) UpdateStorageResponse(ddf.catalog.content.operation.UpdateStorageResponse) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem)

Example 7 with StorageException

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;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) Files(java.nio.file.Files) StorageException(ddf.catalog.content.StorageException)

Example 8 with StorageException

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;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DeleteStorageResponseImpl(ddf.catalog.content.operation.impl.DeleteStorageResponseImpl) Metacard(ddf.catalog.data.Metacard) DeleteStorageResponse(ddf.catalog.content.operation.DeleteStorageResponse) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 9 with StorageException

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;
}
Also used : UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) HashMap(java.util.HashMap) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) StorageException(ddf.catalog.content.StorageException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) StopProcessingException(ddf.catalog.plugin.StopProcessingException) FederationException(ddf.catalog.federation.FederationException) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) UpdateStorageResponse(ddf.catalog.content.operation.UpdateStorageResponse) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem)

Example 10 with StorageException

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;
}
Also used : HashMap(java.util.HashMap) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) StorageRequest(ddf.catalog.content.operation.StorageRequest) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) Map(java.util.Map) HashMap(java.util.HashMap) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

Aggregations

StorageException (ddf.catalog.content.StorageException)17 IOException (java.io.IOException)11 ContentItem (ddf.catalog.content.data.ContentItem)9 Path (java.nio.file.Path)9 ArrayList (java.util.ArrayList)8 URISyntaxException (java.net.URISyntaxException)7 Metacard (ddf.catalog.data.Metacard)6 IngestException (ddf.catalog.source.IngestException)6 CreateStorageResponse (ddf.catalog.content.operation.CreateStorageResponse)4 InternalIngestException (ddf.catalog.source.InternalIngestException)4 URI (java.net.URI)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)3 DeleteStorageRequest (ddf.catalog.content.operation.DeleteStorageRequest)3 DeleteStorageResponse (ddf.catalog.content.operation.DeleteStorageResponse)3 UpdateStorageResponse (ddf.catalog.content.operation.UpdateStorageResponse)3 DeleteStorageRequestImpl (ddf.catalog.content.operation.impl.DeleteStorageRequestImpl)3 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)3 List (java.util.List)3