Search in sources :

Example 1 with ReadStorageResponse

use of ddf.catalog.content.operation.ReadStorageResponse 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) instanceof String && StringUtils.isNotBlank((String) arguments.get(ContentItem.QUALIFIER))) {
                try {
                    resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), (String) arguments.get(ContentItem.QUALIFIER));
                } 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 2 with ReadStorageResponse

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

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

the class FileSystemStorageProviderTest method testReadDeletedReference.

@Test(expected = StorageException.class)
public void testReadDeletedReference() throws Exception {
    Path tempFile = Files.createTempFile("test", "nitf");
    Files.write(tempFile, TEST_INPUT_CONTENTS.getBytes());
    CreateStorageResponse createResponse = assertContentItem(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME, Collections.singletonMap(Constants.STORE_REFERENCE_KEY, tempFile.toFile().getAbsolutePath()));
    URI uri = new URI(createResponse.getCreatedContentItems().get(0).getUri());
    ReadStorageResponse read = provider.read(new ReadStorageRequestImpl(new URI(createResponse.getCreatedContentItems().get(0).getUri()), Collections.emptyMap()));
    assertThat(read.getContentItem(), notNullValue());
    Files.delete(tempFile);
    provider.read(new ReadStorageRequestImpl(new URI(createResponse.getCreatedContentItems().get(0).getUri()), Collections.emptyMap()));
}
Also used : Path(java.nio.file.Path) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) ReadStorageRequestImpl(ddf.catalog.content.operation.impl.ReadStorageRequestImpl) URI(java.net.URI) Test(org.junit.Test)

Example 4 with ReadStorageResponse

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

the class HistorianTest method testTryCommitStorageException.

@Test(expected = IngestException.class)
public void testTryCommitStorageException() throws StorageException, UnsupportedQueryException, SourceUnavailableException, IngestException, URISyntaxException {
    List<Metacard> metacards = getMetacardUpdatePair();
    // Mock out a bad storage provider
    StorageProvider exceptionStorageProvider = mock(StorageProvider.class);
    doThrow(StorageException.class).when(exceptionStorageProvider).commit(any());
    ContentItem item = mock(ContentItem.class);
    when(item.getId()).thenReturn(METACARD_ID);
    when(item.getUri()).thenReturn(RESOURCE_URI);
    when(item.getMetacard()).thenReturn(metacards.get(0));
    ReadStorageResponse readStorageResponse = mock(ReadStorageResponse.class);
    when(readStorageResponse.getContentItem()).thenReturn(item);
    when(exceptionStorageProvider.read(any())).thenReturn(readStorageResponse);
    when(exceptionStorageProvider.create(any())).thenReturn(mock(CreateStorageResponse.class));
    historian.setStorageProviders(Collections.singletonList(exceptionStorageProvider));
    // Parameters for historian
    UpdateStorageRequest storageRequest = mock(UpdateStorageRequest.class);
    UpdateStorageResponse storageResponse = mock(UpdateStorageResponse.class);
    UpdateResponse updateResponse = mock(UpdateResponse.class);
    // send a request to update the metacard
    updateMetacard(storageRequest, storageResponse, metacards.get(1));
    mockQuery(metacards.get(1));
    historian.version(storageRequest, storageResponse, updateResponse);
    verify(exceptionStorageProvider).rollback(any(StorageRequest.class));
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) UpdateResponse(ddf.catalog.operation.UpdateResponse) DeletedMetacard(ddf.catalog.core.versioning.DeletedMetacard) Metacard(ddf.catalog.data.Metacard) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) DeleteStorageRequest(ddf.catalog.content.operation.DeleteStorageRequest) StorageRequest(ddf.catalog.content.operation.StorageRequest) UpdateStorageResponse(ddf.catalog.content.operation.UpdateStorageResponse) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) StorageProvider(ddf.catalog.content.StorageProvider) ContentItem(ddf.catalog.content.data.ContentItem) Test(org.junit.Test)

Example 5 with ReadStorageResponse

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

the class HistorianTest method testRollbackFailed.

@Test(expected = IngestException.class)
public void testRollbackFailed() throws StorageException, UnsupportedQueryException, SourceUnavailableException, IngestException {
    List<Metacard> metacards = getMetacardUpdatePair();
    // Mock out a bad storage provider
    StorageProvider exceptionStorageProvider = mock(StorageProvider.class);
    doThrow(StorageException.class).when(exceptionStorageProvider).commit(any());
    doThrow(StorageException.class).when(exceptionStorageProvider).rollback(any());
    ContentItem item = mock(ContentItem.class);
    when(item.getId()).thenReturn(METACARD_ID);
    when(item.getUri()).thenReturn(RESOURCE_URI);
    when(item.getMetacard()).thenReturn(metacards.get(0));
    ReadStorageResponse readStorageResponse = mock(ReadStorageResponse.class);
    when(readStorageResponse.getContentItem()).thenReturn(item);
    when(exceptionStorageProvider.read(any())).thenReturn(readStorageResponse);
    when(exceptionStorageProvider.create(any())).thenReturn(mock(CreateStorageResponse.class));
    historian.setStorageProviders(Collections.singletonList(exceptionStorageProvider));
    // Parameters for historian
    UpdateStorageRequest storageRequest = mock(UpdateStorageRequest.class);
    UpdateStorageResponse storageResponse = mock(UpdateStorageResponse.class);
    UpdateResponse updateResponse = mock(UpdateResponse.class);
    // send a request to update the metacard
    updateMetacard(storageRequest, storageResponse, metacards.get(1));
    mockQuery(metacards.get(1));
    historian.version(storageRequest, storageResponse, updateResponse);
    verify(exceptionStorageProvider).rollback(any(StorageRequest.class));
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) UpdateResponse(ddf.catalog.operation.UpdateResponse) DeletedMetacard(ddf.catalog.core.versioning.DeletedMetacard) Metacard(ddf.catalog.data.Metacard) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) DeleteStorageRequest(ddf.catalog.content.operation.DeleteStorageRequest) StorageRequest(ddf.catalog.content.operation.StorageRequest) UpdateStorageResponse(ddf.catalog.content.operation.UpdateStorageResponse) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) StorageProvider(ddf.catalog.content.StorageProvider) ContentItem(ddf.catalog.content.data.ContentItem) Test(org.junit.Test)

Aggregations

ReadStorageResponse (ddf.catalog.content.operation.ReadStorageResponse)6 ContentItem (ddf.catalog.content.data.ContentItem)5 ReadStorageRequest (ddf.catalog.content.operation.ReadStorageRequest)5 CreateStorageResponse (ddf.catalog.content.operation.CreateStorageResponse)4 ReadStorageRequestImpl (ddf.catalog.content.operation.impl.ReadStorageRequestImpl)4 URI (java.net.URI)4 Test (org.junit.Test)4 Metacard (ddf.catalog.data.Metacard)3 StorageProvider (ddf.catalog.content.StorageProvider)2 DeleteStorageRequest (ddf.catalog.content.operation.DeleteStorageRequest)2 StorageRequest (ddf.catalog.content.operation.StorageRequest)2 UpdateStorageRequest (ddf.catalog.content.operation.UpdateStorageRequest)2 UpdateStorageResponse (ddf.catalog.content.operation.UpdateStorageResponse)2 DeletedMetacard (ddf.catalog.core.versioning.DeletedMetacard)2 UpdateResponse (ddf.catalog.operation.UpdateResponse)2 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)2 ByteSource (com.google.common.io.ByteSource)1 StorageException (ddf.catalog.content.StorageException)1 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)1 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)1