Search in sources :

Example 11 with StorageException

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

the class ExportCommand method doDelete.

private void doDelete(List<ExportItem> exportedItems, List<ExportItem> exportedContentItems) {
    Instant start;
    console.println("Starting delete");
    start = Instant.now();
    for (ExportItem exportedContentItem : exportedContentItems) {
        try {
            DeleteStorageRequestImpl deleteRequest = new DeleteStorageRequestImpl(Collections.singletonList(new IdAndUriMetacard(exportedContentItem.getId(), exportedContentItem.getResourceUri())), exportedContentItem.getId(), Collections.emptyMap());
            storageProvider.delete(deleteRequest);
            storageProvider.commit(deleteRequest);
        } catch (StorageException e) {
            printErrorMessage("Could not delete content for metacard: " + exportedContentItem.toString());
        }
    }
    for (ExportItem exported : exportedItems) {
        try {
            catalogProvider.delete(new DeleteRequestImpl(exported.getId()));
        } catch (IngestException e) {
            printErrorMessage("Could not delete metacard: " + exported.toString());
        }
    }
    // delete items from cache
    try {
        getCacheProxy().removeById(exportedItems.stream().map(ExportItem::getId).collect(Collectors.toList()).toArray(new String[exportedItems.size()]));
    } catch (Exception e) {
        LOGGER.warn("Could not delete all exported items from cache (Results will eventually expire)", e);
    }
    console.println("Metacards and Content deleted in: " + getFormattedDuration(start));
    console.println("Number of metacards deleted: " + exportedItems.size());
    console.println("Number of content deleted: " + exportedContentItems.size());
}
Also used : DeleteStorageRequestImpl(ddf.catalog.content.operation.impl.DeleteStorageRequestImpl) ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) Instant(java.time.Instant) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) IdAndUriMetacard(org.codice.ddf.commands.catalog.export.IdAndUriMetacard) IngestException(ddf.catalog.source.IngestException) StorageException(ddf.catalog.content.StorageException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ZipException(net.lingala.zip4j.exception.ZipException) StorageException(ddf.catalog.content.StorageException) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) CQLException(org.geotools.filter.text.cql2.CQLException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException)

Example 12 with StorageException

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

the class IngestCommand method submitToStorageProvider.

private void submitToStorageProvider(List<Metacard> metacardList) {
    metacardList.stream().filter(metacard -> metacardFileMapping.containsKey(metacard.getId())).map(metacard -> {
        List<File> fileList = metacardFileMapping.get(metacard.getId());
        List<ContentItem> contentItemList = new ArrayList<>();
        ContentItem contentItem;
        for (File file : fileList) {
            ByteSource byteSource = com.google.common.io.Files.asByteSource(file);
            String fileName = file.getName().split("-")[1];
            String fragment = null;
            if (!file.getPath().contains(CONTENT + File.separator + metacard.getId())) {
                fragment = StringUtils.substringBetween(file.getPath(), CONTENT + File.separator, File.separator + metacard.getId());
            }
            contentItem = new ContentItemImpl(metacard.getId(), fragment, byteSource, metacard.getContentTypeName(), fileName, file.length(), metacard);
            contentItemList.add(contentItem);
        }
        return new CreateStorageRequestImpl(contentItemList, metacard.getId(), new HashMap<>());
    }).forEach(createStorageRequest -> {
        try {
            storageProvider.create(createStorageRequest);
            storageProvider.commit(createStorageRequest);
        } catch (StorageException e) {
            LOGGER.debug("Unable to create content for {}", createStorageRequest.getId(), e);
            try {
                storageProvider.rollback(createStorageRequest);
            } catch (StorageException e1) {
                LOGGER.debug("Unable to perform rollback on temporary content for {} ", createStorageRequest.getId(), e1);
            }
        }
    });
}
Also used : Ansi(org.fusesource.jansi.Ansi) StringUtils(org.apache.commons.lang.StringUtils) CreateRequest(ddf.catalog.operation.CreateRequest) ObjectInputStream(java.io.ObjectInputStream) LoggerFactory(org.slf4j.LoggerFactory) SecurityLogger(ddf.security.common.audit.SecurityLogger) Command(org.apache.karaf.shell.api.action.Command) DirectoryStream(java.nio.file.DirectoryStream) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Path(java.nio.file.Path) InputTransformer(ddf.catalog.transform.InputTransformer) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Serializable(java.io.Serializable) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) StorageException(ddf.catalog.content.StorageException) List(java.util.List) Stream(java.util.stream.Stream) PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) Optional(java.util.Optional) FilenameUtils(org.apache.commons.io.FilenameUtils) IntStream(java.util.stream.IntStream) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) HashMap(java.util.HashMap) PeriodFormatter(org.joda.time.format.PeriodFormatter) ArrayList(java.util.ArrayList) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) ContentItem(ddf.catalog.content.data.ContentItem) CreateResponse(ddf.catalog.operation.CreateResponse) Constants(ddf.catalog.Constants) Metacard(ddf.catalog.data.Metacard) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StorageProvider(ddf.catalog.content.StorageProvider) Exceptions(org.codice.ddf.platform.util.Exceptions) ByteSource(com.google.common.io.ByteSource) ExecutorService(java.util.concurrent.ExecutorService) Period(org.joda.time.Period) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) Logger(org.slf4j.Logger) Files(java.nio.file.Files) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) Argument(org.apache.karaf.shell.api.action.Argument) FileInputStream(java.io.FileInputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) FileVisitOption(java.nio.file.FileVisitOption) Paths(java.nio.file.Paths) Phaser(java.util.concurrent.Phaser) Service(org.apache.karaf.shell.api.action.lifecycle.Service) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) Option(org.apache.karaf.shell.api.action.Option) InputCollectionTransformer(ddf.catalog.transform.InputCollectionTransformer) InputStream(java.io.InputStream) HashMap(java.util.HashMap) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) ByteSource(com.google.common.io.ByteSource) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 13 with StorageException

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

