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);
}
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());
}
}
}
}
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);
}
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;
}
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;
}
Aggregations