Search in sources :

Example 1 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 && contentIdDir.toFile().exists()) {
        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 2 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 (contentIdDir != null && contentIdDir.toFile().exists()) {
                List<Path> paths = new ArrayList<>();
                if (contentIdDir.toFile().isDirectory()) {
                    paths = listPaths(contentIdDir);
                } else {
                    paths.add(contentIdDir);
                }
                for (Path path : paths) {
                    if (path.toFile().exists()) {
                        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 3 with StorageException

use of ddf.catalog.content.StorageException in project ddf by codice.

the class FileSystemStorageProvider method rollback.

@Override
public void rollback(StorageRequest request) throws StorageException {
    String id = request.getId();
    Path requestIdDir = Paths.get(baseContentTmpDirectory.toAbsolutePath().toString(), id);
    deletionMap.remove(id);
    updateMap.remove(id);
    try {
        FileUtils.deleteDirectory(requestIdDir.toFile());
    } catch (IOException e) {
        throw new StorageException("Unable to remove temporary content storage for request: " + id, e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) StorageException(ddf.catalog.content.StorageException)

Example 4 with StorageException

use of ddf.catalog.content.StorageException in project ddf by codice.

the class FileSystemStorageProvider method decryptStream.

private ByteSource decryptStream(InputStream contentInputStream) throws StorageException {
    ByteSource output = null;
    try (InputStream decryptedInputStream = crypter.decrypt(contentInputStream);
        FileBackedOutputStream decryptedOutputStream = new FileBackedOutputStream(128)) {
        // do not use try with resources in order to have these InputStreams in the finally block
        IOUtils.copy(decryptedInputStream, decryptedOutputStream);
        output = decryptedOutputStream.asByteSource();
    } catch (CrypterException | IOException e) {
        LOGGER.debug("Error decrypting InputStream {}. Failing StorageProvider read.", contentInputStream, e);
        throw new StorageException(String.format("Cannot decrypt InputStream %s.", contentInputStream), e);
    }
    return output;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteSource(com.google.common.io.ByteSource) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) IOException(java.io.IOException) CrypterException(ddf.security.encryption.crypter.Crypter.CrypterException) StorageException(ddf.catalog.content.StorageException)

Example 5 with StorageException

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

Aggregations

StorageException (ddf.catalog.content.StorageException)18 IOException (java.io.IOException)11 ContentItem (ddf.catalog.content.data.ContentItem)9 Path (java.nio.file.Path)9 ArrayList (java.util.ArrayList)8 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 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ByteSource (com.google.common.io.ByteSource)3 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