the class FileSystemStorageProvider method commitUpdates.

private void commitUpdates(StorageRequest request) throws StorageException {
    try {
        for (String contentUri : updateMap.get(request.getId())) {
            Path contentIdDir = getTempContentItemDir(request.getId(), new URI(contentUri));
            Path target = getContentItemDir(new URI(contentUri));
            try {
                if (Files.exists(contentIdDir)) {
                    if (Files.exists(target)) {
                        List<Path> files = listPaths(target);
                        for (Path file : files) {
                            if (!Files.isDirectory(file)) {
                                Files.deleteIfExists(file);
                            }
                        }
                    }
                    Files.createDirectories(target.getParent());
                    Files.move(contentIdDir, target, StandardCopyOption.REPLACE_EXISTING);
                }
            } catch (IOException e) {
                LOGGER.debug("Unable to move files by simple rename, resorting to copy. This will impact performance.", e);
                try {
                    Path createdTarget = Files.createDirectories(target);
                    List<Path> files = listPaths(contentIdDir);
                    Files.copy(files.get(0), Paths.get(createdTarget.toAbsolutePath().toString(), files.get(0).getFileName().toString()));
                } catch (IOException e1) {
                    throw new StorageException("Unable to commit changes for request: " + request.getId(), e1);
                }
            }
        }
    } catch (URISyntaxException e) {
        throw new StorageException(e);
    } finally {
        rollback(request);
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) StorageException(ddf.catalog.content.StorageException)

Example 14 with StorageException

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

the class FileSystemStorageProviderTest method testDeleteWithSimilarIds.

@Test
public void testDeleteWithSimilarIds() throws Exception {
    CreateStorageResponse createResponse = assertContentItem(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME);
    String id = createResponse.getCreatedContentItems().get(0).getId();
    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
    String badId = id.substring(0, 6) + uuid.substring(6, uuid.length() - 1);
    boolean hadError = false;
    try {
        CreateStorageResponse badCreateResponse = assertContentItemWithQualifier(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME, badId, "");
    } catch (AssertionError e) {
        // bad id is not a valid ID
        hadError = true;
    } finally {
        if (!hadError) {
            fail("Create succeeded when it should not have! " + badId + "Should not be valid!");
        }
        hadError = false;
    }
    DeleteStorageRequest deleteRequest = new DeleteStorageRequestImpl(createResponse.getCreatedContentItems().stream().map(ContentItem::getMetacard).collect(Collectors.toList()), null);
    when(deleteRequest.getMetacards().get(0).getId()).thenReturn(id);
    DeleteStorageResponse deleteResponse = provider.delete(deleteRequest);
    List<ContentItem> items = deleteResponse.getDeletedContentItems();
    ContentItem item = items.get(0);
    LOGGER.debug("Item retrieved: {}", item);
    assertEquals(id, item.getId());
    assertThat(item.getFilename(), is(""));
    provider.commit(deleteRequest);
    try {
        assertReadRequest(createResponse.getCreatedContentItems().get(0).getUri(), NITF_MIME_TYPE);
    } catch (StorageException e) {
        // The item was deleted so it shouldn't have found it
        hadError = true;
    } finally {
        if (!hadError) {
            fail("read succeeded when it should not have! ");
        }
    }
}
Also used : DeleteStorageRequest(ddf.catalog.content.operation.DeleteStorageRequest) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) DeleteStorageRequestImpl(ddf.catalog.content.operation.impl.DeleteStorageRequestImpl) DeleteStorageResponse(ddf.catalog.content.operation.DeleteStorageResponse) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem) Test(org.junit.Test)

Example 15 with StorageException

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

the class DeleteOperations method performLocalDelete.

private DeleteResponse performLocalDelete(DeleteRequest deleteRequest, DeleteStorageRequest deleteStorageRequest) throws IngestException {
    if (!Requests.isLocal(deleteRequest)) {
        return null;
    }
    try {
        sourceOperations.getStorage().delete(deleteStorageRequest);
    } catch (StorageException e) {
        LOGGER.info("Unable to delete stored content items. Not removing stored metacards", e);
        throw new InternalIngestException("Unable to delete stored content items. Not removing stored metacards.", e);
    }
    DeleteResponse deleteResponse = sourceOperations.getCatalog().delete(deleteRequest);
    deleteResponse = injectAttributes(deleteResponse);
    try {
        historian.version(deleteResponse);
    } catch (SourceUnavailableException e) {
        LOGGER.debug("Could not version deleted item!", e);
        throw new IngestException("Could not version deleted Item!");
    }
    return deleteResponse;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) DeleteResponse(ddf.catalog.operation.DeleteResponse) InternalIngestException(ddf.catalog.source.InternalIngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) StorageException(ddf.catalog.content.StorageException)

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