use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class AbstractCswSource method retrieveResource.
@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> requestProperties) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
Serializable serializableId = null;
String username = cswSourceConfiguration.getUsername();
String password = cswSourceConfiguration.getPassword();
if (requestProperties != null) {
serializableId = requestProperties.get(Core.ID);
if (StringUtils.isNotBlank(username)) {
requestProperties.put(USERNAME_PROPERTY, username);
requestProperties.put(PASSWORD_PROPERTY, password);
}
if (OAUTH_AUTH_TYPE.equals(cswSourceConfiguration.getAuthenticationType()) && StringUtils.isNotBlank(cswSourceConfiguration.getOauthDiscoveryUrl()) && StringUtils.isNotBlank(cswSourceConfiguration.getOauthClientId()) && StringUtils.isNotBlank(cswSourceConfiguration.getOauthClientSecret())) {
requestProperties.put(ID_PROPERTY, cswSourceConfiguration.getId());
requestProperties.put(OAUTH_DISCOVERY_URL, cswSourceConfiguration.getOauthDiscoveryUrl());
requestProperties.put(OAUTH_CLIENT_ID, cswSourceConfiguration.getOauthClientId());
requestProperties.put(OAUTH_CLIENT_SECRET, cswSourceConfiguration.getOauthClientSecret());
requestProperties.put(OAUTH_FLOW, cswSourceConfiguration.getOauthFlow());
}
}
if (canRetrieveResourceById()) {
// If no resource reader was found, retrieve the product through a GetRecordById request
if (serializableId == null) {
throw new ResourceNotFoundException("Unable to retrieve resource because no metacard ID was found.");
}
String metacardId = serializableId.toString();
LOGGER.debug("Retrieving resource for ID : {}", metacardId);
ResourceResponse response = retrieveResourceById(requestProperties, metacardId);
if (response != null) {
return response;
}
}
if (resourceUri == null) {
throw new IllegalArgumentException("Unable to retrieve resource because no resource URI was given");
}
LOGGER.debug("Retrieving resource at : {}", resourceUri);
return resourceReader.retrieveResource(resourceUri, requestProperties);
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ResourceMetacardTransformer method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
LOGGER.trace("Entering resource ResourceMetacardTransformer.transform");
if (!isValid(metacard)) {
throw new CatalogTransformerException("Could not transform metacard to a resource because the metacard is not valid.");
}
if (StringUtils.isNotEmpty(metacard.getResourceSize())) {
arguments.put(Metacard.RESOURCE_SIZE, metacard.getResourceSize());
}
String id = metacard.getId();
LOGGER.debug("executing resource request with id '{}'", id);
final ResourceRequest resourceRequest = new ResourceRequestById(id, arguments);
ResourceResponse resourceResponse = null;
String sourceName = metacard.getSourceId();
if (StringUtils.isBlank(sourceName)) {
sourceName = catalogFramework.getId();
}
String resourceUriAscii = "";
if (metacard.getResourceURI() != null) {
resourceUriAscii = metacard.getResourceURI().toASCIIString();
}
try {
resourceResponse = catalogFramework.getResource(resourceRequest, sourceName);
} catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
}
if (resourceResponse == null) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii));
}
Resource transformedContent = resourceResponse.getResource();
MimeType mimeType = transformedContent.getMimeType();
if (mimeType == null) {
try {
mimeType = new MimeType(DEFAULT_MIME_TYPE_STR);
// There is no method to set the MIME type, so in order to set it to our default
// one, we need to create a new object.
transformedContent = new ResourceImpl(transformedContent.getInputStream(), mimeType, transformedContent.getName());
} catch (MimeTypeParseException e) {
throw new CatalogTransformerException("Could not create default mime type upon null mimeType, for default mime type '" + DEFAULT_MIME_TYPE_STR + "'.", e);
}
}
LOGGER.debug("Found mime type: '{}' for product of metacard with id: '{}'.\nGetting associated resource from input stream. \n", mimeType, id);
LOGGER.trace("Exiting resource transform for metacard id: '{}'", id);
return transformedContent;
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class CatalogFrameworkImplTest method testGetResourceToTestSecondResourceReaderWithSameSchemeGetsCalledIfFirstDoesNotReturnAnything.
/**
* Tests that multiple ResourceReaders with the same scheme will be invoked if the first one did
* not return a Response.
*
* @throws Exception
*/
@Test
// CACHE
@Ignore
public void testGetResourceToTestSecondResourceReaderWithSameSchemeGetsCalledIfFirstDoesNotReturnAnything() throws Exception {
String localProviderName = "ddf";
final String EXPECTED = "result from mockResourceResponse2";
final String DDF = "ddf";
// Mock a Catalog Provider
CatalogProvider provider = mock(CatalogProvider.class);
when(provider.getId()).thenReturn(localProviderName);
when(provider.isAvailable(isA(SourceMonitor.class))).thenReturn(true);
when(provider.isAvailable()).thenReturn(true);
// Create two ResourceReaders. The first should not return anything
// and the second should.
ResourceReader resourceReader1 = mock(ResourceReader.class);
ResourceReader resourceReader2 = mock(ResourceReader.class);
// Set the supported Schemes so that both ResourceReaders use
// the same scheme ("DAD")
Set<String> supportedSchemes = new HashSet<String>();
supportedSchemes.add("DAD");
when(resourceReader1.getSupportedSchemes()).thenReturn(supportedSchemes);
when(resourceReader2.getSupportedSchemes()).thenReturn(supportedSchemes);
List<ResourceReader> resourceReaders = new ArrayList<ResourceReader>();
resourceReaders.add(resourceReader1);
resourceReaders.add(resourceReader2);
// Set up the requests and responses. The first ResourceReader will return null
// and the second one will retrieve a value, showing that if more than one
// ResourceReader with the same scheme are used, they will be called until a
// response is returned
ResourceRequest mockResourceRequest = mock(ResourceRequest.class);
URI myURI = new URI("DAD", "host", "/path", "fragment");
when(mockResourceRequest.getAttributeValue()).thenReturn(myURI);
when(mockResourceRequest.getAttributeName()).thenReturn(new String(ResourceRequest.GET_RESOURCE_BY_PRODUCT_URI));
Result result = mock(Result.class);
Metacard metacard = mock(Metacard.class);
when(metacard.getResourceURI()).thenReturn(myURI);
when(result.getMetacard()).thenReturn(metacard);
List<Result> results = new ArrayList<Result>();
results.add(result);
QueryResponse queryResponse = mock(QueryResponse.class);
when(queryResponse.getResults()).thenReturn(results);
List<Source> federatedSources = new ArrayList<Source>();
FederationStrategy strategy = mock(FederationStrategy.class);
when(strategy.federate(isA(federatedSources.getClass()), isA(QueryRequest.class))).thenReturn(queryResponse);
ResourceResponse mockResourceResponse1 = mock(ResourceResponse.class);
when(mockResourceResponse1.getRequest()).thenReturn(mockResourceRequest);
when(mockResourceResponse1.getResource()).thenReturn(null);
when(resourceReader1.retrieveResource(any(URI.class), anyMap())).thenReturn(null);
Resource mockResource = mock(Resource.class);
when(mockResource.getName()).thenReturn(EXPECTED);
ResourceResponse mockResourceResponse2 = mock(ResourceResponse.class);
when(mockResourceResponse2.getResource()).thenReturn(mockResource);
when(resourceReader2.retrieveResource(any(URI.class), anyMap())).thenReturn(mockResourceResponse2);
FrameworkProperties frameworkProperties = new FrameworkProperties();
frameworkProperties.setResourceReaders(resourceReaders);
frameworkProperties.setFederationStrategy(strategy);
frameworkProperties.setCatalogProviders(Collections.singletonList(provider));
final SourcePoller<SourceStatus> mockStatusSourcePoller = mock(SourcePoller.class);
when(mockStatusSourcePoller.getCachedValueForSource(isA(Source.class))).thenReturn(Optional.empty());
final SourcePoller<Set<ContentType>> mockContentTypesSourcePoller = mock(SourcePoller.class);
when(mockContentTypesSourcePoller.getCachedValueForSource(isA(Source.class))).thenReturn(Optional.empty());
SourceOperations sourceOps = new SourceOperations(frameworkProperties, sourceActionRegistry, mockStatusSourcePoller, mockContentTypesSourcePoller);
QueryOperations queryOps = new QueryOperations(frameworkProperties, sourceOps, null, null);
queryOps.setSecurityLogger(mock(SecurityLogger.class));
queryOps.setPermissions(new PermissionsImpl());
ResourceOperations resOps = new ResourceOperations(frameworkProperties, queryOps, null);
resOps.setId(DDF);
CatalogFrameworkImpl catalogFramework = new CatalogFrameworkImpl(null, null, null, null, resOps, null, null);
sourceOps.bind(provider);
ResourceResponse response = catalogFramework.getResource(mockResourceRequest, DDF);
// Verify that the Response is as expected
org.junit.Assert.assertEquals(EXPECTED, response.getResource().getName());
// Verify that resourceReader1 was called 1 time
// This line is equivalent to verify(resourceReader1,
// times(1)).retrieveResource(any(URI.class), anyMap());
verify(resourceReader1).retrieveResource(any(URI.class), anyMap());
}
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());
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class LocalResourceRetriever method retrieveResource.
@Override
public ResourceResponse retrieveResource(long bytesToSkip) throws ResourceNotFoundException {
final String methodName = "retrieveResource";
LOGGER.trace("ENTERING: {}", methodName);
ResourceResponse resource = null;
if (resourceUri == null) {
throw new ResourceNotFoundException("Unable to find resource due to null URI");
}
Map<String, Serializable> props = new HashMap<>(properties);
if (bytesToSkip > 0) {
props.put(BYTES_TO_SKIP, bytesToSkip);
}
URI derivedUri = null;
Serializable serializable = props.get(ContentItem.QUALIFIER_KEYWORD);
if (serializable != null && serializable instanceof String) {
LOGGER.debug("Received qualifier in request properties, looking for qualified content on metacard with id [{}]", resourceMetacard.getId());
String fragment = (String) serializable;
derivedUri = getDerivedUriWithFragment(resourceMetacard, fragment);
}
String scheme;
URI resourceRetrievalUri;
if (derivedUri == null) {
scheme = resourceUri.getScheme();
resourceRetrievalUri = resourceUri;
} else {
scheme = derivedUri.getScheme();
resourceRetrievalUri = derivedUri;
}
for (ResourceReader reader : resourceReaders) {
if (reader != null && reader.getSupportedSchemes().contains(scheme)) {
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Found an acceptable resource reader ({}) for URI {}", reader.getId(), resourceRetrievalUri.toASCIIString());
}
resource = reader.retrieveResource(resourceRetrievalUri, props);
if (resource != null) {
break;
} else {
LOGGER.debug("Resource returned from ResourceReader {} was null. Checking other readers for URI: {}", reader.getId(), resourceRetrievalUri);
}
} catch (ResourceNotFoundException | ResourceNotSupportedException | IOException e) {
LOGGER.debug("Product not found using resource reader with name {}", reader.getId(), e);
}
}
}
if (resource == null) {
throw new ResourceNotFoundException("Resource Readers could not find resource (or returned null resource) for URI: " + resourceRetrievalUri.toASCIIString() + ". Scheme: " + resourceRetrievalUri.getScheme());
}
LOGGER.debug("Received resource, sending back: {}", resource.getResource().getName());
LOGGER.trace("EXITING: {}", methodName);
return resource;
}
Aggregations