Search in sources :

Example 11 with ContentItem

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

Example 12 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class FileSystemStorageProviderTest method assertReadRequest.

private void assertReadRequest(String uriString, String mimeType, boolean extension) throws StorageException, IOException, URISyntaxException {
    final URI uri = new URI(uriString);
    ReadStorageRequest readRequest = new ReadStorageRequestImpl(uri, Collections.emptyMap());
    ReadStorageResponse readResponse = provider.read(readRequest);
    ContentItem item = readResponse.getContentItem();
    LOGGER.debug("Item retrieved: {}", item);
    assertThat(item, notNullValue());
    assertThat(item.getId(), is(uri.getSchemeSpecificPart()));
    if (uri.getFragment() != null) {
        assertThat(item.getQualifier(), is(uri.getFragment()));
    }
    if (mimeType.equals(NITF_MIME_TYPE)) {
        assertThat(item.getMimeTypeRawData(), is(NITF_MIME_TYPE));
    }
    List<String> parts = provider.getContentFilePathParts(uri.getSchemeSpecificPart(), uri.getFragment());
    String expectedFilePath = baseDir + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_REPOSITORY + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_STORE + File.separator + parts.get(0) + File.separator + parts.get(1) + File.separator + parts.get(2) + (StringUtils.isNotBlank(item.getQualifier()) ? File.separator + item.getQualifier() : "") + File.separator + item.getFilename();
    if (extension) {
        expectedFilePath = expectedFilePath + "." + FileSystemStorageProvider.REF_EXT;
    }
    assertThat(Files.exists(Paths.get(expectedFilePath)), is(true));
    assertTrue(item.getSize() > 0);
}
Also used : ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) ReadStorageRequestImpl(ddf.catalog.content.operation.impl.ReadStorageRequestImpl) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) URI(java.net.URI) ContentItem(ddf.catalog.content.data.ContentItem)

Example 13 with ContentItem

use of ddf.catalog.content.data.ContentItem 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 14 with ContentItem

use of ddf.catalog.content.data.ContentItem 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 15 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class FileSystemStorageProvider method read.

@Override
public ReadStorageResponse read(ReadStorageRequest readRequest) throws StorageException {
    LOGGER.trace("ENTERING: read");
    if (readRequest.getResourceUri() == null) {
        return new ReadStorageResponseImpl(readRequest);
    }
    URI uri = readRequest.getResourceUri();
    ContentItem returnItem = readContent(uri);
    return new ReadStorageResponseImpl(readRequest, returnItem);
}
Also used : ReadStorageResponseImpl(ddf.catalog.content.operation.impl.ReadStorageResponseImpl) URI(java.net.URI) ContentItem(ddf.catalog.content.data.ContentItem)

Aggregations

ContentItem (ddf.catalog.content.data.ContentItem)65 Metacard (ddf.catalog.data.Metacard)37 Test (org.junit.Test)36 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)27 ArrayList (java.util.ArrayList)22 CreateStorageResponse (ddf.catalog.content.operation.CreateStorageResponse)20 HashMap (java.util.HashMap)19 URI (java.net.URI)17 ByteSource (com.google.common.io.ByteSource)16 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)16 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)15 IOException (java.io.IOException)14 UpdateStorageRequest (ddf.catalog.content.operation.UpdateStorageRequest)13 StorageException (ddf.catalog.content.StorageException)12 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)12 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)11 InputStream (java.io.InputStream)11 Map (java.util.Map)11 StorageProvider (ddf.catalog.content.StorageProvider)10 UpdateStorageResponse (ddf.catalog.content.operation.UpdateStorageResponse)10