use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class ResourceOperations method getResource.
//
//
//
@SuppressWarnings("javadoc")
ResourceResponse getResource(ResourceRequest resourceRequest, boolean isEnterprise, String resourceSiteName, boolean fanoutEnabled) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
ResourceResponse resourceResponse = null;
ResourceRequest resourceReq = resourceRequest;
ResourceRetriever retriever = null;
if (fanoutEnabled) {
isEnterprise = true;
}
if (resourceSiteName == null && !isEnterprise) {
throw new ResourceNotFoundException("resourceSiteName cannot be null when obtaining resource.");
}
validateGetResourceRequest(resourceReq);
try {
resourceReq = preProcessPreAuthorizationPlugins(resourceReq);
resourceReq = processPreResourcePolicyPlugins(resourceReq);
resourceReq = processPreResourceAccessPlugins(resourceReq);
resourceReq = processPreResourcePlugins(resourceReq);
Map<String, Serializable> requestProperties = resourceReq.getProperties();
LOGGER.debug("Attempting to get resource from siteName: {}", resourceSiteName);
// At this point we pull out the properties and use them.
Serializable sourceIdProperty = requestProperties.get(ResourceRequest.SOURCE_ID);
final String namedSource = (sourceIdProperty != null) ? sourceIdProperty.toString() : resourceSiteName;
Serializable enterpriseProperty = requestProperties.get(ResourceRequest.IS_ENTERPRISE);
if (enterpriseProperty != null && Boolean.parseBoolean(enterpriseProperty.toString())) {
isEnterprise = true;
}
// check if the resourceRequest has an ID only
// If so, the metacard needs to be found and the Resource URI
StringBuilder resolvedSourceIdHolder = new StringBuilder();
ResourceInfo resourceInfo = getResourceInfo(resourceReq, namedSource, isEnterprise, resolvedSourceIdHolder, requestProperties, fanoutEnabled);
if (resourceInfo == null) {
throw new ResourceNotFoundException("Resource could not be found for the given attribute value: " + resourceReq.getAttributeValue());
}
final URI responseURI = resourceInfo.getResourceUri();
final Metacard metacard = resourceInfo.getMetacard();
final String resolvedSourceId = resolvedSourceIdHolder.toString();
LOGGER.debug("resolvedSourceId = {}", resolvedSourceId);
LOGGER.debug("ID = {}", getId());
// since resolvedSourceId specifies what source the product
// metacard resides on, we can just
// change resourceSiteName to be that value, and then the
// following if-else statements will
// handle retrieving the product on the correct source
final String resourceSourceName = (isEnterprise) ? resolvedSourceId : namedSource;
// retrieve product from specified federated site if not in cache
if (!resourceSourceName.equals(getId())) {
LOGGER.debug("Searching federatedSource {} for resource.", resourceSourceName);
LOGGER.debug("metacard for product found on source: {}", resolvedSourceId);
final List<FederatedSource> sources = frameworkProperties.getFederatedSources().stream().filter(e -> e.getId().equals(resourceSourceName)).collect(Collectors.toList());
if (!sources.isEmpty()) {
if (sources.size() != 1) {
LOGGER.debug("Found multiple federatedSources for id: {}", resourceSourceName);
}
FederatedSource source = sources.get(0);
LOGGER.debug("Adding federated site to federated query: {}", source.getId());
LOGGER.debug("Retrieving product from remote source {}", source.getId());
retriever = new RemoteResourceRetriever(source, responseURI, requestProperties);
} else {
LOGGER.debug("Could not find federatedSource: {}", resourceSourceName);
}
} else {
LOGGER.debug("Retrieving product from local source {}", resourceSourceName);
retriever = new LocalResourceRetriever(frameworkProperties.getResourceReaders(), responseURI, metacard, requestProperties);
}
try {
resourceResponse = frameworkProperties.getDownloadManager().download(resourceRequest, metacard, retriever);
} catch (DownloadException e) {
LOGGER.info("Unable to download resource", e);
}
resourceResponse = putPropertiesInResponse(resourceRequest, resourceResponse);
resourceResponse = validateFixGetResourceResponse(resourceResponse, resourceReq);
resourceResponse = postProcessPreAuthorizationPlugins(resourceResponse, metacard);
resourceResponse = processPostResourcePolicyPlugins(resourceResponse, metacard);
resourceResponse = processPostResourceAccessPlugins(resourceResponse, metacard);
resourceResponse = processPostResourcePlugins(resourceResponse);
resourceResponse.getProperties().put(Constants.METACARD_PROPERTY, metacard);
} catch (DataUsageLimitExceededException e) {
LOGGER.info("RuntimeException caused by: ", e);
throw e;
} catch (RuntimeException e) {
LOGGER.info("RuntimeException caused by: ", e);
throw new ResourceNotFoundException("Unable to find resource");
} catch (StopProcessingException e) {
LOGGER.info("Resource not supported", e);
throw new ResourceNotSupportedException(FAILED_BY_GET_RESOURCE_PLUGIN + e.getMessage());
}
return resourceResponse;
}
use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class ResourceOperations method getResourceInfo.
/**
* Retrieves a resource by URI.
*
* <p>The {@link ResourceRequest} can specify either the product's URI or ID. If the product ID is
* specified, then the matching {@link Metacard} must first be retrieved and the product URI
* extracted from this {@link Metacard}.
*
* @param resourceRequest
* @param site
* @param isEnterprise
* @param federatedSite
* @param requestProperties
* @param fanoutEnabled
* @return
* @throws ResourceNotSupportedException
* @throws ResourceNotFoundException
*/
protected ResourceInfo getResourceInfo(ResourceRequest resourceRequest, String site, boolean isEnterprise, StringBuilder federatedSite, Map<String, Serializable> requestProperties, boolean fanoutEnabled) throws ResourceNotSupportedException, ResourceNotFoundException {
ResourceInfo resourceInfo;
Query query = null;
URI resourceUri = null;
String name = resourceRequest.getAttributeName();
Object value = resourceRequest.getAttributeValue();
validateResourceInfoRequest(name, value);
try {
if (ResourceRequest.GET_RESOURCE_BY_PRODUCT_URI.equals(name)) {
// because this is a get resource by product uri, we already
// have the product uri to return
LOGGER.debug("get resource by product uri");
resourceUri = (URI) value;
URI truncatedUri = resourceUri;
if (StringUtils.isNotBlank(resourceUri.getFragment())) {
resourceRequest.getProperties().put(ContentItem.QUALIFIER_KEYWORD, resourceUri.getFragment());
try {
// Creating the truncated URL this way is important to preserve encoding!!
String uriString = resourceUri.toString();
truncatedUri = new URI(uriString.substring(0, uriString.lastIndexOf('#')));
} catch (URISyntaxException e) {
throw new ResourceNotFoundException("Could not resolve URI by doing a URI based query: " + value);
}
}
query = createPropertyStartsWithQuery(Metacard.RESOURCE_URI, truncatedUri.toString());
} else if (ResourceRequest.GET_RESOURCE_BY_ID.equals(name)) {
// since this is a get resource by id, we need to obtain the
// product URI
LOGGER.debug("get resource by id");
String metacardId = (String) value;
LOGGER.debug("metacardId = {}, site = {}", metacardId, site);
query = createMetacardIdQuery(metacardId);
}
QueryRequest queryRequest = new QueryRequestImpl(anyTag(query, site, isEnterprise), isEnterprise, Collections.singletonList(site == null ? this.getId() : site), resourceRequest.getProperties());
resourceInfo = getResourceInfo(queryRequest, resourceUri, requestProperties, federatedSite, fanoutEnabled);
} catch (UnsupportedQueryException | FederationException e) {
throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE, e);
}
if (resourceInfo.getResourceUri() == null) {
throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE);
}
LOGGER.debug("Returning resourceURI: {}", resourceInfo.getResourceUri());
return resourceInfo;
}
use of ddf.catalog.resource.ResourceNotFoundException 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;
}
use of ddf.catalog.resource.ResourceNotFoundException 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.resource.ResourceNotFoundException in project ddf by codice.
the class ResourceMetacardTransformerTest method testFrameworkThrowsResourceNotFoundException.
@Test
public void testFrameworkThrowsResourceNotFoundException() throws Exception {
String filePath = ABSOLUTE_PATH + TEST_PATH + JPEG_FILE_NAME_1;
URI uri = getUri(filePath);
Metacard metacard = getMockMetacard(uri);
thrown.expect(CatalogTransformerException.class);
thrown.expectMessage("Unable to retrieve resource.");
thrown.expectMessage("Metacard id: " + TEST_ID);
thrown.expectMessage("Uri: " + uri);
thrown.expectMessage("Source: " + TEST_SITE);
boolean expectSuccess = false;
MimeType mimeType = getMimeType(JPEG_MIME_TYPE);
CatalogFramework framework = getFrameworkException(new ResourceNotFoundException("Test Resource Not Found Exception"));
testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
Aggregations