Search in sources :

Example 1 with ContentItemImpl

use of ddf.catalog.content.data.impl.ContentItemImpl in project ddf by codice.

the class InMemoryProcessingFramework method storeProcessRequest.

private <T extends ProcessResourceItem> void storeProcessRequest(ProcessRequest<T> processRequest) {
    LOGGER.trace("Storing update request post processing change(s)");
    Map<String, ContentItem> contentItemsToUpdate = new HashMap<>();
    Map<String, Metacard> metacardsToUpdate = new HashMap<>();
    List<TemporaryFileBackedOutputStream> tfbosToCleanUp = new ArrayList<>();
    for (T item : processRequest.getProcessItems()) {
        if (item.getProcessResource() == null && item.isMetacardModified()) {
            metacardsToUpdate.put(item.getMetacard().getId(), item.getMetacard());
        }
        final ProcessResource processResource = item.getProcessResource();
        TemporaryFileBackedOutputStream tfbos = null;
        if (processResource != null && processResource.isModified() && !contentItemsToUpdate.containsKey(getContentItemKey(item.getMetacard(), processResource))) {
            try {
                tfbos = new TemporaryFileBackedOutputStream();
                long numberOfBytes = IOUtils.copyLarge(processResource.getInputStream(), tfbos);
                LOGGER.debug("Copied {} bytes to TemporaryFileBackedOutputStream.", numberOfBytes);
                ByteSource byteSource = tfbos.asByteSource();
                ContentItem contentItem = new ContentItemImpl(item.getMetacard().getId(), processResource.getQualifier(), byteSource, processResource.getMimeType(), processResource.getName(), processResource.getSize(), item.getMetacard());
                contentItemsToUpdate.put(getContentItemKey(item.getMetacard(), processResource), contentItem);
                tfbosToCleanUp.add(tfbos);
            } catch (IOException | RuntimeException e) {
                LOGGER.debug("Unable to store process request", e);
                if (tfbos != null) {
                    close(tfbos);
                }
            }
        }
    }
    storeContentItemUpdates(contentItemsToUpdate, processRequest.getProperties());
    storeMetacardUpdates(metacardsToUpdate, processRequest.getProperties());
    closeTfbos(tfbosToCleanUp);
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessResource(org.codice.ddf.catalog.async.data.api.internal.ProcessResource) IOException(java.io.IOException) Metacard(ddf.catalog.data.Metacard) ByteSource(com.google.common.io.ByteSource) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 2 with ContentItemImpl

use of ddf.catalog.content.data.impl.ContentItemImpl in project ddf by codice.

the class ContentProducerDataAccessObject method createContentItem.

public void createContentItem(FileSystemPersistenceProvider fileIdMap, ContentEndpoint endpoint, File ingestedFile, WatchEvent.Kind<Path> eventType, String mimeType, Map<String, Object> headers) throws SourceUnavailableException, IngestException {
    LOGGER.debug("Creating content item.");
    String key = String.valueOf(ingestedFile.getAbsolutePath().hashCode());
    String id = fileIdMap.loadAllKeys().contains(key) ? String.valueOf(fileIdMap.loadFromPersistence(key)) : null;
    if (StandardWatchEventKinds.ENTRY_CREATE.equals(eventType)) {
        CreateStorageRequest createRequest = new CreateStorageRequestImpl(Collections.singletonList(new ContentItemImpl(uuidGenerator.generateUuid(), Files.asByteSource(ingestedFile), mimeType, ingestedFile.getName(), 0L, null)), null);
        processHeaders(headers, createRequest, ingestedFile);
        CreateResponse createResponse = endpoint.getComponent().getCatalogFramework().create(createRequest);
        if (createResponse != null) {
            List<Metacard> createdMetacards = createResponse.getCreatedMetacards();
            for (Metacard metacard : createdMetacards) {
                fileIdMap.store(key, metacard.getId());
                LOGGER.debug("content item created with id = {}", metacard.getId());
            }
        }
    } else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(eventType)) {
        UpdateStorageRequest updateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new ContentItemImpl(id, Files.asByteSource(ingestedFile), mimeType, ingestedFile.getName(), 0, null)), null);
        processHeaders(headers, updateRequest, ingestedFile);
        UpdateResponse updateResponse = endpoint.getComponent().getCatalogFramework().update(updateRequest);
        if (updateResponse != null) {
            List<Update> updatedMetacards = updateResponse.getUpdatedMetacards();
            for (Update update : updatedMetacards) {
                LOGGER.debug("content item updated with id = {}", update.getNewMetacard().getId());
            }
        }
    } else if (StandardWatchEventKinds.ENTRY_DELETE.equals(eventType)) {
        DeleteRequest deleteRequest = new DeleteRequestImpl(id);
        DeleteResponse deleteResponse = endpoint.getComponent().getCatalogFramework().delete(deleteRequest);
        if (deleteResponse != null) {
            List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards();
            for (Metacard delete : deletedMetacards) {
                fileIdMap.delete(ingestedFile.getAbsolutePath());
                LOGGER.debug("content item deleted with id = {}", delete.getId());
            }
        }
    }
}
Also used : UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) CreateResponse(ddf.catalog.operation.CreateResponse) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) Update(ddf.catalog.operation.Update) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) List(java.util.List) DeleteRequest(ddf.catalog.operation.DeleteRequest) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 3 with ContentItemImpl

