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