Search in sources :

Example 1 with ReadStorageRequest

use of ddf.catalog.content.operation.ReadStorageRequest 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 2 with ReadStorageRequest

use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.

the class ContentResourceReader method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> arguments) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    LOGGER.trace("ENTERING: retrieveResource");
    ResourceResponse response = null;
    if (resourceUri == null) {
        throw new ResourceNotFoundException("Unable to find resource - resource URI was null");
    }
    if (resourceUri.getScheme().equals(ContentItem.CONTENT_SCHEME)) {
        LOGGER.debug("Resource URI is content scheme");
        String contentId = resourceUri.getSchemeSpecificPart();
        if (contentId != null && !contentId.isEmpty()) {
            if (arguments != null && arguments.get(ContentItem.QUALIFIER_KEYWORD) instanceof String && StringUtils.isNotBlank((String) arguments.get(ContentItem.QUALIFIER_KEYWORD))) {
                try {
                    resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), (String) arguments.get(ContentItem.QUALIFIER_KEYWORD));
                } catch (URISyntaxException e) {
                    throw new ResourceNotFoundException("Unable to create with qualifier", e);
                }
            }
            ReadStorageRequest readRequest = new ReadStorageRequestImpl(resourceUri, arguments);
            try {
                ReadStorageResponse readResponse = storage.read(readRequest);
                ContentItem contentItem = readResponse.getContentItem();
                String fileName = contentItem.getFilename();
                LOGGER.debug("resource name: {}", fileName);
                InputStream is = contentItem.getInputStream();
                response = new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), contentItem.getMimeType(), fileName));
            } catch (StorageException e) {
                throw new ResourceNotFoundException(e);
            }
        }
    }
    LOGGER.trace("EXITING: retrieveResource");
    return response;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) URI(java.net.URI) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) BufferedInputStream(java.io.BufferedInputStream) ReadStorageRequestImpl(ddf.catalog.content.operation.impl.ReadStorageRequestImpl) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem)

Example 3 with ReadStorageRequest

use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.

the class Historian method version.

/**
 * Versions updated {@link Metacard}s and {@link ContentItem}s.
 *
 * @param streamUpdateRequest Needed to pass {@link
 *     ddf.catalog.core.versioning.MetacardVersion#SKIP_VERSIONING} flag into downstream update
 * @param updateStorageResponse Versions this response's updated items
 * @return the update response originally passed in
 * @throws UnsupportedQueryException
 * @throws SourceUnavailableException
 * @throws IngestException
 */
public UpdateStorageResponse version(UpdateStorageRequest streamUpdateRequest, UpdateStorageResponse updateStorageResponse, UpdateResponse updateResponse) throws UnsupportedQueryException, SourceUnavailableException, IngestException {
    if (doSkip(updateStorageResponse)) {
        return updateStorageResponse;
    }
    setSkipFlag(streamUpdateRequest);
    setSkipFlag(updateStorageResponse);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Versioning updated metacards and content: {}", updateStorageResponse);
    }
    List<Metacard> updatedMetacards = updateStorageResponse.getUpdatedContentItems().stream().filter(ci -> StringUtils.isBlank(ci.getQualifier())).map(ContentItem::getMetacard).filter(Objects::nonNull).filter(isNotVersionNorDeleted).collect(Collectors.toList());
    if (updatedMetacards.isEmpty()) {
        LOGGER.trace("No updated metacards applicable to versioning");
        securityLogger.audit("Skipping versioning updated metacards with ids: {}", updateStorageResponse.getUpdatedContentItems().stream().map(ContentItem::getMetacard).filter(Objects::nonNull).map(Metacard::getId).collect(TO_A_STRING));
        return updateStorageResponse;
    }
    Map<String, Metacard> originalMetacards = updateResponse.getUpdatedMetacards().stream().map(Update::getOldMetacard).collect(Collectors.toMap(Metacard::getId, Function.identity(), Historian::firstInWinsMerge));
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Found current data for the following metacards: {}", getList(originalMetacards));
    }
    Collection<ReadStorageRequest> ids = getReadStorageRequests(updatedMetacards);
    Map<String, List<ContentItem>> content = getContent(ids);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Found resources for the following metacards: {}", getList(content));
    }
    Function<String, Action> getAction = id -> content.containsKey(id) ? Action.VERSIONED_CONTENT : Action.VERSIONED;
    Map<String, Metacard> versionMetacards = getVersionMetacards(originalMetacards.values(), getAction, (Subject) updateResponse.getProperties().get(SecurityConstants.SECURITY_SUBJECT));
    CreateStorageResponse createStorageResponse = versionContentItems(content, versionMetacards);
    if (createStorageResponse == null) {
        String message = "Could not version content items for: " + getList(originalMetacards);
        securityLogger.audit(message);
        LOGGER.debug(message);
        return updateStorageResponse;
    }
    setResourceUriForContent(/*mutable*/
    versionMetacards, createStorageResponse);
    CreateResponse createResponse = storeVersionMetacards(versionMetacards);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Successfully created metacard versions under ids: {}", createResponse.getCreatedMetacards().stream().map(Metacard::getId).collect(TO_A_STRING));
    }
    return updateStorageResponse;
}
Also used : UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) LoggerFactory(org.slf4j.LoggerFactory) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) StringUtils(org.apache.commons.lang3.StringUtils) SubjectOperations(ddf.security.SubjectOperations) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) DeletedMetacardImpl(ddf.catalog.core.versioning.impl.DeletedMetacardImpl) LogSanitizer(org.codice.ddf.log.sanitizer.LogSanitizer) Map(java.util.Map) Action(ddf.catalog.core.versioning.MetacardVersion.Action) Collector(java.util.stream.Collector) UpdateStorageResponse(ddf.catalog.content.operation.UpdateStorageResponse) Bundle(org.osgi.framework.Bundle) MetacardVersionImpl(ddf.catalog.core.versioning.impl.MetacardVersionImpl) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Predicate(java.util.function.Predicate) Collection(java.util.Collection) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) MetacardType(ddf.catalog.data.MetacardType) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) StorageException(ddf.catalog.content.StorageException) Operation(ddf.catalog.operation.Operation) List(java.util.List) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Optional(java.util.Optional) UpdateResponse(ddf.catalog.operation.UpdateResponse) AccessController(java.security.AccessController) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) Security(org.codice.ddf.security.Security) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) DeleteResponse(ddf.catalog.operation.DeleteResponse) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Update(ddf.catalog.operation.Update) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) SKIP_VERSIONING(ddf.catalog.core.versioning.MetacardVersion.SKIP_VERSIONING) ContentItem(ddf.catalog.content.data.ContentItem) CreateResponse(ddf.catalog.operation.CreateResponse) Metacard(ddf.catalog.data.Metacard) SecurityConstants(ddf.security.SecurityConstants) StorageProvider(ddf.catalog.content.StorageProvider) ByteSource(com.google.common.io.ByteSource) Result(ddf.catalog.data.Result) Hashtable(java.util.Hashtable) Nullable(javax.annotation.Nullable) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SubjectIdentity(ddf.security.SubjectIdentity) Logger(org.slf4j.Logger) SecurityLogger(ddf.security.audit.SecurityLogger) IngestException(ddf.catalog.source.IngestException) Subject(ddf.security.Subject) IOException(java.io.IOException) ReadStorageRequestImpl(ddf.catalog.content.operation.impl.ReadStorageRequestImpl) TimeUnit(java.util.concurrent.TimeUnit) SourceResponse(ddf.catalog.operation.SourceResponse) CatalogProvider(ddf.catalog.source.CatalogProvider) Filter(org.opengis.filter.Filter) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) InputStream(java.io.InputStream) UuidGenerator(org.codice.ddf.platform.util.uuidgenerator.UuidGenerator) Action(ddf.catalog.core.versioning.MetacardVersion.Action) PrivilegedAction(java.security.PrivilegedAction) CreateResponse(ddf.catalog.operation.CreateResponse) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) Objects(java.util.Objects) List(java.util.List) ArrayList(java.util.ArrayList) ContentItem(ddf.catalog.content.data.ContentItem)