use of ddf.catalog.content.data.impl.ContentItemImpl in project ddf by codice.

the class FileSystemStorageProvider method readContent.

private ContentItem readContent(URI uri) throws StorageException {
    Path file = getContentFilePath(uri);
    if (file == null) {
        throw new StorageException("Unable to find file for content ID: " + uri.getSchemeSpecificPart());
    }
    String extension = FilenameUtils.getExtension(file.getFileName().toString());
    if (REF_EXT.equals(extension)) {
        extension = FilenameUtils.getExtension(FilenameUtils.removeExtension(file.getFileName().toString()));
        try {
            file = Paths.get(new String(Files.readAllBytes(file), Charset.forName("UTF-8")));
        } catch (IOException e) {
            throw new StorageException(e);
        }
    }
    if (!file.toFile().exists()) {
        throw new StorageException("Cannot read " + uri + ".");
    }
    String mimeType;
    try (InputStream fileInputStream = Files.newInputStream(file)) {
        mimeType = mimeTypeMapper.guessMimeType(fileInputStream, extension);
    } catch (Exception e) {
        LOGGER.info("Could not determine mime type for file extension = {}; defaulting to {}", extension, DEFAULT_MIME_TYPE);
        mimeType = DEFAULT_MIME_TYPE;
    }
    if (mimeType == null || DEFAULT_MIME_TYPE.equals(mimeType)) {
        try {
            mimeType = Files.probeContentType(file);
        } catch (IOException e) {
            LOGGER.info("Unable to determine mime type using Java Files service.", e);
            mimeType = DEFAULT_MIME_TYPE;
        }
    }
    LOGGER.debug("mimeType = {}", mimeType);
    long size = 0;
    try {
        size = Files.size(file);
    } catch (IOException e) {
        LOGGER.info("Unable to retrieve size of file: {}", file.toAbsolutePath().toString(), e);
    }
    return new ContentItemImpl(uri.getSchemeSpecificPart(), uri.getFragment(), com.google.common.io.Files.asByteSource(file.toFile()), mimeType, file.getFileName().toString(), size, null);
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) IOException(java.io.IOException) StorageException(ddf.catalog.content.StorageException) URISyntaxException(java.net.URISyntaxException) InvalidPathException(java.nio.file.InvalidPathException) IOException(java.io.IOException) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) StorageException(ddf.catalog.content.StorageException) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 4 with ContentItemImpl

use of ddf.catalog.content.data.impl.ContentItemImpl in project ddf by codice.

the class FileSystemStorageProvider method generateContentFile.

private ContentItem generateContentFile(ContentItem item, Path contentDirectory, String storeReference) throws IOException, StorageException {
    LOGGER.trace("ENTERING: generateContentFile");
    if (!Files.exists(contentDirectory)) {
        Files.createDirectories(contentDirectory);
    }
    Path contentItemPath = Paths.get(contentDirectory.toAbsolutePath().toString(), item.getFilename());
    long copy;
    if (storeReference != null) {
        copy = item.getSize();
        Files.write(Paths.get(contentItemPath.toString() + "." + REF_EXT), storeReference.getBytes(Charset.forName("UTF-8")));
        contentItemPath = Paths.get(storeReference);
    } else {
        try (InputStream inputStream = item.getInputStream()) {
            copy = Files.copy(inputStream, contentItemPath);
        }
        if (copy != item.getSize()) {
            LOGGER.warn("Created content item {} size {} does not match expected size {}" + System.lineSeparator() + "Verify filesystem and/or network integrity.", item.getId(), copy, item.getSize());
        }
    }
    ContentItemImpl contentItem = new ContentItemImpl(item.getId(), item.getQualifier(), com.google.common.io.Files.asByteSource(contentItemPath.toFile()), item.getMimeType().toString(), contentItemPath.getFileName().toString(), copy, item.getMetacard());
    LOGGER.trace("EXITING: generateContentFile");
    return contentItem;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 5 with ContentItemImpl

use of ddf.catalog.content.data.impl.ContentItemImpl 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)

Aggregations

ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)29 ContentItem (ddf.catalog.content.data.ContentItem)25 Metacard (ddf.catalog.data.Metacard)24 Test (org.junit.Test)17 ByteSource (com.google.common.io.ByteSource)14 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)14 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)10 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)8 CreateStorageResponse (ddf.catalog.content.operation.CreateStorageResponse)7 CreateResponse (ddf.catalog.operation.CreateResponse)7 UpdateStorageRequestImpl (ddf.catalog.content.operation.impl.UpdateStorageRequestImpl)6 Serializable (java.io.Serializable)6 URI (java.net.URI)6 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)5 UpdateStorageRequest (ddf.catalog.content.operation.UpdateStorageRequest)5 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)5