use of ddf.catalog.resource.ResourceNotSupportedException 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 {
Metacard metacard;
URI resourceUri;
String name = resourceRequest.getAttributeName();
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");
Object value = resourceRequest.getAttributeValue();
if (value instanceof URI) {
resourceUri = (URI) value;
if (StringUtils.isNotBlank(resourceUri.getFragment())) {
resourceRequest.getProperties().put(ContentItem.QUALIFIER, resourceUri.getFragment());
try {
resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), null);
} catch (URISyntaxException e) {
throw new ResourceNotFoundException("Could not resolve URI by doing a URI based query: " + value);
}
}
Query propertyEqualToUriQuery = createPropertyIsEqualToQuery(Metacard.RESOURCE_URI, resourceUri.toString());
// if isEnterprise, go out and obtain the actual source
// where the product's metacard is stored.
QueryRequest queryRequest = new QueryRequestImpl(anyTag(propertyEqualToUriQuery, site, isEnterprise), isEnterprise, Collections.singletonList(site == null ? this.getId() : site), resourceRequest.getProperties());
QueryResponse queryResponse = queryOperations.query(queryRequest, null, true, fanoutEnabled);
if (!queryResponse.getResults().isEmpty()) {
metacard = queryResponse.getResults().get(0).getMetacard();
federatedSite.append(metacard.getSourceId());
LOGGER.debug("Trying to lookup resource URI {} for metacardId: {}", resourceUri, resourceUri);
if (!requestProperties.containsKey(Metacard.ID)) {
requestProperties.put(Metacard.ID, metacard.getId());
}
if (!requestProperties.containsKey(Metacard.RESOURCE_URI)) {
requestProperties.put(Metacard.RESOURCE_URI, metacard.getResourceURI());
}
} else {
throw new ResourceNotFoundException("Could not resolve source id for URI by doing a URI based query: " + resourceUri);
}
} else {
throw new ResourceNotSupportedException("The GetResourceRequest with attribute value of class '" + value.getClass() + "' is not supported by this instance of the CatalogFramework.");
}
} 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");
Object value = resourceRequest.getAttributeValue();
if (value instanceof String) {
String metacardId = (String) value;
LOGGER.debug("metacardId = {}, site = {}", metacardId, site);
QueryRequest queryRequest = new QueryRequestImpl(anyTag(createMetacardIdQuery(metacardId), site, isEnterprise), isEnterprise, Collections.singletonList(site == null ? this.getId() : site), resourceRequest.getProperties());
QueryResponse queryResponse = queryOperations.query(queryRequest, null, true, fanoutEnabled);
if (!queryResponse.getResults().isEmpty()) {
metacard = queryResponse.getResults().get(0).getMetacard();
resourceUri = metacard.getResourceURI();
federatedSite.append(metacard.getSourceId());
LOGGER.debug("Trying to lookup resource URI {} for metacardId: {}", resourceUri, metacardId);
} else {
throw new ResourceNotFoundException("Could not resolve source id for URI by doing an id based query: " + metacardId);
}
if (!requestProperties.containsKey(Metacard.ID)) {
requestProperties.put(Metacard.ID, metacardId);
}
if (!requestProperties.containsKey(Metacard.RESOURCE_URI)) {
requestProperties.put(Metacard.RESOURCE_URI, resourceUri);
}
} else {
throw new ResourceNotSupportedException("The GetResourceRequest with attribute value of class '" + value.getClass() + "' is not supported by this instance of the CatalogFramework.");
}
} else {
throw new ResourceNotSupportedException("The GetResourceRequest with attribute name '" + name + "' is not supported by this instance of the CatalogFramework.");
}
} catch (UnsupportedQueryException | FederationException e) {
throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE, e);
}
LOGGER.debug("Returning resourceURI: {}", resourceUri);
if (resourceUri == null) {
throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE);
}
return new ResourceInfo(metacard, resourceUri);
}
use of ddf.catalog.resource.ResourceNotSupportedException 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, Long.valueOf(bytesToSkip));
}
for (ResourceReader reader : resourceReaders) {
if (reader != null) {
String scheme = resourceUri.getScheme();
if (reader.getSupportedSchemes().contains(scheme)) {
try {
LOGGER.debug("Found an acceptable resource reader ({}) for URI {}", reader.getId(), resourceUri.toASCIIString());
resource = reader.retrieveResource(resourceUri, props);
if (resource != null) {
break;
} else {
LOGGER.debug("Resource returned from ResourceReader {} was null. Checking other readers for URI: {}", reader.getId(), resourceUri);
}
} 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: " + resourceUri.toASCIIString() + ". Scheme: " + resourceUri.getScheme());
}
LOGGER.debug("Received resource, sending back: {}", resource.getResource().getName());
LOGGER.trace("EXITING: {}", methodName);
return resource;
}
use of ddf.catalog.resource.ResourceNotSupportedException 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 e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
} catch (ResourceNotFoundException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
} catch (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.resource.ResourceNotSupportedException in project ddf by codice.
the class TestResourceMetacardTransformer method testFrameworkThrowsResourceNotSupportedException.
@Test
public void testFrameworkThrowsResourceNotSupportedException() 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 ResourceNotSupportedException("Test Resource Not Supported Exception"));
testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
Aggregations