Example 4 with ReadStorageRequest

use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.

the class FileSystemStorageProviderTest method assertReadRequest.

private void assertReadRequest(String uriString, String mimeType) 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();
    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 5 with ReadStorageRequest

use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.

the class FileSystemStorageProviderTest method testRollback.

@Test(expected = StorageException.class)
public void testRollback() throws Exception {
    String id = UUID.randomUUID().toString().replaceAll("-", "");
    ByteSource byteSource = new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return IOUtils.toInputStream(TEST_INPUT_CONTENTS, StandardCharsets.UTF_8);
        }
    };
    Metacard metacard = mock(Metacard.class);
    when(metacard.getId()).thenReturn(id);
    ContentItem contentItem = new ContentItemImpl(id, byteSource, NITF_MIME_TYPE, TEST_INPUT_FILENAME, TEST_INPUT_CONTENTS.getBytes().length, metacard);
    CreateStorageRequest createRequest = new CreateStorageRequestImpl(Collections.singletonList(contentItem), null);
    CreateStorageResponse createStorageResponse = provider.create(createRequest);
    provider.rollback(createRequest);
    ReadStorageRequest readStorageRequest = new ReadStorageRequestImpl(new URI("content:" + id), null);
    ReadStorageResponse read = provider.read(readStorageRequest);
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) ByteSource(com.google.common.io.ByteSource) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ReadStorageRequestImpl(ddf.catalog.content.operation.impl.ReadStorageRequestImpl) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) URI(java.net.URI) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) Test(org.junit.Test)

Aggregations

ReadStorageRequest (ddf.catalog.content.operation.ReadStorageRequest)7 ContentItem (ddf.catalog.content.data.ContentItem)6 URI (java.net.URI)6 ReadStorageResponse (ddf.catalog.content.operation.ReadStorageResponse)5 ReadStorageRequestImpl (ddf.catalog.content.operation.impl.ReadStorageRequestImpl)5 Metacard (ddf.catalog.data.Metacard)3 ByteSource (com.google.common.io.ByteSource)2 StorageException (ddf.catalog.content.StorageException)2 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)2 CreateStorageResponse (ddf.catalog.content.operation.CreateStorageResponse)2 UpdateStorageRequest (ddf.catalog.content.operation.UpdateStorageRequest)2 UpdateStorageResponse (ddf.catalog.content.operation.UpdateStorageResponse)2 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)2 UpdateResponse (ddf.catalog.operation.UpdateResponse)2 InputStream (java.io.InputStream)2 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)2 Test (org.junit.Test)2 Lists (com.google.common.collect.Lists)1 StorageProvider (ddf.catalog.content.StorageProvider)1 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)1