Search in sources :

Example 41 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse 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 42 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class MetacardApplication method revertContentandMetacard.

private void revertContentandMetacard(Metacard latestContent, Metacard versionMetacard, String id) throws SourceUnavailableException, IngestException, ResourceNotFoundException, IOException, ResourceNotSupportedException, FederationException, UnsupportedQueryException {
    LOGGER.trace("Reverting content and metacard for metacard [{}]. \nLatest content: [{}] \nVersion metacard: [{}]", id, latestContent.getId(), versionMetacard.getId());
    Map<String, Serializable> properties = new HashMap<>();
    properties.put("no-default-tags", true);
    ResourceResponse latestResource = catalogFramework.getLocalResource(new ResourceRequestById(latestContent.getId(), properties));
    ContentItemImpl contentItem = new ContentItemImpl(id, new ByteSourceWrapper(() -> latestResource.getResource().getInputStream()), latestResource.getResource().getMimeTypeValue(), latestResource.getResource().getName(), latestResource.getResource().getSize(), MetacardVersionImpl.toMetacard(versionMetacard, types));
    // Try to delete the "deleted metacard" marker first.
    boolean alreadyCreated = false;
    Action action = Action.fromKey((String) versionMetacard.getAttribute(MetacardVersion.ACTION).getValue());
    if (DELETE_ACTIONS.contains(action)) {
        alreadyCreated = true;
        catalogFramework.create(new CreateStorageRequestImpl(Collections.singletonList(contentItem), id, new HashMap<>()));
    } else {
        // Currently we can't guarantee the metacard will exist yet because of the 1 second
        // soft commit in solr. this busy wait loop should be fixed when alternate solution
        // is found.
        tryUpdate(4, () -> {
            catalogFramework.update(new UpdateStorageRequestImpl(Collections.singletonList(contentItem), id, new HashMap<>()));
            return true;
        });
    }
    LOGGER.trace("Successfully reverted metacard content for [{}]", id);
    revertMetacard(versionMetacard, id, alreadyCreated);
}
Also used : Serializable(java.io.Serializable) Action(ddf.catalog.core.versioning.MetacardVersion.Action) ResourceResponse(ddf.catalog.operation.ResourceResponse) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) HashMap(java.util.HashMap) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 43 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ZipCompression method getResource.

private Resource getResource(Metacard metacard) {
    Resource resource = null;
    try {
        ResourceRequest resourceRequest = new ResourceRequestById(metacard.getId());
        ResourceResponse resourceResponse = catalogFramework.getLocalResource(resourceRequest);
        resource = resourceResponse.getResource();
    } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
        LOGGER.debug("Unable to retrieve content from metacard : {}", metacard.getId(), e);
    }
    return resource;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Resource(ddf.catalog.resource.Resource) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 44 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class TestZipCompression method setUp.

@Before
public void setUp() throws Exception {
    JarSigner jarSigner = mock(JarSigner.class);
    doNothing().when(jarSigner).signJar(any(File.class), anyString(), anyString(), anyString(), anyString());
    zipCompression = new ZipCompression(jarSigner);
    sourceResponse = createSourceResponseWithURISchemes(null, null);
    filePathArgument = new HashMap<>();
    filePathArgument.put("filePath", temporaryFolder.getRoot().getAbsolutePath() + File.separator + "signed.zip");
    catalogFramework = mock(CatalogFramework.class);
    Resource resource = mock(Resource.class);
    InputStream resourceFileStream = new FileInputStream(new File(LOCAL_RESOURCE_PATH));
    when(resource.getName()).thenReturn(LOCAL_RESOURCE_FILENAME);
    when(resource.getInputStream()).thenReturn(resourceFileStream);
    ResourceResponse resourceResponse = new ResourceResponseImpl(resource);
    when(catalogFramework.getLocalResource(any(ResourceRequestById.class))).thenReturn(resourceResponse);
    zipCompression.setCatalogFramework(catalogFramework);
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CatalogFramework(ddf.catalog.CatalogFramework) Resource(ddf.catalog.resource.Resource) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) File(java.io.File) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 45 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class CatalogFrameworkImplTest method testGetResourceWhenNonNullResourceRequestExpectPostResourcePluginToReceiveResourceResponseWithNonNullResourceRequest.

/*
     * Test for "ResourceResponse returns null ResourceRequest in the PostResourcePlugin"
     *
     * The error this test case addresses is as follows: The PostResourcePlugin receives a
     * ResourceResponse with a null ResourceRequest.
     */
@Test
@Ignore
public void testGetResourceWhenNonNullResourceRequestExpectPostResourcePluginToReceiveResourceResponseWithNonNullResourceRequest() throws Exception {
    String sourceId = "myId";
    resourceFramework.setId(sourceId);
    ResourceCacheImpl resourceCache = mock(ResourceCacheImpl.class);
    when(resourceCache.containsValid(isA(String.class), isA(Metacard.class))).thenReturn(false);
    String resourceSiteName = "myId";
    // Execute
    LOGGER.debug("Testing CatalogFramework.getResource(ResourceRequest, String)...");
    ResourceResponse resourceResponse = resourceFramework.getResource(mockResourceRequest, resourceSiteName);
    LOGGER.debug("resourceResponse: {}", resourceResponse);
    // Verify
    /*
         * Verify that when PostResoucePlugin.process() is called, the ResourceResponse argument
         * contains a non-null ResourceRequest.
         */
    verify(mockPostResourcePlugin).process(argument.capture());
    assertNotNull("PostResourcePlugin received a ResourceResponse with a null ResourceRequest.", argument.getValue().getRequest());
/*
         * We really don't need to assert this since we return our mockResourceResponse from
         * PostResourcePlugin.process()
         */
// assertNotNull("ResourceResponse.getResource() returned a ResourceResponse with a null ResourceRequest.",
// resourceResponse.getRequest());
}
Also used : Metacard(ddf.catalog.data.Metacard) ResourceCacheImpl(ddf.catalog.cache.impl.ResourceCacheImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) Matchers.anyString(org.mockito.Matchers.anyString) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ResourceResponse (ddf.catalog.operation.ResourceResponse)71 Test (org.junit.Test)36 HashMap (java.util.HashMap)17 Resource (ddf.catalog.resource.Resource)16 URI (java.net.URI)16 Serializable (java.io.Serializable)15 Metacard (ddf.catalog.data.Metacard)13 IOException (java.io.IOException)13 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)13 ResourceRequest (ddf.catalog.operation.ResourceRequest)12 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)12 MimeType (javax.activation.MimeType)11 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)10 Response (javax.ws.rs.core.Response)10 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)8 URLResourceReader (ddf.catalog.resource.impl.URLResourceReader)7 Matchers.anyString (org.mockito.Matchers.anyString)7 InputStream (java.io.InputStream)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6